2015-12-30 87 views
1

我使用NumPy的linspace在点之间填充数据。获取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值(丢失的样本),但坐标是不准确的字典查找:

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

返回重要错误。 有没有比这个问题更聪明的解决方法?

回答

0

一个选项是舍入。例如到小数点后两位:

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

再次感谢迈克,为我的问题工作! –

0

如果我没有弄错,你试图检索的点不在你的空间中,它不仅仅是一个数值精度问题......如果你想找到最接近任何点的网格点,你应该定义功能而不是使用字幕:

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

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

以及类似的经度。