I want to hand some additional arguments to the animate_all funtion. Therefore, I wrote the new method like this:
JavaScript
x
7
1
def animate_all(index, *fargs):
2
print(index)
3
all_positions_list = fargs[0]
4
vel_list = fargs[1]
5
6
return something
7
However, I have problems calling the method. None of the attempts below worked.
JavaScript
1
17
17
1
animation_1 = animation.FuncAnimation(
2
fig,
3
animate_all(70, all_positions_list, vel_list),
4
interval=200,
5
frames=70,
6
cache_frame_data=False,
7
)
8
9
animation_1 = animation.FuncAnimation(
10
fig,
11
animate_all(all_positions_list, vel_list),
12
interval=200,
13
frames=70,
14
cache_frame_data=False,
15
)
16
17
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.
JavaScript
1
27
27
1
import matplotlib.pyplot as plt
2
import matplotlib.animation as animation
3
4
def animate(x_data, *args):
5
y_data = x_data ** 2
6
colour, style = args
7
x.append(x_data)
8
y.append(y_data)
9
line.set_data(x, y)
10
line.set(color=colour, linestyle=style)
11
return line,
12
13
N = 21
14
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(3, 3))
15
x, y = [], []
16
ax.set_xlim(0, N)
17
ax.set_ylim(0, N ** 2)
18
line, = ax.plot([], [])
19
20
anim = animation.FuncAnimation(
21
fig=fig,
22
func=animate,
23
frames=N,
24
fargs=("red", "--"),
25
)
26
anim.save('anim.gif')
27