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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16183462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:59:22  来源:igfitidea点击:

saving images in python at a very high quality

pythongraphicsmatplotlibsave

提问by dustin

How can I save python plots at very high quality?

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

That is, when I keep zooming in on the object saved in a pdf file, there is no blurring?

也就是说,当我继续放大保存在pdf文件中的对象时,没有模糊?

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

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

png, eps? Or some other? I can't do pdfbecause there is a hidden number that happens that mess with Latexmkcompilation.

png, eps? 还是别的?我不能这样做,pdf因为有一个隐藏的数字会导致Latexmk编译混乱。

采纳答案by spencerlyon2

If you are using matplotliband 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:

如果您正在使用matplotlib并试图在乳胶文档中获得好的数字,请保存为 eps。具体来说,在运行命令绘制图像后尝试这样的事情:

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

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

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

UPDATE:

更新:

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

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

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

Where elevation_angleis a number (in degrees) specifying the polar angle (down from vertical z axis) and the azimuthal_anglespecifies the azimuthal angle (around the z axis).

其中elevation_angle是指定极角(从垂直 z 轴向下)的数字(以度为单位)和azimuthal_angle指定方位角(围绕 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 are replaced with the two angles when you start to click+drag+rotate the image.

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

回答by grofte

Just to add my results, also using matplotlib.

只是为了添加我的结果,也使用 matplotlib。

.eps made all my text bold and removed transparency. .svg gave me high-res pictures that actually looked like my graph.

.eps 使我所有的文本都加粗并删除了透明度。.svg 给了我高分辨率图片,实际上看起来像我的图表。

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# Do the plot code
fig.savefig('myimage.svg', format='svg', dpi=1200)

I used 1200 dpi because a lot of scientific journals require images in 1200 / 600 / 300 dpi depending on what the image is of. Convert to desired dpi and format in GiMP or Inkscape.

我使用 1200 dpi,因为很多科学期刊需要 1200 / 600 / 300 dpi 的图像,具体取决于图像的内容。在 GiMP 或 Inkscape 中转换为所需的 dpi 和格式。

EDIT: Obviously the dpi doesn't matter since .svg are vector graphics and have "infinite resolution".

编辑:显然 dpi 并不重要,因为 .svg 是矢量图形并且具有“无限分辨率”。

回答by Jonatán Iván

Okay, I found spencerlyon2's answer working, however in case anybody would find himself not knowing what to do with that one line, I had to do it this way:

好的,我发现 spencerlyon2 的答案有效,但是如果有人发现自己不知道如何处理那一行,我必须这样做:

beingsaved = plt.figure()

# some scatters
plt.scatter(X_1_x, X_1_y)
plt.scatter(X_2_x, X_2_y)

beingsaved.savefig('destination_path.eps', format='eps', dpi=1000)

回答by Leevo

In case you are working with seabornplots, instead of matplotlib, you can save a .png image like this:

如果您使用的是seaborn图,而不是 matplotlib,您可以像这样保存 .png 图像:

Let's suppose you have a matrixobject (either pandas or numpy), and you want to take a heatmap:

假设您有一个matrix对象(pandas 或 numpy),并且想要获取热图:

import seaborn as sb

image = sb.heatmap(matrix)   # this gets you the heatmap
image.figure.savefig("C:/Your/Path/ ... /your_image.png")   # this saves it

This code is compatible with the latest version of seaborn. Other codes around stackoverflow worked only for previous versions.

此代码与最新版本的 seaborn 兼容。围绕 stackoverflow 的其他代码仅适用于以前的版本。

Another way I like is this. I set the size of the next image as follows:

我喜欢的另一种方式是这样。我设置下一个图像的大小如下:

plt.subplots(figsize=(15,15))

And then later I plot the output in the console, from which I can copy-paste it where I want. (Since seaborn is built on top of matplotlib, there will be no problem.)

然后我在控制台中绘制输出,我可以从中复制粘贴到我想要的位置。(因为seaborn是建立在matplotlib之上的,所以不会有问题。)

回答by Eamonn Kenny

You can save to a figure that is 1920x1080 (or 1080p) using:

您可以使用以下方法将图形保存为 1920x1080(或 1080p):

fig = plt.figure(figsize=(19.20,10.80))

You can also go much higher or lower. The above solutions work well for printing, but these days you want the created image to go into a PNG/JPG or appear in a wide screen format.

你也可以走得更高或更低。上述解决方案适用于打印,但现在您希望创建的图像进入 PNG/JPG 或以宽屏幕格式显示。