问题:以非常高的质量将图像保存在python中

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

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

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

pngeps?还是其他?我不能做,pdf因为有一个隐藏的数字发生,导致Latexmk编译混乱。

How can I save Python plots at very high quality?

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? Or some other? I can’t do pdf, because there is a hidden number that happens that mess with Latexmk compilation.


回答 0

如果您正在使用matplotlib并试图在乳胶文档中获得良好的数据,请另存为eps。具体来说,请在运行命令以绘制图像后尝试以下操作:

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

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

更新:

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

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

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

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

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')

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

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)

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).

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.


回答 1

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

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

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

我使用1200 dpi是因为许多科学期刊要求图像的分辨率为1200/600/300 dpi。在GiMP或Inkscape中转换为所需的dpi和格式。

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

Just to add my results, also using Matplotlib.

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

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.

Obviously the dpi doesn’t matter since .svg are vector graphics and have “infinite resolution”.


回答 2

好的,我发现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)

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

beingsaved = plt.figure()

# Some scatter plots
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)

回答 3

如果您正在使用海图,而不是matplotlib,可以保存一个.png图像,如下所示:

假设您有一个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

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

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

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

然后,稍后在控制台中绘制输出,从中可以将其复制粘贴到所需的位置。(由于seaborn是建立在matplotlib之上的,因此不会有问题。)

In case you are working with seaborn plots, instead of Matplotlib, you can save a .png image like this:

Let’s suppose you have a matrix object (either Pandas or NumPy), and you want to take a heatmap:

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 code around Stack Overflow worked only for previous versions.

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 not be any problem.)


回答 4

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

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

您也可以更高或更低。上述解决方案可以很好地用于打印,但是如今,您希望将创建的图像转换为PNG / JPG或以宽屏格式显示。

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

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。