Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
get rid of the white space around matlab's SUBFIGURE

Subject: get rid of the white space around matlab's SUBFIGURE

From: Emma Robertson

Date: 2 Nov, 2011 13:05:28

Message: 1 of 5

Hi,

Can someone please tell me how to get rid of the extra spaces around Matlab's figure which consists of several subfigures? That is, when I 'eps' and 'pdf' the subfigures, I don't want the subfigure to take more area than the 'actual' plot itself.

Also, is it possible to get rid of the gaps after I have plotted the figure? or do I have to set up the figure's dimensions first and then plot it on top?

I hope I explained my question clearly.

Thanks,
Emma

Subject: get rid of the white space around matlab's SUBFIGURE

From: matt dash

Date: 2 Nov, 2011 17:11:27

Message: 2 of 5

"Emma Robertson" wrote in message <j8rf6n$i4b$1@newscl01ah.mathworks.com>...
> Hi,
>
> Can someone please tell me how to get rid of the extra spaces around Matlab's figure which consists of several subfigures? That is, when I 'eps' and 'pdf' the subfigures, I don't want the subfigure to take more area than the 'actual' plot itself.
>
> Also, is it possible to get rid of the gaps after I have plotted the figure? or do I have to set up the figure's dimensions first and then plot it on top?
>
> I hope I explained my question clearly.
>
> Thanks,
> Emma

I'm assuming by subfigure you mean the thing that you get when you use the subplot command. Try viewing the code for the plotmatrix command (edit plotmatrix)... it contains a double loop to make axes with a specified spacing around lines 90-120. (space=0.02 as the default). You could copy this code and change the spacing to whatever you need it to be. There is a similar line in subplot.m (inset=[...]) but that function is much more longer and harder to parse...

Subject: get rid of the white space around matlab's SUBFIGURE

From: Emma Robertson

Date: 2 Nov, 2011 17:45:28

Message: 3 of 5

yes, I meant subplots instead of subfigures (switching between matlab and latex !)

I spent all day today trying to figure out how you would do this. It works perfectly fine with Figures but I can't make it do the same thing with Subplots included.

****************** Here's what you'd do for a figure *****************
E.g.,
x = -pi:.1:pi;
y = sin(x);
h=figure;
plot(x,y)
colormap(jet)
% Make your figure boundaries tight:
ti = get(gca,'TightInset');
set(gca,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);

% Now you have a tight figure on the screen but if you directly do saveas
% (or print), MATLAB will still add the annoying white space. To get rid
% of them, we need to adjust the ``paper size":
set(gca,'units','centimeters')
pos = get(gca,'Position');
ti = get(gca,'TightInset');

