简体   繁体   中英

plt.savefig produces blank figure in Python

I want to save a plot as a 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. How can I get a png containing the desired plot?

Try using the Figure object directly.

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'. Depending on the GUI toolkit you may need to add fig.show() before the last line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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