Code covered by the BSD License  

Highlights from
Spaceplots

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

Spaceplots

by

Aditya

  • 1 file
  • 139 downloads
  • 4.61538

 

05 Mar 2012 (Updated )

Customize spaces between subplots in a figure

| Watch this File

File Information
Description

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.

Acknowledgements

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 (19)
27 Jan 2015 Emma

Emma

I had the same problem as Matt Rippa.

Comment only
20 Jan 2015 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)
createListeners(p,handle(list));

Error in subplot (line 467)
addAxesToGrid(ax,nrows,ncols,row,col,position);

Error in Main (line 45)
subplot(NPlots,2,ax);

06 Oct 2014 FeedMeImATroll

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

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

  • 1 file
  • 139 downloads
  • 4.61538

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

Comment only
03 Apr 2013 Christine A.

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

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

08 Jan 2013 simbarashe

simbarashe

Outstanding!

04 Jan 2013 Bryan

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:

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

26 Dec 2012 Laurent

Laurent

 
06 Nov 2012 Felix

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

Tareq Abuaisha

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

02 Aug 2012 Christine A.

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:

set(gcf,'PaperPositionMode','auto');

17 Jul 2012 David

David

 
14 Jul 2012 Jochen

Jochen

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

Comment only
29 Jun 2012 Sebastian Hölz

Sebastian Hölz

Excellent !

09 Jun 2012 Ben

Ben

Works very good. Thanks!

09 May 2012 Alkis

Alkis

 
17 Apr 2012 Aditya

Aditya

  • 1 file
  • 139 downloads
  • 4.61538

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

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.

Updates
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