File Exchange

image thumbnail

Subaxis - Subplot

version 1.1.0.0 (3.55 KB) by Aslak Grinsted
Use HTML jargon for multi axis layout.

95 Downloads

Updated 09 Jul 2014

View License

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

Comments and Ratings (53)

Sagar

Sia Xie

Second Kristina's comment. Good code does what it's supposed to do. Great code does what it's supposed to do AND is documented.

Great code!

Can you give an explanation of what each setting changes? Fore example, what is the difference between margin and padding, and what is spacing measured in reference to? Thanks!

Thanks!

David Thoen

Thanks it works great.

mat

Thanks!

w du

I had the same error as reported by Richard below. Replaced a wrong version of the parseArgs function and then subaxis worked nicely.

Yu Jianyuan

Shai

Sébastien

It works well, nice.

dj327

I have the same problem as Ka Ru as I cannot reduce the horizontal spacing as well even if I specified Spacinghoriz to be zero.

Thanks it was helpful for me

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

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)

Sam

spaceplots

Shaohan Hu

awesome!

corentin

great code, mange tak
Corentin

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

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

Matthias

Raul

This function has been very useful! thanks!

Raul

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

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.

Wok

Neil

I wish it could handle things like

subaxis(5,7,2:5)

J G

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

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.

Toby Hijzen

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

Dmitry

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

Keegan

sexiest file exchange program ever.

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!

Oscar

Very useful, thanks

Dan Knudsen

Pearl

Jeff Robble

Thanks for your sharing.

Andrzej Lewarek

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

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.

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.

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

antoine stephany

good job !

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.

Janik Zikovsky

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

Stephan Zeipel

Works great!
Thanks

Updates

1.1.0.0

Added suggestions from file exchange comments.

MATLAB Release Compatibility
Created with R12.1
Compatible with any release
Platform Compatibility
Windows macOS Linux