๐ฏ Specialty Plots: Polar and Vector Plots
The Compass and Wind Story
Imagine youโre a ship captain. You have two special tools:
- A compass that shows directions in a circle (like a pizza!)
- A wind map that shows arrows pointing where the wind blows
Matplotlib gives us these same superpowers for data! Letโs discover them together.
๐งญ Polar Plot Basics
What is a Polar Plot?
Think of a circular dartboard. Instead of going left-right (X) and up-down (Y), we use:
- Angle (theta/ฮธ): Which direction to point (like clock hands)
- Distance ยฎ: How far from the center
import matplotlib.pyplot as plt
import numpy as np
# Create angles (0 to 360 degrees)
theta = np.linspace(0, 2*np.pi, 100)
# Distance from center
r = np.cos(4 * theta)
# Make a polar plot
fig, ax = plt.subplots(
subplot_kw={'projection': 'polar'}
)
ax.plot(theta, r)
plt.show()
The Magic Key: projection='polar'
This tiny phrase transforms a regular plot into a circular one!
graph TD A["Regular Plot"] -->|Add projection='polar'| B["Circular Plot!"] B --> C["Angle = Direction"] B --> D["Distance = From Center"]
Simple Example: Drawing a Circle
theta = np.linspace(0, 2*np.pi, 50)
r = np.ones(50) # All same distance
fig, ax = plt.subplots(
subplot_kw={'projection': 'polar'}
)
ax.plot(theta, r)
plt.show()
Result: A perfect circle! Why? Same distance (r=1) at every angle.
๐ Polar Bar Chart
What is it?
Imagine pizza slices of different sizes arranged in a circle. Each slice shows a value!
Real-life uses:
- Wind rose (weather)
- Daily schedule
- Compass directions
Basic Code
import matplotlib.pyplot as plt
import numpy as np
# 8 directions (like a compass)
N = 8
theta = np.linspace(
0, 2*np.pi, N, endpoint=False
)
# Values for each direction
values = [3, 4, 2, 5, 4, 3, 2, 4]
# Bar width
width = 2*np.pi / N
fig, ax = plt.subplots(
subplot_kw={'projection': 'polar'}
)
ax.bar(theta, values, width=width)
plt.show()
Understanding the Parts
| Part | What it does |
|---|---|
theta |
Where each bar sits (angle) |
values |
How tall each bar is |
width |
How wide each bar is |
Make it Colorful!
colors = plt.cm.viridis(
np.linspace(0, 1, N)
)
ax.bar(
theta, values,
width=width, color=colors
)
graph TD A["Polar Bar Chart"] --> B["theta = Bar Position"] A --> C["height = Bar Size"] A --> D["width = Bar Thickness"] A --> E["color = Make it Pretty!"]
๐น Quiver Plot
What is a Quiver?
A quiver holds arrows. A quiver plot shows many arrows on a grid!
Think of it like: Tiny flags showing wind direction at different spots.
The Arrow Recipe
Each arrow needs:
- X, Y: Where to put it
- U, V: Which way it points and how long
import matplotlib.pyplot as plt
import numpy as np
# Grid of positions
X, Y = np.meshgrid(
np.arange(0, 5, 1),
np.arange(0, 5, 1)
)
# Arrow directions
U = np.ones_like(X) # Right
V = np.ones_like(Y) # Up
plt.quiver(X, Y, U, V)
plt.title('Arrows pointing up-right')
plt.show()
Understanding U and V
| U value | V value | Arrow points |
|---|---|---|
| 1 | 0 | Right โ |
| 0 | 1 | Up โ |
| 1 | 1 | Up-Right โ |
| -1 | 0 | Left โ |
| 0 | -1 | Down โ |
Make Arrows Change Based on Position
X, Y = np.meshgrid(
np.linspace(-2, 2, 10),
np.linspace(-2, 2, 10)
)
# Arrows point AWAY from center
U = X
V = Y
plt.quiver(X, Y, U, V)
plt.title('Exploding outward!')
plt.show()
Magic: When U=X and V=Y, arrows point away from (0,0)!
graph TD A["Quiver Plot"] --> B["X,Y = Arrow Positions"] A --> C["U,V = Arrow Directions"] B --> D["meshgrid creates grid"] C --> E["Bigger U,V = Longer arrows"]
๐ Stream Plot
What is a Stream Plot?
Instead of separate arrows, stream lines flow like rivers!
Think of it like: Dropping leaves in water and watching where they float.
Basic Stream Plot
import matplotlib.pyplot as plt
import numpy as np
# Create grid
X, Y = np.meshgrid(
np.linspace(-3, 3, 30),
np.linspace(-3, 3, 30)
)
# Flow direction
U = -Y # Left when above center
V = X # Up when right of center
plt.streamplot(X, Y, U, V)
plt.title('Circular Flow!')
plt.show()
What Makes Streams Special?
| Feature | Quiver | Stream |
|---|---|---|
| Shows | Individual arrows | Connected flow lines |
| Best for | Exact directions | Overall flow pattern |
| Looks like | Arrows on map | Rivers on map |
Add Color to Show Speed
speed = np.sqrt(U**2 + V**2)
plt.streamplot(
X, Y, U, V,
color=speed,
cmap='coolwarm'
)
plt.colorbar(label='Speed')
plt.show()
Result: Blue = slow, Red = fast!
Control Stream Density
# Fewer lines
plt.streamplot(
X, Y, U, V, density=0.5
)
# More lines
plt.streamplot(
X, Y, U, V, density=2
)
graph TD A["Stream Plot"] --> B["X,Y = Grid Points"] A --> C["U,V = Flow Direction"] A --> D["density = How many lines"] A --> E["color = Speed or value"] C --> F["Lines connect smoothly"]
๐จ Quick Reference
Polar Plot Basics
fig, ax = plt.subplots(
subplot_kw={'projection': 'polar'}
)
ax.plot(theta, r)
Polar Bar Chart
ax.bar(theta, values, width=width)
Quiver Plot
plt.quiver(X, Y, U, V)
Stream Plot
plt.streamplot(X, Y, U, V)
๐ The Big Picture
graph TD A["Specialty Plots"] --> B["Polar Plots"] A --> C["Vector Plots"] B --> D["Polar Line: Circular data"] B --> E["Polar Bar: Direction values"] C --> F["Quiver: Arrow field"] C --> G["Stream: Flow lines"]
You did it! You now understand:
- โ Polar plots for circular data
- โ Polar bars for direction-based values
- โ Quiver plots for arrow fields
- โ Stream plots for flow visualization
These tools help you show wind patterns, circular schedules, magnetic fields, and so much more!
