原文: https://pythonspot.com/matplotlib-save-figure-to-image-file/
保存图形
Matplotlib可以使用savefig()将图直接保存到文件中。
该方法可以像这样使用:
fig.savefig('plot.png')
完整的例子:
import matplotlibimport matplotlib.pyplot as pltimport numpy as npy = [2,4,6,8,10,12,14,16,18,20]x = np.arange(10)fig = plt.figure()ax = plt.subplot(111)ax.plot(x, y, label='$y = numbers')plt.title('Legend inside')ax.legend()#plt.show()fig.savefig('plot.png')
要更改格式,只需更改扩展名,如下所示:
fig.savefig('plot.pdf')
您可以使用打开文件
display plot.png
或在图片或 pdf 查看器中打开它,

保存到 pdf 的图
