File Exchange

image thumbnail

Y-labels alignment in subplots

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

11 Downloads

Updated 18 Jun 2013

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.

Comments and Ratings (10)

cbentter

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

end

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?

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

Ilyas

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.

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.)

gbernardi

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

findall(gcf,'type','axes');

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

Updates

1.1.0.0

I fixed the bug that occurred when legends were added to the subplots.

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

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor