Polar axes help you draw data on a circular grid. This is useful when your data depends on angles and distances, like directions or rotations.
0
0
Polar axes in Matplotlib
Introduction
You want to show wind directions and speeds on a circular chart.
You need to plot data that repeats every 360 degrees, like time of day or compass bearings.
You want to visualize cyclic patterns, such as seasons or phases of the moon.
You want to compare values based on angles, like radar charts or rose diagrams.
Syntax
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.plot(theta, r) plt.show()
Use subplot_kw={'projection': 'polar'} to create polar axes.
theta is the angle in radians, and r is the radius (distance from center).
Examples
This example plots a sine wave on polar axes, creating a flower-like shape.
Matplotlib
import numpy as np import matplotlib.pyplot as plt theta = np.linspace(0, 2 * np.pi, 100) r = np.abs(np.sin(3 * theta)) fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.plot(theta, r) plt.show()
This example shows a simple bar chart on polar axes, where bars extend outward from the center.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.bar([0, 1, 2], [1, 2, 3], width=0.5) plt.show()
Sample Program
This program draws a polar plot of the function |sin(2θ)|. The plot looks like a four-petal flower because the sine function repeats every π radians and the absolute value makes all values positive.
Matplotlib
import numpy as np import matplotlib.pyplot as plt # Create 100 points from 0 to 2*pi theta = np.linspace(0, 2 * np.pi, 100) # Radius is the absolute value of sine of 2*theta r = np.abs(np.sin(2 * theta)) # Create polar plot fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.plot(theta, r) ax.set_title('Polar plot of |sin(2θ)|') plt.show()
OutputSuccess
Important Notes
Angles in polar plots are in radians, not degrees. Use np.deg2rad() to convert degrees to radians if needed.
You can customize the grid, labels, and limits on polar axes just like regular plots.
Summary
Polar axes let you plot data using angles and distances.
Use subplot_kw={'projection': 'polar'} to create polar plots.
Polar plots are great for cyclic or directional data.