[Python] How to limit matplotlib button_press_event to a single axis?

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

  1. Stack

    Stack Membro Participativo

    This script:

    import matplotlib.pyplot as plt
    from matplotlib.widgets import Button

    def fAccept(_):
    print(f'accepted')

    def onclick(event):
    print(f'click {event.xdata, event.ydata}')

    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=0.2)
    fig.canvas.mpl_connect('button_press_event', onclick)
    axAccept = fig.add_axes([0.7, 0.05, 0.1, 0.075])
    bAccept = Button(axAccept, 'Accept')
    bAccept.on_clicked(fAccept)
    plt.show()


    displays

    [​IMG]

    Clicking on bAccept button calls onclick callback. Is there a way to limit onclick callback to the top axis? Alternatively, is there a way to distinguish event coordinates (0.5, 0.5) from the center of bAccept button (green circle) from (0.5, 0.5) coordinates from the center of the upper axis (red circle)?

    Continue reading...

Compartilhe esta Página