Open In App

Plotting polar curves in Python

Last Updated : 23 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.
Python
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:

PLOTTING1

Circle

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 :

[Tex]r\ =\ \frac{a\ .\ b}{\sqrt{(asin\theta)^2\ +\ (bcos\theta)^2}}[/Tex]

where:

  • [Tex]a [/Tex]= length of semi major axis
  • [Tex]b [/Tex]= 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.
Python
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:

PLOTTING-2

ellipse

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 :

[Tex]r = a + a \cdot \cos(\theta)[/Tex]

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.
Python
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:

PLOTTING-3

cardioid

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 :

[Tex]r=θ[/Tex]

  • [Tex]r=θ[/Tex] as r causing spiral to grow outward.
Python
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:

PLOTTING-4

Archimedean spiral

5. Rhodonea

A Rhodonea or Rose curve is a rose-shaped sinusoidal function plotted in polar coordinates. Here r is defined as :

[Tex]r = a \cdot \cos(n \theta)[/Tex]

Where

  • [Tex]a[/Tex]= length of petals
  • [Tex]n[/Tex] = 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.
Python
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:

PLOTTING-5

Rhodonea

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 θ.



Next Article
Article Tags :
Practice Tags :

Similar Reads