File Exchange

image thumbnail

Y-labels alignment in subplots

version (3.03 KB) by gbernardi
Function to align the ylabels within a figure containing multiple subplots


Updated 18 Jun 2013

View Version History

View License

This function takes care of the alignment of ylabels in figures with multiple subplots.
This is done because ylabels are usually not aligned when the yticks labels are of different length in different subplots.
The alignment is performed within each column of subplots and was designed to work with the default MATLAB subplot function as well as with the subplot1 function released on MATLAB central.

Cite As

gbernardi (2021). Y-labels alignment in subplots (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (17)

Patrick Schwarz

Okay, easy quickfix for misalignment between ylabels with and without subscripts: add *_{} to those without subscripts.

Patrick Schwarz

Thanks for this handy little function. Has anybody an idea, how to consider subscripts (for example a_y [m/s^2]) in some of the involved subplots? Those ylabels, which do not have subscripts, are perfectly aligned with the subscript level of the rest.


follow the comment below as there is a typo in the code. it will work if you're lucky, as it measures only how many ticks your y axis has, not how long they are. Worked for me the first time but not the second time.

cbentter found on the 28 Jan 2019 the solution first. the script should look which is the longest label, not how many labels it has. so his comment is correct

"I changed line 118 to tmp_max(k) = size(char(yticks{k}),2); and it worked better."

Sergio Vidal

Marilee Nugent

Thanks for this! But I have the same question as Rajat Desikan - not enough space from ytick labels (the ones that were causing the unevenness in the first place)


Hello. I hadn't logged in in this page for more than 5 years but I realised that some people actually found this function useful. I'm glad to see that.

However, I'm not keeping track of it or updating it anymore (and not using Matlab that much anymore). I am surprised that it still works, considering the major upgrade Mathworks included in their graphical engine around 2015 (or 2016, can't remember). Anyway, if anyone wants to keep this updated, there might be ways to add contributors (I see that now github is supported). Let me know.

Mathieu Blauw

I don't know if this funtion is still being updated, but I've been using panel to plot my figures. Therefore, some subplots do not exist such that computing the height and width is not possible from the total number of axes. Perhaps you could obtain the number of rows/cols via the absolute position of the plots


I changed line 118 to tmp_max(k) = size(char(yticks{k}),2); and it worked better.

Thanks, it works great.

Owen Gebler

A great little function. However, it wouldn't work for me as is, I had to make a change to line 118, so that it returned the size of the first dimension within the yticks cell array element rather than the second i.e. :

tmp_max(k) = size(yticks{k},1);

I can't say I know why this needed to be changed, but once it was the function worked perfectly!

Seb Biass

Hi, great function. FYI, I had problems when plotting different axes on the left and right (e.g. plotyy or yyaxis) as all labels would align to the right. Putting everything in an extra loop seems to have solved the problem:

axSide = {'left', 'right'};
for iS = 1:numel(axSide)
% Get the handles of each axis (it will take also the legends' handles)
allAxes = findall(figureHandle,'type','axes','YaxisLocation',axSide{iS});
% Get the handles of each legend
allLegends = findobj(figureHandle,'Type','axes','Tag','legend');

% Rest of the code


Rajat Desikan

One problem - sometimes, the y-label overlaps with the tick labels of some subplots. How do I uniformly increase the gap between all y-labels and tick labels?

Admiral Artus

Thank you very much! It does what it says, and it does it very well!

Sebastian Wolff


Thank you so much for this. Having the y axis labels not aligned was driving me crazy. This should be embedded within Matlab's figure creating actually. So important in my opinion.

Jonathan Hunt

Another function which should be in the core Matlab functionality! This nice little script does what it claims to do.

As documented by the function description (in the code) resizing the figure after aligning the labels messes up their positioning, but that's no issue provided that this is executed after all resizing operations are complete. Running this function when subfigures have misaligned labels also has the benefit that labels which protrude further from the axes are less likely to get chopped off by the figure renderer during export operations. (Fixing this problem is actually why I'm using it, aside from the need to tidy things up.)


I updated the file to solve the bug occurring when legends were added.


returns not only the axes' handles but also the legends' handles, which I didn't know.

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!