简体   繁体   English

plt.savefig在Python中生成空白图

[英]plt.savefig produces blank figure in Python

I want to save a plot as a png. 我想将情节保存为png。 The following code produces a blank figure: 以下代码生成一个空白图:

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace (0 , 10, 1000)
y = x**2
plt.plot(x, y)
plt.savefig('line_plot.png', dpi=100)

I am using Python 3.6 together with Anaconda and Spyder 3.2.0. 我正在使用Python 3.6和Anaconda以及Spyder 3.2.0。 How can I get a png containing the desired plot? 如何获得包含所需图表的png?

Try using the Figure object directly. 尝试直接使用Figure对象。

fig=plt.figure()
plt.plot(x,y)
fig.savefig('line_plot.png', dpi=100)

If you are interested in a PNG use '.png' instead of '.jpg'. 如果您对PNG感兴趣,请使用'.png'而不是'.jpg'。 Depending on the GUI toolkit you may need to add fig.show() before the last line. 根据GUI工具包,您可能需要在最后一行之前添加fig.show()。

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

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