[Python] Rescale axes concerning visible artists only

Discussão em 'Python' iniciado por Stack, Novembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    I have a request regarding the use of autoscale_view(). I was expecting it to scale the plot considering only the visible artists, but it does not have such an effect.

    I have a small test script as follows, any help is welcome:

    import matplotlib.pyplot as plt
    import numpy as np

    x1 = np.linspace(0, 10, 100)
    y1 = np.sin(x1)
    x2 = np.linspace(5, 15, 100)
    y2 = np.cos(x2)

    fig, ax = plt.subplots()
    ax.set_title('Click on legend line to toggle line on/off')
    (line1, ) = ax.plot(x1, y1, lw=2, label='1 Hz')
    (line2, ) = ax.plot(x2, y2, lw=2, label='2 Hz')
    leg = ax.legend()

    lines = [line1, line2]
    map_legend_to_ax = {} # Will map legend lines to original lines.

    pickradius = 5
    for legend_line, ax_line in zip(leg.get_lines(), lines):
    legend_line.set_picker(pickradius)
    map_legend_to_ax[legend_line] = ax_line

    def on_pick(event):
    legend_line = event.artist
    ax_line = map_legend_to_ax[legend_line]
    visible = not ax_line.get_visible()
    ax_line.set_visible(visible)
    legend_line.set_alpha(1.0 if visible else 0.2)

    ax.relim()
    ax.autoscale_view()
    fig.canvas.draw()

    fig.canvas.mpl_connect('pick_event', on_pick)
    plt.show()

    Continue reading...

Compartilhe esta Página