[Python] How to address line (or similar) elements in a matplotlib window

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    How do I address a matplotlib element in a window? In this code, a window is created and three lines a re drawn. with the "m" key you can add another line, "l" lists all lines with its position and "n" changes all lines of a certain position to a fixed new one. Just for my education purpose. How do I address a line? I would like to get a lines address f.e. when I hover the mouse pointer over it and change its properties later.

    import sys
    import matplotlib.pyplot as plt
    import matplotlib
    import numpy as np

    #print relevant info to terminal

    Taste = "a"
    Vorgang = "a" + " :"

    def on_press(event): #get the key pressed and store it in a global variable
    global Taste, Vorgang
    print("you pressed: ", event.name, " Key: ", event.key, " location: ", event.xdata, event.ydata)
    Taste = event.key
    Vorgang = event.name + " on_press-test"
    print ("Taste: ", Taste, "Vorgang: ", Vorgang, "event.key Type: ",type(event.key), "Taste type: ", type(Taste))

    # cretae window with axis and plot lines
    fig, ax = plt.subplots(1,1)
    fig.canvas.mpl_connect('key_press_event', on_press)
    ax = plt.gca()
    plt.plot([1.6, 2.7], [1.5, 6.4])
    plt.title("interactive test")
    plt.xlabel("index")
    ax.plot([5.1, 7.2])
    ax.plot([3.1, 2.2])

    # get all lines and print them to terminal
    for x in ax.get_lines():
    print (x, " - ", "0.0: ", x.get_xydata()[0,0], "|- 0.1: ", x.get_xydata()[0,1], "|- 1.0: ", x.get_xydata()[1,0], "|- 1.1: ", x.get_xydata()[1,1],)
    print (ax.tables)

    #start interactive mode and show plot
    plt.ion()
    plt.show()

    #list all plot elements
    print ("Start==========================")
    for x in fig.axes:
    print (ax.lines)
    print (x)
    print("1st layer")
    for line in ax.lines:
    print ("x-data ",line.get_xdata())
    print ("y-data ",line.get_ydata())
    # print (dir(line)) # use only to read modules
    print (matplotlib.lines.Line2D)

    #loop to react on keys pressed
    while True:
    if plt.waitforbuttonpress():
    print("Test-if1: ", Taste, " Vorgang: ",Vorgang)
    if Taste == "Q" or Taste == "q": #quit app
    print ("Quit!")
    sys.exit()
    if Taste =="m": #add another line on a fixed position
    ax.plot([2.1, 6.2],[4.3,2.8])
    plt.draw()
    if Taste =="n": #move added line to a new position
    for line in ax.lines:
    Xdata = line.get_xdata()
    Ydata = line.get_ydata()
    print (Xdata, Ydata, "type Xdata: ", type(Xdata), len(Xdata), " ;; ", line.get_xdata())
    a = np.array([2.1, 6.2])
    print ("a: ", type(a), a.dtype)
    if np.array_equal(Xdata,a):
    line.set_xdata([2.6, 0.7])
    line.set_ydata([1, 2])
    plt.draw()
    if Taste =="l": #list all lines positions
    plt.draw()
    for line in ax.lines:
    Xdata = line.get_xdata()
    Ydata = line.get_ydata()
    print (Xdata, Ydata, "type Xdata: ", type(Xdata), len(Xdata), " ;; ", line.get_xdata())
    plt.draw()


    # to end app.
    plt.ioff()
    plt.show()


    The command print (ax.axes.lines.index) gives:


    axes.lines.index <
    ​

    following line gives this answer but not the actual address: print (ax.axes.lines.index)

    result:


    <Axes.ArtistList of 3 lines> Axes(0.125,0.11;0.775x0.77)
    ​

    Continue reading...

Compartilhe esta Página