以与图形窗口相同的大小打印图形



我正在尝试以PDF格式保存图形,这样我就不会在其周围出现多余的空白,即图形的大小应与图形窗口相同。

我很确定这样做的方法是

figureNames = {'a', 'b', 'c'};
for i = 1:3
    set(figure(i), 'paperpositionmode', 'auto');
    print(figure(i), '-dpdf', figureNames{i})
end

但它不起作用。我得到了一如既往的空白。有人可以启发我出了什么问题吗?

您可以尝试将图形转换为pdf文件的"多合一"解决方案。我使用 mlf2pdf(http://www.mathworks.com/matlabcentral/fileexchange/28545)它似乎工作得很好。此外,由于所有内容的乳胶排版,生产的数字的质量要好得多

我对此也有问题。解决方法是打印-depsc(彩色)或-deps(如果您只需要灰度图形)。封装的后脚本文件几乎没有白边距。您以后可以毫无问题地将.eps文件转换为pdf,如果您在LaTeX中工作,则可以按原样使用它。

似乎将

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

最新更新