简体   繁体   English

使用savefig增加matplotlib.pyplot中的每英寸点数(DPI)

[英]using savefig to increase dots per inch (DPI) in matplotlib.pyplot

This answer, suggests using plt.savefig in order to increase the DPI. 答案建议使用plt.savefig来增加DPI。 I am a relative newbie, and not sure how to use .savefig. 我是一个相对较新手,不确定如何使用.savefig。

savefig's call signature requires fname to reference the file (or object?) which needs more DPI: savefig的呼叫签名要求fname引用需要更多DPI的文件(或对象?):

在此处输入图片说明

In the following code, what should I use for fname? 在以下代码中,我应该对fname使用什么? Should I create an object and then reference that? 我应该创建一个对象然后引用它吗?

#previous code generates two dataframes now converted to two lists...

Max_Vals = DFMAX1.tolist()
Min_Vals = DFMIN1.tolist()

fig = plt.figure()

plt.plot(Max_Vals, 'g-')
plt.plot(Min_Vals, 'b-')

fig.set_size_inches(30.,18.)
plt.show()

在此处输入图片说明

When I run savefig without fname: 当我运行不带fname的savefig时:

plt.savefig(dpi=300)

I get an error: 我收到一个错误:

在此处输入图片说明

Grateful for any help. 感谢您的帮助。

The point of plt.savefig() is it allows you to export the graph to a file. plt.savefig()的要点是它允许您将图形导出到文件。 If you're just using plt.show() you're only showing the image, at which point to copy it elsewhere you have to use print-screen or similar. 如果仅使用plt.show() ,则仅显示图像,此时将其复制到其他位置,则必须使用打印屏幕或类似工具。

Try running: 尝试运行:

#previous code generates two dataframes now converted to two lists...

Max_Vals = DFMAX1.tolist()
Min_Vals = DFMIN1.tolist()

fig = plt.figure()

plt.plot(Max_Vals, 'g-')
plt.plot(Min_Vals, 'b-')

fig.set_size_inches(30.,18.)
plt.savefig('100dpi.png', dpi=100)
plt.savefig('200dpi.png', dpi=200)

At this point, two image files will be saved in your working folder (likely the same folder as your script) - one with 100dpi, the second with 200dpi. 此时,两个图像文件将被保存在您的工作文件夹(可能与脚本位于同一文件夹)中-一个具有100dpi,第二个具有200dpi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM