删除一个次要情节

83
我正在尝试找到一种在matplotlib中动态删除子图的方法。我看到它们有一个remove方法,但是我遇到了错误。
NotImplementedError: cannot remove artist

我很惊讶地发现这个在任何地方都找不到。有人知道怎么做吗?
from matplotlib import pyplot as plt

fig, axs = plt.subplots(1,3)

axs[0].plot([1,2],[3,4])
axs[2].plot([0,1],[2,3])

plt.draw()
plt.tight_layout()

enter image description here

3个回答

158

使用fig.delaxesplt.delaxes来移除不需要的子图。

fig, axs = plt.subplots(1,3)
axs[0].plot([1,2],[3,4])
axs[2].plot([0,1],[2,3])

fig.delaxes(axs[1])

plt.draw()
plt.tight_layout()

这里输入图片描述


1
非常感谢您的回答!那么现在如何消除子图之间的空白,并让两个图填满整个窗口宽度呢?tight_layout() 对我来说效果不佳... - Roman Sverdlov

31
ax.set_visible(False)

在大多数情况下,这将足够。


5

文档中移除图形的轴:

axs[1].remove()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接