File Exchange

image thumbnail

samexaxis (nice subplots with same x axis)

version 1.0 (5.36 KB) by

Makes it alot easier to make nice figures with the same x axis.



No License


* Automatically sets xlim to the same for all subplots Removes xticklabels where it is not needed.
* Can join a subplots (if it is made with subplot instead of subaxis)
* Can set the ylabel position to the same for all subplots
* Can almost allways avoid that the yticklabels are written on top of each other
* Can automatically add a),b),c) to each subplot

% helper function to clean up subplots that have common x axises

USAGE: samexaxis([optionalarguments])

Optional arguments:
* YAxisLocation (default='left') : choose left,right, alternate or alternate2
* XAxisLocation (default='bottom') : choose bottom,top or both
* YLabelDistance (default=1.4)
* Box (default='on')
* XTick
* XTickLabel
* XMinorTick
* ABC : add a),b),c), ... to each sub plot
* Join: joins the subplots vertically
* YTickAntiClash: try to aviod yticklabel clashes (default=false)



Comments and Ratings (39)

please how to realizethe same thing with axis y?

Tunhe Zhou

I am not able to use this it says error in 33


Juan (view profile)

thx for the function!!
I recommend subtightplot fucntion by Felipe G. Nievinski too.

Federico dos

Great function!!. How can I change the font size of a) -> b) -> c)? Thank you very much

Rajesh Ranjan

Does not work when one of the subplots is 'yyplot'. The plot with the second y-axis disappears

Sreeyuth Lal

@Lokesh: As Yohai suggested, also download this file and then your problem should be solved:

this is not working .... it says error in line 33.


ABID (view profile)

how can I make single ylable instead of two or three??
for refrence please see figure 5 on


ABID (view profile)

how can I make single ylable instead of two or three??
for refrence please see figure 5 on

David Wang

Very useful function. One minor issue: When subplots' y axis limits are explicitly set, it tends to result in yticklabel clashes. For example,

set(gca,'YLim',[-3 3]);
set(gca,'YLim',[-4 4]);


Hugo (view profile)

Thx, still working great!

Bartek Luks


Sachin (view profile)


Liang (view profile)

great work!

Daniel H.

Daniel H.

I have exactly the same problem as Kuo-Hsien. Can anybody recommend a solution? Other than that, great work!


I tried to use this in 7.12.0 and parseArgs does not exist. Is there a trivial work aroung?

Régis Dupuy

very nice.
it is a pity though that it cannot be used with plotyy


J G (view profile)

This is great! However, I would like to plot 18 subplots (6 rows, 3 cols). I would like to join the 6 subplots in each column and have a total of 3 x-axis (one per column). I have downloaded subaxis and this works with your example however I can't get this working on my data.


Pearl (view profile)

Li Liao

I think this is a nice code, but I would like to plot two y axis in each panel, I used plotyy first, then use this code, but seems not work.


Hi Aslak, can you let me know how to how "abc" result as (a)-->(b)-->(c)-->(d)-->(e)-->(f) not (b)-->(a)-->(d)-->(c)-->(f)-->(e)

Please see this code:

for ii=2:6



Byron (view profile)

I was able to take the xlims and adjust that section of code to work for my program and fix the limits on the y axis I needed, Thanks.

Curtis Wood

Really nice function.

Per Pemberton

Produces really neat plots

Tony Antonacci

To resolve the error seen in Ramis' statement, download the parseArgs.m and Subaxi.m files at the following link:
After that, the sameaxis functions works perfectly. I found it very useful and well written.

Ramis Örlü

I downloaded parseArgs as well, but got the following error for the example of the author:

??? Function 'fieldnames' is not defined for values of class 'cell'.
Error in ==> parseargs at 35
fields = fieldnames(X);
Error in ==> samexaxis at 33
Args=parseArgs(varargin,Args,{'abc' 'join' 'ytickanticlash'});

Olivier Berne

Needs the parseArgs routine downloadable here.

jon erickson

The example worked as expected. I also downloaded subaxis, and am wondering if you can provide an example like Carles asked for: a 3 x 2 tiled plot using sameaxis and subaxis? that would be very helpful. Thanks!

Dimitri Shvorob

I am running the author's sample with the 4/14/05 update of samexaxis and Matlab 7.0.4, but get an error:

??? Undefined command/function 'parseArgs'.

Error in ==> samexaxis at 33
Args=parseArgs(varargin,Args,{'abc' 'join' 'ytickanticlash'});

Error in ==> test at 4

An extremely useful function, I wish it worked :(

Aslak Grinsted

Carlos try this: (download subaxis)

subaxis(3,2,1,'sv',0,'sh',0.1) %vertical spacing = zero, horizontal spacing=10%
for ii=2:6
subaxis(ii) %it remembers the settings you made the first subplot with
set(subaxis(5),'xticklabelmode','auto') %samexaxis might turn ticks off ...

carles maria

Excellent job!
Nevertheless, i am having an error when i am trying to do it for 6 subplots (3 rows, 2 cols). I would like to get 2 x axis (one per col) and join the 3 subplots that each col has got. is that possible?

Dimitri Shvorob

Aslak, would it be possible, by any chance, to see an R13 version of your function, for poor souls not yet having Matlab 7?

Aslak Grinsted

I think the error is caused by you having a nonstandard version of sort.m on your path.

Anny-Claude Duval

Unfortunately,it is exactly what I was looking for. But I need some help. I'm not able to run the example. I obtained this error.

??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can
crash MATLAB and/or your computer.

Error in ==> ismember at 110
[s,idx] = sort(s(:));

Aslak Grinsted

Please rate this... It would be nice to receive some encouragement to keep submitting useful bits on matlabcentral.


Made more clever ytickanticlash. Should no longer hide lines exactly at the ylimits. And it will not change ylimits of axes that doesn't have ylim set to auto.

Bug fix: "abc" is not always treated as true.
"Join" argument now stretches subplots.
Changed example to use subplot instead of subaxis.

MATLAB Release
MATLAB 7 (R14)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today