简体   繁体   English

为极坐标创建一个网格

[英]create a meshgrid for polar coordinates

I want to create a mesh grid for polar coordinates using the following arrays. 我想使用以下数组为极坐标创建一个网格。

R = 1.15
r = numpy.linspace(R,5,100)
theta = numpy.linspace(0,2*numpy.pi,145)

I tried it this way, using numpy : 我用numpy这样尝试过:

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

but I am getting this error: 但我收到此错误:

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

How do I generate the grid and display the points? 如何生成网格并显示点?

Do not convert to a cartesian coordinate system if you simply want two 2D arrays that specify coordinates r and theta on a polar grid. 如果仅需要两个在极坐标网格上指定坐标rtheta 2D数组,则不要转换为笛卡尔坐标系。

To clarify, the error you're seeing is because you can not perform element-wise multiplication between two arrays of unequal shape, which is what you have. 为了澄清,您看到的错误是因为您无法在形状不相等的两个数组之间执行逐元素乘法。

You should be calling it like this: 您应该这样称呼它:

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

Then you could convert to cartesian coordinates (if really needed) by typing: 然后,您可以通过键入以下内容转换为笛卡尔坐标(如果确实需要):

X = radius_matrix * numpy.cos(theta_matrix)
Y = radius_matrix * numpy.sin(theta_matrix)

Visualisation could be done on a polar grid immediately, using eg matplotlib: 可视化可以立即在极坐标网格上完成,例如使用matplotlib:

import matplotlib.pyplot as plt
ax = plt.subplot(111, polar=True)
ax.plot(theta_matrix, radius_matrix, color='r', ls='none', marker='.')

Have a look at the polar plot demo if you'd like another example. 如果您想要另一个示例,请看极坐标图演示

Alternatively, you could plot the polar grid you made by plotting the cartesian coordinates we obtained previously on a cartesian grid: 另外,您可以通过在笛卡尔网格上绘制先前获得的笛卡尔坐标来绘制极坐标网格:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM