matplotlib滑块重绘不更新figtext
我使用 matplotlib 中的滑块根据 GUI 输入更新一些图表。 我所有的图表都更新得很好。 但是当我使用Figtext时,我遇到了更新后的文本会覆盖现有文本的问题。
import numpy as np
import pylab as p
from matplotlib.widgets import Slider
p.subplot(111)
x = np.arange(0,500,1)
f = np.sin(x/100.0)
l11, = p.plot(f)
ax = p.axes([0.25, 0.05, 0.7, 0.03], axisbg='lightgoldenrodyellow')
slider1 = Slider(ax, 'amplitude', -1.0, 1.5, valinit=0)
def update(val):
f = slider1.val * np.sin(x/100.0)
l11.set_ydata(f)
np.set_printoptions(precision=2)
p.figtext(0.5, 0.65, str(slider1.val) )
p.draw()
slider1.on_changed(update)
p.show()
I used sliders in matplotlib to update a few graphs based on GUI input.
All my graphs update well.
But when I use figtext, I have the problem that the updated text will write over the existing one.
import numpy as np
import pylab as p
from matplotlib.widgets import Slider
p.subplot(111)
x = np.arange(0,500,1)
f = np.sin(x/100.0)
l11, = p.plot(f)
ax = p.axes([0.25, 0.05, 0.7, 0.03], axisbg='lightgoldenrodyellow')
slider1 = Slider(ax, 'amplitude', -1.0, 1.5, valinit=0)
def update(val):
f = slider1.val * np.sin(x/100.0)
l11.set_ydata(f)
np.set_printoptions(precision=2)
p.figtext(0.5, 0.65, str(slider1.val) )
p.draw()
slider1.on_changed(update)
p.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次调用
p.figtext(0.5, 0.65, str(slider1.val))
时,您都会创建一个新的Text
对象,该对象将写入之前的对象之上。您应该做的是保存对第一个Text
对象的引用,并通过调用其set_text()
方法来更新其内容。我已经用一个工作示例更新了您的代码。Every time you call
p.figtext(0.5, 0.65, str(slider1.val))
you are creating a newText
object which is being written on top of the previous ones. What you should do is save a reference to the firstText
object and update its contents by calling itsset_text()
method. I have updated your code with a working example.