File Exchange

## suplabel

version 1.5.0.0 (2.53 KB) by Ben Barrowes

### Ben Barrowes (view profile)

Places text as a title, xlabel, or ylabel on a group of subplots.

Updated 08 Aug 2018

Editor's Note: This file was a File Exchange Pick of the Week

PLaces text as a title, xlabel, or ylabel on a group of subplots. Returns a handle to the label and a handle to the axis.

[ax,h]=suplabel(text,whichLabel,supAxes)

returns handles to both the axis and the label.

ax=suplabel(text,whichLabel,supAxes)

returns a handle to the axis only. suplabel(text) with one input argument assumes whichLabel='x'

whichLabel is any of 'x', 'y', or 't', specifying whether the text is to be the xlable, ylabel, or title respectively.

supAxes is an optional argument specifying the Position of the "super" axes surrounding the subplots. supAxes defaults to [.075 .075 .85 .85] specify supAxes if labels get chopped or overlay subplots

EXAMPLE:
subplot(2,2,1);ylabel('ylabel1');title('title1')
subplot(2,2,2);ylabel('ylabel2');title('title2')
subplot(2,2,3);ylabel('ylabel3');xlabel('xlabel3')
subplot(2,2,4);ylabel('ylabel4');xlabel('xlabel4')
[ax,h1]=suplabel('super X label');
[ax,h2]=suplabel('super Y label','y');
[ax,h3]=suplabel('super Title' ,'t');
set(h3,'FontSize',30)

suptitle (Matlab Central)

Leone Campos

### Leone Campos (view profile)

After using the 'suplabel' routine, I got problems to manipulate the image view (zoom in, zoom out, etc).

Leone Campos

Calum Maitland

### Calum Maitland (view profile)

Johan van Veerden

### Johan van Veerden (view profile)

Great! Thanks for this. Would be nice to add yyaxes functionality, to allow for two y-axis suplabels, one on the right and one on the left.

Hao Lu

Thanks

YooKwang

### YooKwang (view profile)

gooooooood

Leonardo Fernandes

Simyon

Thanks!

Atiq Mahmud

### Atiq Mahmud (view profile)

Genevieve Le Houx

### Genevieve Le Houx (view profile)

Exactly what I needed

Márton Geda

### Márton Geda (view profile)

To reflect my previous comment:

uistack(ax,'bottom');

This line might be an even better solution to the problem

Márton Geda

### Márton Geda (view profile)

Thanks, it works great. However, if you use it on a figure which has 'visible' 'off' then it will turn into visible. You can fix this by changing line 86 from:

for k=1:length(currax), axes(currax(k));end % restore all other axes

to

for k=1:length(currax), set(gcf,'CurrentAxes',currax(k));end % restore all other axes

Andrew

Thanks =]

wasiu yahya

### wasiu yahya (view profile)

This is a very useful function.
I give it a 5 star

Suejin Lin

Sina Kiaei

### Sina Kiaei (view profile)

Pravin Prame Kumar

Martin

### Martin (view profile)

Does a great job. However, if you change line 65 from:
if ~isstr(text) | ~isstr(whichLabel)
to
if ((iscell(text) & ~isstr([text{:}])) | (~iscell(text) &~isstr(text) )) | ~isstr(whichLabel)
the function will also display multiline titles contained in a cell array

Matilda

### Matilda (view profile)

with Matlab R2015b, suplabel changes all my other single plot titles fontsize.
Has anyone else had this problem ?
Thanks !

Randolph Crawford

### Randolph Crawford (view profile)

Works great in R2016a!

Greg Holst

### Greg Holst (view profile)

Awesome. This should be a standard function. R2016a.

Sardar Usama

### Sardar Usama (view profile)

Very useful! MATLAB should pre-include this in next version!

Omar Mian

### Omar Mian (view profile)

When I don't fill every column space in the figure with a subplot, the title gets centred above the created sub-plots rather than the whole figure Window. e.g.:

figure
subplot(2,3,1);
subplot(2,3,2);
[ax,h]=suplabel('super Title' ,'t');

How can I prevent this or move the title to the centre of the figure window?

Thanks

Dan Weaver

### Dan Weaver (view profile)

Very useful; however, it is awkward to adjust the axes together, and isn't compatible with plotyy.

Andy

### Andy (view profile)

Useful script.

Added new argument(handles) and then ah=handles if present. This then allows choice of handles to be used to define location of axes titles. Therefore allowing titles of plots in a grid that have common titles in rows/columns but not common titles for all plots.

For example, this type of plot could be created. http://walkingwithrichard.files.wordpress.com/2013/03/different-speeds-time-normalised2.png

Philipp

### Philipp (view profile)

First of all thank you very much for the excellent package.
My question is the following:
Is there any way to horizontally align the ylabels? The problem is that my Y-axis numbers go from 0.1 to 0.0001, causing the ylabels to be wherever but not nicely aligned.

