Code covered by the BSD License  

Highlights from


4.6 | 13 ratings Rate this file 158 Downloads (last 30 days) File Size: 9.75 KB File ID: #35464
image thumbnail




05 Mar 2012 (Updated )

Customize spaces between subplots in a figure

| Watch this File

File Information

Matlab's standard "subplot" function leaves large blank spaces around the plot boxes and gives the user no control over them. Spaceplots is an elementary function to get control over these spaces. I wanted to save space by making tight subplots, so I wrote this function. Thought it will be useful to others too.

>> Create your figure first, and then use spaceplots. It is a very non-intrusive function.

>> Works well with all subplot arrangements (including a mixture of large and small plots)

>> Preserves the axes line-up in the grid by manipulating insets properly

>> Does not work with multiple plots created in a figure by any function other than the Matlab default "subplot" function.

I hope the function is simple enough for anyone to rectify errors or make changes that suit them.


This file inspired Subtightplot.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (17)
06 Oct 2014 FeedMeImATroll

Is there a way to connect different subplots, as in subplot(4,2,[1,3,5])?

14 Jul 2014 V_oron

Hello! I would like to report a bug. In some case of figure deleting one get the error in function "removeAxesFromGrid(p,ax)" at command "rmappdata(p,'SubplotListeners')" -- this field does not exist. So, I propose to add a verification via "isappdata".

31 May 2013 Aditya

With v3.0, the earlier errors with legend and colorbars will now disappear!

03 Apr 2013 Christine A.

For those who get the error
"Attempted to access GridLeft...index must be a positive integer or logical", you need to call spaceplots BEFORE changing axis ticks, or adding colorbar, etc... For example for the colorbar, try

a2=subplot(2,1,2); scatter(rand(10,1),rand(10,1),5,rand(10,1))
axes(a1); colorbar
axes(a2); colorbar

08 Jan 2013 simbarashe


04 Jan 2013 Bryan

When I use image or imagesc with a colour bar I get the following error:

Attempted to access GridLeft(0,4); index must be a positive integer or logical.

Error in spaceplots (line 228)
axLeft = GridLeft(r0,c0) + InsetLeft(r0,c0);

Try this code as an example:

for i = 1:8
subplot(2, 4, i);
imagesc(rand(100, 10));
spaceplots([.01 .01 .01 .01], [.02 .02]);

26 Dec 2012 Laurent  
06 Nov 2012 Felix

With the axis ticks disables (set(gca,'xtick',[],'ytick',[])) I receive an error in spaceplots 228: "Attempted to access GridLeft(0,12); index must be a positive integer or logical.". Does anyone know how to fix it? I want to have all the plots directly adjacent without any visible axis or ticks in between.

05 Nov 2012 Tareq Abuaisha

Thanks a lot man :-)
I really need this function, the sapce between subplots was only wasted space!!!

02 Aug 2012 Christine A.

Excellent! This is a very useful function. Note that, to save the figure exactly as it appears on the screen, you need this extra line:


17 Jul 2012 David  
14 Jul 2012 Jochen

Thanks a lot Aditya for taking the vodoo out of subplots! ;)

29 Jun 2012 Sebastian Hölz

Excellent !

09 Jun 2012 Ben

Works very good. Thanks!

09 May 2012 Alkis  
17 Apr 2012 Aditya

Hi Ralph,

Thanks for your feedback. I've updated the file with your suggestion and a couple of minor fixes.

I'm curious though, I've never had the problem you mentioned during my use. Comparing floats is bound to crash when you are comparing values of two variables that have been computed. But in this case essentially it is the exact same value assigned to the variables being compared, in which case there should be no problem.

10 Apr 2012 Ralph

Great function. I like the idea of simplicity and non-obtrusiveness. On first use, it failed on my system (therefor four stars instead of five). This was because of this condition evaluating to false where it should have been true:

if axPos(4) == defAx(4)

Comparing floats can be a dangerous thing. For improved robustness you might want to consider rewriting this into

if abs(axPos(4)-defAx(4)) < eps

The same holds for the similar check a couple of lines below.

17 Apr 2012

1. Fixed the issues occurring in successive uses of the function on the same figure.

17 Apr 2012

1. Fixed issues arising from successive uses of the function on the same figure.

31 May 2013

v3.0: Uses undocumented Matlab property 'LooseInset' which makes the function much simpler and cleaner.

03 Jun 2013

Included subplot.m for v3

Contact us