Code covered by the BSD License  

Highlights from
Subaxis - Subplot

4.86207

4.9 | 30 ratings Rate this file 243 Downloads (last 30 days) File Size: 3.55 KB File ID: #3696
image thumbnail

Subaxis - Subplot

by

 

08 Jul 2003 (Updated )

Use HTML jargon for multi axis layout.

| Watch this File

File Information
Description

SUBAXIS Create axes in tiled positions. (just like subplot)
    Usage:
       h=subaxis(rows,cols,cellno[,settings])
       h=subaxis(rows,cols,cellx,celly[,settings])
       h=subaxis(rows,cols,cellx,celly,spanx,spany[,settings])
 
  SETTINGS: Spacing,SpacingHoriz,SpacingVert
            Padding,PaddingRight,PaddingLeft,PaddingTop,PaddingBottom
            Margin,MarginRight,MarginLeft,MarginTop,MarginBottom
            Holdaxis
 
            all units are relative (e.g from 0 to 1)
 
            Abbreviations of parameters can be used.. (Eg MR instead of MarginRight)
            (holdaxis means that it wont delete any axes below.)
 
 
  Example:
 
    >> subaxis(2,1,1,'SpacingVert',0,'MR',0);
    >> imagesc(magic(3))
    >> subaxis(2,'p',.02);
    >> imagesc(magic(4))
 
  2001 / Aslak Grinsted (Feel free to modify this code.)

Acknowledgements

This file inspired Flow Cytometry Gui For Matlab, Continuous Sound And Vibration Analysis, and Subtightplot.

