matplotlib滑块重绘不更新figtext

发布于 2024-12-01 00:50:54 字数 606 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-12-08 00:50:54

每次调用 p.figtext(0.5, 0.65, str(slider1.val)) 时,您都会创建一个新的 Text 对象,该对象将写入之前的对象之上。您应该做的是保存对第一个 Text 对象的引用,并通过调用其 set_text() 方法来更新其内容。我已经用一个工作示例更新了您的代码。

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)

# the text on the figure
fig_text = p.figtext(0.5, 0.65,  str(slider1.val))

def update(val):
    f = slider1.val*np.sin(x/100.0)
    l11.set_ydata(f)
    np.set_printoptions(precision=2)

    # update the value of the Text object
    fig_text.set_text(str(slider1.val))

    p.draw()

slider1.on_changed(update)
p.show()

Every time you call p.figtext(0.5, 0.65, str(slider1.val)) you are creating a new Text object which is being written on top of the previous ones. What you should do is save a reference to the first Text object and update its contents by calling its set_text() method. I have updated your code with a working example.

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)

# the text on the figure
fig_text = p.figtext(0.5, 0.65,  str(slider1.val))

def update(val):
    f = slider1.val*np.sin(x/100.0)
    l11.set_ydata(f)
    np.set_printoptions(precision=2)

    # update the value of the Text object
    fig_text.set_text(str(slider1.val))

    p.draw()

slider1.on_changed(update)
p.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文