Matplotlib Span Lines and Vector Field Functions


Matplotlib 参考文档Matplotlib Reference Documentation

Span line functions are used to add reference lines and shaded bands that span the entire Axes in a chart; vector field functions are used to draw arrow plots and streamline plots.

Function Overview

FunctionDescription
axhline()Add a horizontal line spanning the Axes
axhspan()Add a horizontal shaded band spanning the Axes
axvline()Add a vertical line spanning the Axes
axvspan()Add a vertical shaded band spanning the Axes
axline()Add an infinite line passing through two points
quiver()Draw a vector field (arrow plot)
quiverkey()Add a scale legend to a vector field
barbs()Draw a wind barb plot (for meteorology)
streamplot()Draw a streamline plot

Span Functions

axhline() / axvline()

matplotlib.pyplot.axhline(y=0, xmin=0, xmax=1, **kwargs)
matplotlib.pyplot.axvline(x=0, ymin=0, ymax=1, **kwargs)

axhspan() / axvspan()

matplotlib.pyplot.axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs)
matplotlib.pyplot.axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs)

axline()

matplotlib.pyplot.axline(xy1, xy2=None, *, slope=None, **kwargs)

Examples

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5),
                                layout='constrained')

# Left plot: span curves
ax1.plot(x, y, 'k-', alpha=0.7)

ax1.axhline(y=0, color='gray', linewidth=0.8)       # y=0 horizontal line
ax1.axhline(y=0.5, color='green', linestyle='--')    # y=0.5
ax1.axvline(x=np.pi, color='red', linestyle='--')    # x=π
ax1.axvspan(np.pi, 2*np.pi, alpha=0.15,             # Shaded band
            color='red', label='Phase 2')
ax1.axhspan(0.5, 1.0, xmin=0.6, xmax=1.0,             # Local shaded band
            alpha=0.15, color='blue')

ax1.set_title('Span Functions')
ax1.legend()

# Right plot: axline defines an infinite line through two points
ax2.plot([0, 2, 4], [0, 2, 3], 'ro', label='Data points')
ax2.axline((0, 0), (2, 2), color='blue', linestyle='--',
           label='Through (0,0) and (2,2)')
ax2.axline((0, 0), slope=1.5, color='green',
           linestyle=':', label='From (0,0) with slope=1.5')
ax2.set_xlim(-1, 5)
ax2.set_ylim(-1, 5)
ax2.set_title('axline()')
ax2.legend()
ax2.grid(True, alpha=0.3)

plt.show()

Vector Field Functions

quiver() - Vector Field

matplotlib.pyplot.quiver(*args, **kwargs)
# X, Y, U, V: 网格坐标和矢量分量
# C: 可选的颜色数据

streamplot() - Streamline Plot

matplotlib.pyplot.streamplot(x, y, u, v, density=1,
    linewidth=None, color=None, cmap=None, **kwargs)

barbs() - Wind Barb Plot

matplotlib.pyplot.barbs(*args, **kwargs)
# X, Y, U, V: 网格坐标和风分量

Examples

import matplotlib.pyplot as plt
import numpy as np

# Create vector field data
x = np.linspace(-3, 3, 15)
y = np.linspace(-3, 3, 15)
X, Y = np.meshgrid(x, y)

# Vortex field
U = -Y
V = X
# Velocity magnitude
speed = np.sqrt(U**2 + V**2)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5),
                                layout='constrained')

# Left plot: quiver arrow plot
q = ax1.quiver(X, Y, U, V, speed, cmap='viridis',
               scale=50, width=0.005)
fig.colorbar(q, ax=ax1, label='Speed')
ax1.set_title('quiver() - Arrow Field')
ax1.set_aspect('equal')

# Right plot: streamplot streamline plot
strm = ax2.streamplot(x, y, U, V, color=speed,
                      cmap='plasma', density=1.5, linewidth=1.5)
fig.colorbar(strm.lines, ax=ax2, label='Speed')
ax2.set_title('streamplot() - Streamlines')
ax2.set_aspect('equal')

plt.show()

barbs() Example

Examples

import matplotlib.pyplot as plt
import numpy as np

# Simulated wind field data
x = np.linspace(0, 10, 10)
y = np.linspace(0, 8, 8)
X, Y = np.meshgrid(x, y)

# Wind components (uniform eastward wind + random perturbation)
U = 10 + np.random.randn(*X.shape) * 3
V = np.random.randn(*X.shape) * 5

fig, ax = plt.subplots(figsize=(8, 5), layout='constrained')

# Wind barb plot: barbs represent wind speed, direction indicates wind direction
ax.barbs(X, Y, U, V, length=6)

ax.set_title('barbs() - Wind Barbs (Meteorological)')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_xlim(-1, 11)
ax.set_ylim(-1, 9)
plt.show()

Matplotlib 参考文档Matplotlib Reference Documentation

Other Extensions