42

If I print a plot in EPS format, the content of the EPS file is fully occupied with the plot. But if I print a plot in PDF format, then there are big margins above and below the plot in the PDF file. How can I save a plot in a PDF file without the big margin around the plot?

My guess is how to automatically choose the proper "paper" size in a PDF file to print to, according to the plot size.

This is a question I have asked at tex.stackexchange.com, where I have got replies which mostly tried to solve the problem outside MATLAB, and I still don't quite understand the only reply that tried to solve from within MATLAB. So I would like to see if there are more opinions here.

Community
  • 1
  • 1
Tim
  • 1
  • 141
  • 372
  • 590
  • 2
    This question is answered in another thread: http://stackoverflow.com/questions/3801730/get-rid-of-the-white-space-around-matlab-figures-pdf-output – emrea Apr 30 '12 at 18:25

8 Answers8

20

What do you mean by "the proper size"? MATLAB figures are like vector graphics, so you can basically choose the size you want on your plot.

You can set the size of the paper and the position of the figure with the function set.

Example:

plot(epx(1:5));
set(gcf, 'PaperPosition', [0 0 5 5]); %Position plot at left hand corner with width 5 and height 5.
set(gcf, 'PaperSize', [5 5]); %Set the paper to have width 5 and height 5.
saveas(gcf, 'test', 'pdf') %Save figure

Enter image description here

The above code will remove most of the borders, but not all. This is because the left-hand corner ([0 0] in the position vector) is not the "true" left-hand corner. To remove more of the borders, you can adjust the PaperPosition and PaperSize vectors.

Example:

plot(exp(1:5))
set(gcf, 'PaperPosition', [-0.5 -0.25 6 5.5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
saveas(gcf, 'test', 'pdf')

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ghaul
  • 3,340
  • 1
  • 19
  • 24
  • How could you set smaller margins for the window itself and not only to the paper? Thanks. – Royi May 20 '11 at 09:08
9

Axes sizing in MATLAB can be a bit tricky sometimes. You are correct to suspect the paper sizing properties as one part of the problem. Another is the automatic margins MATLAB calculates. Fortunately, there are settable axes properties that allow you to circumvent these margins. You can reset the margins to be just big enough for axis labels using a combination of the Position and TightInset properties which are explained here. Try this:

>> h = figure;
>> axes;
>> set(h, 'InvertHardcopy', 'off');
>> saveas(h, 'WithMargins.pdf');

and you'll get a PDF that looks like: MATLAB plot with auto-margins but now do this:

>> tightInset = get(gca, 'TightInset');
>> position(1) = tightInset(1);
>> position(2) = tightInset(2);
>> position(3) = 1 - tightInset(1) - tightInset(3);
>> position(4) = 1 - tightInset(2) - tightInset(4);
>> set(gca, 'Position', position);
>> saveas(h, 'WithoutMargins.pdf');

and you'll get: MATLAB plot with auto-margins removed

b3.
  • 7,094
  • 2
  • 33
  • 48
  • How could you set smaller margins for the window itself and not only to the paper? Thanks. – Royi May 20 '11 at 09:09
  • @Drazick - The above does this. It creates the small margins in the window first before saving it to PDF so you should see the small margins on your screen. Have a look at the link in the answer for more on the TightInset property and axes positioning in general. – b3. May 20 '11 at 16:39
  • what if I have more than one axis in the figure? – craq Aug 14 '15 at 12:38
  • @craq - You can use the same approach with more than one axis. I find it easier to work in pixel units when positioning axes but it'll work with normalized units like in the example above as well. – b3. Aug 23 '15 at 20:59
9

This works for displaying purposes:

set(gca(), 'LooseInset', get(gca(), 'TightInset'));

Should work for printing as well.

Royi
  • 4,640
  • 6
  • 46
  • 64
6

The script in How to get rid of the white margin in MATLAB's saveas or print outputs does what you want.

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

... 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)]);
William Price
  • 4,033
  • 1
  • 35
  • 54
emrea
  • 1,335
  • 9
  • 18
  • updated link to the same thing: http://www.mathworks.com/matlabcentral/fileexchange/34024-get-rid-of-the-white-margin-in-saveas-output – craq Aug 14 '15 at 12:58
4

It seems to me that all approaches (file exchange solutions unconsidered) here are lacking the essential step, or finally leading to it via some blurry workarounds.

The figure size needs to equal the paper size and the white margins are gone.

A = hgload('myFigure.fig');

% set desired output size
set(A, 'Units','centimeters')
height = 15;
width = 19;

% the last two parameters of 'Position' define the figure size
set(A, 'Position',[25 5 width height],...
       'PaperSize',[width height],...
       'PaperPositionMode','auto',...
       'InvertHardcopy', 'off',...
       'Renderer','painters'...     %recommended if there are no alphamaps
   );

saveas(A,'printout','pdf')

Will give you a pdf output as your figure appears, in exactly the size you want. If you want to get it even tighter you can combine this solution with the answer of b3.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
4

The function export_fig on the MATLAB file exchange will crop the whitespace around an output PDF/EPS file by default when it exports a figure.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user664303
  • 2,053
  • 3
  • 13
  • 30
3

Save to EPS and then convert to PDF:

saveas(gcf, 'nombre.eps', 'eps2c')
system('epstopdf nombre.eps') %Needs TeX Live (maybe it works with MiKTeX).

You will need some software that converts EPS to PDF.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fabian
  • 39
  • 1
-2
system ('/usr/bin/pdfcrop filename.pdf'); 
Lukas
  • 11