【问题标题】:Polar contour plot in MatplotlibMatplotlib 中的极坐标等值线图
【发布时间】:2011-09-26 18:34:03
【问题描述】:

我有一组数据,我想使用 Matplotlib 在极坐标中生成等高线图。

我的数据如下:

  • theta - 角度值的一维数组
  • radius - 半径值的一维数组
  • value - 我想用于轮廓的一维值数组

这些都是正确对齐的一维数组 - 例如:

theta   radius   value
30      1        2.9
30      2        5.3
35      5        9.2

也就是说,所有的值都重复了足够多的次数,以使这个由三个变量组成的“表”的每一行都定义一个点。

如何根据这些值创建极坐标等值线图?我考虑过将半径和 theta 值转换为 x 和 y 值并在笛卡尔坐标中进行,但是轮廓函数似乎需要二维数组,我不太明白为什么。

有什么想法吗?

【问题讨论】:

    标签: python matplotlib graphing


    【解决方案1】:

    Matplotlib 的 contour() 函数期望数据被排列为点的二维网格和每个网格点的对应值网格。如果您的数据自然地排列在网格中,您可以将 r、theta 转换为 x、y 并使用contour(r*np.cos(theta), r*np.sin(theta), values) 来绘制图表。

    如果您的数据不是自然网格化的,您应该遵循 Stephen 的建议并使用 griddata() 将您的数据插入到网格中。

    以下脚本显示了两者的示例。

    import pylab as plt
    from matplotlib.mlab import griddata
    import numpy as np
    
    # data on a grid
    r = np.linspace(0, 1, 100)
    t = np.linspace(0, 2*np.pi, 100)
    r, t = np.meshgrid(r, t)
    z = (t-np.pi)**2 + 10*(r-0.5)**2
    
    plt.subplot(121)
    plt.contour(r*np.cos(t), r*np.sin(t), z)
    
    # ungrid data, then re-grid it
    r = r.flatten()
    t = t.flatten()
    x = r*np.cos(t)
    y = r*np.sin(t)
    z = z.flatten()
    xgrid = np.linspace(x.min(), x.max(), 100)
    ygrid = np.linspace(y.min(), y.max(), 100)
    xgrid, ygrid = np.meshgrid(xgrid, ygrid)
    zgrid = griddata(x,y,z, xgrid, ygrid)
    
    plt.subplot(122)
    plt.contour(xgrid, ygrid, zgrid)
    
    plt.show()
    

    【讨论】:

    【解决方案2】:

    我不知道是否可以直接绘制极坐标等值线图,但如果您转换为笛卡尔坐标,您可以使用griddata 函数将您的一维数组转换为二维。

    【讨论】:

      猜你喜欢
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 2020-06-19
      相关资源
      最近更新 更多