笛卡尔图到极坐标图的转换

2024-09-28 17:08:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的线性图: linear plot

我想用以下方法创建极坐标图:

def cart2pol(x, y):
    rho = np.sqrt(x**2 + y**2)
    phi = np.arctan2(y, x)
    return(rho, phi)

但我不明白它是如何工作的。如何使用上述def在极坐标中创建绘图

要创建我正在使用的线性绘图

ax.plot(xd, yd2)

其中:xd=范围(0,50)和yd2=yd[0:50],yd-来自我的信号的数据

下一步:

xd_2 = xd[2:50]
yd3 = yd[2:50]
def cart2pol(xd_2, yd3):
    xdkw = np.power(xd_2,2)
    yd3kw = np.power(yd3,2)
    rho = np.sqrt(xdkw + yd3kw)
    phi = np.arctan2(yd3kw, xdkw)
    return(rho, phi)

接下来:

plt.polar(cart2pol(xd_2,yd3))

我得到一个错误:

ValueError: x and y can be no greater than 2-D, but have shapes (2,) and (2, 50, 48) 

Tags: plotdefnp线性sqrtphirhoxd
1条回答
网友
1楼 · 发布于 2024-09-28 17:08:04

此方法将笛卡尔坐标(x,y)转换为等效极坐标(rho,phi)并返回它

在二维极坐标系中,点由(ρ,φ)表示, 在哪里

  • ρ(rho)-径向坐标,即点到原点的距离
  • φ(φ)-角度坐标,是连接原点和点的直线与参考方向的角度

将笛卡尔坐标系转换为极坐标系

  • x轴作为相应极平面中的参考方向
  • 对于坐标(X,Y),ρ=sqrt(X^2+Y^2)
  • 对于坐标(X,Y),φ=atan2(Y,X)。注意-要理解为什么我们使用atan2而不是atan,请检查以下答案Python atan or atan2, what should I use?

极坐标可以通过以下方式绘制:

cartesian_coordinates = [(1, 2), (-3, 4), (5, 6)]
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
for x, y in cartesian_coordinates:
    rho, phi = cart2pol(x, y)
    ax.plot(phi, rho)

相关问题 更多 >