plt.savefig('saved_figure.png')
值得注意的是savefig()函数并不是plt实例所独有的。你也可以在一个Figure对象上使用它:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
x = np.arange(0, 10, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.savefig('saved_figure.png')
plt.plot(x, y)
fig.savefig('saved_figure.png')
savefig()函数有一个强制性的filename参数。这里,我们已经指定了文件名和格式。
此外,它还接受其他选项,如dpi、transparent、buts_inches、quality等等。
- DPI参数定义了每英寸的点(像素)数。这基本上是我们要制作的图像的分辨率。默认值是100
- transparent参数可以用来创建一个透明背景的Plot。如果你要在演示文稿、论文中使用Plot图像,或者想在自定义设计中展示它,可以使用这个属性
- 你可以通过使用facecolor参数来改变脸部的颜色。它接受一个color,默认为white
- bub_inches参数接受一个字符串,指定我们要绘制的方框周围的边界。如果我们想把它设置为tight,即尽可能多地裁剪方框周围,我们可以把bub_inches参数设置为’tight’
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.savefig('saved_figure-tight.png', bbox_inches = 'tight', facecolor='red')