如何覆盖动画方法? matplotlib.animation.FuncAnimation

How to overwrite animate method? matplotlib.animation.FuncAnimation

我想向 animate_all 函数传递一些额外的参数。因此,我写了这样的新方法:

def animate_all(index, *fargs):
    print(index)
    all_positions_list = fargs[0]
    vel_list = fargs[1]

    return something

但是,我在调用该方法时遇到问题。 None 下面的尝试成功了。

animation_1 = animation.FuncAnimation(
    fig,
    animate_all(70, all_positions_list, vel_list),
    interval=200,
    frames=70,
    cache_frame_data=False,
)

animation_1 = animation.FuncAnimation(
    fig,
    animate_all(all_positions_list, vel_list),
    interval=200,
    frames=70,
    cache_frame_data=False,
)

帧通常会“自动”传递,但如果我扩展了该功能则不会。有人有解决方案吗?

这里是一个示例,说明如何将 fargs 与动画功能一起使用。

import matplotlib.pyplot as plt
import matplotlib.animation as animation

def animate(x_data, *args):
    y_data = x_data ** 2
    colour, style = args
    x.append(x_data)
    y.append(y_data)
    line.set_data(x, y)
    line.set(color=colour, linestyle=style)
    return line,
    
N = 21
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(3, 3))
x, y = [], []
ax.set_xlim(0, N)
ax.set_ylim(0, N ** 2)
line, = ax.plot([], [])

anim = animation.FuncAnimation(
    fig=fig, 
    func=animate, 
    frames=N,
    fargs=("red", "--"),
)
anim.save('anim.gif')

输出: