Plotting polar curves in Python
A point in polar coordinates is represented as (r, θ) where r is the distance from the origin and θ is the angle measured from the origin. Any mathematical function in Cartesian coordinate system can be plotted using the polar coordinates. The matplotlib.pyplot module contains a function polar() which can be used for plotting curves in polar coordinates.
Syntax : matplotlib.pyplot.polar(theta, r, **kwargs)
Parameters :
- theta - Angle values (in radians)
- r - Distance values
For each curve, we generate an array of theta values (angle) and then we will calculate the corresponding r values based on the equation of the curve. Lets see some of its example:
1. Circle
A circle is a shape where all points are at the same distance (radius) from the center. Hence, r is a constant value equal to the radius.
- rads = np.arange(0, (2 * np.pi), 0.01): Generates an array of angles in radians starting from 0 to just under 2π with a step size of 0.01 radians.
import numpy as np
import matplotlib.pyplot as plt
plt.axes(projection='polar')
r = 2
rads = np.arange(0, (2 * np.pi), 0.01)
for rad in rads:
plt.polar(rad, r, 'g.')
plt.show()
Output:

2. Ellipse
An ellipse is the locus of a point moving in a plane such that sum of its distances from two other points (foci) is constant. Here r is defined as :
where:
a = length of semi major axisb = length of semi minor axis
- r = (a*b)/math.sqrt((a*np.sin(rads))**2 + (b*np.cos(rads))**2): Calculates radius r for each angle in rads based on ellipse formula using a and b as semi-major and semi-minor axes.
import numpy as np
import matplotlib.pyplot as plt
import math
plt.axes(projection = 'polar')
a = 4
b = 3
rads = np.arange(0, (2 * np.pi), 0.01)
for rad in rads:
r = (a*b)/math.sqrt((a*np.sin(rad))**2 + (b*np.cos(rad))**2)
plt.polar(rad, r, 'g.')
plt.show()
Output:

3. Cardioid
A cardioid is the locus of a point on the circumference of a circle as it rolls around another identical circle. Here r is defined as :
Where a = length of axis of cardioid
- r = a + (a*np.cos(rad)): Calculates r for each angle in rad based on cardioid formula with a as the axis length.
import numpy as np
import matplotlib.pyplot as plt
import math
plt.axes(projection = 'polar')
a=4
rads = np.arange(0, (2 * np.pi), 0.01)
for rad in rads:
r = a + (a*np.cos(rad))
plt.polar(rad,r,'g.')
plt.show()
Output:

4. Archimedean spiral
An Archimedean spiral is the locus of a point moving uniformly on a straight line which itself is turning uniformly about one of its end points. Here r is defined as :
r=θ as r causing spiral to grow outward.
import numpy as np
import matplotlib.pyplot as plt
plt.axes(projection = 'polar')
rads = np.arange(0, 2 * np.pi, 0.001)
for rad in rads:
r = rad
plt.polar(rad, r, 'g.')
plt.show()
Output:

5. Rhodonea
A Rhodonea or Rose curve is a rose-shaped sinusoidal function plotted in polar coordinates. Here r is defined as :
Where
a = length of petalsn = number of petals
- r = a * np.cos(n*rad): Calculates r for each angle in rad based on rose curve formula using a as the amplitude and n as number of petals.
import numpy as np
import matplotlib.pyplot as plt
plt.axes(projection='polar')
a = 1
n = 6
rads = np.arange(0, 2 * np.pi, 0.001)
for rad in rads:
r = a * np.cos(n*rad)
plt.polar(rad, r, 'g.')
plt.show()
Output:

Polar coordinates offer a unique way to represent and visualize mathematical functions and helps plotting various curves each with its own specific equation for radius r based on angle θ.