Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Making Vector Field Plots with Python
Making a 2-D vector field (or “quiver”) plot is somewhat similar to making a contour plot because the vectors must be calculated on a grid. This type of plot is most useful when the vectors do not have a third component. The first step is to find the Cartesian components of the field to be plotted. The example program will plot the magnetic field of a long wire along z axis carrying a current of I = 50 A in the +z direction. In cylindrical coordinates, the magnetic field is
where s is the distance from the wire and . If distances are in centimeters, the magnetic field is in mT.
From the diagram above, the Cartesian components are
where .
The meshgrid command is used to make the two grids where X and Y which contain the x and y coordinates for each point. Two additional grids (called Bx and By in the example program) are filled with the values of the x and y components of the magnetic field.
The quiver command from the pylab library makes the 2-D vector field plot, which is assigned the name QP in the example program below.
The quiverkey command adds a “key” which shows the scale for the lengths of the vectors in the plot. The first argument (QP) is the name of the plot. The second and third arguments give the position of the key in the horizontal and vertical directions from the lower, right corner as fractions of the size of the plot. The “1.02” places the key above the plot, which makes it easier to see. The fourth and fifth arguments are the length of the vector and its text label. The final argument is for the placement of the label (N = above, S = below, W = left, and E = right).
The axis command is used to set the left, right, bottom, and top limits (in that order) of the axes. It helps to extend the axes beyond the limits of the grid to make room for the vectors near the edges.