2017-05-12 48 views
5

Dikey olarak düzenlenmiş üç alt resim içeren bir rakam var. Şekle tıkladıktan sonra, ikinci alt çizimin ax2 gizlenmesini ve diğer bölümlerin boşluğu doldurmasını istiyorum. Şekle ikinci bir tıklama, orijinal çizimi ve düzeni geri yüklemelidir.matplotlib: altpip'i gizler ve boşlukları diğer alt piksellerle doldurur

Altpip ax2'un gizlenmesi bir sorun değil, ancak diğer alt piksellerin konumlarını nasıl yeniden düzenleyebilirim?

set_position ve set_subplotspec yöntemlerini kullanarak yeni bir GridSpec oluşturmayı denedim, ancak hiçbir şey işe yaramadı. Eminim ki burada bir şey kaçırıyorum, herhangi bir yardım takdir edilecektir.

Bu

benim kodudur:

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

cevap

7

iki farklı GridSpec s tanımlayabilirsiniz. Biri 3 alt düzlemden diğerine sahip olacaktır. 2. Orta eksenlerin görünürlüğüne bağlı olarak, diğer iki eksenin konumunu birinci veya ikinci GridSpec'e uydurmak üzere değiştirirsiniz.
(herhangi bir kukla figürü ya da öylesine, diğer cevaplar önerebiliriz gibi gerek yoktur.)

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

Sol: Orijinal; sağa:

1

Yeni gridspec örneğini oluşturmak ve aslında görmek asla bu yüzden, ikinci bir figür (size plt.show önce bu kapatabilirsiniz bazı kukla figürler oluşturmak için kullanabilirsiniz bu, sadece burada eksenlerden bazı pozisyonlar almak istiyoruz). o zaman kalan iki eksen konumlarını değiştirmek için toggle_ax2 işlevinde ax.set_position() kullanabilirsiniz, yani kukla figürü ve özgün figür gelen ax1 ve ax3 için iki olası pozisyonları saklayarak

.

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

Orijinal yapılandırma: enter image description here

ax2 çıkardıktan sonra: enter image description here