似乎设置PaperPositionMode
为auto
将消除 EPS 文件的多余空白,但不是 PDF。
为了摆脱 PDF 的空白,我编写了一个小脚本来将纸张调整为图形大小。由于它很短,我将它包含在下面以防其他人需要它。
它的灵感来自这个文档,以及这个StackOverflow 问题。
我的解决方案通过仅操作纸张大小而不是图形轴来工作,因为操作轴会遇到子图的问题。这也意味着将保留一些空白。在 MATLAB 的词汇表中,这些数字是由它OuterPosition
而不是TightInset
.
function [filename] = printpdf(fig, name)
% printpdf Prints image in PDF format without tons of white space
% The width and height of the figure are found
% The paper is set to be the same width and height as the figure
% The figure's bottom left corner is lined up with
% the paper's bottom left corner
% Set figure and paper to use the same unit
set(fig, 'Units', 'centimeters')
set(fig, 'PaperUnits','centimeters');
% Position of figure is of form [left bottom width height]
% We only care about width and height
pos = get(fig,'Position');
% Set paper size to be same as figure size
set(fig, 'PaperSize', [pos(3) pos(4)]);
% Set figure to start at bottom left of paper
% This ensures that figure and paper will match up in size
set(fig, 'PaperPositionMode', 'manual');
set(fig, 'PaperPosition', [0 0 pos(3) pos(4)]);
% Print as pdf
print(fig, '-dpdf', name)
% Return full file name
filename = [name, '.pdf'];
end