[Python] Get Line2D object from LineCollection

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    In pyplot I use axes.vlines to draw a single vline. Now I want to put an entry into the legend. axes.legend() expects a list of line objects. vlines() returns a LineCollection object. Is there a way to extract the line2D from the LineCollection.

    fig, ax = plt.subplots(1,1)
    ax.plot(meas['times'], meas['data'], label='measurement')
    vlins = ax.vlines([trigger_time], 0, 100)
    lines, labels = ax.get_legend_handles_labels()
    # there is no: vvvvvvvvv
    ax.legend( lines + vlins.to_list(), labels + ['trigger event']


    There is lot of documentation of how to create a LineCollection. But it seems to be a one way ticket.

    Continue reading...

Compartilhe esta Página