Code covered by the BSD License  

Highlights from

4.4 | 15 ratings Rate this file 128 Downloads (last 30 days) File Size: 9.75 KB File ID: #35464 Version: 1.5
image thumbnail



Aditya (view profile)

  • 1 file
  • 4.35714


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 (22)
13 Oct 2015 tastygrain

I edited the spaceplots code to work with the default version of subplot.m for a colleague using 2014b. It seems to work so far, but I haven't had a lot of access to thoroughly test it. I uploaded it here:!70JXSZAT!n2HV5YjrrfQONSj_iqv5U9ZtKwUOVi9uff7OM4JLBTc

Comment only
25 Mar 2015 Simon

Simon (view profile)

Unfortunately spaceplots is no longer useful due to it's incompatibility with R2015a. Would love to see an update for it as it was fantastic for earlier versions of MatLab

12 Mar 2015 Brian Dombeck

Ditto the issues of Matt Rippa and Emma before. Would be nice to have this updated for R2014b (or I guess even 2015a, now...)

Comment only
27 Jan 2015 Emma

Emma (view profile)

I had the same problem as Matt Rippa.

Comment only
20 Jan 2015 Matt Rippa

Hello. It looks like subplot packaged with spaceplots_v3 is not compatible with R2014b. Works really great with R2014a!

No constructor 'handle.listener' with matching signature found.

Error in subplot>createListeners (line 495)
list = [...

Error in subplot>addAxesToGrid (line 549)

Error in subplot (line 467)

Error in Main (line 45)

06 Oct 2014 FeedMeImATroll

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

Comment only
14 Jul 2014 V_oron

V_oron (view profile)

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

Aditya (view profile)

  • 1 file
  • 4.35714

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

Comment only
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

Bryan (view profile)

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

Felix (view profile)

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

David (view profile)

14 Jul 2012 Jochen

Jochen (view profile)

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

Comment only
29 Jun 2012 Sebastian Hölz

Excellent !

09 Jun 2012 Ben

Ben (view profile)

Works very good. Thanks!

09 May 2012 Alkis

Alkis (view profile)

17 Apr 2012 Aditya

Aditya (view profile)

  • 1 file
  • 4.35714

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.

Comment only
10 Apr 2012 Ralph

Ralph (view profile)

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

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

17 Apr 2012 1.2

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

31 May 2013 1.4

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

03 Jun 2013 1.5

Included subplot.m for v3

Contact us