为极坐标创建网格

create a meshgrid for polar coordinates

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

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,) 

如何生成网格并显示点?

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

澄清一下,您看到的错误是因为您无法在两个形状不等的数组之间执行逐元素乘法,这正是您遇到的情况。

你应该这样称呼它:

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()