2017-05-12 10 views
5

Mam figurę, która zawiera trzy podziały ułożone pionowo. Po kliknięciu na rysunek chcę, aby drugi subplot ax2 został ukryty, a pozostałe działki wypełniły przestrzeń. Drugie kliknięcie na rysunku powinno przywrócić pierwotną fabułę i układ.matplotlib: ukryj subplot i wypełnij przestrzeń z innymi subplotami

Ukrywanie subplota ax2 nie stanowi problemu, ale jak mogę zmienić położenie pozostałych wątków?

Próbowałem utworzyć nowy GridSpec, używając metod set_position i set_subplotspec, ale nic się nie udało. Jestem pewien, że czegoś tutaj brakuje, każda pomoc będzie doceniona.

To jest mój kod:

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() 

Odpowiedz

7

Można zdefiniować dwa różne GridSpec s. Jeden miałby 3 podpoziomy, drugi 2. W zależności od widoczności środkowych osi, zmienia się położenie pozostałych dwóch osi, aby wykonać pierwszą lub drugą GridSpec.
(Nie ma potrzeby jakiegokolwiek manekina rysunku lub tak, jak inni mogą sugerować odpowiedzi.)

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() 

lewej: oryginalny; po prawej: po kliknięciu:

enter image description here

1

Można utworzyć nowy gridspec instancji i używać, aby stworzyć jakieś fikcyjne dane w drugim rysunku (można zamknąć to przed wami plt.show, więc nie można rzeczywiście zobaczyć to, chcemy tylko pobrać niektóre pozycje z osi).

Zapisując dwie możliwe pozycje dla ax1 i ax3 z tego manekina figury i oryginalnego rysunku, a następnie można użyć ax.set_position() w funkcji toggle_ax2 zmienić pozycje pozostałych dwóch osiach.

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() 

Oryginalna konfiguracja: enter image description here

Po usunięciu ax2: enter image description here

Powiązane problemy