None Notebook

This notebook contains material from CBE30338; content is available on Github.

< A.2 Modular Simulation using Python Generators | Contents | Tag Index | B.0 Student Projects >

Open in Colab

Download

A.3 Animation in Jupyter Notebooks

by Jeffrey Kantor (jeff at nd.edu). The latest version of this notebook is available at https://github.com/jckantor/CBE30338.

A.3.1 Summary

Animation features are built into the Python matplotlib library and available through Jupyter notebooks. Unfortunately, however, the documentation is not particularly robust. This short notebook provides simple demonstrations of using animiation for the visualization of simulation results.

The resulting animations are html video files embedded within the Jupyter notebook. The videos can be viewed in any modern web browser. Since they don't require an active Python kernal, the animations can be seen when the even when notebook is viewed as a static HTML web page.

The techniques used below come from blog postings Animating the Lorenz System in 3D and Embedding Matplotlib Animations in Jupyter Notebooks.

A.3.2 Prerequisites

You should be able to view the animations with any modern web browser. However, to create animiations using the following cells you will need to install a video codec. By default matplotlib is configured to use ffmpeg. You find instructions on installing ffmpeg here.

A.3.3 Step-by-Step Approach to Animation with Matplotlib

A.3.3.1 Step 1. Create the background frame.

The following cell creates a background frame for the animation. These are standard matplotlib graphics commands, but where we have kept a reference to the resulting objects. The objects include fig for the figure object, ax1 and ax2 for each of the two plotting axes, and then txt_title, line1, line2, pt1, and line3 for graphical elements that will be updated in each frame of the animation.

A.3.3.2 Step 2. Define a function to draw each frame

The animiation function is a function you define to draw individual frames in the animation. The variable n will be the frame number. The function draws the frame by resetting the data values for the global objects txt_title, line1, line2, pt1, and line3 that were defined above.

A.3.3.3 Step 3. Create the Animation Object

The animation class includes a function FuncAnimation that incorporations a user-specified function to update the figure for each frame of the animation. The result is an animation object which is subsequently called to actually produce the desired animation.

A.3.3.4 Step 4. Render the Animation

The final step is to actually render and display the desired animation. This is the compute-intensive step in the procedure. The next cell imports HTML which is used to display a HTML elements created in a python script. The animation is rendered to html5 video with the to_html5_video() function and then displayed with HTML().

An alternative to the use of HTML is to set the default rendering of an animation object.

Once the default rendering of the animation object is set to html5, all you have to do is ask to display the animation object. Everything is then done behind the scenes to render and display the desired animation.

A.3.4 Example: Phase Plane Animation for an Exothermic Stirred-Tank Reactor

This is more complex example that demonstrates the complex dynamics of an exothermic continuous stirred-tank reactor when operated with an unstable steady state. The computational strategy is to compute solutions to the dynamical model for a set of initial conditions, then step through the solutions simulataneously to draw frames in the animation.

< A.2 Modular Simulation using Python Generators | Contents | Tag Index | B.0 Student Projects >

Open in Colab

Download