Animations with matplotlib 1: Making waves
January 17, 2021
This is the first part of a series on how to use the matplotlib.animation
module to create animated plots. The examples here show how to visualize traveling waves and the phenomenon of superposition.
Introduction #
matplotlib
1 is a python library for creating high quality scientific plots. It contains a module matplotlib.animation
that can be used to create animations. This post shows examples of using this module to visualize wave motion.
Background #
This is a short technical background on wave motion that explains the ideas behind the code examples below
Wave equation #
Mathematically, wave motion is described by a partial differential equation called the wave equation. A 1-D version of the wave equation has the following form: $$ \frac{\partial^2 u}{\partial t^2} = c^2 \frac{\partial^2 u}{\partial x^2} $$
where $u(x, t)$ is the solution for some given initial conditions - usually the initial position $u(x, 0)$ and initial velocity $\partial u / \partial t (x, 0)$ of the system.

Initial position of plucked string
Traveling waves #
One way to solve the wave equation is by using traveling waves. A traveling wave is a function of the form: $$ u(x, t) = f(x \pm ct) $$ At time $t = 0$, the function $f(x)$ is a shape in space. Replacing $x$ by $x-ct$ transposes this shape to the right by a distance $ct$. Therefore, $f(x - ct)$ is the shape $f(x)$ traveling to the right at the speed $c$. The first set of animations simulate traveling waves.
Superposition #
A traveling wave is a general solution of the wave equation. The wave equation is linear, which means any linear combination of solutions is also a solution. More strongly, it can be shown that all solutions to the wave equation are of the form: $$u(x, t) = F(x + ct) + G(x - ct)$$ that is, a superposition of two traveling waves.
The specific solution for the plucked string problem is trivially simple. Given the initial shape, $f(x)$, and assuming the string is stationary at time 0, the subsequent motion of the string is2: $$u(x, t) = \frac{1}{2} \left[ f(x + ct) + f(x - ct) \right] $$ This is known as the d’Alembert solution to the wave equation.
The second set of animations visualize this solution for the plucked string problem.
Examples #
The examples below show how to use matplotlib.animation
to create animations of traveling waves. These examples follow a common template:
- Create a plot with the initial shape $y = f(x) = u(x, 0)$
- Update the shape by applying the transform $f(x) \to f(x - ct)$
The function FuncAnimation()
creates the animation by repeatedly calling the update function.
First load the needed libraries:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.animation as anim
from IPython.display import HTML
from pathlib import Path
Example 1: Traveling waves #
The first example produces a traveling sine wave:
|
|
This produces the following animation:
The function $f()$ can have any arbitrary shape. For example, this next animation code block produces a traveling wave packet whose shape is defined by the first highlighted line. Also note how changing the velocity to a negative value (see second highlighted line) changes the direction of traveling wave.
|
|
Example 2: Superposition #
The next set of examples visuailzes the d’Alembert solution to the wave equation for a plucked string that is anchored at $x = 0$ and $x = \pi$, and at time $t = 0$ is in the inverted-V configuration. The initial shape $f(x)$ is defined over $x \in [0, \pi]$. To construct the traveling wave solution, first extend this over entire real line by constructing an odd periodic extension
def y(x):
"""Odd periodic extension of plucked string"""
pi = np.pi
x = x % (2 * pi)
if (x < pi/2):
h = x / (pi/2)
elif (x >= pi/2) and (x < 3 * pi/2):
h = 1 - (x - pi/2)/(pi/2)
else:
h = (x - 2*pi)/(pi/2)
return(h)
f = np.vectorize(y)
The extended function $f(x)$ looks like this:
Next, use this periodic extension to construct two traveling waves. The superposition of these traveling waves produces the dynamics of the plucked string as shown in this animation:
This animation is the output of the following code block:
|
|
Example 3: Varying initial shape #
The final examples use the traveling wave solution to simulate a vibrating string with different initial shapes. The shapes are created using interpolation. The next animation shows the behavior for an asymmetric shape:
This is generated using the following code block:
|
|
The last example uses a higher order interpolation to produce a smoother initial shape that looks more natural.
This is the output of the following code block
|
|
Summary and References #
These examples demonstrate how the matplotlib.animation
module can be used to create dynamic visualizations. The complete code for this post is available in this jupyter notebook
References:
- https://matplotlib.org/3.3.3/api/animation_api.html
- https://towardsdatascience.com/animations-with-matplotlib-d96375c5442c
- https://brushingupscience.com/2016/06/21/matplotlib-animations-the-easy-way/
- https://brushingupscience.com/2019/08/01/elaborate-matplotlib-animations/
- Chapter 1 of Fourier Analysis - An Introduction by Stein & Shakarchi
- Partial Differential Equations for Scientists and Engineers by Farlow
- Download jupyter notebook with code
-
If the initial velocity is not 0, then the full solution is: $$u(x, t) = \frac{1}{2} \left[ f(x + ct) + f(x - ct) \right] + \frac{1}{2c} \int_{x - ct}^{x + ct} g(y) dy $$ ↩︎