【问题标题】:about the args of matplotlib.animation.FuncAnimation关于 matplotlib.animation.FuncAnimation 的 args
【发布时间】:2012-11-30 01:20:25
【问题描述】:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
ax = fig.add_subplot(111)

x = np.arange(0, 2*np.pi, 0.01)        # x-array
i=1
line, = ax.plot(x, np.sin(x))

def animate():
    i= i+2
    x=x[1:] + [i]
    line.set_ydata(np.sin(x))  # update the data
    return line,

#Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, init_func=init,
    interval=25, blit=True)
plt.show()

我得到这样的错误:animate() 没有参数(1 个给定).. 很困惑。我什至不给回调函数一个参数。有什么我错过的吗?

谢谢。

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    看起来文档已经关闭,或者至少在这里不清楚:该函数有一个内在的第一个参数,即帧号。所以你可以简单地将其定义为def animate(*args)def animate(framenumber, *args),甚至def animate(framenumber, *args, **kwargs)

    另见this example

    请注意,之后您会遇到其他问题:

    • ix 内的 animate 应声明为 global。或者更好的是,通过FuncAnimation 中的fargs 关键字将它们作为参数传递。

    • x = x[1:] + [i] 不像你想象的那样工作。 Numpy 数组的工作方式与列表不同:它将[i] 添加到x[1:] 的每个元素并将其分配给x,从而使x 缩短一个元素。一种可能的正确方法是x[:-1] = x[1:]; x[-1] = i

    【讨论】:

    • 非常感谢。我不得不说文档太“简短”。希望会更好...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2022-11-09
    • 2016-09-10
    相关资源
    最近更新 更多