Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

matlab

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.

like image 969
Tim Avatar asked Mar 01 '11 05:03

Tim


People also ask

How do I save a plot as a PDF?

Plot the data frame with 'o' and 'rx' style. To save the file in PDF format, use savefig() method where the image name is myImagePDF. pdf, format = ”pdf”. To show the image, use the plt.

How to save matlab figure without white space?

One way to minimize the white space when saving or copying the contents of a plot is to use the axes toolbar, which appears when you hover over the upper right corner of the axes. An alternative method is to use the exportgraphics and copygraphics functions, which provide more flexibility.

How do I save multiple graphs in a PDF in Python?

Create another figure (fig2) or activate and existing figure using figure() method. Plot the second line using plot() method. Initialize a variable, filename, to make a pdf file. Create a user-defined function save_multi_image() to save multiple images in a PDF file.


2 Answers

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

like image 91
Ghaul Avatar answered Sep 29 '22 01:09

Ghaul


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

like image 40
b3. Avatar answered Sep 29 '22 01:09

b3.