# Saving tightly cropped PDFs in Matlab

20 views (last 30 days)
Theo on 21 Jul 2011
I'm sure many can empathize with my question:
I have a figure that needs to be saved in a pdf format (a) without axes or labels, and (b) in the tightest bounding box possible---that is, if the figure is with x and y limits on [-2, 2], then the final result should be a 2x2 box. No more, no less.
Unfortunately, Matlab makes it extremely difficult to do this, apparently. I have been using the excellent savefig program to try and bypass the bounding box issues. And then I try something like this:
x = linspace(-2,2,200); y = linspace(-2,2,200);
[xmat,ymat] = meshgrid(x,y);
z = xmat + 1i*ymat;
[C,h] = contourf(x,y,z.^2,30);
set(gca,'Visible','off')
savefig('mypdf', 'pdf');
However, the final output is a pdf with tight bounding boxes on the bottom and left, but there is a small whitespace on the right and top.
Can anybody tell me how to get rid of that whitespace?

Oleg Komarov on 21 Jul 2011
Make the axes slighly bigger:
Edit
% Create a figure measure in pixels positioned at 360 (from left) 150 (from bottom), 560 (long) and 420 (tall)
figure('units','pixels','position',[360 150 560 420])
axes('un','pix','pos',[0 0 570 430]);
x = linspace(-2,2,200); y = linspace(-2,2,200);
[xmat,ymat] = meshgrid(x,y);
z = xmat + 1i*ymat;
[C,h] = contourf(x,y,z.^2,30);
set(gca,'Visible','off')
savefig myfig pdf