2017-05-12 9 views
5

के साथ स्थान भरें मुझे एक ऐसा आंकड़ा मिला है जिसमें तीन उप-स्थान शामिल हैं जिन्हें लंबवत रूप से व्यवस्थित किया गया है। एक बार जब मैं आकृति में क्लिक करता हूं, तो मैं दूसरा सबप्लॉट 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() 

उत्तर

7

आप दो अलग अलग GridSpec रों परिभाषित कर सकते हैं। एक में 3 सबप्लॉट होंगे, दूसरा 2. मध्यम अक्षों की दृश्यता के आधार पर, आप दूसरे दो अक्षों की स्थिति को पहले या दूसरे ग्रिडस्पेक का पालन करने के लिए बदलते हैं।
(किसी भी डमी आंकड़ा या तो, जैसे अन्य जवाब सुझाव दे सकता है के लिए कोई जरूरत नहीं है।)

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 से पहले इस बंद कर सकते हैं में कुछ डमी आंकड़े बनाने के लिए है, तो आप वास्तव में कभी नहीं देखना यह, हम सिर्फ अक्षों से कुछ पदों को पकड़ना चाहते हैं)।

कि डमी आंकड़ा और मूल आंकड़े से ax1 और ax3 के लिए दो संभावित पदों के भंडारण, तो आप अपने toggle_ax2 समारोह में ax.set_position() उपयोग कर सकते हैं शेष दो कुल्हाड़ियों की स्थिति बदलने के लिए के द्वारा।

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

संबंधित मुद्दे