Plotting Vector Fields using Matplotlib in three steps — with examples
Vector fields are important tools we use to describe phenomena in physics and science in general. Most people have seen graphics with vector fields on television, on the weather channels for example. They are used to describe directions and velocities of wind or ocean currents. In physics, for example, velocity fields describe the motions of systems of particles in the plane or in space, gravitational fields described by Newton’s Law of Gravitation describes the magnitude and direction of the gravitational field in space, electric force fields defined by Coulomb’s Law describes same for the electric field in space, and so on.
In this article, I’ll show how to plot vector fields using Matplotlib, and how to do it with Pyplot. Examples covered here are taken from one of the undergraduate Calculus courses [1]. Let’s start. I’m going to use the quiver package to plot vector fields. In Matplolib we call quiver to plot a 2D vector field in the form:
quiver ([X, Y], U, V, [C], **kw)
where X and Y define arrow location (coordinates, U and V are arrow directions, and C sets the color (optionally). More details can be found in official Matplotlib documents.
Let’s look at a simple example. First, we need to import the Matplotlib package.
The second step is to define the points where we want to calculate and plot vectors. If we have a vector field defined as:
F = xi+3yj
then we could want to visualize this vector field around origin — point (0,0), going from x-range(-5,5) and y — range (-5,5). So this would be a square of dimension 10 x 10 with a center in origin. And let us calculate vectors in 100 points inside this square.
Next, we are going to give the values for u=x and v=3y, for our example.
And now, we can plot this vector function.
The result is shown in Figure 1.
That’s it, we are done. For plots, we can add labels and titles, and how to do this can be seen in the next few examples.
Example 2. Graph vectors in vector field F = 1/8 ( 2xyi + y²j ).
In this example, F = ui+vj, with u=2xy/8 and v=y²/8. In the code below I added title and label marking vector of unit length (note: vectors lengths are autoscaled, and their magnitude on the plot is not in true ration to axis units — how to change this is shown in the last example). The label is set with line quiverkey(q, X, Y, U, label,**kw) where X and Y set the location of the label on the graph and U is the length od vector (V=0 by default in the label).
Example 3 shows how to plot a vector field in space (3 — dimensional). We use mplot3d toolkit to plot in 3d, and we need to define the mesh grid in three dimensions (x, y, z). For a given field we again need to define u,v, and w as F=ui+vj+wk. Everything else is identical as before. In quiver(…) length=0.2 is defined to scale the length of vectors to 20%. In this example, the vector field is defined as F=xi+yj+zk.
How to plot only every n-th vector in vector field using quiver. We do it adiing [::n] after x, y, u and v in quiver(…) line:
quiver(x[::n], y[::n], u[::n], v[::n]), where n=1,2,3,… For example, this is the vector field F=xi+3yj.
There are some more options allowed by quiver. You can change the color of arrows to green for example with the command:
quiver(x, y, u, v, color=’green’).
There is another way to change the color of arrows, by adding quiver(x, y, u, v, M) where matrix M contains numerical values that are going to define different arrow colors, by colormapping via norm and camp.[2] This is shown in the next example where elements of matrix M are chosen as a square root of sum x²+y² (any other way to calculate M would be equally good).
[1] Ron Larson and Bruce Edwards — Calculus 10th Edition
[2] Matplotlib quiver documentation