How to save a plot into a PDF file without a large margin around [duplicate]
Asked Answered
N

8

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.

Notify answered 1/3, 2011 at 5:22 Comment(1)
This question is answered in another thread: #3802230Condemnatory
D
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

Dagney answered 1/3, 2011 at 9:50 Comment(1)
How could you set smaller margins for the window itself and not only to the paper? Thanks.Inge
S
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

Sorrow answered 1/3, 2011 at 9:29 Comment(4)
How could you set smaller margins for the window itself and not only to the paper? Thanks.Inge
@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.Sorrow
what if I have more than one axis in the figure?Baptlsta
@Baptlsta - 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.Sorrow
I
9

This works for displaying purposes:

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

Should work for printing as well.

Inge answered 20/5, 2011 at 9:22 Comment(1)
This removes the border of the figure in the visualisation window but does not change the PDF figure.Pyosis
C
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)]);
Condemnatory answered 1/12, 2011 at 7:3 Comment(1)
updated link to the same thing: mathworks.com/matlabcentral/fileexchange/…Baptlsta
S
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.

Salesperson answered 1/12, 2011 at 17:20 Comment(0)
D
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.

Deathful answered 17/10, 2013 at 7:31 Comment(0)
A
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.

Ahearn answered 6/5, 2012 at 22:33 Comment(0)
S
-2
system ('/usr/bin/pdfcrop filename.pdf'); 
Slaw answered 17/10, 2013 at 3:38 Comment(1)
Please consider adding an explanation also to go with the code/command.Glasper

© 2022 - 2024 — McMap. All rights reserved.