Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How does Python's Matplotlib.pyplot.quiver exactly work?
Matplotlib's pyplot.quiver() creates 2D field plots by drawing arrows to represent vectors at specific coordinates. Each arrow shows both direction and magnitude of vector data.
Syntax
plt.quiver(X, Y, U, V, **kwargs)
Parameters
X, Y − Arrow positions (starting points)
U, V − Arrow directions and magnitudes (vector components)
angles − How to interpret U and V ('xy' uses coordinate system)
scale_units, scale − Controls arrow sizing
color − Arrow colors (single color or array)
Basic Example
Let's create a simple quiver plot with three vectors ?
import numpy as np
import matplotlib.pyplot as plt
# Vector data: [x_start, y_start, x_direction, y_direction]
vectors = np.array([[0, 0, 3, 2],
[0, 0, 4, 5],
[0, 0, 9, 9]])
# Separate coordinates and directions
X, Y, U, V = zip(*vectors)
plt.figure(figsize=(8, 6))
ax = plt.gca()
# Create quiver plot
ax.quiver(X, Y, U, V,
angles='xy',
scale_units='xy',
scale=1,
color=['red', 'green', 'blue'])
# Set axis limits
ax.set_xlim([-1, 10])
ax.set_ylim([-1, 10])
ax.grid(True, alpha=0.3)
ax.set_title('Basic Quiver Plot')
plt.show()
How It Works
Each arrow starts at position (X, Y) and extends by vector components (U, V). The final arrow tip reaches (X+U, Y+V).
Wind Field Example
Quiver plots are commonly used to visualize wind or velocity fields ?
import numpy as np
import matplotlib.pyplot as plt
# Create a grid of positions
x = np.arange(0, 5, 1)
y = np.arange(0, 4, 1)
X, Y = np.meshgrid(x, y)
# Define wind components (circular pattern)
U = -Y + 2
V = X - 2
plt.figure(figsize=(8, 6))
plt.quiver(X, Y, U, V,
angles='xy',
scale_units='xy',
scale=1,
alpha=0.7)
plt.title('Wind Field Visualization')
plt.xlabel('X Position')
plt.ylabel('Y Position')
plt.grid(True, alpha=0.3)
plt.show()
Key Parameters Comparison
| Parameter | Purpose | Common Values |
|---|---|---|
angles='xy' |
Use coordinate system angles | 'xy', 'uv' |
scale_units='xy' |
Scaling reference | 'xy', 'inches', 'dots' |
scale=1 |
Arrow length scaling | 1 (no scaling), 0.5 (half size) |
Conclusion
Use quiver() to visualize vector fields like wind patterns, gradients, or flow data. The key is understanding that arrows start at (X,Y) and point in direction (U,V).
