1

I have a mean precipitation netCDF file with the following characteristics:

<xarray.Dataset>
Dimensions:  (rlat: 412, rlon: 424)
Coordinates:
    lat      (rlat, rlon) float64 21.99 22.03 22.07 22.11 ... 66.81 66.75 66.69
    lon      (rlat, rlon) float64 -10.06 -9.964 -9.864 ... 64.55 64.76 64.96
  * rlat     (rlat) float64 -23.38 -23.26 -23.16 -23.05 ... 21.61 21.73 21.83
  * rlon     (rlon) float64 -28.38 -28.26 -28.16 -28.05 ... 17.93 18.05 18.16
Data variables:
    pr       (rlat, rlon) float32 0.0001465 0.000145 ... 0.007854 0.004692
Attributes: (12/22)
    CDI:                            Climate Data Interface version 1.3.2
    Conventions:                    CF-1.6
    NCO:                            4.4.2
    CDO:                            Climate Data Operators version 1.3.2 (htt...
    contact:                        Fredrik Boberg, Danish Meteorological Ins...
    creation_date:                  2019-10-15 18:05:48
    ...                             ...
    rcm_version_id:                 v1
    project_id:                     CORDEX
    CORDEX_domain:                  EUR-11
    product:                        output
    tracking_id:                    hdl:21.14103/a879aaf7-ddeb-436a-96fd-b717...
    c3s_disclaimer:                 This data has been produced in the contex...

I am trying the plot function mentioned in the xarray package documentation https://docs.xarray.dev/en/stable/user-guide/plotting.html

out_ds.plot()

but it produces the following error:

Dataset.plot cannot be called directly. Use an explicit plot method, e.g. ds.plot.scatter(...)
1
  • It would help if you provide a link to the dataset... Commented Dec 18, 2022 at 15:25

2 Answers 2

1

With EOmaps this should do the job:

# Create a dummy-NetCDF file
import xarray as xar
import pandas as pd
import numpy as np

lon, lat = np.meshgrid(np.linspace(-10, 64, 412), np.linspace(21, 66, 424))
pr = np.random.normal(0,1, lon.shape)
ncdata = pd.DataFrame(dict(lon=lon.flat, lat=lat.flat, pr=pr.flat)
                      ).set_index(["lon", "lat"]).to_xarray()

print(ncdata)
# Dimensions:  (lon: 100, lat: 100)
# Coordinates:
#   * lon      (lon) float64 -10.0 -9.253 -8.505 -7.758 ... 61.76 62.51 63.25 64.0
#   * lat      (lat) float64 21.0 21.45 21.91 22.36 ... 64.64 65.09 65.55 66.0
# Data variables:
#     pr       (lon, lat) float64 0.1526 2.008 0.2311 ... -0.6103 -0.6002 -0.7471
from eomaps import Maps
m = Maps(crs=4326)
m.add_feature.preset.coastline()

m2 = m.new_layer_from_file.NetCDF(
        ncdata,
        coords=("lon", "lat"),
        shape="raster",
        parameter="pr",
        data_crs=4326,        
        )
Sign up to request clarification or add additional context in comments.

Comments

0

The direct plotting options with dataset out_ds are very limited, basically you can only do either quiver, streamplot or scatter and these are most likely not the kind of maps you need to do. Therefore I suggest making the plot using matplotlib and either pcolormesh for colored values of precipitation or contourf for filled contours (most of the cases I prefer pcolormesh to visualize the data).

Here is the code (I generated Dataset with lon, lat and variable pr) and below is the plotting routine:

#!/usr/bin/env ipython
# --------------------
import numpy as np
import xarray as xr
# -------------------
# make data:
nx,ny = 20,20;
lon = np.linspace(9,30,nx);
lat = np.linspace(54,66,ny);
prec = np.random.random((ny,nx))
out_ds = xr.Dataset(data_vars=dict(pr=(['lat','lon'],prec)),coords=dict(lon=(["lon"],lon),lat=(["lat"],lat)));
# -----------------------------------------------------
import matplotlib as mpl;
mpl.rcParams['font.size'] = 20
import matplotlib.pylab as plt

fig = plt.figure(figsize=(15,15));ax = fig.add_subplot(111);
p0 = ax.pcolormesh(out_ds.lon,out_ds.lat,out_ds.pr,vmin=0.0,vmax=1.0);
cb = plt.colorbar(p0,ax=ax)
plt.show()

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.