matplotlib widgets Slider 演示源代码解释 [英] matplotlib widgets Slider demo source code explanation
问题描述
我试图理解源代码以便能够使用matplotlib 的滑块小部件.定义函数 update()
的那段代码真的让我很困扰:
I'm trying to understand the source code to be able to use the Slider widgets of matplotlib. The piece of code defining function update()
really bothers me:
我不认为在 def update(val):
中有参数 val
的意义,而 val
的值是没有提到任何地方. def reset(event):
函数也存在同样的问题.到目前为止,我已经完成了一些简单的测试:
I don't see the point of having the argument val
in def update(val):
, and the value of val
is not referred to anywhere. It's the same issue with the def reset(event):
function. Some simple tests I've done so far:
- 将
val
参数的名称更改为其他随机单词,例如def update(wtf):
而不更改函数体.生成的代码仍然可以正常工作. - 向参数添加一些默认值,例如
def update(wtf=None):
而不更改函数体.生成的代码仍然可以正常工作. - 只需删除参数.
def update():
.滑块不再改变情节,这意味着脚本不起作用.
- change the name of
val
argument to other random word, say,def update(wtf):
without changing the body of the function. The resulting code still works as desired. - Adding some default value to the argument, say,
def update(wtf=None):
without changing the body of the function. The resulting code still works as desired. - Simply remove the argument.
def update():
. The Slider NO LONGER changes the plot, meaning the script is not working.
我不明白这个 update
函数或 val
参数是如何工作的.谁能解释一下?
I don't understand how this update
function or the val
argument works. Can someone explain?
推荐答案
on_changed
回调将向函数提供滑块的当前值.update
函数因此需要一个参数.在示例中未使用此参数的原因是 update
函数需要两个滑块的值(与哪个滑块无关),因此直接从Slider实例获取值.
The on_changed
callback will supply the current value of the slider to the function. The update
function therefore needs an argument. The reason this argument is not used in the example is that the update
function needs the values of both sliders, independently of which one is changed, and therefore takes the values directly from the Slider instances.
为了更好地理解滑块的工作方式,可以考虑使用以下简化版本:
To better understand the way the slider works, one may consider the following simplified version:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
s = np.sin(6*np.pi*t)
l, = plt.plot(t, s, lw=2, color='red')
plt.axis([0, 1, -1.2, 1.2])
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor="lightblue")
sfreq = Slider(axfreq, 'Freq', 0.1, 20.0, valinit=3)
def update(val):
l.set_ydata(np.sin(2*np.pi*val*t))
fig.canvas.draw_idle()
sfreq.on_changed(update)
plt.show()
这里您只有一个滑块,该值被传递给更新函数,用于计算新的 ydata 值.
Here you only have one slider and the value is passed to the update function, where it is used to calculate new ydata values.
非常相似,按钮的 on_click
回调将click事件传递给函数.在这种情况下,这是没有用的,但是可能会被用来查看使用了哪个鼠标按钮,或者确切地单击了何处.
Very simlarly, the on_click
callback of the button passes a click event to the function. This is pretty useless in this case, but could potentially be used to see, which mouse button has been used, or where exactly the click has happened.
def reset(event):
if event.button == 1:
sfreq.reset()
samp.reset()
else:
print("Please use the left mouse button")
button.on_clicked(reset)
这篇关于matplotlib widgets Slider 演示源代码解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!