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.)
Aslak Grinsted (2021). Subaxis - Subplot (https://www.mathworks.com/matlabcentral/fileexchange/3696-subaxis-subplot), MATLAB Central File Exchange. Retrieved .
Inspired: smplot for small multiples, Flow Cytometry GUI for Matlab, subtightplot, Continuous Sound and Vibration Analysis, Ogive optimization toolbox
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
used it to reduce the spacing in my subplots. Worked like a charm even when used together with the "addaxis' function. Aslak you saved the day!
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!
Thanks it works great.
Thanks!
I had the same error as reported by Richard below. Replaced a wrong version of the parseArgs function and then subaxis worked nicely.
It works well, nice.
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
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)
spaceplots
awesome!
great code, mange tak
Corentin
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
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
This function has been very useful! thanks!
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]');
-------------------------------
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.
I wish it could handle things like
subaxis(5,7,2:5)
Great function! I'm having trouble with multiple rows and columns though - do you have an example? Thanks
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.
Very usefull and handy function.
I'm just missing functionality like
subplot(2,2,[1 3])
This saved me much time, and I doubt I'll be going back to subplot.
sexiest file exchange program ever.
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!
Very useful, thanks
Thanks for your sharing.
Just great. It was exactly what I was looking for. Thanks!
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.
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.
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...
good job !
Great Matlab programming. I'm not using subplot anymore...
I also enjoyed parseArgs, which is very useful to include in my own mfiles.
A worthy replacement to subplot, which is not nearly flexible enough. Being able to span cells is great!
Works great!
Thanks