0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Create Polar Plot in Matplotlib: Simple Guide

To create a polar plot in matplotlib, use subplot(projection='polar') to set polar coordinates, then plot data with plot(). This draws data points based on angles and radii on a circular graph.
๐Ÿ“

Syntax

Use plt.subplot(projection='polar') to create a polar axes. Then use plot(theta, r) where theta is the angle in radians and r is the radius.

  • plt.subplot(projection='polar'): sets up a polar coordinate system.
  • theta: array of angles in radians.
  • r: array of radius values.
  • plot(): draws the line or points on the polar plot.
python
import matplotlib.pyplot as plt
import numpy as np

# Example data
theta = np.linspace(0, 2 * np.pi, 100)
r = np.abs(np.sin(theta))

ax = plt.subplot(projection='polar')
ax.plot(theta, r)
plt.show()
๐Ÿ’ป

Example

This example shows how to plot a simple polar plot with angles from 0 to 2ฯ€ and radius as sine of the angle.

python
import numpy as np
import matplotlib.pyplot as plt

# Create array of angles from 0 to 2*pi
theta = np.linspace(0, 2 * np.pi, 100)
# Radius is sine of angle
r = np.abs(np.sin(theta * 3))

# Create polar subplot
ax = plt.subplot(projection='polar')
# Plot data
ax.plot(theta, r)

plt.title('Simple Polar Plot')
plt.show()
Output
A circular plot with three wave-like peaks around the circle, labeled 'Simple Polar Plot'.
โš ๏ธ

Common Pitfalls

Common mistakes include:

  • Using degrees instead of radians for angles. Matplotlib expects radians.
  • Not setting projection='polar' in subplot(), which creates a normal plot instead of polar.
  • Mixing up radius and angle arrays in plot().

Always convert degrees to radians with np.radians() if needed.

python
import numpy as np
import matplotlib.pyplot as plt

# Wrong: using degrees directly
theta_deg = np.linspace(0, 360, 100)
r = np.abs(np.sin(np.radians(theta_deg * 3)))

# Wrong: no polar projection
plt.subplot(111)
plt.plot(theta_deg, r)  # This will not create a polar plot
plt.title('Wrong Plot')
plt.show()

# Correct way
theta_rad = np.radians(theta_deg)
plt.subplot(projection='polar')
plt.plot(theta_rad, r)
plt.title('Correct Polar Plot')
plt.show()
Output
First plot: a normal Cartesian plot with angle degrees on x-axis and radius on y-axis. Second plot: a circular polar plot with correct angle-radius mapping.
๐Ÿ“Š

Quick Reference

Remember these key points for polar plots in Matplotlib:

  • Use projection='polar' in subplot().
  • Angles must be in radians.
  • Radius values define distance from center.
  • Use plot(theta, r) to draw lines.
  • Use scatter(theta, r) for points.
โœ…

Key Takeaways

Use plt.subplot(projection='polar') to create polar coordinate axes.
Angles must be in radians, not degrees, for polar plots.
Plot data with ax.plot(theta, r) where theta is angle and r is radius.
Common errors include forgetting polar projection or using degrees directly.
Polar plots show data in circular form based on angle and radius.