gpt4 book ai didi

python - 为极坐标创建网格

转载 作者:太空狗 更新时间:2023-10-30 01:07:44 25 4
gpt4 key购买 nike

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

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

关于python - 为极坐标创建网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28926813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com