[Python] Generating a discrete polar surface map in cartesian coordinates

Discussão em 'Python' iniciado por Stack, Abril 5, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to generate a surface plot with discrete arc-shaped cells on a 2D cartesian plane. I am able to get decent results by plotting a 3D surface plot (using plot_surface) and viewing it from the top, but matplotlib can be a bit finicky with 3D, so I'd prefer to do it in 2D. I can also get similar results using pcolormesh on a polar plot, but again, I want a 2D cartesian plane. How can I do this in matplotlib?

    MRE:

    import numpy as np
    import matplotlib.pyplot as plt

    r = np.linspace(2, 5, 25)
    theta = np.linspace(0, np.pi, 25)
    R, Theta = np.meshgrid(r, theta)

    X = R*np.cos(Theta)
    Y = r*np.sin(Theta)
    U = R*np.cos(Theta)*np.exp(R*Theta/500)

    fig, ax = plt.subplots(figsize=(8,6), subplot_kw={"projection":"3d"})
    surf = ax.plot_surface(X, Y, U, cmap="viridis", rstride=1, cstride=1)
    ax.view_init(elev=90, azim=-90)
    ax.set_proj_type("ortho")
    ax.zaxis.line.set_lw(0.)
    ax.set_zticks([])
    ax.set_aspect("equalxy")
    fig.colorbar(surf, shrink=0.5, aspect=5)
    fig.tight_layout()

    fig, ax = plt.subplots(figsize=(8,6), subplot_kw={"projection":"polar"})
    ax.pcolor(Theta, R, U, shading="nearest")
    ax.set_xlim(0, np.pi)
    ax.grid(False)
    fig.tight_layout()


    3D plot version:

    [​IMG]

    2D polar plot version:

    [​IMG]

    Continue reading...

Compartilhe esta Página