Mathplotlib does not align my polar curve to its correct radians

  Kiến thức lập trình

When I try to input the following values for a Rose Polar curve:
a = 1 (amplitude), b = 2 (frequency/no. of petals; if b>=2, then b*=2). And then set it to a sin.

hence the equation, r=1sin2(tetha-0)

this graph shows. How to fix it so it aligns to its correct radians for quadrant 1 which is pi/4.

here’s my code

import numpy as np
import matplotlib.pyplot as plt

def plot_rose_polar(a, b, use_sine, phi, original_b):
# Radians
n = 2001
theta = np.linspace(0, 2.0 * np.pi, n)
turn = theta – phi

# Calculate r based on the user's choice of sine or cosine
if use_sine:
    r = a * np.sin(b * turn)
    r = a * np.cos(b * turn)

# Plot the graph
plt.figure(figsize=(6, 6))
ax = plt.subplot(111, polar=True)
ax.plot(theta, r, color='xkcd:darkgreen', label='rose curve')
trig_function = 'sin' if use_sine else 'cos'
ax.set_title(f'Rose Polar Graph: r={a}{trig_function}({original_b}(θ - {phi}))', va='bottom')

# Radians Annotation
radian_ticks = [0, np.pi/6, np.pi/4, np.pi/3, np.pi/2, 2*np.pi/3, 3*np.pi/4, 5*np.pi/6, np.pi, 7*np.pi/6, 5*np.pi/4, 4*np.pi/3, 3*np.pi/2, 5*np.pi/3, 7*np.pi/4, 11*np.pi/6, 2*np.pi]
radian_labels = ['0', r'$frac{pi}{6}$', r'$frac{pi}{4}$', r'$frac{pi}{3}$', r'$frac{pi}{2}$', r'$frac{2pi}{3}$', r'$frac{3pi}{4}$', r'$frac{5pi}{6}$', r'$pi$', r'$frac{7pi}{6}$', r'$frac{5pi}{4}$', r'$frac{4pi}{3}$', r'$frac{3pi}{2}$', r'$frac{5pi}{3}$', r'$frac{7pi}{4}$', r'$frac{11pi}{6}$', r'$2pi$']

def main():
while True:
# Inputs
print(“Rose Polar Graph: r = a * cos(bθ) or r = a * sin(bθ)”)
a = float(input(“Enter the value of a: “))
original_b = float(input(“Enter the value of b: “))
if original_b % 2 == 0:
b = original_b * 2
b = original_b

        trig_choice = input("Use sine or cosine? (enter 'sin' or 'cos'): ").strip().lower()
        if trig_choice == 'sin':
            use_sine = True
        elif trig_choice == 'cos':
            use_sine = False
            print("Invalid choice. Please enter 'sin' or 'cos'.")

        phi_choice = input("Do you want to turn the graph? (yes or no): ").strip().lower()
        if phi_choice == 'yes':
            phi = float(input("Enter the value of phi (in radians): "))
            phi = 0
        # Plot the rose polar graph
        plot_rose_polar(a, b, use_sine, phi, original_b)

        convert_choice = input("Do you want to convert the graph from sine to cosine or vice versa? (yes or no): ").strip().lower()
        if convert_choice == 'yes':
            use_sine = not use_sine
            plot_rose_polar(a, b, use_sine, phi, original_b)
    except ValueError:
        print("Invalid input. Please enter numeric values for a, b, and phi.")
    cont = input("Do you want to plot another graph? (yes or no): ").strip().lower()
    if cont != 'yes':

if name == “main“:

It does not align to its correct radians

New contributor

Paul S. Oga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.