[Python] matplotlib - Unable to update plot with button widget

Discussão em 'Python' iniciado por Stack, Março 21, 2025.

  1. Stack

    Stack Membro Participativo

    The code below draws a button and an axes object in which it's meant to print out the number of times the button has been pressed. However, it never updates the axes with the number of presses.

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

    fig, ax = plt.subplots(figsize=(7,7))
    ax.set_visible(False)

    class MyClass:

    def __init__(self, fig):
    self.N_presses = 0

    def button_fx(self, event):
    ax_str.clear()
    self.N_presses += 1
    text_out = "Pushed {} times".format(self.N_presses)
    ax_str.text(0.5, 0.5, text_out)
    print(text_out)

    MC = MyClass(fig)

    ax_str = fig.add_axes((0.25, 0.5, 0.5, 0.1))

    ax_button = fig.add_axes((0.25, 0.3, 0.5, 0.1))
    my_button = Button(ax_button, "Push this button", color="0.75", hovercolor="0.875")
    my_button.on_clicked(func=MC.button_fx)

    plt.show()


    As a check, I also have it print out the number of presses to the console, which happens as it should. It's only the axes that seem to be out of reach. Why can't I update the axes ax_str with new text using button_fx? I can't even plot to it. Is there a workaround?

    (Note: although it doesn't add anything in this MWE, the class is essential for my actual use case, so I need to know how to solve this problem while keeping the class structure.)

    Continue reading...

Compartilhe esta Página