Visualizing a 2D vector field
So far, we have been working with 2D scalar fields: functions that associate a value to each point of the 2D plane. Vector fields associate a 2D vector to each point of the 2D plane. Vector fields are common in Physics as they provide solutions to differential equations. matplotlib provides functions to visualize vector fields.
Getting ready
For this example, we will need the SymPy package; a package for symbolic computations. This package has been used only to keep the example short and is not required for working with vector fields.
How to do it...
To illustrate the visualization of vector fields, let's visualize the velocity flow of an incompressible fluid around a cylinder. We do not need to bother about how to compute such a vector field but only about how to show it. The pyplot.quiver()
function is what we need; refer to the following code:
import numpy as np import sympy from sympy.abc import x, y from matplotlib import pyplot as plt import matplotlib.patches...