[Python] polar plot -- sin transformation

Discussão em 'Python' iniciado por Stack, Julho 8, 2025.

  1. Stack

    Stack Membro Participativo

    I want to plot a very simple function that would look like a simple sinusoidal f(x) = r + n* sin(n*x) in a Cartesian coordinate plane.

    Given this, now I want to plot this in a polar plot -- ideally as a sin wave along a circular path.

    This is my code

    import numpy as np
    import matplotlib.pyplot as plt

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


    def inner(theta, n_petals, amplitude, base_radius):
    return base_radius + amplitude * np.cos(n_petals * theta)

    inner_bc = inner(theta, n_petals=5, amplitude=2, base_radius=5)


    # Create polar plot
    plt.figure(figsize=(6, 6))
    ax = plt.subplot(111, projection='polar')
    ax.grid(True)
    ax.plot(theta, inner_bc, color='red', linewidth=0.8)
    plt.show()


    but this has a bug that is, it compresses as r getting lower and expands when r getting higher -- producing a plot that is not preserving angles of a sin wave.

    [​IMG]

    can someone please suggest a way to correct this function such that it plots the graph correctly ? My guess is, it has to be properly accounted for x-y to r-theta transformation, only in the radial direction but I couldn't wrap my head around how to do it properly. Could someone shed some light on this ?

    Continue reading...

Compartilhe esta Página