File Exchange

image thumbnail


version (9.75 KB) by Aditya
Customize spaces between subplots in a figure


Updated 03 Jun 2013

View Version History

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.

Cite As

Aditya (2021). Spaceplots (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (30)

G. M. Wolfe

Shadowing a canned MATLAB function like subplot with a custom modified version is . . . less than ideal.

Leonardo Marín

Completely rubish.

Al in St. Louis

@tastygrain, it works perfectly for me. It is exactly what I needed.

Nathaniel Werner

Jannik Huber

In general the function works fine. But if you export the figure to eps there is still the problem, that the ylabel get lost :( Did anyohne have the same problem and was able to fix it?

pepe pepe

Thomas Marullo

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

Thomas Marullo

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


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


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


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


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




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



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.

Tareq Abuaisha

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:




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

Sebastian Hölz

Excellent !


Works very good. Thanks!



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.


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.

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: subtightplot

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!