Code covered by the BSD License  

Highlights from
tight subplot

4.46154

4.5 | 14 ratings Rate this file 185 Downloads (last 30 days) File Size: 1.76 KB File ID: #27991
image thumbnail

tight subplot

by Pekka Kumpulainen

 

23 Jun 2010

Fills the figure with axes subplots with easily adjustable margins and gaps between the axes.

| Watch this File

File Information
Description

Subplot tends to leave a lot of empty space around the axes. Tight_subplot allows to adjust the spacing between the axes as well as the margins around the axes.
For example:
ha = tight_subplot(3,2,[.01 .03],[.1 .01],[.01 .01])
for ii = 1:6; axes(ha(ii)); plot(randn(10,ii)); end
set(ha(1:4),'XTickLabel',''); set(ha,'YTickLabel','')

MATLAB release MATLAB 7.10 (2010a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (15)
02 Jul 2010 Alex

i dont know what mathworks was thinking with their subplot routine... this is much nicer

11 Aug 2010 Alexander

Thank you for this contribution. I like it very much.
In my opinion the input check should be like that:
if nargin<3 || isempty(gap); gap = .02; end
if nargin<4 || isempty(marg_h); marg_h = .05; end
if nargin<5 || isempty(marg_w); marg_w = .05; end

So you can modify marg_w and take the defaults for the other parameters.
tight_subplot(nrows,ncolumns,[],[],[.1 .05]);

15 Sep 2010 Thomas

Good work. I needed to create several plots with several sub-plots within a loop. Had trouble with empty plots as the result and Matlab was broke until I restarted it. Even a plot(1:100, rand(1, 100)) would return an empty figure. Anyway, I returned the value for the axes position matrix "ha", and removed the internal axes command. When I was ready to plot in one of the figures I had being created to a specific subplot location I called:

figure(h4);
subplot('Position', ha4(fc4).pos); p=plot(1:100, rand(1, 100));

where ha4(fc4).pos corresponds to the fc4'th subplot position in figure with handle "h4".

18 Oct 2010 Einar Olason

Excellent little script! Thanks a lot!

Still, I have one suggestion: Instead of saying "axes(ha(ii));" in the example a better way may be to say "subplot(ha(ii));". Using axes gave me unpredictable behaviour when setting up two plots side by side. I would (almost always) plot to the axes on the right, no mater how often I tried giving the "axes(ha(1))" command (or "axes(ha(2))" for that matter). Using subplot solved that. Don't know why though ...

26 Dec 2010 kos go  
27 Feb 2011 Brent Boehlert  
27 Feb 2011 Brent Boehlert

Oops -- didn't know that rating the submission meant submitting. Excellent function that saved me tons of time in having to generate graphics through GIS. No clue why the subplot command has this issue, but thanks for addressing it!

28 Feb 2011 Dan

Nice function, and one that I've come to rely on. It would be great if axes could span multiple columns/rows as you can do using the built-in subplot. Otherwise, great.

10 May 2011 Carlos Baiz  
10 Jan 2012 Matthew Schroeder  
10 Jan 2012 Matthew Schroeder

Great work. Would be nice to be able to have the subplots fill a uipanel instead of the entire figure (doesn't play with within a GUI).

17 Jan 2012 Ravi

Thanks a lot for the script.

01 Mar 2012 Rakesh Chalasani  
17 Apr 2012 Karl

I second what Dan said. Spanning multiple columns/rows would be really useful. For example:
ha = tight_subplot(2,2,0.1,0.1,0.1)
axes(ha(1));plot(x1,y1);
axes(ha(2));plot(x2,y2);
axes(ha([3 4]))plot(x3,y3);

17 Apr 2012 Aditya

Hey,

Great function. Pity I came across it after I spent much of my time writing a similar function. As some people have said above, I wanted the function to work with multiple rows/columns spanning subplots. So I included that capability in the following function:

http://www.mathworks.com/matlabcentral/fileexchange/35464-spaceplots

Just thought it might be useful to some.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
plot Pekka Kumpulainen 23 Jun 2010 10:32:14
subplot Pekka Kumpulainen 23 Jun 2010 10:32:14
visualization Pekka Kumpulainen 23 Jun 2010 10:32:14
plot kuoping 23 Jun 2010 11:01:15
subplot Fabrice 08 Jul 2011 05:30:54
subplot Peter 22 Nov 2011 09:45:05
plot Superb 05 Apr 2012 12:31:19
plot Tailai 21 May 2012 20:45:26

Contact us at files@mathworks.com