Python中打印命令的尺寸错误

2024-06-12 09:33:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个图,它是风速数据的垂直横截面,因此在x轴上有纬度坐标,y轴有垂直水平

我计算了一个风速,它有一个纬度分量和一个垂直分量。当我打印它给出的形状(37,81)时,我知道这个数据中有37个级别,所以我假设这意味着37是垂直分量,81是纬度分量。然而,我的问题是,我希望这个变量的81和37是相反的,所以37是y轴。有没有办法交换这个变量的两个组成部分

这是我到目前为止所做的代码:

    # read tos
f = Dataset('data/era5_u_wind_vertical_levels_1979_2018_DJF.nc', mode='r')
lons = f.variables['longitude'][0]
lats = f.variables['latitude'][:]
levs = f.variables['level'][:]
field_u = f.variables['u'][0,:,:,0]
field_uu = f.variables['u'].units
f.close()

f = Dataset('data/era5_v_wind_vertical_levels_1979_2018_DJF.nc', mode='r')
#lons = f.variables['longitude'][:]
#lats = f.variables['latitude'][:]
field_v = f.variables['v'][0,:,:,0]
field_vv = f.variables['v'].units
f.close()

print np.shape(field_u), np.shape(field_v)
print lons

#steps = 50
#lonsnew = np.linspace(lon0, lon1, steps)
#latsnew = np.linspace(lat0, lat1, steps)

# compute absolute windspeed
wspd = np.sqrt(np.square(field_v) + np.square(field_u))
print wspd.shape

# test
print np.min(wspd), np.max(wspd)
print lats.shape, levs.shape, wspd.shape

#create lat/levs grid
[lats, levs] = np.meshgrid(levs, lats)
print lats.shape, levs.shape, wspd.shape

# define countour levels and plot filled contours
levels = np.arange(0, 21, 1)
cmap = plt.cm.gist_rainbow_r
norm = colors.BoundaryNorm(levels, cmap.N)

# actual plotting command
myplot = m.pcolormesh(lats, levs, wspd, norm=norm, cmap=cmap)

我得到的错误是: 类型错误:C(37,81)的尺寸与X(37)和/或Y(81)不兼容;请参阅帮助(pcolormesh)


Tags: normfieldnpvariablessteps分量cmapprint