Code covered by the BSD License  

Highlights from
tight subplot

4.7 | 35 ratings Rate this file 411 Downloads (last 30 days) File Size: 1.76 KB File ID: #27991 Version: 1.0
image thumbnail

tight subplot



Fills the figure with axes subplots with easily adjustable margins and gaps between the axes.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

Subplot tends to leave a lot of empty space around the axes. Tight_subplot allows to adjust the spacing between the axes as well as the margins around the axes.
For example:
ha = tight_subplot(3,2,[.01 .03],[.1 .01],[.01 .01])
for ii = 1:6; axes(ha(ii)); plot(randn(10,ii)); end
set(ha(1:4),'XTickLabel',''); set(ha,'YTickLabel','')


This file inspired Subtightplot, Tight Plots, Correlation Matrix Scatterplot, and Smplot For Small Multiples.

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (39)
14 Dec 2015 K E

K E (view profile)

You can find tightfig.m here,

Comment only
13 Dec 2015 pingfanyu

Undefined function or variable 'tightfig'.
Why i encountered this problem and how can i do?

Comment only
29 Oct 2015 Natan

Natan (view profile)

this is one of the most useful FEX contributions I have ever encountered. Thank you for the effort!

28 Apr 2015 Ashley Zhang

Excellent function!
Yet I have one problem. When I plot odd number of subplot, say 7 plot arrayed as 4*2, there is always one blank plot that I cannot remove even though I delete the last value in ha.
Any one have any idea to fix it?

* Problem fixed by setting axis, XColor and YColorthe of the last plot invisible.

subplot(ha(8)); set(gca,'Color','none','XColor','none','YColor','none');

11 Feb 2015 Naveen Gowdayyanadoddi

Excellent function. Works fine for me.

06 Feb 2015 Tero

Tero (view profile)

06 Oct 2014 FeedMeImATroll

Excellent function, thanks. The one thing I am missing: Is there a way to connect different subplots, as in subplot(4,2,[1,3,5])?

Comment only
03 Aug 2013 hadi

hadi (view profile)

17 Jul 2013 Petter

Petter (view profile)

tnx, great job

16 Jul 2013 Enrico Fonda  
23 Apr 2013 Rina

Rina (view profile)

13 Apr 2013 Panagiotis Papagiannopoulos

this file really helped me! however i have one question. i want to set fixed limits of the x,y axis in each subplot.For example: (axis([0 700 -500 10000])).i tried to do it but with no result. any advice of how can i do this?

11 Mar 2013 Ahmed Fasih  
18 Jan 2013 Beth

Beth (view profile)

27 Dec 2012 Felipe G. Nievinski

It's great that it offers variable outer margins in addition to inner gaps. I wish it could support asymmetric plots, where a panel spans multiple rows/columns, though.

01 Nov 2012 Lucas Koorneef  
26 Oct 2012 Philip Valencia

Nice, very handy, though by default I prefer my tick labels left on... others who want this might just mod the file.. just remove in the inner loop where it sets the ticks off... i.e.

for ix = 1:Nw
ii = ii+1;
ha(ii) = axes('Units','normalized', ...
'Position',[px py axw axh]);
px = px+axw+gap(2);

22 Oct 2012 Thomas

Thomas (view profile)

17 Sep 2012 Gal Haspel

useful and does what it claims

16 Aug 2012 Giovanni

This is a great script. I'm experiencing a bug that makes my ticks disappear. Here's a sample:

figure('position',[100 100 1000 600])
for a=ha'
xlim(a,[.1 10])
ylim(a,[-90 10])
% 1
scatter(ha(1),x11,10*log10(y11),30,[0 .5 0],'x');
scatter(ha(1),x12,10*log10(y12),30,[0 0 1],'filled');
% 2
scatter(ha(2),x21,10*log10(y21),30,[0 .5 0],'x');
scatter(ha(2),x22,10*log10(y22),30,[0 0 1],'filled');
% 3
scatter(ha(3),x31,10*log10(y31),30,[0 .5 0],'x');
scatter(ha(3),x32,10*log10(y32),30,[0 0 1],'filled');

