matplotlib widgets Slider demo源码解释

2022-01-18 00:00:00 python matplotlib widget

问题描述

我试图了解 源代码 以便能够使用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:

  1. val 参数的名称更改为其他随机词,例如 def update(wtf): 而不更改函数的主体.生成的代码仍然可以正常工作.
  2. 为参数添加一些默认值,例如 def update(wtf=None): 而不更改函数的主体.生成的代码仍然可以正常工作.
  3. 只需删除参数.def update():.滑块不再改变情节,这意味着脚本不起作用.
  1. 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.
  2. 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.
  3. 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 回调将点击事件传递给函数.在这种情况下,这毫无用处,但可能会用于查看使用了哪个鼠标按钮,或者点击的确切位置.

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)

相关文章