plotting quantified data

[1]:
import xarray as xr

# to be able to read unit attributes following the CF conventions
import cf_xarray.units  # must be imported before pint_xarray
import pint_xarray
from pint_xarray import unit_registry as ureg

xr.set_options(display_expand_data=False)
[1]:
<xarray.core.options.set_options at 0x7efe774d7a00>

load the data

[2]:
ds = xr.tutorial.open_dataset("air_temperature")
data = ds.air
data
[2]:
<xarray.DataArray 'air' (time: 2920, lat: 25, lon: 53)>
[3869000 values with dtype=float32]
Coordinates:
  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
  * lon      (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
  * time     (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
Attributes:
    long_name:     4xDaily Air temperature at sigma level 995
    units:         degK
    precision:     2
    GRIB_id:       11
    GRIB_name:     TMP
    var_desc:      Air temperature
    dataset:       NMC Reanalysis
    level_desc:    Surface
    statistic:     Individual Obs
    parent_stat:   Other
    actual_range:  [185.16 322.1 ]

quantify the data

Note: this example uses the data provided by the xarray.tutorial functions. As such, the units attributes follow the CF conventions, which pint does not understand by default. To still be able to read them we are using the registry provided by cf-xarray.

[3]:
quantified = data.pint.quantify()
quantified
[3]:
<xarray.DataArray 'air' (time: 2920, lat: 25, lon: 53)>
[K] 241.2 242.5 243.5 244.0 244.09999 ... 297.38998 297.19 296.49 296.19 295.69
Coordinates:
  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
  * lon      (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
  * time     (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
Attributes:
    long_name:     4xDaily Air temperature at sigma level 995
    precision:     2
    GRIB_id:       11
    GRIB_name:     TMP
    var_desc:      Air temperature
    dataset:       NMC Reanalysis
    level_desc:    Surface
    statistic:     Individual Obs
    parent_stat:   Other
    actual_range:  [185.16 322.1 ]

work with the data

[4]:
monthly_means = quantified.pint.to("degC").sel(time="2013").groupby("time.month").mean()
monthly_means
[4]:
<xarray.DataArray 'air' (month: 12, lat: 25, lon: 53)>
[°C] -28.68323 -28.486452 -28.479755 -28.668543 ... 24.353071 24.25759 24.215012
Coordinates:
  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
  * lon      (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
  * month    (month) int64 1 2 3 4 5 6 7 8 9 10 11 12

Most operations will preserve the units but there are some which will drop them (see the duck array integration status page). To work around that there are unit-aware versions on the .pint accessor. For example, to select data use .pint.sel instead of .sel:

[5]:
monthly_means.pint.sel(
    lat=ureg.Quantity(4350, "angular_minute"),
    lon=ureg.Quantity(12000, "angular_minute"),
)
[5]:
<xarray.DataArray 'air' (month: 12)>
[°C] -26.076784 -31.216885 -22.494358 ... -3.8546731 -14.511912 -21.413227
Coordinates:
    lat      float32 4.35e+03
    lon      float32 1.2e+04
  * month    (month) int64 1 2 3 4 5 6 7 8 9 10 11 12

plot

xarray’s plotting functions will cast the data to numpy.ndarray, so we need to “dequantify” first.

[6]:
monthly_means.pint.dequantify(format="~P").plot.imshow(col="month", col_wrap=4)
[6]:
<xarray.plot.facetgrid.FacetGrid at 0x7efe760dce50>
../_images/examples_plotting_12_1.png