I would expect to see xticks at .1, 1 and 10, but on my system they simply disappear. If I tried and add them manually the log scale gets screwed up, so there's gotta be another way around. I'm using Matlab R2012a.

09 Jul 2012 Marin

Marin (view profile)

Really excellent file, thank you. Maybe an extra example wouldn't be a bad idea in the comments for the new Matlab user to better learn to use this file.

I have two questions though if I may ask them here...

1. How do i add numbers on the y axis? (only on the rightmost side of course)

2. How do I write x & y labels? (only one that applies to all axis)

Thank you in advance.

02 Jul 2012 alexpapi  
28 Jun 2012 Kevin Claytor

Very nice, needed something exactly like this, and it's quite easy to use.

16 Jun 2012 Jiangmin zhang

i cannot do anything but give a five star! absolutely smarter than mathworks!

17 Apr 2012 Aditya

Aditya (view profile)

  • 1 file
  • 4.35714


Great function. Pity I came across it after I spent much of my time writing a similar function. As some people have said above, I wanted the function to work with multiple rows/columns spanning subplots. So I included that capability in the following function:

Just thought it might be useful to some.

17 Apr 2012 Karl

Karl (view profile)

I second what Dan said. Spanning multiple columns/rows would be really useful. For example:
ha = tight_subplot(2,2,0.1,0.1,0.1)
axes(ha([3 4]))plot(x3,y3);

01 Mar 2012 Rakesh Chalasani  
17 Jan 2012 Ravi

Ravi (view profile)

Thanks a lot for the script.

10 Jan 2012 Matthew Schroeder

Great work. Would be nice to be able to have the subplots fill a uipanel instead of the entire figure (doesn't play with within a GUI).

Comment only
10 Jan 2012 Matthew Schroeder  
10 May 2011 Carlos Baiz  
28 Feb 2011 Dan

Dan (view profile)

Nice function, and one that I've come to rely on. It would be great if axes could span multiple columns/rows as you can do using the built-in subplot. Otherwise, great.

27 Feb 2011 Brent Boehlert

Oops -- didn't know that rating the submission meant submitting. Excellent function that saved me tons of time in having to generate graphics through GIS. No clue why the subplot command has this issue, but thanks for addressing it!

27 Feb 2011 Brent Boehlert  
26 Dec 2010 kos go

kos go (view profile)

18 Oct 2010 Einar Olason

Excellent little script! Thanks a lot!

Still, I have one suggestion: Instead of saying "axes(ha(ii));" in the example a better way may be to say "subplot(ha(ii));". Using axes gave me unpredictable behaviour when setting up two plots side by side. I would (almost always) plot to the axes on the right, no mater how often I tried giving the "axes(ha(1))" command (or "axes(ha(2))" for that matter). Using subplot solved that. Don't know why though ...

15 Sep 2010 Thomas

Thomas (view profile)

Good work. I needed to create several plots with several sub-plots within a loop. Had trouble with empty plots as the result and Matlab was broke until I restarted it. Even a plot(1:100, rand(1, 100)) would return an empty figure. Anyway, I returned the value for the axes position matrix "ha", and removed the internal axes command. When I was ready to plot in one of the figures I had being created to a specific subplot location I called:

subplot('Position', ha4(fc4).pos); p=plot(1:100, rand(1, 100));

where ha4(fc4).pos corresponds to the fc4'th subplot position in figure with handle "h4".

11 Aug 2010 Alexander

Thank you for this contribution. I like it very much.
In my opinion the input check should be like that:
if nargin<3 || isempty(gap); gap = .02; end
if nargin<4 || isempty(marg_h); marg_h = .05; end
if nargin<5 || isempty(marg_w); marg_w = .05; end

So you can modify marg_w and take the defaults for the other parameters.
tight_subplot(nrows,ncolumns,[],[],[.1 .05]);

02 Jul 2010 Alex

Alex (view profile)

i dont know what mathworks was thinking with their subplot routine... this is much nicer

Contact us