【问题标题】:create a meshgrid for polar coordinates为极坐标创建网格网格
【发布时间】:2015-03-08 13:13:13
【问题描述】:

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

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

如何生成网格并显示点?

【问题讨论】:

    标签: python numpy plot polar-coordinates


    【解决方案1】:

    如果您只想在极坐标网格上指定坐标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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多