Celluloid
:movie_camera: Matplotlib animations made easy
Install / Use
/learn @jwkvam/CelluloidREADME
celluloid
Easy Matplotlib Animation
<p align="center"> <a href="https://github.com/jwkvam/celluloid/blob/master/examples/sines.py"> <img src="https://user-images.githubusercontent.com/86304/48657442-9c11e080-e9e5-11e8-9f54-f46a960be7dd.gif"> </a> </p>Creating animations should be easy. This module makes it easy to adapt your existing visualization code to create an animation.
Install
pip install celluloid
Manual
Follow these steps:
- Create a matplotlib
Figureand create aCamerafrom 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.
Viewing in Jupyter Notebooks
View videos in notebooks with IPython.
from IPython.display import HTML
animation = camera.animate()
HTML(animation.to_html5_video())
Examples
Minimal
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()
<p align="center">
<a href="https://github.com/jwkvam/celluloid/blob/master/examples/simple.py">
<img src="https://user-images.githubusercontent.com/86304/48666133-66660980-ea70-11e8-9024-b167c21a5e83.gif">
</a>
</p>
Subplots
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 t:
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()
Images
Domain coloring example.
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import hsv_to_rgb
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
for a in np.linspace(0, 2 * np.pi, 30, endpoint=False):
x = np.linspace(-3, 3, 800)
X, Y = np.meshgrid(x, x)
x = X + 1j * Y
y = (x ** 2 - 2.5) * (x - 2.5 * 1j) * (x + 2.5 * 1j) \
* (x - 2 - 1j) ** 2 / ((x - np.exp(1j * a)) ** 2
* (x - np.exp(1j * 2 * a)) ** 2)
H = np.angle(y) / (2 * np.pi) + .5
r = np.log2(1. + np.abs(y))
S = (1. + np.abs(np.sin(2. * np.pi * r))) / 2.
V = (1. + np.abs(np.cos(2. * np.pi * r))) / 2.
rgb = hsv_to_rgb(np.dstack((H, S, V)))
ax.imshow(rgb)
camera.snap()
animation = camera.animate()
<p align="center">
<a href="https://github.com/jwkvam/celluloid/blob/master/examples/complex.py">
<img src="https://user-images.githubusercontent.com/86304/48747098-f483f080-ec26-11e8-9734-c409e9b0c9ec.gif">
</a>
</p>
Legends
import matplotlib
from matplotlib import pyplot as plt
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
for i in range(5):
t = plt.plot(range(i, i + 5))
plt.legend(t, [f'line {i}'])
camera.snap()
animation = camera.animate()
<p align="center">
<a href="https://github.com/jwkvam/celluloid/blob/master/examples/legends.py">
<img src="https://user-images.githubusercontent.com/86304/48750564-9100bf80-ec34-11e8-87fb-bc5c7ddcc6e7.gif">
</a>
</p>
Limitations
- The axes' limits should be the same for all plots. The limits of the animation will be the limits of the final plot.
- Legends will accumulate from previous frames. Pass the artists to the
legendfunction to draw them separately. - Animating the title does not work. As a workaround you can create a text object:
ax.text(0.5, 1.01, 'computed title', transform=ax.transAxes)
- This can demand a lot of memory since it uses
ArtistAnimationunder the hood. This means that all artists are saved to memory before the animation is constructed. - This is a black box. If you want to understand how matplotlib animations work, using this library may hinder you. If you want to be an expert matplotlib user, you may want to pass on this library.
Credits
Inspired by plotnine.
Related Skills
node-connect
342.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
85.3kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
342.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
342.5kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
