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.

4.59091
23 Ratings

85 Downloads

Updated

No License

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)

Comments and Ratings (35)

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: http://www.mathworks.com/matlabcentral/fileexchange/3448-parse-function-arguments

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

ABID

ABID (view profile)

how can I make single ylable instead of two or three??
for refrence please see figure 5 on
http://pubs.acs.org/doi/pdf/10.1021/ja405844j

ABID

ABID (view profile)

how can I make single ylable instead of two or three??
for refrence please see figure 5 on
http://pubs.acs.org/doi/pdf/10.1021/ja405844j

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

Hugo

Hugo (view profile)

Thx, still working great!

Bartek Luks

Sachin

Sachin (view profile)

Liang

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!

Filippo

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

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

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.

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.

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.

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

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
samexaxis('abc','xmt','on','ytac','join','yld',1)

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%
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')

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?

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.

Updates

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