2D Vector Field Plots with quiver(X, Y, U, V)ΒΆ
A quiver plot draws arrows on a 2D grid to represent a vector field, where each arrowβs direction and length encode the vector at that point. The Axes.quiver() method takes grid coordinates (X, Y) and vector components (U, V) and renders the field as a collection of arrows.
Why this matters for data science: Quiver plots are essential for visualizing gradient fields, optical flow in computer vision, velocity fields in fluid dynamics, and force fields in physics simulations. In ML, they are particularly useful for visualizing the gradient of a loss function across a 2D parameter slice, showing how optimization would proceed from any starting point. The angles='xy' parameter ensures arrows point in the correct direction relative to the data coordinates (rather than screen coordinates), while scale_units='xy' and scale=5 control arrow sizing relative to the axis units. The width=.015 parameter adjusts arrow shaft thickness. The vector field here (U = X + Y, V = Y - X) creates a rotational flow pattern, which is a common test case for verifying vector field visualizations.
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data
x = np.linspace(-4, 4, 6)
y = np.linspace(-4, 4, 6)
X, Y = np.meshgrid(x, y)
U = X + Y
V = Y - X
# plot
fig, ax = plt.subplots()
ax.quiver(X, Y, U, V, color="C0", angles='xy',
scale_units='xy', scale=5, width=.015)
ax.set(xlim=(-5, 5), ylim=(-5, 5))
plt.show()