Boriboon

### Boriboon (view profile)

this is really useful!!!

Ayhan

Aree

### Aree (view profile)

Response to Will (Nov 2014): "Handles of type Legend cannot be made the current Axes."

Before 2014b, I used to add the line

currax=[currax;findobj(gcf,'tag','legend')];

as suggested by comment by Jeff (Mar 2012) so that the legends do not get deleted out. I found out after 2014b adding Legends into axes list created problems.

But now, I can remove the line with no ill-effects (so it seems).

IOCAS Shen

### IOCAS Shen (view profile)

This is giving me problems with R2014b. I get the following error:
Error using axes
Handles of type Legend cannot be made the current Axes.
Error in suplabel (line 87)
for k=1:length(currax), axes(currax(k));end % restore all other axes

I think it has something to do with this:
http://www.mathworks.com/help/matlab/graphics_transition/why-are-colorbars-and-legends-not-valid-axes-handles.html

Patricia

### Patricia (view profile)

Thanks for this function, very helpful!
I have a problem when saving the figure though.
I use it together with the tight_subplot function and I noticed that when I modify the positions of the labels (either x,y,or t) and save it as as figure (.fig - haven't tried other formats), it saves without the modifications, which is a bit frustrating. Do you have any idea on why this might be happening?

Thanks!!

Patricia

Brian

Sebastien Roy

### Sebastien Roy (view profile)

Great script. Very useful to document a scene with subplots. However, it crashes if no supAxes is supplied and no visible axe is in the figure. This case happens if, for example, a figure is composed of subplots filled with images with imshow.

Leo Simon

### Leo Simon (view profile)

Great script. Is there any way to use \bar and \tilde with it? It appears not to be possible. E.g.,
\suplabel('\xi','t')
works fine but
\suplabel('\bar{\xi}','t')
can't interpret the string. The handle doesn't appear to have an 'Interpreter' field either.

Yuji Zhang

### Yuji Zhang (view profile)

Hi everyone~ I read the code again and found what was wrong... I　should put in the argument [.08 .08 .5 .5]. When I changed in the function supAxes=[.08 .08 .5 .5] (line 46), it was overwritten (line 49-57). Thanks...

Yuji Zhang

### Yuji Zhang (view profile)

Hi, I changed the position by doing supAxes=[.08 .08 .5 .5];

but the title position does change...

Does anybody have the same thing or know what's going on? Any thoughts are appreciated. Thanks!

Felipe

### Felipe (view profile)

if (nargout < 2), clear h; end
if (nargout < 1), clear ax; end

Felipe

### Felipe (view profile)

Hint: if you need to specify the optional supAxes argument, do set(ax,'Visible','one'), redimension it manually/visually, then do get(ax,'Position') to obtain the desired value.

Rishabh Kasliwal

### Rishabh Kasliwal (view profile)

i just plugged in suplabel('my title','t') on my subplot containining figure and it worked !. works pretty well for my purpose

Matlab2010

### Matlab2010 (view profile)

problems with plotyy.
calling this after plotyy removes the 2nd axis plot, as per omar above.

cant seem to get it to work with a pre call either

Morten

### Morten (view profile)

The titles/labels can be moved away from the figure edges by manually changing the value of the "axBuf=.04;" to "axBuf=.001;", this fixed the issue for me.

It appears that the code allows TeX commands (like '\fontsize{14}TEXT HERE') for the y-axis command, but not x...

Jeff

### Jeff (view profile)

excellent, thank you for this. very easy to use. I added the following after line 39 in order to preserve sublot legends:

currax=[currax;findobj(gcf,'tag','legend')];

Erica B

### Erica B (view profile)

Anyway to get the super label to not be squished at the top of the figure window, and closer to the subplots? Top of super title is nearly cut-off, while there is a chunk of space between the super title and the subplots.

I tried
[ax,h1]=suplabel([' ',' ','Title'],'t');
to put some empty lines above the title, but it's still squished at the top of the window...

Jeff Evans

K R

### K R (view profile)

This is fantastic - a huge time saver and so easy to use. Thank you!

Omar Mian

### Omar Mian (view profile)

Does not work with plotyy.....it removes the 2nd axis plot

Christopher

### Christopher (view profile)

And I have some code that changes that figure creation time using suplabel from 30 seconds back down to 5.2 seconds. Apply the following to suplabel, replacing the loop through ah (lines 74 to 82) with:
%%%%%
AH=get(ah);
ii=find(strcmp({AH(:).Visible}.','on'));
thisPos=reshape([AH(ii).Position],4,length(ii)).';
leftMin=min(thisPos(:,1));
bottomMin=min(thisPos(:,2));
leftMax=max(thisPos(:,1)+thisPos(:,3));
bottomMax=max(thisPos(:,2)+thisPos(:,4));
%%%%%
and replacing line 110, kudos to Daniel Golden for his suggestion above, with
%%%%%
ch = get(get(ax, 'parent'), 'children');
set(get(ax, 'parent'), 'children', [ch(2:end); ch(1)]);
%%%%%
Now (almost) all multi-handle axis operations are vectorized.

I do not know how to do it yet, but I will try to put up a unified diff patch if I can figure how to attach a file.

Christopher

### Christopher (view profile)

Has anyone given thought to vectorizing or otherwise speeding up suplabel? I have figures with many subplots (of histograms), eg., subplot(6,8,i); and using suplabel to create the x- and y-labels changes the figure creation time from 5 seconds to 30 seconds.

Anthony Hopf

### Anthony Hopf (view profile)

Using Lars advice I can use multi-line and latex!! Nice function

Anthony Hopf

### Anthony Hopf (view profile)

Can't seem to get multi-line to work... posted in NewsReader

Gemma

### Gemma (view profile)

Thank you very much.. i know have some awesomely titled plots :)

Geo

### Geo (view profile)

A very useful script!!! Thank you a lot!

GERMÁN

### GERMÁN (view profile)

when I want three titles in the super title. How should it be?

L1 = "Energy spectrum function E = f (FFT (\ kappa) ^ 2) ';
L2 = ['height'];
L3 = ['Regression']
[AX4, h3] = suplabel ([L1, L2, L3], 't');

thanks

GERMÁN

### GERMÁN (view profile)

when I want three subttulos the super title. How should it be?

L1 = "Energy spectrum function E = f (FFT (\ kappa) ^ 2) ';
L2 = ['height'];
L3 = ['Regression']
[AX4, h3] = suplabel ([L1, L2, L3], 't');
thanks!!

Jason

### Jason (view profile)

Is there a way to have a second "super Y label" that corresponds to subplots 2 and 4 of the example figure?

Thanks.

Patrick Tai

### Patrick Tai (view profile)

I want to have a super-legend.
When the subplots are all multi-lined with the same grouping scheme, their legends will be the same. Putting a legend in each will be too busy and take too much space.
Is there a way to put the common legend outside the plot matrix?

Patrick Tai

Daniel Golden

### Daniel Golden (view profile)

The problem with zooming is that suplabel places its axis on top of all other axes in the figure. You can fix this by adding the following lines under the
ax=axes('Units','Normal','Position',supAxes,'Visible','off');
line:

ch = get(get(ax, 'parent'), 'children');
set(get(ax, 'parent'), 'children', [ch(2:end); ch(1)]);

This moves the "suplabel" axis to the bottom of the figures' axes list and allows you to mess with the original axes on the figure.

Lars

### Lars (view profile)

Quite useful and simple to use. You might consider to change the argument checking to allow for cell strings (multi-line) labels:

if ~(ischar(text) || (iscell(text) && all(cellfun(@ischar,text))))
error('text must be a string or a cell string')
end
if ~ischar(whichLabel)
error('whichLabel must be a string')
end

sophie neve

### sophie neve (view profile)

I have seen another think that is sad : no greek letter is allowed
Example :
suplabel('log_{10} (Re \epsilon *)','y');

does not write the epsilon, right?

sophie neve

### sophie neve (view profile)

I have aproblem with it : if I only do 4 subplot out of 6, for example 1,2,4,5 and not 3 and 6, the title is not centered any more.... sad :(

Joe McGlinchy

### Joe McGlinchy (view profile)

GREAT! works perfect thanks

Scott Otterson

### Scott Otterson (view profile)

A quick solution to a common problem. Zooming would be nice -- I haven't checked the underpinnings, but if anyone is interested in taking a look

http://www.mathworks.com/matlabcentral/fileexchange/19314

decorates plots with extra labels and survives the zoom button

Omar Mian

### Omar Mian (view profile)

potentially useful for me, but not until zoom/rotate issue is fixed.

Omar Mian

Henry

### Henry (view profile)

Good tool, but like all other comments, I cannot zoom/rotate the image afterwards.

Jake Munster

Useful tool, but as Tom Van Grotel said, I can't zoom in on the subplots! Setting 'HitTest' to 'Off' did not work =(

Pierrick Mialle

Pablo Pablo

I can not rotate an image when I use this function.

Tom Van Grootel

Axes below the "Ghost" axes made by SUPLABEL are not editable (zoom, scale, rotate). Hint to improve this: 'HitTest' set to 'Off', but this still did not solve it.

Exactly what I was looking for! Thanks for this.

pippo baudo

Pablo Velarde

Excellent!

him s

nopes..not working

jon erickson

Thank you--just what I was looking for! very easy to use. Just to follow up on comment 3) above, the default values are [0.08 0.08 0.84 0.84], not a major change.

urs (us) schwarz

1) nice little helper; 2) it would be more convenient if supaxes would default to the outer boundaries of all axes on the canvas if it is not defined by the user (see mtit on the FEX); 3) supaxes' default values do not correspond to those shown in the help section