Python 获取numpy linspace生成的坐标

Python 获取numpy linspace生成的坐标,python,numpy,dictionary,interpolation,Python,Numpy,Dictionary,Interpolation,我使用NumPy的linspace来填充点之间的数据 lats = (-66.44421,-66.57947,-64.81464,-64.69528) lons = (-73.03290,-72.73904,-64.71657,-65.03036) NO3 = (33.48,24.01,17.20,20.03) xi = np.linspace(min(lats),max(lats),360) yi = np.linspace(min(lons),max(lons),360) # gri

我使用NumPy的linspace来填充点之间的数据

lats = (-66.44421,-66.57947,-64.81464,-64.69528)
lons = (-73.03290,-72.73904,-64.71657,-65.03036)
NO3  = (33.48,24.01,17.20,20.03) 

xi = np.linspace(min(lats),max(lats),360)
yi = np.linspace(min(lons),max(lons),360)

# grid the data.
zi = griddata((lats, lons), NO3, (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,facecolors='none', edgecolors='k',s=26)
plt.show()
我想根据linspace生成的坐标对从网格数据zi中检索值(缺少样本),但对于dict查找,坐标并不精确:

# record index and value of linspace coordinates as key and value
xi_coords = {value: index for index, value in enumerate(xi)}
yi_coords = {value: index for index, value in enumerate(yi)}
# how to retrieve a value inbetween at say... (-65.11018,-67.08512)
zi[xi_coords[-65.11018], yi_coords[-67.08512]]
返回一个键错误。
这个问题有没有更聪明的解决方法?

如果我没有弄错的话,您尝试检索的点不在您的邻域中,这不仅仅是一个数值精度问题。。。如果要查找距离任何给定点最近的网格点,应定义函数,而不是使用DICT:

latmin = min(lats)
latmax = max(lats)
npoints = 360

def get_lat_index(lat):
    return int(round((npoints-1)*(lat-latmin)/(latmax-latmin)))

经度也类似。

一个选项是四舍五入。例如,两位小数:

xi_coords = {round(value, 2): index for index, value in enumerate(xi)}
yi_coords = {round(value, 2): index for index, value in enumerate(yi)}
zi[xi_coords[-65.11], yi_coords[-67.08]]