How to set the plot in matlab to a specific size?
Asked Answered
M

4

8

Generally, I wish to plot a rather complex x-y plot (lots of overlapping curves) to an A3 format so:

A4 210x297  
A3 = A4*2 = 420 x 297  
... - 10mm each side = 400 x 277 (size of desired plot window)

What is the easiest way to set the size of the plot so it fits that size when printed in PDF (or any other common output format)?

Muffler answered 26/9, 2011 at 22:15 Comment(2)
Hi. Was my answer sufficient?Valdemar
@Ionis - Can't say yet. I have to try it on the plotter tomorrow morning to see if it works for real. I'll definitely let you know though.Muffler
V
8

See the matlab documentation for figure properties.

Namely:

  • PaperSize - Explicitly defines the size of the canvas.
  • PaperType - Sets PaperSize to one of several standard paper sizes.

The built in plot tool lets you save figures as all sorts of image formats so you should be good to go. Fiddling with the above settings should get your figure to be the correct size for printing.

Happy plotting!

Valdemar answered 26/9, 2011 at 22:38 Comment(2)
Some care is required though: mathworks.co.uk/support/bugreports/328969Gilly
As a supplement to this please check out @Grose and his answer.Valdemar
G
24

As @LewisNorton explained, you need to set the Paper*** properties of the figure. Below is an example for producing a PDF file with dimensions 420 x 297 mm (A3 size), where the margins between the plot and the file borders are 10 mm each (top,bottom,left,right).

%# centimeters units
X = 42.0;                  %# A3 paper size
Y = 29.7;                  %# A3 paper size
xMargin = 1;               %# left/right margins from page borders
yMargin = 1;               %# bottom/top margins from page borders
xSize = X - 2*xMargin;     %# figure size on paper (widht & hieght)
ySize = Y - 2*yMargin;     %# figure size on paper (widht & hieght)

%# create figure/axis
hFig = figure('Menubar','none');
plot([0 1 nan 0 1], [0 1 nan 1 0]), axis tight
set(gca, 'XTickLabel',[], 'YTickLabel',[], ...
    'Units','normalized', 'Position',[0 0 1 1])

%# figure size displayed on screen (50% scaled, but same aspect ratio)
set(hFig, 'Units','centimeters', 'Position',[0 0 xSize ySize]/2)
movegui(hFig, 'center')

%# figure size printed on paper
set(hFig, 'PaperUnits','centimeters')
set(hFig, 'PaperSize',[X Y])
set(hFig, 'PaperPosition',[xMargin yMargin xSize ySize])
set(hFig, 'PaperOrientation','portrait')

%# export to PDF and open file
print -dpdf -r0 out.pdf
winopen out.pdf

screenshot_MATLAB screenshot_PDF

Without a printer at hand, I am using a virtual screen ruler to check the measurements; Simply display the PDF file with your preferred viewer, and the set the zoom level at 100% (I am using Sumatra PDF). If you want to try this yourself, just be aware that some viewers (Adobe Reader) might be using a custom DPI not matching the system default resolution (mine at 96 pixels/inch).

Here you can see the bottom and left margins equal to 10mm. The same holds for the other two margins:

margins_1cm

Note that in the example above, I made the axis cover the entire figure (no gray area in the figure). By default, MATLAB leaves some empty space for tick labels, axis labels, title, etc.. This is of course different from the margins mentioned above, which I assume you already know :)

Grose answered 27/9, 2011 at 22:57 Comment(0)
V
8

See the matlab documentation for figure properties.

Namely:

  • PaperSize - Explicitly defines the size of the canvas.
  • PaperType - Sets PaperSize to one of several standard paper sizes.

The built in plot tool lets you save figures as all sorts of image formats so you should be good to go. Fiddling with the above settings should get your figure to be the correct size for printing.

Happy plotting!

Valdemar answered 26/9, 2011 at 22:38 Comment(2)
Some care is required though: mathworks.co.uk/support/bugreports/328969Gilly
As a supplement to this please check out @Grose and his answer.Valdemar
A
2

Download export fig(Requires Ghostscript).

Try running:

 surf(peaks);title('voila');export_fig 'test.pdf';

When using Adobe to print the pdf file, set 'Page Scaling' to 'Fit to Printable Area'.

Arleta answered 26/9, 2011 at 22:43 Comment(1)
Honestly, export_fig is sometimes failing quite hard. I prefer the other answer.Goldstein
T
0

If you want a custom shape (e.g. for a long, thin plot or for a square plot to include in another file), set both the PaperSize and PaperPosition options.

set(gcf, 'PaperSize', [30 10], 'PaperPosition', [0 0 30 10])
print -pdf filename
Torero answered 24/5, 2017 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.