Consider this code taken from the matplotlib website which animates a sine curve moving to the right.
"""
A simple example of an animated plot
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))
def animate(i):
line.set_ydata(np.sin(x + i/10.0)) # update the data
return line,
# Init only required for blitting to give a clean slate.
def init():
line.set_ydata(np.ma.array(x, mask=True))
return line,
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init, interval=25, blit=True)
# Commenting out the previous line, and uncommenting the following line gives a static image.
#animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init, interval=25, blit=True)
plt.show()
This code is identical to the one I linked on Matplotlib, except for the
two commented lines before plt.show()
. Notice that after binding the output of animation.FuncAnimation()
to the name ani
, this name is not used anywhere in the arguments to plt.show()
!
So thinking that this binding was redundant, I deleted the ani=
part.
The result was that I got a static image. As long as the output of animation.FuncAnimation
is bound to something the animation works as expected. Otherwise not.
what is happening here?
I am using Python 2.7.11 (Anaconda distribution) on Ubuntu 14.04.