提问者:小点点

如何从matplotlib中删除框架(pyplot.figure vs matplotlib.figure)(matplotlib中frameon=False)


为了删除图中的框架,我写了

frameon=False

适用于pyplot。图,但带有matplotlib。图仅删除灰色背景,框架保持不变。另外,我只想显示线条,图中其他部分都是透明的。

有了pyplotlib,我可以做我想做的,我想用matplotlib做这件事,原因很长,我宁愿不提延伸我的问题。


共3个答案

匿名用户

ax。axis('off'),正如Joe Kington指出的,将删除除绘制线以外的所有内容。

对于那些只想移除框架(边框)并保留标签、标记等的人,可以通过访问轴上的脊椎对象来实现。给定一个axis对象ax,应删除所有四条边上的边框:

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

并且,如果从绘图中删除xy记号:

 ax.get_xaxis().set_ticks([])
 ax.get_yaxis().set_ticks([])

匿名用户

首先,如果您使用的是savefig,请注意,除非您另有指定(例如fig.savefig('blah.png',transparent=True)),否则它将在保存时覆盖地物的背景色。

但是,要删除屏幕上的轴和图形背景,需要同时设置ax。补丁图。补丁不可见。

例如。

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)

但是,在这种情况下,您可能希望使轴占据整个图形。如果您手动指定轴的位置,您可以告诉它占据整个图形(或者,您可以使用子地块调整,但对于单个轴来说,这更简单)。

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)

匿名用户

在新版本的matplotlib中摆脱丑陋框架的最简单方法:

import matplotlib.pyplot as plt
plt.box(False)

如果必须始终使用面向对象方法,那么执行以下操作:ax.set_frame_on(False)