celluloid
Easy Matplotlib Animation
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.
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()
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()
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()
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.
Credits
Inspired by plotnine.
Metadata
Release files for celluloid 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| celluloid-0.2.0.tar.gz | 6.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| celluloid-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.8 kB
Release files / celluloid-0.2.0.tar.gz
| Download URL | celluloid-0.2.0.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
568b1512c4a97483759e9436c3f3e5dc5566da350179aa1872992ec8d82706e1
|
|
BLAKE2b-256 checksum How to use checksums |
0d3180e96c4b221de342eef55f1f07de84b11b5f7cfb8c3b00e235a0bdd0b476
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.20.0
|
Release files / celluloid-0.2.0-py3-none-any.whl
| Download URL | celluloid-0.2.0-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5cebd41276d9658f7667a2f1f565f566763a6110273b4af35c958b1407a9f277
|
|
BLAKE2b-256 checksum How to use checksums |
60a77fbe80721c6f1b7370c4e50c77abe31b4d5cfeb58873d4d32f48ae5a0bae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.20.0
|