Book Image

matplotlib Plotting Cookbook

By : Alexandre Devert
Book Image

matplotlib Plotting Cookbook

By: Alexandre Devert

Overview of this book

Table of Contents (15 chapters)
matplotlib Plotting Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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): 
  u = sympy.lambdify((x, y), psi.diff(y), 'numpy') 
  v = sympy.lambdify((x, y), -psi.diff(x), 'numpy') 
  return u, v 

psi = cylinder_stream_function() 
U_func, V_func = velocity_field(psi) 

xmin, xmax, ymin, ymax = -3...