2017-05-12 27 views
5

我有一个图,其中包含三个垂直排列的子图。一旦我点击进入该图,我想要隐藏第二个子图块ax2,而其他图则填充该空间。第二次点击图形应该恢复原始图和布局。matplotlib:隐藏子图和其他子图填充空间

隐藏子区域ax2不是问题,但是如何重新排列其他子图的位置?

我已经尝试使用set_positionset_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() 

回答

7

您可以定义两个不同的GridSpec秒。一个将有3个子图,另外2个。根据中间轴的可见性,您可以更改其他两个轴的位置以遵守第一个或第二个GridSpec。
(无需任何虚拟人物或如此,像其他的答案可能暗示。)

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 

fig = plt.figure() 

gs = gridspec.GridSpec(3, 1, height_ratios=[5, 2, 1], hspace=0.3) 
gs2 = gridspec.GridSpec(2,1, height_ratios=[5,3]) 

ax1 = fig.add_subplot(gs[0]) 
ax2 = fig.add_subplot(gs[1], sharex=ax1) 
ax3 = fig.add_subplot(gs[2], sharex=ax2) 

ax1.plot([1,2,3], [1,2,3], color="crimson") 
ax2.plot([1,2,3], [2,3,1], color="darkorange") 
ax3.plot([1,2,3], [3,2,1], color="limegreen") 

visible = True 

def toggle_ax2(event): 
    global visible 
    visible = not visible 
    ax2.set_visible(visible) 
    if visible: 
     ax1.set_position(gs[0].get_position(fig)) 
     ax3.set_position(gs[2].get_position(fig)) 
    else: 
     ax1.set_position(gs2[0].get_position(fig)) 
     ax3.set_position(gs2[1].get_position(fig)) 

    plt.draw() 

fig.canvas.mpl_connect('button_press_event', toggle_ax2) 
plt.show() 

左:原件;右:点击后

enter image description here

1

您可以创建一个新gridspec实例,并用它来创建在第二个数字(您可以在plt.show之前关闭此一些虚拟的人物,所以你从来没有真正看到它,我们只是想在这里从轴上获取一些位置)。

通过从虚拟人物和原来的数字存储为ax1ax3的两个可能的位置,那么你可以使用ax.set_position()toggle_ax2函数改变剩下的两个轴的位置。

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) 

# Store the original positions of ax1 and ax3 
pos1_1 = ax1.get_position() 
pos3_1 = ax3.get_position() 

# Create a second gridspec for when ax2 is hidden. Keep 5:1 ratio 
gs2 = gridspec.GridSpec(2, 1, height_ratios=[5, 1]) 
fig2 = plt.figure() 
ax1_2 = fig2.add_subplot(gs2[0]) 
ax3_2 = fig2.add_subplot(gs2[1]) 

# Store the positions of ax1 and ax3 in the new gridspec 
pos1_2 = ax1_2.get_position() 
pos3_2 = ax3_2.get_position() 

# Close the dummy figure2 
plt.close(fig2) 

visible = True 

def toggle_ax2(event): 
    global visible 

    visible = not visible 
    ax2.set_visible(visible) 

    # Use the stored positions to switch between 
    # different arrangements of ax1 and ax3 
    if visible: 
     ax1.set_position(pos1_1) 
     ax3.set_position(pos3_1) 
    else: 
     ax1.set_position(pos1_2) 
     ax3.set_position(pos3_2) 
    plt.draw() 

fig.canvas.mpl_connect('button_press_event', toggle_ax2) 
plt.show() 

原装配置: enter image description here

去除ax2后: enter image description here

相关问题