2017-05-12 30 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

相關問題