2014-01-26 24 views
3

我想繪製一個輪廓不均勻間隔的數據在python中使用numpy,matplotlib plyplot和scipy。python numpy scipy griddata是南或所有相同的值

鑑於以下代碼片段,爲什麼zi要麼是空的,要麼是所有相同的值?

import numpy as np 
import matplotlib.pyplot as plt 
from scipy.interpolate import griddata 

lon_min = 1.8783669 
lon_max = 1.8792678 
lat_min = 57.45827 
lat_max = 57.459293 

x = [ 520.99012099,652.23665224,800.,0.,520.99012099 
    652.23665224,800.,0.,520.99012099,652.23665224 ...] 

y = [ 0.,379.47214076,437.53665689,600.,0. 
    379.47214076,437.53665689,600.,0.,379.47214076 ...] 

z = [ 56.6,56.6,56.6,56.6,45.3,45.3,45.3,45.3,57.8,57.8 ...] 

xi = np.linspace(lon_min,lon_max,10) 
yi = np.linspace(lat_min,lat_max,10) 
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='nearest') 

plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k') # this is blank or all the same colour because zi is either nan or all the same number depending on the method I use. 

應用一個比特調試它看起來像滋要麼是NAN如果我使用方法=立方/線性或全部相同數目如果我使用方法=最近

print xi 
print yi 
print zi  

給出: XI = [1.8783669 1.878376 1.8783851 1.8783942 1.8784033 1.8784124 1.8784215 1.8784306 1.8784397 1.8784488 1.8784579 1.878467 1.8784761 1.8784852 1.8784943 1.8785034 1.8785125 ....]

yi = [57.45827  57.45828033 57.45829067 57.458301 57.45831133 
    57.45832167 57.458332 57.45834233 57.45835267 57.458363 
    57.45837333 57.45838367 57.458394 57.45840433 57.45841467 
    57.458425 57.45843533 57.45844567 57.458456 57.45846633 .... ] 

zi = [[ nan nan nan ..., nan nan nan] 
[ nan nan nan ..., nan nan nan] 
[ nan nan nan ..., nan nan nan] 
..., 
[ nan nan nan ..., nan nan nan] 
[ nan nan nan ..., nan nan nan] 
[ nan nan nan ..., nan nan nan]] 

zi = [[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7] 
[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7] 
[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7] 
..., 
[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7] 
[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7] 
[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7]] 
+1

你可以把它變成實際的Python代碼?處理幾乎不可運行的代碼是令人難以置信的煩惱。看到[這](http://stackoverflow.com/questions/16423774/string-representation-of-a-numpy-array-with-commas-separating-its-elements)一些幫助正確地輸出陣列。人們將更有可能以這種方式回答這個問題。 – YXD

+0

griddata調用對於'scipy.interpolate.griddata'也是無效的 –

+0

從文檔看起來好像你正在傳遞griddata正確的值 http://docs.scipy.org/doc/scipy/reference/generated/ scipy.interpolate.griddata.html 的GridData(點值,十一,方法=「線性」,fill_value =楠) 你逝去的5個值的GridData,你知道哪些值你以爲你是路過 – user1938107

回答

0

您是否試圖用三輪車直接分析您的數據?

http://matplotlib.org/api/pyplot_api.html?highlight=tricontour#matplotlib.pyplot.tricontour

plt.tricontour(x, y, z) 

,或者如果您需要查看基礎網:

import matplotlib.tri as mtri 
triang = mtri.Triangulation(x, y) 
plt.tricontour(triang, z) 
plt.triplot(triang) 

在你的情況下,三角實際上是在減少到3個三角形,因爲你有重複點,因此最多隻能選擇一個唯一的z值用於相同的位置。你可以更好地看到使用tricontourf會發生什麼,以填充輪廓。重複的點也解釋了爲什麼一個插值程序可能會與此數據集的煩惱......

現在,如果你隨機選擇爲每個4個數據點的任意1個Z值,你可以做

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.tri as mtri 

x = np.array([520.99012099, 652.23665224, 800., 0.]) 
y = np.array([0., 379.47214076, 437.53665689, 600.]) 
z = np.array([45.3, 57.8, 57.8, 57.8]) 

triang = mtri.Triangulation(x, y) 
refiner = mtri.UniformTriRefiner(triang) 
refi_triang, refi_z = refiner.refine_field(z, subdiv=4) 

levels = np.linspace(45, 61, 33) 

CS_colors = plt.tricontourf(refi_triang, refi_z, levels=levels) 
plt.triplot(triang, color="white") 
plt.colorbar() 

CS_lines = plt.tricontour(refi_triang, refi_z, levels=levels, colors=['black']) 
plt.clabel(CS_lines, CS_lines.levels, inline=True, fontsize=10) 

plt.show() 

enter image description here

+0

如果我只是做plt.tricontour(x,y,z)我得到一個空白圖像,底層網格顯示爲一個三角形:http://postimg.org/image/xczqqcpcf/ – SamMaj

+0

我剛剛看到您的評論並更新了答案。 – GBy

+0

謝謝GBy我知道,以下代碼示例後,如果我然後修改我的數據具有唯一的x,y座標,然後我得到這個http://postimg.org/image/6o2hne2fn/看起來更好,我認爲(雖然我實際上不知道它應該是什麼樣子!)如果我然後嘗試使用plt.tricontour(x,y,z)創建一個輪廓,我可以獲取這個http://postimg.org/image/ti01adgib/0233c1ac/ how我現在真的得到一個輪廓? – SamMaj

0

您確定網格中的所有條目都是NaN。要驗證,運行這段代碼

nan = 0 
notnan = 0 
for index,x in np.ndenumerate(zi): 
    if not np.isnan(x): 
     notnan+=1 
    else: 
     nan+=1 

print 'nan ', nan 
print 'not nan', notnan 
print 'sum ', nan+notnan 
print 'shape ', zi.shape 

可以使用命令繪製子曰:

plt.imshow(zi)