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
Selected Reading
How to make a quiver plot in polar coordinates using Matplotlib?
A quiver plot in polar coordinates displays vector fields using arrows positioned at polar coordinates (radius, angle). Matplotlib's quiver() function with polar projection creates these directional arrow plots.
Basic Polar Quiver Plot
First, let's create a simple quiver plot with vectors radiating outward ?
import numpy as np
import matplotlib.pyplot as plt
# Create polar coordinate grid
radii = np.linspace(0.2, 1, 4)
thetas = np.linspace(0, 2 * np.pi, 12)
theta, r = np.meshgrid(thetas, radii)
# Create figure with polar projection
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'), figsize=(8, 6))
# Define vector components (radial and tangential)
dr = np.ones_like(theta) * 0.1 # radial component
dtheta = np.zeros_like(theta) # tangential component
# Create quiver plot
ax.quiver(theta, r, dr, dtheta, color='blue', alpha=0.7)
ax.set_title('Basic Polar Quiver Plot')
plt.show()
Complex Vector Field Example
Here's a more complex example showing rotating vectors ?
import numpy as np
import matplotlib.pyplot as plt
# Set up polar grid
radii = np.linspace(0.1, 1.0, 5)
thetas = np.linspace(0, 2 * np.pi, 16)
theta, r = np.meshgrid(thetas, radii)
# Create figure with polar projection
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'), figsize=(8, 8))
# Define vector field components
dr = np.cos(2 * theta) * r # radial component varies with angle
dtheta = np.sin(theta) * (1 - r) # tangential component
# Create quiver plot with color mapping
quiv = ax.quiver(theta, r, dr, dtheta,
np.sqrt(dr**2 + dtheta**2), # color by magnitude
cmap='viridis', alpha=0.8)
ax.set_title('Complex Polar Vector Field')
plt.colorbar(quiv, ax=ax, shrink=0.5, label='Vector Magnitude')
plt.show()
Customizing Polar Quiver Plots
You can customize arrow properties and add grid lines ?
import numpy as np
import matplotlib.pyplot as plt
# Create coordinate grid
radii = np.linspace(0.2, 0.8, 3)
thetas = np.linspace(0, 2 * np.pi, 8, endpoint=False)
theta, r = np.meshgrid(thetas, radii)
# Create figure
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'), figsize=(8, 8))
# Vector components for spiral pattern
dr = 0.1 * np.ones_like(theta)
dtheta = r * 0.5
# Customized quiver plot
ax.quiver(theta, r, dr, dtheta,
color='red', width=0.004, scale=2,
headwidth=3, headlength=4)
# Customize polar plot
ax.set_ylim(0, 1)
ax.set_theta_direction(-1) # clockwise
ax.set_theta_zero_location('N') # 0° at top
ax.grid(True, alpha=0.3)
ax.set_title('Customized Polar Quiver Plot', pad=20)
plt.show()
Key Parameters
| Parameter | Description | Example Values |
|---|---|---|
theta, r |
Position coordinates | Meshgrid arrays |
dr, dtheta |
Vector components | Radial and angular directions |
scale |
Arrow size scaling | 1, 2, 5 |
width |
Arrow shaft width | 0.002, 0.004, 0.006 |
Conclusion
Polar quiver plots effectively visualize vector fields in circular coordinate systems. Use subplot_kw=dict(projection='polar') to create the polar axes and customize arrow properties for better visualization.
Advertisements
