【问题标题】:Retrieving data points from scipy interpolate/griddata从 scipy interpolate/griddata 中检索数据点
【发布时间】:2016-04-02 00:55:07
【问题描述】:

我使用 Scipy 的 Griddata 使用绘制的点(显示为空)填充此 gridded data。 有没有办法根据 (x,y) 坐标获取插值 (z)? 这是绘图的代码,x,y,z 值都是 Series。

    xi = np.linspace(min(lats),max(lats),360)
    yi = np.linspace(min(lons),max(lons),360)
    # grid the data.
    zi = griddata((lats, lons), nuits, (xi[None,:], yi[:,None]), method='cubic')
    # contour the gridded data.
    plt.contourf(xi,yi,zi,15,cmap=cMap)
    plt.colorbar()
    # plot data points.
    #plt.scatter(lats,lons,c=nuits,marker='o',s=26,cmap=cMap)
    plt.scatter(lats,lons,facecolors='none', edgecolors='k',s=26)
    plt.show()

【问题讨论】:

  • zi不是你想要的吗?
  • 嗨迈克! zi 是整个网格数据集。我希望能够根据 (xi,yi) 坐标从 zi 中找到一个值。有没有比找到与 xi 和 yi 相同的索引更好的方法。

标签: python numpy scipy geospatial interpolation


【解决方案1】:

这可行:

xi_coords = {value: index for index, value in enumerate(xi)}
yi_coords = {value: index for index, value in enumerate(yi)}
xic = <your coordinate>
yic = <your coordinate>
zi[xi_coords[xic], yi_coords[yic]]

【讨论】:

  • 感谢 Mike 的解决方案,这个解决方案出现的一个问题是 linspace 生成的坐标与我正在搜索的坐标有点偏离
  • 很好,它有帮助。 linspace 生成的坐标 是一个不同的问题。查找有效,对吗?所以这个问题得到了回答。顺便说一句,如果它解决了你的问题,你可以accept 一个答案。我建议您提出一个关注 linspace 部分的新问题。提供小而真实的输入数据,你得到什么,你想要什么。
  • @MikeMüller griddata 是否可以在 3d 中工作,比如 griddata((x, y, z), vals, ..)
【解决方案2】:

您可以通过以下方式从 (xi,yi) 坐标中获取插值 zi 坐标:

# (xi,yi) coords to get the interpolated zi coords
# where len(xic) = len(yic)  
xic = <your coordinate>
yic = <your coordinate>

# sort these coordinates in an increasing order
s_xic = np.sort(xic)
s_yic = np.sort(yic)

# indices belonging to xic, yic, that would sort the array
ind_s_xic = np.argsort(xic)
ind_s_yic = np.argsort(yic)

 
dict_xic = dict(zip(ind_s_xic, np.array(range(len(xic))))
dict_yic = dict(zip(ind_s_yic, np.array(range(len(yic))))


xi,yi = np.meshgrid(s_xic, s_yic)

# zi_grid has dimensions ( len(yic), len(xic) )
zi_grid = griddata((lats, lons), nuits, (xi, yi), method='cubic')

# zic is the interpolated z-coordinate data with an arrangement order,
# corresponding to the x and y-coordinate data in xic and yic
zic =  [ zi_grid[dict_yic[i], dict_xic[i]] for i in range(len(xic)) ]

访问How does one use numpy's Meshgrid function with a random interval rather than a equally spaced one? 了解meshgrid 的工作原理。

Meshgrid 可以从您的不均匀间隔 (xi,yi) 坐标创建,然后,griddata 可以使用您的 meshgrid 从基于点 = (lats, lons)、值 = nuits 创建的插值表面插值 z 坐标.

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 2016-10-02
    相关资源
    最近更新 更多