Skip to main content

Section 1.5 Basic Animation

In this section we will create some basic animations in Matplotlib. Static graphs convey a lot of information in one visual. The right animation can help you convey complicated information simply.
Matplotlib has a module called animation that uses frames of a changing graph to create gif and mp4 files. First, however, we will need to learn about for loops and user-defined functions to efficiently repeat and reuse code.
 4 
Animation is basically a slideshow of pictures or frames.

Subsection Repeating blocks of code with for loops

A for loop repeats a block of code for each item in a list or array. The code below uses a for loop to add elements to an array one at a time.
Note that we converted the Python list into a Numpy array before printing. NumPy also has an append function that effectively recreates the entire array before appending which is inefficient in a for loop.

You Try 1.33.

  • Edit the code above and see what changes if you don’t convert the list to a NumPy array.
  • Edit the code above to create an array of even numbers between 0 and 10 using a for loop.
You can change the value of a variable as you iterate through a for loop to repeatedly perform a computation.

You Try 1.34.

Edit the code below to add up all the entries in the given NumPy array.

Subsection Creating a gif in Matplotlib

We can use for loops for a simple animation by iterating through frames of a changing graph and writing each frame to a gif file using animation’s class PillowWriter.
There are many ways to alter a graph over time. For this example we will start with an empty graph and draw the curve by adding points one at a time.
You might have noticed an oddly placed comma in the above code. Essentially ax.plot returns a list/tuple with one element. The comma allows us to unpack the first element in the list and name it curve, which will be the plotted curve that gets modified in each frame.

You Try 1.35.

  • Play with various graph features and customizations.
  • Add a title and labels to your graph.

You Try 1.36.

  • Change the dpi and see how the gif is affected.
  • Change the frames per second and see how the gif is affected.
Here is an example animating two curves on the same axes object.

Subsection Reusing blocks of code with user-defined functions

A Python function accepts inputs as arguments and can output through a return statement. For example, numpy.sum() takes in an array and returns the sum of the values in that array (along an axis). When we want to reuse code in multiple places, we can create a user-defined function with def.
The user-defined function below takes in a radius and outputs the area of a circle.
The signature decides the name of the function and how many arguments the function requires. The next line documents what the function does, triple quoted so you can use multiple lines. Next is the body of code the function runs when called. Once compiled, user defined functions can be called just like any other Python function.

You Try 1.37.

  • Edit the code above to compute the area of a different circle.
  • What error do you get if you add print(area) to the above code?
Hint.
Note that variables defined inside a function are local to that function.
User-defined functions organize code into more manageable chunks. If we find ourselves repeating blocks of code, we should probably use a function instead.

Subsection Creating an mp4 in Matplotlib

In matplotlib’s animation, a class called FuncAnimation can be used to create mp4s. FuncAnimation takes a matplotlib figure, uses frames to iterate through a user-defined function that alters elements of the graph, then saves as an mp4.
The argument frames is always iteratively passed into the user-defined function with a hidden for loop and can just be an integer. Meanwhile the argument interval determines the delay between frames in milliseconds and blit chooses whether FuncAnimation recreates the entire figure every frame, or just updates the altered graph elements for efficiency.

You Try 1.38.

Edit the code above to create an mp4 that draws both a sine wave and a cosine wave.

Subsection Creating an interactive slider

We can also use interact to create a slider that plots one point at a time. We will need two user-defined functions, one that increments through the input points for the slider and another that plots the graph for just those input points.

You Try 1.39.

  • Play with various graph features and customizations.
  • Change the number of frames to see how the interactive is affected.
Hint.
Try aligning and misaligning the number of frames and the number of inputs plotted.
Here is the same interactive for projectile motion.
Matplotlib allows you to not only graph, but make your graphs interactive. You can use interact to create interactive elements. The animation module can be used to animate your graphs. Hopefully this is enough to get you started.

Summary.

  • For loops can be used to animate your graphs and PillowWriter saves those frames into a gif.
  • The FuncAnimation class and a user-defined function can be used to animate your graph and save frames to an mp4.
  • We can use interact to create a slider element and create two user defined functions: one that takes the slider information to create points and another that makes graphs for just those points.

Exercises Exercises

1.

Animate a 3D graph.

2.

Showcase your skills from so far in the term to create an animated graph in Matplotlib for any other class (and using one feature we haven’t learned in class).

3.

What graph do you want to animate? Create it!