MATLAB release MATLAB 6.1 (R12.1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (37)
05 Jun 2014 Richard Hodges

I tried the first example line (in ML 2014a):

subaxis(2,1,1,'SpacingVert',0,'MR',0);

I got an error:
Error using parseArgs (line 111)
Unknown named parameter: spacingvert

Error in subaxis (line 45)
Args=parseArgs(varargin,Args,{'Holdaxis'},{'Spacing' {'sh','sv'}; 'Padding' {'pl','pr','pt','pb'}; 'Margin' {'ml','mr','mt','mb'}});

What is wrong?

Richard

25 Apr 2014 Sam

I think a minor minor fix to Jakob's code is to change the

if numel(Args.NumericArguments{1}) == 2

line to

if numel(Args.NumericArguments{1}) > 1

in order to deal with inputs of type subplot(2,3,1:3)

25 Apr 2014 Sam  
31 Mar 2014 Subhashree Mishra  
25 Mar 2014 Leonardo Rolim  
18 Mar 2014 Aslak Grinsted

spaceplots

01 Nov 2013 Shaohan Hu

awesome!

07 Aug 2013 corentin

great code, mange tak
Corentin

21 May 2013 Jakob

I slightly modified Matthias code below to make it work for an arbitrary long horizontal subregion subplot(m,n,x:y), e.g. subplot(2,3,1:3) or subplot(3,3,4:9):

if numel(Args.NumericArguments{1}) == 2 % restore subplot(m,n,[x y]) behaviour
[x1 y1] = ind2sub([Args.cols Args.rows],Args.NumericArguments{1}(1)); % subplot and ind2sub count differently (column instead of row first) --> switch cols/rows
[x2 y2] = ind2sub([Args.cols Args.rows],Args.NumericArguments{1}(end));
else
x1=mod((Args.NumericArguments{1}-1),Args.cols)+1; x2=x1;
y1=floor((Args.NumericArguments{1}-1)/Args.cols)+1; y2=y1;
end

12 Mar 2013 Matthias

I could restore the subplot(2,2,[1 3]) behaviour by changing the case 1 in the switch length(Args.NumericArguments) block (line 64) to this:

if numel(Args.NumericArguments{1}) == 2 % restore subplot(m,n,[x y]) behaviour
[x1 y1] = ind2sub([Args.cols Args.rows],Args.NumericArguments{1}(1)); % subplot and ind2sub count differently (column instead of row first) --> switch cols/rows
[x2 y2] = ind2sub([Args.cols Args.rows],Args.NumericArguments{1}(2));
else
x1=mod((Args.NumericArguments{1}-1),Args.cols)+1; x2=x1;
y1=floor((Args.NumericArguments{1}-1)/Args.cols)+1; y2=y1;
end

12 Mar 2013 Matthias  
07 Mar 2013 Raul

This function has been very useful! thanks!

07 Mar 2013 Raul  
01 Feb 2013 Ka Ru

Thanks a lot for this great file
Some of you mentioned, that they would like to generate, subplots of the form
subaxis(2,2,1:2). Here a small example
how it can be done:
---------------------------
% Generate figure with three subplots
%
% |-plot1-| |-plot2-|
% |------plot3------|
% Define functions
t=0:0.05:20;
y1=sin(pi*t);
y2=cos(pi*t);
y3=y1.*exp(-0.1*t);
%Define spacings
spaceH=0.03;spaceV=0.15;marTop=0.08;marBot=0.1;
padding=0.025;margin=0.02;marginL=0.03;

% Generate plot
fig=figure(1);

subaxis(2,2,1,'SpacingHoriz', spaceH,
'SpacingVert',spaceV, 'PL',padding,'PR',padding,'mt',...
marTop,'mb',marBot,'ML',marginL,'MR',margin);
plot(t,y1);
title('Sin'); xlabel('t [s]');

subaxis(2,2,2,'SpacingHoriz', spaceH,
'SpacingVert',spaceV, 'PL',padding,'PR',padding,'mt',...
marTop,'mb',marBot,'ML',marginL,'MR',margin);
plot(t,y2);
title('Cos');xlabel('t [s]');

sub=subaxis(2,2,3,'SpacingHoriz', spaceH, 'SpacingVert',spaceV, 'PL',padding,'PR',padding,'mt',...
marTop,'mb',marBot,'ML',marginL,'MR',margin);
%change with of the third subplot
pos=get(sub,'position');
set(sub,'Position',[pos(1) pos(2) pos(3)*2.2 pos(4)],'color', [1 1 1]);
plot(t,y3);
title('Exp');xlabel('t [s]');
-------------------------------

30 Dec 2012 Rachael

This is a great program for better graphical control of subplots. I have received many positive comments from my colleagues and shared this program with them also.

09 Nov 2012 Wok  
07 Aug 2012 Neil

I wish it could handle things like

subaxis(5,7,2:5)

07 Aug 2012 Humberto Fioravante Ferro  
23 Apr 2012 J G

Great function! I'm having trouble with multiple rows and columns though - do you have an example? Thanks

17 Apr 2012 Karl

I'm in agreement with Toby. None of the functions on MATLAB central that address the spacing issue seem to handle spanning multiple columns/rows (tight_subplot.m,subplotSpacing.m or subaxis). The search continues.

01 Feb 2012 Toby Hijzen

Very usefull and handy function.
I'm just missing functionality like
subplot(2,2,[1 3])

08 Dec 2011 Dmitry

This saved me much time, and I doubt I'll be going back to subplot.

09 Oct 2011 Keegan

sexiest file exchange program ever.

15 Jul 2011 AMVR

Great work! It works great in general, although I'm having problems using subaxis to display images in their original aspect ratio. Let me explain, when displaying an image on a figure, I like asking the figure to display the image preserving the natural aspect ratio of the image.

I usually accomplish this by calling imshow, e.g.:

figure;
imshow(I, []);

Now, if I try to do this with subaxis (asking it to use 0 padding, 0 padding and 0 spacing):

--------------------
figure;
I = imread('pout.tif');

subaxis(1,2, 1, 'sh',0,'sv',0,'MR',0, 'ML', 0, 'MT', 0, 'MB', 0, 'PaddingTop', 0, 'PaddingBottom', 0, 'PaddingLeft', 0, 'PaddingRight', 0);

imshow(I', []);

subaxis(1,2, 1, 'sh',0,'sv',0,'MR',0, 'ML', 0, 'MT', 0, 'MB', 0, 'PaddingTop', 0, 'PaddingBottom', 0, 'PaddingLeft', 0, 'PaddingRight', 0);

imshow(I', []);
--------------------

You'll see that the figure displays the images correctly (i.e. it preserves their aspect ratio), BUT there is white space around the images within the figure, even though I explicitly asked subaxis to remove whitespace in the figure.

Any thoughts?

Other that, it works like a charm. Thanks!

29 Apr 2011 Oscar

Very useful, thanks

07 Apr 2011 Dan Knudsen  
10 Nov 2010 Pearl  
17 Dec 2009 Jeff Robble  
07 Nov 2009 Raymond Cheng

Thanks for your sharing.

28 Jul 2008 Andrzej Lewarek

Just great. It was exactly what I was looking for. Thanks!

22 Feb 2008 Eduardo Gonzalez

Just great. I don't use subplot any more. Useful not just for subplots but to eliminate the blank space around the graphic in one plot.

30 Jan 2008 Stine Gro Jensen

This is just what I needed! Great! I fully agree with the other reviewers. Take your time to work it out, and you will never use subplot again.

29 Jul 2007 jon erickson

Awesome--far superior to subplot. This function is a slightly tricky to use at first, but a few minutes of practice/playing around and you'll never go back to subplot.
mathworks should make this standard...

09 Jan 2006 antoine stephany

good job !

30 Aug 2005 Marcello Vichi

Great Matlab programming. I'm not using subplot anymore...
I also enjoyed parseArgs, which is very useful to include in my own mfiles.

29 Apr 2005 Janik Zikovsky

A worthy replacement to subplot, which is not nearly flexible enough. Being able to span cells is great!

28 Apr 2004 Stephan Zeipel

Works great!
Thanks

Updates
09 Jul 2014

Added suggestions from file exchange comments.

Contact us