python meshgrid_python – 为极坐标创建一个meshgrid

我想使用以下数组为极坐标创建网格.

R = 1.15

r = numpy.linspace(R,5,100)

theta = numpy.linspace(0,2*numpy.pi,145)

我用这种方式尝试了,使用numpy:

X,Y=numpy.meshgrid(r*numpy.cos(theta),r*numpy.sin(theta))

但我收到这个错误:

ValueError: operands could not be broadcast together with shapes (100,) (145,)

如何生成网格并显示点?

解决方法:

如果您只想要两个在极坐标网格上指定坐标r和theta的二维数组,请不要转换为笛卡尔坐标系.

为了澄清,你看到的错误是因为你不能在两个不等形状的数组之间执行逐元素乘法,这就是你所拥有的.

你应该这样称呼它:

radius_matrix, theta_matrix = numpy.meshgrid(r,theta)

然后您可以通过键入以下内容转换为笛卡尔坐标(如果确实需要):

X = radius_matrix * numpy.cos(theta_matrix)

Y = radius_matrix * numpy.sin(theta_matrix)

使用例如可以在极地网格上进行可视化. matplotlib:

import matplotlib.pyplot as plt

ax = plt.subplot(111, polar=True)

ax.plot(theta_matrix, radius_matrix, color='r', ls='none', marker='.')

如果您想要另一个例子,请查看polar plot demo.

或者,您可以通过绘制我们先前在笛卡尔网格上获得的笛卡尔坐标来绘制您绘制的极坐标网格:

plt.plot(X,Y, 'r. ')

plt.show()

标签:python,plot,numpy,polar-coordinates

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值