2015-12-30 140 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))) 

以及類似的經度。