以非常高的质量在 Python 中保存图像

2022-01-24 00:00:00 python matplotlib graphics save

问题描述

如何以非常高的质量保存 Python 绘图?

How can I save Python plots at very high quality?

也就是说,当我不断放大保存在 PDF 文件中的对象时,为什么没有任何模糊?

That is, when I keep zooming in on the object saved in a PDF file, why isn't there any blurring?

另外,保存它的最佳模式是什么?

Also, what would be the best mode to save it in?

png, eps?还是其他?我不能做 pdf,因为有一个隐藏的数字发生在 Latexmk 编译中.

png, eps? Or some other? I can't do pdf, because there is a hidden number that happens that mess with Latexmk compilation.


解决方案

如果你使用的是 Matplotlib 并试图在 LaTeX 文档中获得好的数字,另存为 EPS.具体来说,在运行命令绘制图像后尝试这样的操作:

If you are using Matplotlib and are trying to get good figures in a LaTeX document, save as an EPS. Specifically, try something like this after running the commands to plot the image:

plt.savefig('destination_path.eps', format='eps')

我发现 EPS 文件效果最好,而 dpi 参数才是真正让它们在文档中看起来不错的原因.

I have found that EPS files work best and the dpi parameter is what really makes them look good in a document.

要在保存之前指定图形的方向,只需在 plt.savefig 调用之前调用以下命令,但在创建绘图之后(假设您使用名称为 的轴进行绘图斧头):

To specify the orientation of the figure before saving, simply call the following before the plt.savefig call, but after creating the plot (assuming you have plotted using an axes with the name ax):

ax.view_init(elev=elevation_angle, azim=azimuthal_angle)

其中 elevation_angle 是一个数字(以度为单位),指定极角(从垂直 z 轴向下),azimuthal_angle 指定方位角(围绕 z 轴).

Where elevation_angle is a number (in degrees) specifying the polar angle (down from vertical z axis) and the azimuthal_angle specifies the azimuthal angle (around the z axis).

我发现最容易确定这些值的方法是首先绘制图像,然后旋转它并观察当前角度值出现在窗口底部,就在实际绘图下方.请记住,默认情况下会显示 x、y、z 位置,但是当您开始单击+拖动+旋转图像时,它们会替换为两个角度.

I find that it is easiest to determine these values by first plotting the image and then rotating it and watching the current values of the angles appear towards the bottom of the window just below the actual plot. Keep in mind that the x, y, z, positions appear by default, but they are replaced with the two angles when you start to click+drag+rotate the image.

相关文章