I'm trying out some animating with python and found something weird while working through the "Animation" part of this tutorial. Relevant code:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(7, 6.5)
ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
patch = plt.Circle((5, -5), 0.75, fc='y')
def init():
patch.center = (5, 5)
ax.add_patch(patch)
return patch,
def animate(i):
x, y = patch.center
x = 5 + 3 * np.sin(np.radians(i))
y = 5 + 3 * np.cos(np.radians(i))
patch.center = (x, y)
return patch,
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=360,
interval=20,
blit=True)
plt.show()
On my pc the python script showed two circles instead of one, with one of them being fixed at (5,5). After some searching I figured out that the init()
function gets called two times. This seems to happen consistently - I added a print "test" call to the init()
function of the simple_anim script from the matplotlib examples like this:
def init():
print "test"
line.set_ydata(np.ma.array(x, mask=True))
return line,
and got
pedsb@pedsb:~/shook_/PSM.Application.Simulation$ python simple_anim.py
test
test
Is this an intended behaviour? How can I fix the code from the tutorial so it only shows one circle? I can set the initial patch.center
to somewhere off-screen, but I don't really like that "solution".