2016-07-18 54 views
0

我正在研究一個Python代碼來繪製Eddy Kinetic Energy。我對python相當陌生,而且對於我遇到的錯誤感到困惑。我並不擔心在地圖上繪製我的數據,我只想看看我是否可以將其繪製成圖。這裏是我的代碼和錯誤:爲什麼我得到這個錯誤:TypeError:輸入必須是二維數組

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.cm as cm 
from pylab import * 
from netCDF4 import Dataset 
from mpl_toolkits.basemap import Basemap 
import matplotlib.cm as cm 

from mpl_toolkits.basemap import shiftgrid 
test = Dataset('p.34331101.atmos_daily.nc', 'r') 

lat = test.variables['lat'][:] 
lon = test.variables['lon'][:] 
level = test.variables['level'][5] 
time = test.variables['time'][:] 
u = test.variables['ucomp'][:] 
v = test.variables['vcomp'][:] 
temp = test.variables['temp'][:] 

print(lat.shape) 
print(u.shape) 
#uz = np.reshape(u, (30, 26, 90)) 
uzm = np.nanmean(u, axis=3) 

#vz = np.reshape(v, (30, 26, 90)) 
vzm = np.nanmean(v, axis=3) 
print(uzm.shape) 

ustar = u-uzm[:,:,:,np.newaxis] 
vstar = v-vzm[:,:,:,np.newaxis] 

EKE = np.nanmean(.5*(ustar**2 + vstar**2), axis=3) 

EKE1 = np.asarray(EKE) 
%matplotlib inline 

print(EKE.shape) 

levels=[-10, -5, 0, 5, 10] 
plt.contour(EKE[1,1,:]) 
#EKE is time, level, lat and the shape is (30, 26, 90) 

類型錯誤:輸入必須是一個二維數組。

回答

0

Bret,如果您在錯誤中包含更多信息,您可能會獲得更多幫助,您是否沒有看到行號?

我會冒險猜測你的問題是將一維數組傳遞給contour()。這有時似乎與直覺相反,但當你在一個索引中指定單個值時,numpy會自動減小這些維度。

即嘗試

print(EKE.shape) 
print(EKE[1,1,:].shape) 
print(EKE[1:2,1:2,:].shape) 
相關問題