Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Subplot matrix: remove dead space between plots

5,007 views
Skip to first unread message

Jim Rockford

unread,
May 29, 2007, 3:14:28 AM5/29/07
to
I have a 4x4 array of subplots that I'd like to make closer together
(i.e. eliminate dead space/gray space). The code that generates the
figure is

***********************************************
figure;
fig = gcf;
for index=1:16
pHandles(index)=subplot(4,4,index);
colormap(mycolormap); % 64x3 colormap
image(zeros(16,16)+33); % Each image is a 16x16 matrix
axis('square'); axis off;
end
set(fig,'DoubleBuffer','on');
***********************************************

Note: I initialize each subplot figure with a blank image, each of
which will be updated
as I collect data for my purposes.

There is quite a bit of dead space between the individual plots. Is
there a reasonably simple way to control the size of each square? I'm
looking for a single control parameter that I can tweak until I like
the sizing. I'm running Matlab 7.1 (sp3).

Thanks,
Jim

Peter Bone

unread,
May 29, 2007, 6:09:38 AM5/29/07
to

This will remove all white space. Play around with the values to give
the right amount.

set(gca,'Position',[0 0 1 1])

Jim Rockford

unread,
May 30, 2007, 2:22:35 AM5/30/07
to
On May 29, 6:09 am, "Peter Bone" <peterb...@hotmail.com> wrote:
> This will remove all white space. Play around with the values to give
> the right amount.
>
> set(gca,'Position',[0 0 1 1])

Thanks, but this doesn't quite do what I want. It did lead me in the
right direction though. It seems there is no easy, one-line
adjustment to "fatten up" the subplot windows. For each subplot I
used

handle(index) = subplot('Position',[left(index) bottom(index) width
height])

where I had to pre-define left(index) and bottom(index) for each of
the subplots. Pretty tedious stuff, but it works. Matlab really
could use a more convenient graphical interface for this purpose.

Jim


Steven Lord

unread,
May 30, 2007, 10:00:52 AM5/30/07
to

"Jim Rockford" <jim.ro...@gmail.com> wrote in message
news:1180506155.1...@q75g2000hsh.googlegroups.com...

You can do what you want through the figure's regular interface. Create a
set of subplot axes:


subplot(2,1,2); surf(peaks); subplot(2,1,1); plot(1:10)


Click on the arrow (Edit Plot) between the Print button (the button with a
printer as the icon) and the Zoom In button (whose icon is a magnifying
glass with a + symbol in it) in the figure toolbar.

Click on the axes whose position you want to adjust, then drag the corners
or sides of that axes by clicking on the selection handles.

Once you've set up the layout the way you want, if you want to use that
layout for more figures, go to the File menu and select "Generate M-File".
You can then use the generated M-file on its own, or pull out the
appropriate pieces and copy them into your program. You can also save the
figure as a FIG file and reopen it with OPENFIG when you want to use it
later.

--
Steve Lord
sl...@mathworks.com


Seth

unread,
Jun 6, 2011, 9:14:04 PM6/6/11
to
"Steven Lord" <sl...@mathworks.com> wrote in message <f3k02k$n6s$1...@fred.mathworks.com>...

I think what would be really nice is if Matlab didn't put so much default white space in between subplots. Steve's solution works, but you have to go in and edit by hand. This is incredibly annoying and time consuming. Matlab should just default to much, much less white space in between subplots. I am constantly manually editing subplots to decrease the white space. I have so many routines that create figures automatically, I just wish ML would make the default spacing smaller. What is even more annoying, is if you set the x-axis ticks to not display, it has no effect on the amount of white space in between subplots. It just removes the labels without putting the subplots any closer together.

--Seth

Steven_Lord

unread,
Jun 6, 2011, 11:48:08 PM6/6/11
to

"Seth " <seth.g.cl...@aero.org> wrote in message
news:isju0s$6dp$1...@newscl01ah.mathworks.com...


> "Steven Lord" <sl...@mathworks.com> wrote in message
> <f3k02k$n6s$1...@fred.mathworks.com>...

*snip*

> I think what would be really nice is if Matlab didn't put so much default
> white space in between subplots. Steve's solution works, but you have to
> go in and edit by hand. This is incredibly annoying and time consuming.
> Matlab should just default to much, much less white space in between
> subplots. I am constantly manually editing subplots to decrease the white
> space. I have so many routines that create figures automatically, I just
> wish ML would make the default spacing smaller. What is even more
> annoying, is if you set the x-axis ticks to not display, it has no effect
> on the amount of white space in between subplots. It just removes the
> labels without putting the subplots any closer together.

That sounds like a perfectly reasonable enhancement request to submit to
Technical Support. Explain how frequently you need to modify your figures to
reduce the space and explain how much space you include around an axes in
your application.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Kacey

unread,
Aug 11, 2011, 2:24:28 PM8/11/11
to
"Seth" wrote in message <isju0s$6dp$1...@newscl01ah.mathworks.com>...

Hi there,

I'm just beginning to learn Matlab programming. For viewing my data, I need to create a 16 x 8 array of subplots which I have done. But the catch is modifying and resizing all of the subplots simultaneously with a simple statement. I'm having a hell of a time doing this. I want to designate my own x-axis limits without having to click on every single graph. Also, I'm trying to get rid of the dead space between all 128 of my subplots, which seems from your previous conversations that I have to do that manually anyhow. Do you know how to set the x-axis limits at least without having to modify every subplot in the generated code?

Please let me know if you are able to help me out.

Thanks for your time.

