admin管理员组文章数量:1402786
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.)
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.)
Share Improve this question asked Mar 21 at 18:34 BGreenBGreen 4824 silver badges20 bronze badges1 Answer
Reset to default 1change :
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:
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()
本文标签: pythonmatplotlibUnable to update plot with button widgetStack Overflow
版权声明:本文标题:python - matplotlib - Unable to update plot with button widget - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744339254a2601378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论