Matplotlib卷轴b

2024-05-14 12:43:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个行泛化算法,并希望添加一个滚动条到绘图,这将增加公差(即,使行越来越泛化)。使用matplotlib这怎么可能?

所以总而言之,我希望能够点击并拖动一个滑块,它将在线条上显示公差的增加效果。


仍然在挣扎。我只想要一个从1到10的简单比例的滑块。


是的,演示很有帮助,我只是为了让一个滑杆工作而大摇大摆,这就是我目前所拥有的

fig = mp.figure()
ax = fig.add_subplot(111)
fig.subplots_adjust(left=0.25, bottom=0.25)
min0=1
max0=10
tolerance = 0

chain1 = ChainLoader('Wiggle1.txt')
chain = chain1[0]

chain2 =  chain.generalise(tolerance)

axcolor = 'lightgoldenrodyellow'
axmin = fig.add_axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
axmax  = fig.add_axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)

tolerance = Slider(axmin, 'Min', 1, 10, valinit=min0)
#smax = Slider(axmax, 'Max', 0, 30000, valinit=max0)

def update(val):
    tolerance = tolerance.val
    #pp.show()

tolerance.on_changed(update)
#smax.on_changed(update)
chain2 =  chain.generalise(tolerance)
pp.plotPolylines(chain2)
pp.show()   

我的问题是如何编写def update部分。有什么帮助吗?

from PointPlotter import PointPlotter 
from ChainHandler import ChainLoader
pp=PointPlotter()
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider 

ax = plt.subplot(111)
plt.subplots_adjust(left=0.25, bottom=0.25)

tolerance = 0 
f0 = 0
chain2 = ChainLoader('Wiggle1.txt')
for chain in chain2:

    chain2 =  chain.generalise(tolerance)
    pp.plotPolylines(chain2)

axcolor = 'lightgoldenrodyellow'

axtol = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)

tolerance = Slider(axtol, 'tol', 0.1, 30.0, valinit=f0)

def update(val):
    tolerance = tolerance.val 
    for chain in chain2:

        chain2 =  chain.generalise(tolerance)
        pp.plotPolylines(chain2)

        pp.plotPolylines(chain2)

tolerance.on_changed(update) 

plt.show()

太近了!它现在正在打印,但在使用滚动条时返回“unbundlocalerror:local变量'tolerance'referenced before assignment”。@有什么帮助吗?


Tags: importaddchainmatplotlibfigupdatepltval
1条回答
网友
1楼 · 发布于 2024-05-14 12:43:36

您需要slider小部件(doc)

以下是示例中的演示:

http://matplotlib.org/examples/widgets/slider_demo.html

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

ax = plt.subplot(111)
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
s = a0*np.sin(2*np.pi*f0*t)
l, = plt.plot(t,s, lw=2, color='red')
plt.axis([0, 1, -10, 10])

axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
axamp  = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)

sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)

def update(val):
    amp = samp.val
    freq = sfreq.val
    l.set_ydata(amp*np.sin(2*np.pi*freq*t))
    plt.draw()
sfreq.on_changed(update)
samp.on_changed(update)

resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):
    sfreq.reset()
    samp.reset()
button.on_clicked(reset)

rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)
def colorfunc(label):
    l.set_color(label)
    plt.draw()
radio.on_clicked(colorfunc)

plt.show()

为了适应你的情况:

#smax.on_changed(update)
chain2 =  chain.generalise(tol)
pp.plotPolylines(chain2)

def update(val):
    tol = tolerance.val # get the value from the slider
    chain2 =  chain.generalise(tol) # shove that value into your code
    ax.cla() # clear the axes
    pp.plotPolylines(chain2) # re-plot your new results

# register the call back
tolerance.on_changed(update)

请小心重新使用变量名(使用tolerance两次,使用float一次,使用Slider一次,python会很高兴地用完全不同类型的新变量替换旧变量)。

update中,我使用了最强力的方法,清除axes,然后重新绘制,一般来说,您需要获取plotPolylines返回的艺术家,并用新数据更新这些艺术家。(如果您需要该步骤的帮助,请打开一个包含有关数据结构的详细信息的新问题)。

理解.on_changed的方法是,当滑块注意到它已被更改时,它将使用单个参数(val)调用您传入的函数(update),该参数是滑块的当前值。在这个函数中,你可以做任何你想做的事情,每次滑块改变时,它都会被完全执行。

相关问题 更多 >

    热门问题