与 python 中的绘图 nc 文件相关的错误“输入 z 必须是 2D,而不是 3D”

问题描述 投票:0回答:1

我使用 500 hPa NOAA 再分析版本 3 绘制了特定时期内垂直速度的平均距平 (https://www.psl.noaa.gov/mddb2/makePlot.html?variableID=142267)。当使用以下代码时,我收到“输入 z 必须是 2D,而不是 3D”错误:

from netCDF4 import Dataset as NetCDFFile 
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
fig = plt.figure(figsize=(10,9), constrained_layout=True ) 
ax = fig.add_subplot(111).set_title('b1' , size=10 , fontweight='bold') #(nrow , ncol ,nfig)
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0 )
nc1 = NetCDFFile('C:/Users/user/Desktop/project/nc2024Habib/final-mjo/obs1-vv-ac.nc')
lat = nc1.variables['lat'][:]
lon = nc1.variables['lon'][:]
#z=nc1.variables['level'][0]
omega = nc1.variables['omega'][:]
map= Basemap    (projection='merc',llcrnrlon=0.,llcrnrlat=10.,urcrnrlon=80.,urcrnrlat=45.,resolution='i',fix_aspect=False)  
map.drawcoastlines()
map.drawstates()
map.drawcountries()
map.drawlsmask(land_color='Linen', ocean_color='#CCFFFF')
map.drawcounties() 
parallels = np.arange(15.,45.,5.)
meridians = np.arange(30.,70.,5.)
map.drawparallels(parallels,labels=[1,0,0,0],fontsize=10 , linewidth=0.9,dashes=[4,9900], fontweight='bold' ,fontstyle='oblique')
map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10, linewidth=0.9,dashes=[4,9900] , fontweight='bold', fontstyle='oblique')
lons,lats= np.meshgrid(lon-180,lat )
X,Y = map(lons,lats)
clevs1 = np.arange(-0.02,0.01,0.005)
cs1 = map.contourf(X,Y, omega[0,:,:],clevs1,cmap='RdBu_r')
plt.show()

如果有人能帮助我解决这个错误,我将不胜感激

multidimensional-array raster netcdf contourf
1个回答
0
投票

打印

omega[0,:,:].ndim
omega[0,:,:].shape
;它将是一个 3 维数组(如错误所述),而您应该将 2D 数组传递给
contourf()

最有可能的是,

omega
的维度为时间/高度/纬度/经度(按某种顺序),因此您应该再切一个,例如:

cs1 = map.contourf(X, Y, omega[0,0,:,:], clevs1, cmap='RdBu_r')
© www.soinside.com 2019 - 2024. All rights reserved.