【问题标题】:Circular interpolated heat map plot using python使用python的圆形插值热图
【发布时间】:2014-10-03 20:29:00
【问题描述】:

我有代表圆内点的值的数据。我想创建一个类似于http://matplotlib.org/examples/pylab_examples/image_interp.html 的热图。有谁熟悉用圆圈做这个的方法吗?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以通过在轴上使用极坐标投影来做到这一点。请注意,根据您给出的示例,这不适用于 imshow 。 (见:http://en.it-usenet.org/thread/15998/715/) 但是,您仍然可以进行插值,然后绘制热图。下面是一个简单的例子:

    from pylab import *
    import numpy as np
    from scipy.interpolate import griddata
    
    #create 5000 Random points distributed within the circle radius 100
    max_r = 100
    max_theta = 2.0 * np.pi
    number_points = 5000
    points = np.random.rand(number_points,2)*[max_r,max_theta]
    
    #Some function to generate values for these points, 
    #this could be values = np.random.rand(number_points)
    values = points[:,0] * np.sin(points[:,1])* np.cos(points[:,1])
    
    #now we create a grid of values, interpolated from our random sample above
    theta = np.linspace(0.0, max_theta, 100)
    r = np.linspace(0, max_r, 200)
    grid_r, grid_theta = np.meshgrid(r, theta)
    data = griddata(points, values, (grid_r, grid_theta), method='cubic',fill_value=0)
    
    #Create a polar projection
    ax1 = plt.subplot(projection="polar")
    ax1.pcolormesh(theta,r,data.T)
    plt.show()
    

    请注意,我使用的 fill_value 为 0,因此网格中任何落在随机数据凸面形状之外的值都将为 0。

    如果您希望这样做,则需要在执行相同操作之前将数据转换为极坐标(假设您的读数是笛卡尔坐标)。为此,您可以使用:

    def convert_to_polar(x, y):
        theta = np.arctan2(y, x)
        r = np.sqrt(x**2 + y**2)
        return theta, r 
    

    您可能会发现这些问题的答案也很有帮助: image information along a polar coordinate system Adding a colorbar to a pcolormesh with polar projection

    特别是其中的第一个有一个非常详细的答案。

    【讨论】:

      【解决方案2】:

      我很欣赏这是一个老问题,但是当我利用 Weir_Doe 的答案并以稍微不同的方式开发它时,我想我会贡献我的方法,希望它可以帮助其他人。

      我试图做类似的事情,并以系统的方式收集 r 和 theta 的结果,因此我最终得到了一个网格。一旦有了网格,您就可以使用缩放来获得更高清晰度的图像。

      from pylab import *
      import numpy as np
      from scipy.ndimage import zoom
      import pandas as pd
      
      max_r = 100
      max_theta = 2.5 * np.pi
      number_points = 5
      
      #Generate a grid 100 x 100 r x theta
      r = np.arange(0, max_r,max_theta/number_points)
      theta = np.arange(0,max_theta,max_theta/number_points)
      grid_r, grid_theta = np.meshgrid(r, theta)
      
      #Generate random numbers for each grid point
      values = (np.sin(grid_r)+np.cos(grid_theta)).flatten()
      
      
      #I always find it easier to put it in a dataframe
      df = pd.DataFrame(grid_r.flatten()).rename(columns={0:'r'})
      df['theta'] = grid_theta.flatten()
      df['values'] = values
      df = df.pivot(index='theta', columns='r')
      #printing the dataframe at this point is very helpful conceptually
      
      #Create a polar projection
      ax1 = plt.subplot(projection="polar")
      ax1.pcolormesh(df.index,r,df.values.T)
      plt.show()
      
      #Zoom in to the grid, this interpolates the results onto a finer grid
      #Here I chose a 10x finer grid, this is more efficient than to interpolate onto specified points
      zoom_factor=10
      
      zoomed_df = zoom(df, zoom_factor)
      zoomed_index = zoom(theta, zoom_factor)
      zoomed_columns = zoom(r, zoom_factor)
      high_def_grid = pd.DataFrame(zoomed_df, index=zoomed_index, columns=zoomed_columns)
      
      
      #Create a polar projection
      ax1 = plt.subplot(projection="polar")
      ax1.pcolormesh(high_def_grid.index,high_def_grid.columns,high_def_grid.values.T)
      plt.show()
      

      这会产生 2 张图像,即预插值图像:

      Uninterpolate polar plot

      以及插值后的情节:

      Interpolated polar plot

      就像我说的那样,这只有在以系统方式收集数据的情况下才有效,但出于科学目的,情况就是如此。

      此外,使用 pandas 数据框不是必需的步骤,但我发现这样做在概念上要简单得多。

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2021-09-28
        • 2020-11-17
        • 1970-01-01
        • 1970-01-01
        • 2020-10-12
        • 2013-01-22
        • 1970-01-01
        • 2013-03-31
        • 2012-12-02
        相关资源
        最近更新 更多