删除子图

84 投票
3 回答
70572 浏览
提问于 2025-04-17 14:44

我正在尝试找一种方法,在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()

在这里输入图片描述

3 个回答

5

从图形中移除坐标轴,具体可以参考这个文档

axs[1].remove()
32
ax.set_visible(False)

在大多数情况下,这样就足够了。

160

使用 fig.delaxes 或者 plt.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()

enter image description here

撰写回答