pythonmatplotlibmatplotlib-widget

matplotlib - Unable to update plot with button widget


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.)


Solution

  • change :

    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)

    to

    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) plt.draw() ######## I've added this line print(text_out)

    and see if you get what you wanted:

    enter image description here

    this code works too:

    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
            
            self.ax_str = fig.add_axes((0.25, 0.5, 0.5, 0.1))
    
            self.ax_button = fig.add_axes((0.25, 0.3, 0.5, 0.1))
            self.my_button = Button(self.ax_button, "Push this button", color="0.75", hovercolor="0.875")
            self.my_button.on_clicked(func=self.button_fx)
        
        def button_fx(self, event):
            self.ax_str.clear()
            self.N_presses += 1
            self.text_out = "Pushed {} times".format(self.N_presses)
            self.ax_str.text(0.5, 0.5, self.text_out)
            plt.draw()
            print(self.text_out)
            
        
    MC = MyClass(fig)
    plt.show()