摆脱matlab图的pdf输出周围的空白区域

mjb*_*wn2 50 pdf matlab

我想在LaTeX文档中使用我的matlab图的PDF版本.我使用带有PDF选项的"saveas"命令保存数字,但是我在pdf文件中的图表周围留下了巨大的空白区域.这是正常的吗?我怎么能摆脱它?当然,因为我有很多情节,所以自动进行.

zel*_*lus 15

出口数字出版是一个很好的起点.而不是-deps使用-dpdfPDF输出.

您可以使用下面的代码修复边界框问题.

set(gcf, 'PaperSize', [6.25 7.5]);
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperPosition', [0 0 6.25 7.5]);

set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperSize', [6.25 7.5]);
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperPosition', [0 0 6.25 7.5]);

set(gcf, 'renderer', 'painters');
print(gcf, '-dpdf', 'my-figure.pdf');
print(gcf, '-dpng', 'my-figure.png');
print(gcf, '-depsc2', 'my-figure.eps');
Run Code Online (Sandbox Code Playgroud)

您可以在Tobin的文章中阅读更多相关信息.


Ant*_*nio 8

上面的答案似乎太复杂了.这个函数使用一个数字句柄和一个字符串来打印pdf文件中的东西而没有巨大的边距.

function printpdf(h,outfilename)

set(h, 'PaperUnits','centimeters');
set(h, 'Units','centimeters');
pos=get(h,'Position');
set(h, 'PaperSize', [pos(3) pos(4)]);
set(h, 'PaperPositionMode', 'manual');
set(h, 'PaperPosition',[0 0 pos(3) pos(4)]);
print('-dpdf',outfilename);
Run Code Online (Sandbox Code Playgroud)

例如,要打印当前图形,您可以使用以下命令调用它:

printpdf(gcf,'trash')
Run Code Online (Sandbox Code Playgroud)

但是,如果你真的想要一个类似于Matlab生成的eps的pdf图形,也就是说,只有绘图(或一组子图)的矩形凸包,这里是更复杂的版本:

function printpdf(h,outfilename)

% first use the same non-relative unit system for paper and screen (see
% below)
set(h,'PaperUnits','centimeters');

% now get all existing plots/subplots
a=get(h,'Children');
nfigs=length(a);

% bounds will contain lower-left and upper-right corners of plots plus one
% line to make sure single plots work
bounds=zeros(nfigs+1,4);
bounds(end,1:2)=inf;
bounds(end,3:4)=-inf;

% generate all coordinates of corners of graphs and store them in
% bounds as [lower-left-x lower-left-y upper-right-x upper-right-y] in
% the same unit system as paper (centimeters here)
for i=1:nfigs
    set(a(i),'Unit','centimeters');
    pos=get(a(i),'Position');
    inset=get(a(i),'TightInset');
    bounds(i,:)=[pos(1)-inset(1) pos(2)-inset(2) ...
        pos(1)+pos(3)+inset(3) pos(2)+pos(4)+inset(4)];
end

% compute the rectangular convex hull of all plots and store that info
% in mypos as [lower-left-x lower-left-y width height] in centimeters
auxmin=min(bounds(:,1:2));
auxmax=max(bounds(:,3:4));
mypos=[auxmin auxmax-auxmin];

% set the paper to the exact size of the on-screen figure using
% figure property PaperSize [width height]
set(h,'PaperSize',[mypos(3) mypos(4)]);

% ensure that paper position mode is in manual in order for the
% printer driver to honor the figure properties
set(h,'PaperPositionMode', 'manual');

% use the PaperPosition four-element vector [left, bottom, width, height]
% to control the location on printed page; place it using horizontal and
% vertical negative offsets equal to the lower-left coordinates of the
% rectangular convex hull of the plot, and increase the size of the figure
% accordingly
set(h,'PaperPosition',[-mypos(1) -mypos(2) ...
    mypos(3)+mypos(1) mypos(4)+mypos(2)]);

% print stuff
print('-dpdf',outfilename);
Run Code Online (Sandbox Code Playgroud)


小智 6

我对这个b4有点担心,找到了一个简单的答案.保存为.eps,然后将.eps转换为.pdf.在Mac OS中,这可以在预览中完成.


Yai*_*man 5

除了这里的其他建议,您也可以尝试在描述使用LooseInset财产http://UndocumentedMatlab.com/blog/axes-looseinset-property/周围的图轴删除多余的空间.


小智 5

我只是花了一些时间尝试大多数这些选项,但我的朋友 Espen 指出了最简单的:如果您在 LaTeX 中使用 graphicsx 包,请使用标准的 Matlab pdf 输出并使用 \includegraphics 中的修剪选项。Beamer 幻灯片中的示例:

\includegraphics[trim = 0.1in 2.5in 0.1in 2.5in, clip, scale=0.5]{matlabgraphic.pdf}

这里修剪参数的顺序是左、下、右、上。关键是要大量修剪顶部和底部以摆脱多余的空间。更多在Wikibooks


Ayb*_*gür 5

对于光栅图像输出(例如 png),以下内容位于我的 Makefile 中:

convert -trim input.png input-trimmed.png
Run Code Online (Sandbox Code Playgroud)

这需要imagemagick。

更新:对于我最近发布的所有出版物,我使用了https://github.com/altmany/export_fig,这是我迄今为止找到的最佳解决方案(以及单个包中针对其他问题的许多其他解决方案)。我觉得至少有这样强大的工具应该已经是 Matlab 的正式组成部分了。我用它作为:

export_fig -transparent fig.pdf
Run Code Online (Sandbox Code Playgroud)

它导出当前图形,默认裁剪输出。

需要鬼脚本