I want to hand some additional arguments to the animate_all funtion. Therefore, I wrote the new method like this:
def animate_all(index, *fargs):
print(index)
all_positions_list = fargs[0]
vel_list = fargs[1]
return something
However, I have problems calling the method. None of the attempts below worked.
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,
)
Frames usually gets passed “automaticly” but not if I extended the function. Does somebody have a solution?
Advertisement
Answer
Here is an example of how you can use fargs with an animation function.
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')
