Easy Matplotlib Animation
Creating animations should be easy. This module makes it easy to adapt your existing visualization code to create an animation.
pip install celluloid
Follow these steps:
- Create a matplotlib
Figure
and create aCamera
from it:
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
- Reusing the figure and after each frame is created, take a snapshot with the camera.
plt.plot(...)
plt.fancy_stuff()
camera.snap()
- After all frames have been captured, create the animation.
animation = camera.animate()
animation.save('animation.mp4')
The entire module is less than 50 lines of code.
As simple as it gets.
from matplotlib import pyplot as plt
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
for i in range(10):
plt.plot([i] * 10)
camera.snap()
animation = camera.animate()
Animation at the top.
import numpy as np
from matplotlib import pyplot as plt
from celluloid import Camera
fig, axes = plt.subplots(2)
camera = Camera(fig)
t = np.linspace(0, 2 * np.pi, 128, endpoint=False)
for i in np.linspace(0, 2 * np.pi, 128, endpoint=False):
axes[0].plot(t, np.sin(t + i), color='blue')
axes[1].plot(t, np.sin(t - i), color='blue')
camera.snap()
animation = camera.animate()
- The axes' limits should be the same for all plots. The limits of the animation will be the limits of the final plot.
Inspired by plotnine.