sub*_*ash 5 python plot numpy polar-coordinates
我想使用以下数组为极坐标创建网格.
R = 1.15
r = numpy.linspace(R,5,100)
theta = numpy.linspace(0,2*numpy.pi,145)
Run Code Online (Sandbox Code Playgroud)
我试过这种方式,使用numpy:
X,Y=numpy.meshgrid(r*numpy.cos(theta),r*numpy.sin(theta))
Run Code Online (Sandbox Code Playgroud)
但我收到这个错误:
ValueError: operands could not be broadcast together with shapes (100,) (145,)
Run Code Online (Sandbox Code Playgroud)
如何生成网格并显示点?
如果您只想要两个指定坐标的二维数组r并theta在极坐标网格上,请不要转换为笛卡尔坐标系.
为了澄清,你看到的错误是因为你不能在两个不等形状的数组之间执行逐元素乘法,这就是你所拥有的.
你应该这样称呼它:
radius_matrix, theta_matrix = numpy.meshgrid(r,theta)
Run Code Online (Sandbox Code Playgroud)
然后您可以通过键入以下内容转换为笛卡尔坐标(如果确实需要):
X = radius_matrix * numpy.cos(theta_matrix)
Y = radius_matrix * numpy.sin(theta_matrix)
Run Code Online (Sandbox Code Playgroud)
可视化可以立即在极地网格上完成,例如使用matplotlib:
import matplotlib.pyplot as plt
ax = plt.subplot(111, polar=True)
ax.plot(theta_matrix, radius_matrix, color='r', ls='none', marker='.')
Run Code Online (Sandbox Code Playgroud)
如果你想要另一个例子,看看极地情节演示.
或者,您可以通过绘制我们先前在笛卡尔网格上获得的笛卡尔坐标来绘制您绘制的极坐标网格:
plt.plot(X,Y, 'r. ')
plt.show()
Run Code Online (Sandbox Code Playgroud)