Matplotlib: hide the subtitle and fill in the blanks with other subtasks

I have a figure containing three subheadings that are arranged vertically. As soon as I click on the figure, I want the second subplot to ax2be hidden, and other subjects fill the gap. The second click on the figure should restore the original graph and layout.

Hiding a subheading is ax2not a problem, but how can I change the position of other subplots?

I tried to create a new GridSpecone using set_positionand methods set_subplotspec, but nothing worked. I am sure that something is missing here, any help will be appreciated.

This is my code:

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()
+6
source share
2 answers

GridSpec s. 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

+7

gridspec ( , plt.show, , ).

ax1 ax3 , 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

+1

Source: https://habr.com/ru/post/1017203/


All Articles