MATLAB:正确调整图形大小
我有一个图形,我想调整其大小,然后打印为 PDF。 就可以使用类似的东西
set(hFig, 'PaperUnits', 'centimeters')
set(hFig, 'PaperSize', [x_B x_H]);
只要我不大幅调整图形大小, 。如果我降低高度,那么在某些时候 xlabel 会移出图形。我已经搜索了很多,但只找到了手动调整底层轴对象大小的解决方案,
scalefactor = 0.96;
movefactor = 0.82;
hAx = get(gcf,'CurrentAxes');
g = get(hAx,'Position');
% 1=left, 2=bottom, 3=width, 4=height
g(2) = g(2) + (1-movefactor)/2*g(4);
g(4) = scalefactor*g(4);
set(hAx,'Position',g);
我不喜欢这种方法,因为我必须手动调整这两个因素。
在打印之前,我将所有文本对象的“解释器”设置为“乳胶”(如果这是值得关注的)。 打印是
print(hFig, '-dpdf', '-loose', 'test.pdf');
通过使用“-loose”来放松边界框来实现的。非常感谢任何帮助!
编辑: 看来解释器(none,tex,latex)确实在其中发挥了作用。我受到这篇文章的启发(http://stackoverflow.com/questions/5150802/how-to-save-plot-into-pdf-without-large-margin-around)并提出了这个解决方案:
tightInset = get(gca, 'TightInset');
position(1) = tightInset(1);
position(3) = 1 - tightInset(1) - tightInset(3);
if strcmpi(x_Interpreter,'latex')
position(2) = tightInset(2)+ 1*tightInset(4);
position(4) = 1 - tightInset(2) - 2*tightInset(4);
else
position(2) = tightInset(2)+ 0*tightInset(4);
position(4) = 1 - tightInset(2) - 1*tightInset(4);
end
set(gca, 'Position', position);
I have a figure that I would like to resize and afterwards print as a PDF.
Using something like
set(hFig, 'PaperUnits', 'centimeters')
set(hFig, 'PaperSize', [x_B x_H]);
works as long as I do not resize the figure too drastically. If I reduce the height then at some points the xlabel moves out of the figure. I have searched a lot but only found an solution to manually resize the underlying axes-object
scalefactor = 0.96;
movefactor = 0.82;
hAx = get(gcf,'CurrentAxes');
g = get(hAx,'Position');
% 1=left, 2=bottom, 3=width, 4=height
g(2) = g(2) + (1-movefactor)/2*g(4);
g(4) = scalefactor*g(4);
set(hAx,'Position',g);
I do not like this approach since I have to manually adjust the two factors.
Before printing I set the 'interpreter' to 'latex' of all text-objects (if that is of concern).
Printing is achieved using
print(hFig, '-dpdf', '-loose', 'test.pdf');
I hoped to loosen the bounding box by using '-loose'. Any help is highly appreciated!
edit:
It seems that really the interpreter (none, tex, latex) plays a role in this. I got inspired by this post here (http://stackoverflow.com/questions/5150802/how-to-save-plot-into-pdf-without-large-margin-around) and came up with this solution:
tightInset = get(gca, 'TightInset');
position(1) = tightInset(1);
position(3) = 1 - tightInset(1) - tightInset(3);
if strcmpi(x_Interpreter,'latex')
position(2) = tightInset(2)+ 1*tightInset(4);
position(4) = 1 - tightInset(2) - 2*tightInset(4);
else
position(2) = tightInset(2)+ 0*tightInset(4);
position(4) = 1 - tightInset(2) - 1*tightInset(4);
end
set(gca, 'Position', position);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能无法完全解决您的问题(它可能只是帮助清理您的代码),但我发现 fig 文件交换中的代码很有帮助:它可以让您轻松设置图形的确切大小,而无需边框空白。
This may not solve your problem completely (it may just help clean up your code), but I found the fig code in the file exchange to be helpful: it lets you easily set the exact size of figures without bordering white space.