Polar Plots
In a polar coordinate system a point is described by an angle θ (theta) and a distance r from the centre, instead of x and y. Polar plots are natural for anything directional or cyclic: wind directions, antenna patterns, times of day, months of the year.
Creating polar Axes
Pass subplot_kw={"projection": "polar"} to plt.subplots(). Then plot(theta, r) works as usual, with angles in radians:
import numpy as np
import matplotlib.pyplot as plt
theta = np.linspace(0, 2 * np.pi, 400)
r = 1 + 0.5 * np.cos(3 * theta) # a three-leaf rose
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
ax.plot(theta, r, color="C3")
ax.set_title("r = 1 + 0.5 cos(3θ)")
plt.show()
print("Axes type:", ax.name)
print(f"r ranges from {r.min():.2f} to {r.max():.2f}")Axes type: polar
r ranges from 0.50 to 1.50
ax.name is "polar": the polar Axes is a different class with its own methods such as set_theta_zero_location.
Orientation: compass style
By default 0° points east (right) and angles grow counter-clockwise, like in mathematics. For compass-like data, put 0° at the top and go clockwise:
import numpy as np
import matplotlib.pyplot as plt
directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
hours = np.array([42, 30, 18, 12, 25, 70, 95, 60]) # hours of wind from each direction
angles = np.deg2rad(np.arange(0, 360, 45))
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.bar(angles, hours, width=np.deg2rad(40), color="C0", alpha=0.8, edgecolor="white")
ax.set_xticks(angles, directions)
ax.set_rlabel_position(112.5) # move the radial labels between E and SE
ax.set_title("Wind rose (hours per direction)")
plt.show()
print("Prevailing wind from:", directions[int(hours.argmax())])Prevailing wind from: W
A polar bar chart like this is called a wind rose. width is the angular width of each bar, also in radians.
Data over a cycle
Months of the year form a circle: December is next to January. A polar line closes that loop naturally. To close the line, repeat the first point at the end:
import numpy as np
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
sydney = [26, 26, 25, 22, 19, 17, 16, 18, 20, 22, 24, 25]
helsinki = [-3, -4, 0, 6, 12, 16, 19, 17, 12, 6, 1, -2]
angles = np.linspace(0, 2 * np.pi, 12, endpoint=False)
closed = np.append(angles, angles[0])
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.plot(closed, sydney + sydney[:1], marker="o", label="Sydney")
ax.plot(closed, helsinki + helsinki[:1], marker="o", label="Helsinki")
ax.set_xticks(angles, months)
ax.set_rlim(-10, 30)
ax.set_rlabel_position(255)
ax.legend(loc="upper right", bbox_to_anchor=(1.3, 1.1))
plt.show()
print("Warmest month in Sydney:", months[sydney.index(max(sydney))])
print("Warmest month in Helsinki:", months[helsinki.index(max(helsinki))])Warmest month in Sydney: Jan
Warmest month in Helsinki: Jul
set_rlim sets the radial range. Because the minimum is −10, the centre represents −10 °C rather than zero, so be careful when comparing distances from the centre.
Radar (spider) charts
A radar chart compares several attributes of a few items. It is a closed polar line with one spoke per attribute:
import numpy as np
import matplotlib.pyplot as plt
skills = ["Speed", "Battery", "Camera", "Screen", "Price", "Storage"]
phone_a = [8, 6, 9, 7, 4, 7]
phone_b = [6, 9, 6, 8, 8, 5]
angles = np.linspace(0, 2 * np.pi, len(skills), endpoint=False).tolist()
angles += angles[:1]
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
for name, values, color in [("Phone A", phone_a, "C0"), ("Phone B", phone_b, "C1")]:
data = values + values[:1]
ax.plot(angles, data, color=color, label=name)
ax.fill(angles, data, color=color, alpha=0.2)
ax.set_xticks(angles[:-1], skills)
ax.set_ylim(0, 10)
ax.set_yticks([2, 4, 6, 8, 10])
ax.legend(loc="lower right", bbox_to_anchor=(1.25, -0.1))
plt.show()
print("Totals: A =", sum(phone_a), "| B =", sum(phone_b))Totals: A = 41 | B = 42
Note: Radar charts look attractive but are hard to read precisely, and the filled area depends on the order of the spokes. Use them for a quick visual profile, not for exact comparisons.
| Method | Purpose |
|---|---|
set_theta_zero_location("N") |
where 0° points ("N", "E", "S", "W" …) |
set_theta_direction(-1) |
clockwise angles |
set_rlim(a, b) / set_ylim(a, b) |
radial range |
set_rticks([...]) |
radial tick positions |
set_thetagrids(degrees, labels) |
angular grid lines in degrees |
Test yourself
How do you create polar Axes with plt.subplots?
- plt.subplots(polar=True) only
- plt.subplots(subplot_kw={"projection": "polar"})
- plt.polar_subplots()
- plt.subplots(kind="polar")
In which unit does a polar Axes expect the angles?
- Degrees
- Radians
- Gradians
- Hours
Why repeat the first value at the end of a radar chart's data?
- To double its weight
- To close the polygon back to the starting spoke
- It is required by fill()
- To add a legend entry