No BSD License  

Highlights from
samexaxis (nice subplots with same x axis)

4.6

4.6 | 21 ratings Rate this file 85 Downloads (last 30 days) File Size: 5.36 KB File ID: #7169
image thumbnail

samexaxis (nice subplots with same x axis)

by

 

17 Mar 2005 (Updated )

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

| Watch this File

File Information
Description

Features:

* 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)
 
 
  Example:
subplot(3,1,1);
plot(randn(100,1),randn(100,1),'x');
ylabel('QF')
subplot(3,1,2);
plot(randn(100,1),randn(100,1)*10,'x');
ylabel('HT');
subplot(3,1,3);
plot(randn(100,1),randn(100,1)*33,'x');
ylabel('DV');

samexaxis('abc','xmt','on','ytac','join','yld',1)

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 (28)
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,

subplot(211)
plot(randn(1,100));
set(gca,'YLim',[-3 3]);
subplot(212)
plot(randn(1,100));
set(gca,'YLim',[-4 4]);
samexaxis('xmt','off','ytac','yld',1,'box','on','join')

21 Mar 2014 Hugo

Thx, still working great!

03 Oct 2013 Bartek Luks  
15 Nov 2012 Sachin  
27 Jul 2012 Liang

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

yes. Download it from
http://www.mathworks.com/matlabcentral/fileexchange/3448-parse-function-arguments

04 Oct 2011 Filippo

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

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

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  
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:

subaxis(1,2,3,'sv',0,'sh',0.1)
spacing=10%
plot(1:6,randn(6,1))
for ii=2:6
subaxis(ii)
with
plot(1:6,randn(6,1))
end
samexaxis('abc','ytac')
set(subaxis(5),'xticklabelmode','auto')
set(subaxis(6),'xticklabelmode','auto')

Thanks.

04 Dec 2008 Byron

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:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=3696&objectType=file
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
samexaxis('abc','xmt','on','ytac','join','yld',1)

An extremely useful function, I wish it worked :(

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%
plot(1:6,randn(6,1))
for ii=2:6
subaxis(ii) %it remembers the settings you made the first subplot with
plot(1:6,randn(6,1))
end
samexaxis('ytac')
set(subaxis(5),'xticklabelmode','auto') %samexaxis might turn ticks off ...
set(subaxis(6),'xticklabelmode','auto')

29 Jun 2005 carles maria

hello
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?

31 Mar 2005 Aslak Grinsted

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

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.

Updates
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