File Exchange

image thumbnail


version 1.5 (9.75 KB) by

Customize spaces between subplots in a figure

16 Ratings



View License

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.

Comments and Ratings (25)

pepe pepe

@tastygrain, what I mean is that plot types are being removed but not put back in.

This is great but it doesn't work when the overall plot is not symmetrical, ie, 2 rows, first row is 1 plot spanning 2 cols, second row is 1 plot in each col. Your code deletes the 1st plot in row 1.


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


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

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


Emma (view profile)

I had the same problem as Matt Rippa.

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)

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


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


Aditya (view profile)

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

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




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



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.

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

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:



David (view profile)


Jochen (view profile)

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


Sebastian Hölz

Excellent !


Ben (view profile)

Works very good. Thanks!


Alkis (view profile)


Aditya (view profile)

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.


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.



Included subplot.m for v3


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


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


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

MATLAB Release
MATLAB 7.4 (R2007a)

Inspired: subtightplot

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video