【问题标题】:Cartesian plot to polar plot conversion笛卡尔图到极坐标图的转换
【发布时间】:2021-09-22 08:02:26
【问题描述】:

我有这样的线性图: 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=range(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) 

【问题讨论】:

  • 你的意思是你想用phi作为横轴,rho作为纵轴来绘图吗?
  • 在极坐标中作为“r”和“theta”
  • okej,但是如何将我的线性图转换为极坐标图?在 matlab 中是一个 cmd:cart2pol,就是这样。
  • 您最初用来绘制的xy- 列表(或numpy 数组)可以使用您定义的cart2pol(x,y) 转换为极坐标,然后使用matplotlib.pyplot.polar 绘制它。我不确定问题到底出在哪里。

标签: python polar-coordinates


【解决方案1】:

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

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

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

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

  • 以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)

【讨论】:

  • 但是,使用上面的代码后,我得到了这样的图:imgur.com/bj498O5,但我使用不正确:cartesian_coordinates = [(x,y)] fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) for x, y in cartesian_coordinates: rho, phi = cart2pol(x, y) ax.plot(phi, rho) 其中 x,y 是创建我的线性图的坐标 (i.stack.imgur.com/1Hzsu.png)
猜你喜欢
  • 2013-08-05
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多