问题描述
我想知道如何在地图上绘制来自带有 cartopy 的 NetCDF 文件的变量“r”(相对湿度)的数据?
enter code here
from netCDF4 import Dataset
import cartopy.crs as ccrs
import numpy as np
import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ds = Dataset('relative_humidityEne_Dic2003_2020CMAS.nc','r')
lon = ds.variables["longitude"]
lat = ds.variables["latitude"]
level = ds.variables["level"]
time = ds.variables["time"]
r = ds.variables["r"]
ax = plt.axes(projection=ccrs.PlateCarree())
r_ds = ds.variables['r'][0,:,:]
plt.contourf(lon,lat,r_ds,transform=ccrs.PlateCarree())
ax.coastlines()
plt.show()
root group (NETCDF3_64BIT_OFFSET data model,file format NETCDF3):
Conventions: CF-1.6
history: 2021-02-19 10:03:12 GMT by grib_to_netcdf-2.16.0: /opt/ecmwf/eccodes/bin/grib_to_netcdf -S param -o /cache/data7/adaptor.marsdev.internal-1613728991.1847723-6796-17-440f19fd-bb8f-478f-bc36-959e3e9d9c42.nc /cache/tmp/440f19fd-bb8f-478f-bc36-959e3e9d9c42-adaptor.marsdev.internal-1613728966.210842-6796-20-tmp.grib
dimensions(sizes): longitude(480),latitude(241),level(8),time(53)
variables(dimensions): float32 longitude(longitude),float32 latitude(latitude),int32 level(level),int32 time(time),int16 r(time,level,latitude,longitude)
groups:
dict_keys(['longitude','latitude','level','time','r'])
las dimenciones variable r es: ('time','longitude')
(53,8,241,480)
----------------------------------------------------------------------
<class 'netCDF4._netCDF4.Variable'>
int16 r(time,longitude)
scale_factor: 0.001964639476266317
add_offset: 64.43876426873042
_FillValue: -32767
missing_value: -32767
units: %
long_name: Relative humidity
standard_name: relative_humidity
unlimited dimensions:
current shape = (53,480)
filling on
las dimenciones variable r es: ('time',480)
############################################ ########### 消息错误
回溯(最近一次调用最后一次): 文件“/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py”,第 3331 行,在 run_code 中 exec(code_obj,self.user_global_ns,self.user_ns) 文件“”,第 1 行,在 runfile('/home/leo/Documentos/Universidad/Trabajo_de_investigación/PerfilesverticalesContaminantesAtmosfera/Datos/readNetdcf42003_2020CMAS.py',wdir='/home/leo/Documentos/Universidad/Trabajo_de_filesémonsverticalsAtmosfera/Datos/readNetdcf42003_2020CMAS.py' 运行文件中的文件“/snap/pycharm-professional/230/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py”,第 197 行 pydev_imports.execfile(filename,global_vars,local_vars) # 执行脚本 文件“/snap/pycharm-professional/230/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py”,第 18 行,在 execfile 中 exec(compile(contents+"\n",file,'exec'),glob,loc) 文件“/home/leo/Documentos/Universidad/Trabajo_de_investigación/PerfilesverticalesContaminantesAtmosfera/Datos/readNetdcf42003_2020CMAS.py”,第65行,在 plt.contourf(lon,变换=ccrs.PlateCarree()) 文件“/home/leo/.local/lib/python3.8/site-packages/matplotlib/pyplot.py”,第2577行,在contourf __ret = gca().contourf( 文件“/home/leo/.local/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py”,第 321 行,包装器 返回 func(self,*args,**kwargs) 文件“/home/leo/.local/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py”,第1586行,在contourf中 结果 = matplotlib.axes.Axes.contourf(self,**kwargs) 文件“/home/leo/.local/lib/python3.8/site-packages/matplotlib/init.py”,第1447行,在内部 返回 func(ax,*map(sanitize_sequence,args),**kwargs) 文件“/home/leo/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py”,第6335行,在contourf 轮廓 = mcontour.QuadContourSet(self,**kwargs) init 中的文件“/home/leo/.local/lib/python3.8/site-packages/matplotlib/contour.py”,第 816 行 kwargs = self._process_args(*args,**kwargs) 文件“/home/leo/.local/lib/python3.8/site-packages/matplotlib/contour.py”,第 1430 行,在 _process_args x,y,z = self._contour_args(args,kwargs) 文件“/home/leo/.local/lib/python3.8/site-packages/matplotlib/contour.py”,第 1488 行,在 _contour_args x,z = self._check_xyz(args[:3],kwargs) 文件“/home/leo/.local/lib/python3.8/site-packages/matplotlib/contour.py”,第 1514 行,在 _check_xyz x = np.asarray(x,dtype=np.float64) 文件“/usr/local/lib/python3.8/dist-packages/numpy/core/_asarray.py”,第102行,在asarray中 返回数组(a,dtype,copy=False,order=order) 类型错误:array() 不接受任何参数(给定 1 个)
解决方法
您应该可以使用我的 ncplot 包 (https://pypi.org/project/ncplot/) 执行此操作:
from ncplot import view
view('relative_humidityEne_Dic2003_2020CMAS.nc','r')
这将创建一个交互式绘图。