为了go 掉图中的框架,我写
frameon=False
使用pyplot.figure
效果很好,但使用matplotlib.Figure
只会移除灰色背景,框架会保持不变.另外,我只想显示线条,并且所有的睡觉图形都是透明的.
用pyploy我可以做我想做的事,我想用matplotlib做这件事,因为有一些很长的原因,我不愿提及扩展我的问题.
为了go 掉图中的框架,我写
frameon=False
使用pyplot.figure
效果很好,但使用matplotlib.Figure
只会移除灰色背景,框架会保持不变.另外,我只想显示线条,并且所有的睡觉图形都是透明的.
用pyploy我可以做我想做的事,我想用matplotlib做这件事,因为有一些很长的原因,我不愿提及扩展我的问题.
首先,如果您使用的是savefig
,请注意它在保存时会覆盖地物的背景 colored颜色 ,除非您另行指定(例如fig.savefig('blah.png', transparent=True)
).
但是,要删除屏幕上轴和图形的背景,您需要将ax.patch
和fig.patch
都设置为不可见.
例如.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10))
for item in [fig, ax]:
item.patch.set_visible(False)
with open('test.png', 'w') as outfile:
fig.canvas.print_png(outfile)
(当然,在SO的白色背景上,你无法分辨出区别,但一切都是透明的……)
如果不想显示线以外的任何内容,请使用ax.axis('off')
关闭轴:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10))
fig.patch.set_visible(False)
ax.axis('off')
with open('test.png', 'w') as outfile:
fig.canvas.print_png(outfile)
但是,在这种情况下,您可能希望使轴占据整个图形.如果手动指定轴的位置,您可以告诉它占据整个图形(或者,您可以使用subplots_adjust
,但对于单个轴来说,这更简单).
import matplotlib.pyplot as plt
fig = plt.figure(frameon=False)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.plot(range(10))
with open('test.png', 'w') as outfile:
fig.canvas.print_png(outfile)