为什么我收到此错误:TypeError:输入必须是2D数组

时间:2016-07-18 20:25:45

标签: python-2.7 contour

我正在编写一个python代码来绘制Eddy Kinetic Energy。我对python很新,我对我遇到的错误感到困惑。我还没有担心在地图上绘制我的数据,我只是想知道我是否可以将它绘制成情节。这是我的代码和错误:

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)

TypeError:输入必须是2D数组。

1 个答案:

答案 0 :(得分:0)

布雷特,如果你在错误中包含更多信息,你可能会得到更多的帮助,你没有得到一个行号吗?

我猜测你的问题是将1D数组传递给contour()。这有时似乎是违反直觉的,但是当您在索引中指定单个值时,numpy会“自动”减少维度。

即。试试

print(EKE.shape)
print(EKE[1,1,:].shape)
print(EKE[1:2,1:2,:].shape)