[英]Retrieving data points from scipy interpolate/griddata
我使用 Scipy 的 Griddata 使用繪制的點(顯示為空)填充此網格化數據。 有沒有辦法根據 (x,y) 坐標獲取插值 (z)? 這是繪圖的代碼,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()
這會起作用:
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]]
您可以通過以下方式從 (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 坐標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.