Plotting rose curve in python with even petals

Question:

I was trying to plot a rose curve in python with the following code:

import numpy as np
from matplotlib import pyplot as plt

theta = np.linspace(0, 2*np.pi, 1000)

r = 3* np.sin(8 * theta)

plt.polar(theta, r, 'r')

plt.show()

Now this is the rose curve $r= 3 sin{8theta}$, where since $8$ is even so the curve should have $2.8=16$ petals. But the output gives me a rose with $8$ petals.
I’m not sure if python understands the mathematical definition of the rose curve or we modified our definition so that Python understands it.
Does anyone here has any advices on what I should do in my code to get the output I desire?
Any help is appreciated.

Thank you.

Asked By: aiyy

||

Answers:

Your problem is similar to the problem encountered in this related question: Polar plot of a function with negative radii using matplotlib

The problem is that matplotlib.pyplot.polar does not plot a curve in polar coordinates the way a mathematician would expect it.

A mathematician would expect that a radius of 0 designates the origin, and a negative radius is reflected across the origin; in particular, the polar coordinates (r, t) and (-r, t+pi) should designate the same point.

You can force this behaviour by manually adding pi to theta when r is negative, and replace r with its absolute value.

import numpy as np
from matplotlib import pyplot as plt

theta = np.linspace(0, 2*np.pi, 1000)

r = 3* np.sin(8 * theta)

# plt.polar(theta, r, 'r')  # remove this line
plt.polar(theta+(r<0)*np.pi, np.abs(r), 'r')

plt.show()

16-petals rose curve

Answered By: Stef