“Celluloid, Easy Matplotlib Animations”
I really enjoy working with data visualization and I always wonder what’s the best way to provide more direct and intuitive visual interactions when I have to explain some result or complex model.
Lately, I’ve been growing to use GIFs and quick videos. Even if this makes the coding part harder and more complex, the result generally is much more efficient in communicating my findings and process.
But in Python, there’s always an easier and simpler way and to simplify the animating process, Celluloid was born.
Using only 50 lines of code to deal with Matplotlib Artists and ArtistAnimations Celluloid creates an animation from the series of images you want to plot into the Camera abstraction.
Let’s start by installing the library with
$ pip install celluloid
Now let’s get to it!
Let’s create a simple plot just to demonstrate the basic usage of how to run the code in a Jupyter notebook, but we could also use the method save(‘filename.gif_or_mp4’)
from celluloid import Camera ## getting the camera
import matplotlib.pyplot as plt
import numpy as np
from IPython.display import HTML ## to show the animation in Jupyter
fig, ax = plt.subplots() ## creating my fig
camera = Camera(fig)## the camera gets the fig we'll plot
for i in range(10):
ax.plot([i] * 5, c='black') ## 5 element array from 0 to 9
camera.snap() ## the camera takes a snapshot of the plot
animation = camera.animate() ## animation ready
HTML(animation.to_html5_video()) ## displaying the animation
#data-science #jupyter-notebook #tutorial #data-visualization #python