Visualizing the streamlines of a 2D vector field

Using arrows to represent a vector field works fairly well. But matplotlib can do better than this—it can show the streamlines of a vector field. A streamline shows how the vector field flows. In this recipe, we will show you how to create streamlines.

How to do it...

Let's use the fluid flow example of the previous recipe. We will simply replace the arrows with streamlines, as shown in the following code:

import numpy as np import sympy from sympy.abc import x, y from matplotlib import pyplot as plt import matplotlib.patches as patches def cylinder_stream_function(U = 1, R = 1): r = sympy.sqrt(x ** 2 + y ** 2) theta = sympy.atan2(y, x) return U * (r - R ** 2 / r) * sympy.sin(theta) def velocity_field(psi): ...

Get matplotlib Plotting Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.