我一直在玩Matplotlib,我不知道如何改变图形的背景颜色,或者如何使背景完全透明。
发布于 2011-01-17 05:01:42
如果您只希望图形和轴的整个背景都是透明的,则只需在使用fig.savefig
保存图形时指定transparent=True
即可。
例如:
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(range(10))
fig.savefig('temp.png', transparent=True)
如果你想要更细粒度的控制,你可以简单地设置figure和axes背景补丁的facecolor和/或alpha值。(要使面片完全透明,我们可以将None
设置为0,或者将facecolor设置为'none'
(作为字符串,而不是对象alpha!))
例如:
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()
https://stackoverflow.com/questions/4581504
复制相似问题