set(gcf, 'PaperUnits','centimeters');
set(gcf, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperPosition',[0 0 pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
%Done! Now you can saveas your figure:
saveas(h,'output.pdf');


********* Here's my attempt to do the same for a figure with subplots ************

figure(2)
h1=subplot(3,2,1);
pcolor(a1,b1,c1);colorbar;shading flat
t1=get(h1,'TightInset');
p1=get(h1,'Position');
set(gca,'Position',[t1(1) t1(2) 1-t1(3)-ti(1) 1-t1(4)-t1(2)]);
set(gca,'units','centimeters')
set(gcf, 'PaperUnits','centimeters');
set(gcf, 'PaperSize', [p1(3)+t1(1)+t1(3) p1(4)+t1(2)+t1(4)]);
set(gcf, 'PaperPositionMode', 'manual');


%%
h2=subplot(3,2,2);
pcolor(a1,b1,c1);colorbar;shading flat
t2=get(h2,'TightInset');
p2=get(h2,'Position');
set(gca,'units','centimeters')
set(gcf, 'PaperUnits','centimeters');
set(gcf, 'PaperSize', [p2(3)+t2(1)+t2(3) p2(4)+t2(2)+t2(4)]);
set(gcf, 'PaperPositionMode', 'manual');

%%
h3=subplot(3,2,3);
pcolor(a1,b1,c1);colorbar;shading flat
t3=get(h3,'TightInset');
p3=get(h3,'Position');
set(gca,'units','centimeters')
set(gcf, 'PaperUnits','centimeters');
set(gcf, 'PaperSize', [p3(3)+t3(1)+t3(3) p3(4)+t3(2)+t3(4)]);
set(gcf, 'PaperPositionMode', 'manual');

and so on ..... for the rest of the subplots.

I think the problem is in set(gca,'Position',..) AND/OR set(gcf, 'PaperSize',......).

Please can someone help ... I have already spent a whole day on this - I'm not going to have more time to figure this out.

Any help is greatly appreciated.

Subject: get rid of the white space around matlab's SUBFIGURE

From: TideMan

Date: 2 Nov, 2011 19:11:49

Message: 4 of 5

On Nov 3, 6:45 am, "Emma Robertson" <m...@hotmail.co.uk> wrote:
> yes, I meant subplots instead of subfigures (switching between matlab and latex !)
>
> I spent all day today trying to figure out how you would do this.  It works perfectly fine with Figures but I can't make it do the same thing with Subplots included.
>
> ****************** Here's what you'd do for a figure *****************
> E.g.,
> x = -pi:.1:pi;
> y = sin(x);
> h=figure;
> plot(x,y)
> colormap(jet)
> % Make your figure boundaries tight:
> ti = get(gca,'TightInset');
> set(gca,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);
>
> % Now you have a tight figure on the screen but if you directly do saveas
> % (or print), MATLAB will still add the annoying white space. To get rid
> % of them, we need to adjust the ``paper size":
> set(gca,'units','centimeters')
> pos = get(gca,'Position');
> ti = get(gca,'TightInset');
>
> set(gcf, 'PaperUnits','centimeters');
> set(gcf, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
> set(gcf, 'PaperPositionMode', 'manual');
> set(gcf, 'PaperPosition',[0 0 pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
> %Done! Now you can saveas your figure:
> saveas(h,'output.pdf');
>
> ********* Here's my attempt to do the same for a figure with subplots ************
>
> figure(2)
> h1=subplot(3,2,1);
> pcolor(a1,b1,c1);colorbar;shading flat
> t1=get(h1,'TightInset');
> p1=get(h1,'Position');
> set(gca,'Position',[t1(1) t1(2) 1-t1(3)-ti(1) 1-t1(4)-t1(2)]);
> set(gca,'units','centimeters')
> set(gcf, 'PaperUnits','centimeters');
> set(gcf, 'PaperSize', [p1(3)+t1(1)+t1(3) p1(4)+t1(2)+t1(4)]);
> set(gcf, 'PaperPositionMode', 'manual');
>
> %%
> h2=subplot(3,2,2);
> pcolor(a1,b1,c1);colorbar;shading flat
> t2=get(h2,'TightInset');
> p2=get(h2,'Position');
> set(gca,'units','centimeters')
> set(gcf, 'PaperUnits','centimeters');
> set(gcf, 'PaperSize', [p2(3)+t2(1)+t2(3) p2(4)+t2(2)+t2(4)]);
> set(gcf, 'PaperPositionMode', 'manual');
>
> %%
> h3=subplot(3,2,3);
> pcolor(a1,b1,c1);colorbar;shading flat
> t3=get(h3,'TightInset');
> p3=get(h3,'Position');
> set(gca,'units','centimeters')
> set(gcf, 'PaperUnits','centimeters');
> set(gcf, 'PaperSize', [p3(3)+t3(1)+t3(3) p3(4)+t3(2)+t3(4)]);
> set(gcf, 'PaperPositionMode', 'manual');
>
> and so on ..... for the rest of the subplots.
>
> I think the problem is in set(gca,'Position',..) AND/OR set(gcf, 'PaperSize',......).
>
> Please can someone help ... I have already spent a whole day on this - I'm not going to have more time to figure this out.
>
> Any help is greatly appreciated.

I think you should ditch subplot and start from scratch.
A figure (however you define its size) goes from 0 to 1 in both
directions with the origin at the bottom left.
So, you can lay out your figure containing several panels using axes:
% Bottom left
axes('Position',[left bottom width height])
plot(x,y)

% Bottom right
axes('Position',[left+width+gapx bottom width height])
plot(x1,y1)

% Top left
axes('Position',[left bottom+gapy+height width height])


% Top right
axes('Position',[left+width+gapx bottom+gapy+height width height])

Now you have complete control of where your panels sit in the figure.
For example, if the x axes are common, you can move the top panels
close to the top of the bottom panels (by reducing gapy) and omit the
x tick labels (using set(gca,'XTickLabel','')).

Subject: get rid of the white space around matlab's SUBFIGURE

From: Emma Robertson

Date: 2 Nov, 2011 19:34:12

Message: 5 of 5


>
> I think you should ditch subplot and start from scratch.
> A figure (however you define its size) goes from 0 to 1 in both
> directions with the origin at the bottom left.
> So, you can lay out your figure containing several panels using axes:
> % Bottom left
> axes('Position',[left bottom width height])
> plot(x,y)
>
> % Bottom right
> axes('Position',[left+width+gapx bottom width height])
> plot(x1,y1)
>
> % Top left
> axes('Position',[left bottom+gapy+height width height])
>
>
> % Top right
> axes('Position',[left+width+gapx bottom+gapy+height width height])
>
> Now you have complete control of where your panels sit in the figure.
> For example, if the x axes are common, you can move the top panels
> close to the top of the bottom panels (by reducing gapy) and omit the
> x tick labels (using set(gca,'XTickLabel','')).


Thanks, but this isn't helping - I'm getting really frustrated now!!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us