Matplotlib FuncAnimation for scatter plot
I am trying to use FuncAnimation
Matplotlib to animate displaying one point per animation frame.
# modules
#------------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as py
from matplotlib import animation
py.close('all') # close all previous plots
# create a random line to plot
#------------------------------------------------------------------------------
x = np.random.rand(40)
y = np.random.rand(40)
py.figure(1)
py.scatter(x, y, s=60)
py.axis([0, 1, 0, 1])
py.show()
# animation of a scatter plot using x, y from above
#------------------------------------------------------------------------------
fig = py.figure(2)
ax = py.axes(xlim=(0, 1), ylim=(0, 1))
scat = ax.scatter([], [], s=60)
def init():
scat.set_offsets([])
return scat,
def animate(i):
scat.set_offsets([x[:i], y[:i]])
return scat,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(x)+1,
interval=200, blit=False, repeat=False)
Unfortunately, the final animated plot does not match the original plot. The animated plot also flashes several dots during each frame of the animation. Any suggestions on how to properly animate a scatterplot using the package animation
?
source to share
The only problem with your example is how you fill in the new coordinates in the function animate
. set_offsets
expects a Nx2
ndarray and you provide a tuple of two 1d arrays.
So just use this:
def animate(i):
data = np.hstack((x[:i,np.newaxis], y[:i, np.newaxis]))
scat.set_offsets(data)
return scat,
And to keep the animation you want to call:
anim.save('animation.mp4')
source to share
Disclaimer, I wrote a library to try and do this easily, but with a help ArtistAnimation
called celluloid . You basically write your render code as usual and just take a photo after each frame is drawn. Here's a complete example:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
dots = 40
X, Y = np.random.rand(2, dots)
plt.xlim(X.min(), X.max())
plt.ylim(Y.min(), Y.max())
for x, y in zip(X, Y):
plt.scatter(x, y)
camera.snap()
anim = camera.animate(blit=True)
anim.save('dots.gif', writer='imagemagick')
source to share