No License

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

» Watch video

Highlights from
samexaxis (nice subplots with same x axis)

4.6 | 23 ratings Rate this file 65 Downloads (last 30 days) File Size: 5.36 KB File ID: #7169 Version: 1.0
image thumbnail

samexaxis (nice subplots with same x axis)


Aslak Grinsted (view profile)


17 Mar 2005 (Updated )

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

| Watch this File

File Information


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


MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (34)
26 Sep 2016 Rajesh Ranjan

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

Comment only
10 Aug 2015 Sreeyuth Lal

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

Comment only
27 Jul 2015 Lokesh Saharan

27 Jul 2015 Lokesh Saharan

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

Comment only
15 Jan 2015 ABID

ABID (view profile)

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

15 Jan 2015 ABID

ABID (view profile)

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

Comment only
02 Jun 2014 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]);

21 Mar 2014 Hugo

Hugo (view profile)

Thx, still working great!

03 Oct 2013 Bartek Luks

15 Nov 2012 Sachin

Sachin (view profile)

27 Jul 2012 Liang

Liang (view profile)

great work!

25 Jun 2012 Daniel H.

25 Jun 2012 Daniel H.

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

28 Nov 2011 Yohai

Yohai (view profile)

yes. Download it from

Comment only
04 Oct 2011 Filippo

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

Comment only
25 Jul 2011 Régis Dupuy

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

29 Mar 2011 J G

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.

10 Nov 2010 Pearl

Pearl (view profile)

28 Apr 2010 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.

18 Jan 2010 Kuo-Hsien

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


04 Dec 2008 Byron

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.

18 Jan 2008 Curtis Wood

Really nice function.

16 Oct 2007 Per Pemberton

Produces really neat plots

31 Jul 2007 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.

01 May 2007 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'});

08 Nov 2006 Olivier Berne

Needs the parseArgs routine downloadable here.

01 Sep 2006 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!

17 Jun 2006 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 :(

Comment only
07 Jul 2005 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 ...

Comment only
29 Jun 2005 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?

31 Mar 2005 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?

Comment only
31 Mar 2005 Aslak Grinsted

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

Comment only
30 Mar 2005 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(:));

29 Mar 2005 Aslak Grinsted

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

Comment only
21 Mar 2005

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

29 Mar 2005

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.

Contact us