Kacey

Christopher Hummersone

unread,
Sep 29, 2011, 12:52:28 PM9/29/11
to
Jim (and Kacey, assuming you're using similar code to Jim),

You could just add these lines into your loop (after the line with the AXIS commands):

sub_pos = get(gca,'position'); % get subplot axis position
set(gca,'position',sub_pos.*[1 1 1.2 1.2]) % stretch its width and height

Chris

Jim Rockford <jim.ro...@gmail.com> wrote in message <1180422868.1...@h2g2000hsg.googlegroups.com>...

Aurélien BERTIN

unread,
Jun 22, 2012, 9:14:07 AM6/22/12
to
Dear all,

I had the same problem and found a convenient "patch".

I first use instead of simply figure that :
scrsz = get(0,'ScreenSize'); % left, bottom, width, height
figure('position',[1 scrsz(4)/100 scrsz(3)/0.5 scrsz(4)]);

You get there a full screen size figure (useful for the next part).

After, I used Christopher's patch to close the second and the fourth subplot (I have 4 plots) from the first and the third. To do that, after the second and the fourht subplot, I've written:
sub_pos = get(gca,'position'); % get subplot axis position
set(gca,'position',sub_pos.*[0.7 1 1 1]) % stretch its width and height

Note the sub_pos patch only minimize the 'left' component of subplot position, and it's sufficient to close the graphes.

Finally, I use the 'export_fig.m' function, which is amazing, and produces a really nice figure with edges croped. Set the background to 'w', and the image is perfect, as the quality.

Hope it will help! Regards

"Christopher Hummersone" <christopher...@surrey.ac.uk> wrote in message <j627oc$9oo$1...@newscl01ah.mathworks.com>...

Christopher Hummersone

unread,
Jun 22, 2012, 9:37:05 AM6/22/12
to
To clarify, set the figure colour to 'w' when using export_fig:

set(gcf,'color','w')

If producing publication-quality graphics is the intention, then setting the figure size in this way seems like a bit of a fudge; a quick-and-dirty solution. If you want figures to typeset nicely (with appropriately-sized text) then you should set the figure size accordingly (and use export_fig). For example, if I were publishing a figure that had to fit in a 7" column, then set the figure to be 7 inches wide (and say, 4" tall):

figure('units','inches','position',[6 6 7 4],'color','w')

The set your text to be 9 points, for example:

set(gca,'fontsize',9)

Then when you export (using export_fig) and place the result in your document with 1:1 sizing (taking the DPI into account if using a raster format), you know the figure will be no more than 7" wide, and the text will be 9 points on the page.

Yes, this makes sizing the subplots a bit more tricky, but it removes all of the other guess work that would otherwise be required when producing decent-looking graphics.

Chris

"Aurélien BERTIN" <ber...@ipgp.fr> wrote in message <js1r2v$a8q$1...@newscl01ah.mathworks.com>...

Aurélien BERTIN

unread,
Jun 22, 2012, 10:05:07 AM6/22/12
to
Thanks for the tip!

I was indeed using set(gcf,'Color','w');, I wasn't clear enough!

Regards

"Christopher Hummersone" <christopher...@surrey.ac.uk> wrote in message <js1se1$ghp$1...@newscl01ah.mathworks.com>...

Per Magne Knutsen

unread,
Oct 25, 2012, 9:18:08 PM10/25/12
to
Jim,
I realize this is an old (!) post but after reading it having a similar issue and not finding a satisfactory answer in this thread I wrote a small function to expand and contract all axes in a figure.

function subplotsqueeze(hFig, nF)
% Stretch width and height of all subplots in a figure window
% subplotsqueeze(H, F) will stretch subplots in figure with handle H by the
% proportional factor F.
%
% Examples:
% subplotsqueeze(gcf, 1.2) will expand all axes by 20%
% subplotsqueeze(gcf, 0.8) will contract all axes by 20%
%
% Expansion and contraction is equal in both directions and axes remain
% centered on their current locations.
%
hAx = findobj(hFig, 'type', 'axes');
for h = 1:length(hAx)
vCurrPos = get(hAx(h), 'position'); % current position
set(hAx(h), 'position', (vCurrPos.*[1 1 nF nF])-[vCurrPos(3)*(nF-1)/2 vCurrPos(4)*(nF-1)/2 0 0]);
end
return

Diandra Fabre

unread,
Jul 2, 2016, 9:20:12 AM7/2/16
to
Thank you for your subplotsqueeze function, it worked perfectly !

"pk78912" wrote in message <k6cocg$an6$1...@newscl01ah.mathworks.com>...

Jessica Garwood

unread,
Nov 16, 2016, 8:41:09 PM11/16/16
to
I noticed that sometimes combining multiple subplots produces less white space, even if the math suggests the proportions should be the same. It can be a quick fix.

With your code, for each of your plots, you could combine 4 plots in a 8X8 figure:

fig = gcf;
clf
for index=[1:2:8, 17:2:24, 33:2:40, 49:2:56]
pHandles(index)=subplot(8,8,[index:index+1, index+8:index+9]);
colormap(mycolormap); % 64x3 colormap
image(zeros(16,16)+33); % Each image is a 16x16 matrix
axis('square'); axis off;
end
set(fig,'DoubleBuffer','on');

Cheers,

Jess

Lionel Trébuchon

unread,
Apr 19, 2017, 8:07:09 PM4/19/17
to
Thanks for the function Mr. Knutsen!

It also worked perfectly for me.

Greetings,
Lionel
0 new messages