I would like to know how I can plot on a map the data from the variable "r" (relative humidity) of a NetCDF file with cartopy?
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', 'level', 'latitude', 'longitude')
(53, 8, 241, 480)
----------------------------------------------------------------------
<class 'netCDF4._netCDF4.Variable'>
int16 r(time, level, latitude, 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, 8, 241, 480)
filling on
las dimenciones variable r es: ('time', 'level', 'latitude', 'longitude')
(53, 8, 241, 480)
######################################################### menssage error
Traceback (most recent call last): File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 3331, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in runfile('/home/leo/Documentos/Universidad/Trabajo_de_investigación/PerfilesVerticalesContaminantesAtmosfera/Datos/readNetdcf42003_2020CMAS.py', wdir='/home/leo/Documentos/Universidad/Trabajo_de_investigación/PerfilesVerticalesContaminantesAtmosfera/Datos') File "/snap/pycharm-professional/230/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "/snap/pycharm-professional/230/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/leo/Documentos/Universidad/Trabajo_de_investigación/PerfilesVerticalesContaminantesAtmosfera/Datos/readNetdcf42003_2020CMAS.py", line 65, in plt.contourf(lon, lat, r_ds, transform=ccrs.PlateCarree()) File "/home/leo/.local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2577, in contourf __ret = gca().contourf( File "/home/leo/.local/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 321, in wrapper return func(self, *args, **kwargs) File "/home/leo/.local/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 1586, in contourf result = matplotlib.axes.Axes.contourf(self, *args, **kwargs) File "/home/leo/.local/lib/python3.8/site-packages/matplotlib/init.py", line 1447, in inner return func(ax, *map(sanitize_sequence, args), **kwargs) File "/home/leo/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 6335, in contourf contours = mcontour.QuadContourSet(self, *args, **kwargs) File "/home/leo/.local/lib/python3.8/site-packages/matplotlib/contour.py", line 816, in init kwargs = self._process_args(*args, **kwargs) File "/home/leo/.local/lib/python3.8/site-packages/matplotlib/contour.py", line 1430, in _process_args x, y, z = self._contour_args(args, kwargs) File "/home/leo/.local/lib/python3.8/site-packages/matplotlib/contour.py", line 1488, in _contour_args x, y, z = self._check_xyz(args[:3], kwargs) File "/home/leo/.local/lib/python3.8/site-packages/matplotlib/contour.py", line 1514, in _check_xyz x = np.asarray(x, dtype=np.float64) File "/usr/local/lib/python3.8/dist-packages/numpy/core/_asarray.py", line 102, in asarray return array(a, dtype, copy=False, order=order) TypeError: array() takes no arguments (1 given)