简体   繁体   English

从 scipy interpolate/griddata 检索数据点

[英]Retrieving data points from scipy interpolate/griddata

I used Scipy's Griddata to fill in this gridded data using the points plotted (shown as empty).我使用 Scipy 的 Griddata 使用绘制的点(显示为空)填充此网格化数据 Is there a way to get the interpolated values (z) based on (x,y) coordinates?有没有办法根据 (x,y) 坐标获取插值 (z)? here's the code for the plot, and the x,y,z values are all Series.这是绘图的代码,x、y、z 值都是系列。

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

This would work:这会起作用:

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]]

You can get your interpolated zi coordinates from your (xi,yi) coordinates by:您可以通过以下方式从 (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)) ]

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

Meshgrid can be created from your unevenly spaced (xi,yi) coordinates, following which, griddata can use your meshgrid to interpolate the z-coords from the interpolated surface created based on points = (lats, lons), values = nuits. Meshgrid 可以从您的不均匀间隔的 (xi,yi) 坐标创建,之后,griddata 可以使用您的 meshgrid 从基于点 = (lats, lons),值 = nuits 创建的插值表面插入 z 坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM