fig.savefig
保存图片的时候,指定参数transparent=True
。import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(range(10))
fig.savefig('temp.png', transparent=True)
如果您希望有更细粒度的控制,可以直接设置图表和坐标轴背景补丁的facecolor和/或alpha值。(为了使补丁完全透明,我们可以将alpha设置为0,或者将facecolor设置为“none”(作为字符串,而不是对象None!))
例如:
import matplotlib.pyplot as plt
fig = plt.figure()
fig.patch.set_facecolor('blue')
fig.patch.set_alpha(0.7)
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.patch.set_facecolor('red')
ax.patch.set_alpha(0.5)
# If we don't specify the edgecolor and facecolor for the figure when
# saving with savefig, it will override the value we set earlier!
fig.savefig('temp.png', facecolor=fig.get_facecolor(), edgecolor='none')
plt.show()
fig.savefig
的评论。 - gboffi