【发布时间】:2015-12-15 13:59:31
【问题描述】:
我正在努力弄清楚这段代码中发生了什么并对其进行调整以使我自己的代码能够正常工作。我不确定它是 Python 函数通用的东西,还是 matplotlib 动画包特有的东西。
代码来自Jake Vanderplas animation tutorial,但在网络上也有类似的例子。
这是被调用的动画函数:
# animation function. This is called sequentially
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
这是调用它的行:
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
animate 接受参数i,但他调用它时没有变量i。 animation.FuncAnimation 是否知道在其中传递一个值或者这是如何工作的?
顺便说一句——return 语句后面的逗号是否有意义?
【问题讨论】:
-
return line, == return (line,) -
最有可能的是,
FuncAnimation调用函数animate并将框架作为参数。那个“逗号”意味着返回是一个元组,如果它不存在,则返回将是一个变量。 -
好的,谢谢,真的没想到,这是我第一次尝试动画,需要一些东西,以为可能是省略换行符,但显然不是。将阅读return语句必须多一点。..
标签: python function animation matplotlib