Vector Fields (quiver & streamplot)
A vector field plot draws an arrow at each point in a grid to show direction and strength, letting you visualize forces, winds, currents, or any quantity that has both a magnitude and a heading.
Learn Vector Fields (quiver & streamplot) in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise…
Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll build arrow grids with ax.quiver() , trace smooth flow lines with ax.streamplot() , control arrow scale and line density, and color both by magnitude.
A quiver plot needs four ingredients: the X and Y positions of each arrow and the U and V components of the vector at that position. U is how far the arrow points along x, V is how far along y. You almost always build the positions with np.meshgrid() , which turns two 1-D ranges into a full 2-D grid of coordinates.
What you'll see: a 6×6 grid of blue arrows that curl counter-clockwise around the center — short near the middle and longer toward the edges — because the field U = −Y, V = X gets stronger the farther you move from the origin.
By default Matplotlib auto-scales arrow length to fit the axes. Pass scale to take control: a larger scale makes arrows shorter, a smaller scale makes them longer. To color arrows by strength, compute the magnitude with np.sqrt(U**2 + V**2) and pass it as the fifth argument — Matplotlib runs it through a colormap.
Where quiver shows discrete arrows, ax.streamplot() traces the continuous paths a particle would follow through the field. It takes the same X, Y, U, V data but draws smooth curves with little arrowheads. Use density to pack in more or fewer lines, and pass an array to color to tint the streamlines by magnitude.
What you'll see: smooth concentric loops swirling around the center, each carrying a tiny arrowhead, tinted from cool blue near the calm middle to brighter colors where the flow speeds up at the edges.
Replace each ___ to draw a quiver field of a uniform wind blowing up and to the right.
Your scale is too high, so arrows are shrunk to nothing. ✅ Lower the scale, or drop the argument entirely and let Matplotlib auto-scale.
❌ streamplot: "The rows of 'x' must be equal"
streamplot needs an evenly spaced, strictly increasing grid. ✅ Build X and Y with np.linspace + np.meshgrid , not hand-typed lists.
U and V must have the same 2-D shape as X and Y. ✅ Derive U and V from X and Y (e.g. U = -Y ) so the shapes always line up.
Overlay a coarse quiver field on top of fine streamlines of the same vortex, so you see both the individual arrows and the overall flow.
Lesson complete — you can visualize vector fields!
You built coordinate grids with meshgrid, drew arrow fields with quiver, scaled them and colored them by magnitude, and traced smooth flow lines with streamplot.
🚀 Up next: Customizing Spines — hide, move, and restyle the axis borders for a clean, minimalist look.
Practice quiz
How many arrays does ax.quiver() need for X, Y positions and U, V components?
- Two
- Three
- Four
- Five
Answer: Four. quiver takes X, Y, U, V — four arrays for positions and vector components.
What does np.meshgrid() build for a quiver plot?
- A 2D grid of coordinates
- A list of colors
- A single 1D range
- A colormap
Answer: A 2D grid of coordinates. np.meshgrid turns two 1D ranges into a full 2D grid of X, Y coordinates.
In ax.quiver, what do U and V represent?
- The grid spacing
- The arrow color and size
- The figure width and height
- The x and y components of each vector
Answer: The x and y components of each vector. U is the component along x and V is the component along y at each point.
How does the scale argument affect arrow length?
- Larger scale makes arrows shorter
- Larger scale makes arrows longer
- Scale only changes color
- Scale has no effect on length
Answer: Larger scale makes arrows shorter. scale is inverted: a larger scale shrinks arrows, a smaller scale lengthens them.
How do you compute the magnitude of vectors U and V?
- np.abs(U + V)
- np.sqrt(U**2 + V**2)
- U * V
- np.max(U, V)
Answer: np.sqrt(U**2 + V**2). The magnitude is np.sqrt(U**2 + V**2) at each grid point.
Which method traces continuous flow lines through the field?
- ax.flowline()
- ax.contour()
- ax.quiver()
- ax.streamplot()
Answer: ax.streamplot(). ax.streamplot() draws smooth curves following the vector field.
What does the density argument control in streamplot?
- The arrow head size
- The line color
- How many streamlines are drawn
- The grid spacing
Answer: How many streamlines are drawn. density sets how tightly the streamlines are packed; 1 is the default.
To color quiver arrows by magnitude M, where do you pass M?
- As a keyword color=M only
- As the fifth positional argument ax.quiver(X, Y, U, V, M)
- As the first argument
- It cannot be done
Answer: As the fifth positional argument ax.quiver(X, Y, U, V, M). Pass M as the fifth positional argument; Matplotlib maps it through a colormap.
What kind of grid does streamplot require?
- A random scatter of points
- Any hand-typed list of points
- Only integer coordinates
- An evenly spaced, strictly increasing grid
Answer: An evenly spaced, strictly increasing grid. streamplot needs an evenly spaced, strictly increasing grid from linspace + meshgrid.
Which argument picks the colormap for magnitude-colored arrows?
- palette=
- colors=
- cmap=
- shade=
Answer: cmap=. cmap= selects the colormap, e.g. cmap='viridis'.