我有一个图,其中包含三个垂直排列的子图。一旦我点击进入该图,我想要隐藏第二个子图块ax2
,而其他图则填充该空间。第二次点击图形应该恢复原始图和布局。matplotlib:隐藏子图和其他子图填充空间
隐藏子区域ax2
不是问题,但是如何重新排列其他子图的位置?
我已经尝试使用set_position
和set_subplotspec
方法创建新的GridSpec
,但没有任何结果。我确信我在这里失去了一些东西,任何帮助将不胜感激。
这是我的代码:
import matplotlib.pyplot as plt
from matplotlib import gridspec
fig = plt.figure()
gs = gridspec.GridSpec(3, 1, height_ratios=[5, 2, 1])
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1], sharex=ax1)
ax3 = fig.add_subplot(gs[2], sharex=ax2)
visible = True
def toggle_ax2(event):
global visible
visible = not visible
ax2.set_visible(visible)
plt.draw()
fig.canvas.mpl_connect('button_press_event', toggle_ax2)
plt.show()