How to save a chart in a PDF file without a large margin

If I print the chart in EPS format, the contents of the EPS file are completely occupied by the chart. But if I print the plot in PDF format, then there are large fields above and below the graph in the PDF file. How to save the plot in a PDF file without a large margin around the graph?

My assumption is how to automatically select the right β€œpaper” size in the PDF file to print according to the size of the chart.

This is the question I asked at tex.stackexchange.com where I have answers that basically tried to solve the problem outside of MATLAB , and I still do not quite understand the only answer that I tried to solve from MATLAB. So I would like to see if there are more opinions here.

+38
matlab
Mar 01 '11 at 5:22
source share
8 answers

What do you mean by "right size"? MATLAB numbers are similar to vector graphics, so you can basically choose the size you want on your plot.

You can set the paper size and position of the shape using the set function.

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 code above will remove most of the boundaries, but not all. This is because the left corner ( [0 0] in the position vector) is not the "true" left corner. To remove more 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

+17
Mar 01 '11 at 9:50
source share

This works to display goals:

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

It should work for printing. A.

+10
May 20 '11 at 9:22
source share

Axle size in MATLAB can sometimes be a little complicated. You are right to suspect paper properties as one of the problems. The other is the automatic fields that MATLAB calculates. Fortunately, there are custom axis properties that circumvent these fields. You can reset so that the fields are large enough for the axis labels using a combination of the Position and TightInset , which are explained here . Try the following:

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

and you get a PDF file that looks like this: MATLAB plot with auto-margins but now do the following:

 >> 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 will receive: MATLAB plot with auto-margins removed

+9
Mar 01 2018-11-11T00:
source share
+6
Dec 01 2018-11-11T00:
source share

The export_fig function in MATLAB file sharing cuts spaces around the default PDF / EPS output file when exporting a shape.

+4
Dec 01 '11 at 17:20
source share

It seems to me that all approaches (file sharing solutions that are not considered) do not have a significant step here or, finally, lead to it through some blurry workarounds.

The size of the figure should be equal to the size of the paper, and there are no white margins.

 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') 

Gives you the output in pdf format, since your figure will appear exactly in the right size. If you want it to be even denser, you can combine this solution with answer b3 .

+4
Oct 17 '13 at 7:31 on
source share

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 software that converts EPS to PDF.

+3
May 6 '12 at 10:33 PM
source share
 system ('/usr/bin/pdfcrop filename.pdf'); 
-2
Oct 17 '13 at 3:38
source share



All Articles