Quiver and Stream Plots in Matplotlib

Plot vector fields in Matplotlib: arrows on a grid with ax.quiver() and continuous flow lines with ax.streamplot().

To plot a vector field in Matplotlib, give each point (x, y) a horizontal component u and a vertical component v. ax.quiver(x, y, u, v) draws an arrow at each point, and ax.streamplot(x, y, u, v) draws lines that follow the flow.

Quick Example

Two counter-rotating eddies, drawn both ways:

import matplotlib.pyplot as plt
import numpy as np

def eddies(x, y):
    u = np.zeros_like(x)
    v = np.zeros_like(y)
    for cx, spin in [(-1.4, 1), (1.4, -1)]:
        dx, dy = x - cx, y
        strength = spin * np.exp(-(dx**2 + dy**2) / 2)
        u += -dy * strength
        v += dx * strength
    return u, v

x, y = np.meshgrid(np.linspace(-3, 3, 19), np.linspace(-2, 2, 13))
u, v = eddies(x, y)

fine_x, fine_y = np.meshgrid(np.linspace(-3, 3, 120), np.linspace(-2, 2, 80))
fine_u, fine_v = eddies(fine_x, fine_y)

fig, (left, right) = plt.subplots(1, 2, figsize=(10, 3.8), layout="constrained")
left.quiver(x, y, u, v, color="#264653")
left.set_title("quiver")
right.streamplot(fine_x, fine_y, fine_u, fine_v, color="#2a9d8f", density=1.2)
right.set_title("streamplot")
for ax in (left, right):
    ax.set_aspect("equal")
plt.show()

Two rotating eddies shown as a grid of arrows on the left and as continuous looping streamlines on the right

The arrow plot shows direction and relative strength at specific points. The stream plot shows the shape of the paths, but its lines don't show speed unless you map it to color or width.

Build the Grid With meshgrid()

All four inputs to quiver() are normally 2D arrays of the same shape. np.meshgrid() turns two 1D coordinate arrays into the x and y grids, and your field formula fills u and v. If you leave out x and y, the arrows are placed at array indices.

streamplot() is stricter: the grid must be evenly spaced, with x increasing along rows and y increasing down the columns, which is what meshgrid() produces from linspace(). For measurements at scattered locations, use quiver(), or interpolate onto a regular grid first.

Color Arrows by Magnitude

A fifth array colors the arrows. Compute the speed with np.hypot():

speed = np.hypot(u, v)

fig, ax = plt.subplots(figsize=(8, 4.8), layout="constrained")
arrows = ax.quiver(x, y, u, v, speed, cmap="plasma", pivot="mid", width=0.005)
fig.colorbar(arrows, ax=ax, label="Speed (m/s)", shrink=0.85)
ax.set_aspect("equal")
plt.show()

Quiver plot with arrows colored by speed, brightest in the fast upward flow between the two eddies and dark where the flow is slow

pivot="mid" centers each arrow on its grid point. The default, "tail", starts the arrow there.

Control Arrow Length With scale

Matplotlib picks an arrow length automatically. scale sets how many data units of velocity fit in one unit of arrow length (by default, the width of the plot), so a larger scale gives shorter arrows. To draw arrows in the same units as the axes (an arrow with u=1 is 1 unit long), pass angles="xy", scale_units="xy", scale=1.

fig, ax = plt.subplots(figsize=(6, 5), layout="constrained")
vectors = {"a": (3, 1), "b": (1, 3), "a + b": (4, 4)}
for (name, (dx, dy)), color in zip(vectors.items(), ["#e63946", "#1d3557", "#7b2cbf"]):
    ax.quiver(0, 0, dx, dy, angles="xy", scale_units="xy", scale=1, color=color)
    ax.annotate(name, (dx, dy), xytext=(5, 5), textcoords="offset points", color=color)

ax.set_xlim(-0.5, 5)
ax.set_ylim(-0.5, 5)
ax.set_aspect("equal")
ax.grid(alpha=0.3)
plt.show()

Three arrows from the origin showing vectors a, b and their sum, ending at their exact coordinates

Add a Key for Arrow Length

ax.quiverkey() draws a reference arrow with a label. The position is in axes fractions:

fig, ax = plt.subplots(figsize=(8, 4.8), layout="constrained")
arrows = ax.quiver(x, y, u, v, scale=18, color="#386641")
ax.quiverkey(arrows, X=0.82, Y=0.93, U=0.5, label="0.5 m/s", labelpos="E")
ax.set_aspect("equal")
ax.set_ylim(-2.2, 2.7)  # room for the key
plt.show()

Quiver plot with a reference arrow in the top right labeled 0.5 meters per second

Color and Widen Streamlines by Speed

color and linewidth accept arrays the same shape as the grid:

fine_speed = np.hypot(fine_u, fine_v)

fig, ax = plt.subplots(figsize=(8, 4.8), layout="constrained")
stream = ax.streamplot(
    fine_x, fine_y, fine_u, fine_v, density=1.4,
    color=fine_speed, cmap="viridis",
    linewidth=0.5 + 3 * fine_speed / fine_speed.max())
fig.colorbar(stream.lines, ax=ax, label="Speed (m/s)", shrink=0.85)
ax.set_aspect("equal")
plt.show()

Streamplot with thick yellow lines where the eddies flow fastest and thin dark lines where flow is slow

For fig.colorbar(), pass stream.lines, not the whole return value.

Flow Around an Obstacle

Set points inside a solid shape to np.nan and the streamlines go around it. start_points chooses where lines begin, and broken_streamlines=False (Matplotlib 3.6+) keeps them from stopping when they get close together:

gx, gy = np.meshgrid(np.linspace(-3, 3, 200), np.linspace(-2, 2, 140))
r2 = gx**2 + gy**2
flow_u = 1 - (gx**2 - gy**2) / r2**2
flow_v = -2 * gx * gy / r2**2
flow_u[r2 < 1] = np.nan
flow_v[r2 < 1] = np.nan

starts = np.column_stack([np.full(17, -2.95), np.linspace(-1.9, 1.9, 17)])

fig, ax = plt.subplots(figsize=(8, 4.8), layout="constrained")
ax.streamplot(gx, gy, flow_u, flow_v, start_points=starts, color="#0077b6",
              broken_streamlines=False, integration_direction="forward")
ax.add_patch(plt.Circle((0, 0), 1, color="#6c757d"))
ax.set_aspect("equal")
ax.set_xlim(-3, 3)
ax.set_ylim(-2, 2)
plt.show()

Evenly started streamlines flowing left to right and bending around a gray circle

quiver or streamplot?

quiver() streamplot()
Shows Direction and magnitude at points Paths through the field
Input grid Any points Evenly spaced grid
Magnitude Arrow length and color Color and line width only
Good for Measurements, a few vectors, wind maps Circulation, flow patterns

Similar Topics