[Matplotlib-users] animation with FuncAnimation (mpl 3.0.2)

Alan Isaac alan.isaac at gmail.com
Fri Mar 22 15:03:55 EDT 2019


I think I found a bug, and I have a question.
Consider the code below.

1. Although frames=10, `animate` is called 11 times, twice with an argument of 0.
(The `print` calls show this.) I suspect this is a bug?

2. Why is the assignment of the FuncAnimation instance to a name
required for the animation to work, and exactly how does this trick work?
How could I change this example to avoid this hidden magic?

Thank you,
Alan Isaac

import random
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
xs, ys = [],[]
l1, = ax.plot(xs, ys, 'b.')
ax.set_xlim(0,10)
ax.set_ylim(0,10)

def animate(i):
     print(i)
     xs.append(random.random()*10)
     ys.append(random.random()*10)
     l1.set_data(xs, ys)

ani = FuncAnimation(fig, animate, frames=10, interval=200, repeat=False)
plt.show()


More information about the Matplotlib-users mailing list