Plotting is where cf_xarray really shines in our biased opinion.

from cf_xarray.datasets import airds

air = airds.air
             CF Axes: * X: ['lon']
                      * Y: ['lat']
                      * T: ['time']
                        Z: n/a

      CF Coordinates: * longitude: ['lon']
                      * latitude: ['lat']
                      * time: ['time']
                        vertical: n/a

       Cell Measures:   area: ['cell_area']
                        volume: n/a

      Standard Names: * latitude: ['lat']
                      * longitude: ['lon']
                      * time: ['time']

              Bounds:   n/a

       Grid Mappings:   n/a


Only DataArray.plot is currently supported.

Using CF standard names

Note the use of "latitude" and "longitude" (or "X" and "Y") in the following as a “standard” substitute for the dataset-specific "lat" and "lon" variables.

air.isel(time=0).cf.plot(x="X", y="Y")
<matplotlib.collections.QuadMesh at 0x7f0dfb3ea1d0>
_images/fe0e7cdade3a691e819959d901321ec7d1499723f8132b76b1a9e69ea8b9f18c.png, Y=[0, 1, 2]).cf.plot(x="longitude", hue="latitude")
[<matplotlib.lines.Line2D at 0x7f0df3168be0>,
 <matplotlib.lines.Line2D at 0x7f0df3168c10>,
 <matplotlib.lines.Line2D at 0x7f0df3168d00>]
_images/4a19d7c444a320fe65e657ea8c182e4eb47857d1dd17c7d1e0fb8e86a8ba1974.png"longitude", y="latitude", col="T")
<xarray.plot.facetgrid.FacetGrid at 0x7f0e32ce5510>

Automatic axis placement

Now let’s create a fake dataset representing a (x,z) cross-section of the ocean. The vertical coordinate here is “pressure” which increases downwards. We follow CF conventions and mark pres as axis: Z, positive: "down" to indicate these characeristics.

import matplotlib as mpl
import numpy as np
import xarray as xr

ds = xr.Dataset(
        "pres": ("pres", np.arange(20), {"axis": "Z", "positive": "down"}),
        "x": ("x", np.arange(50), {"axis": "X"})
ds["temp"] = 20 * xr.ones_like(ds.x) *  np.exp(- ds.pres / 30)
             CF Axes: * X: ['x']
                      * Z: ['pres']
                        Y, T: n/a

      CF Coordinates: * vertical: ['pres']
                        longitude, latitude, time: n/a

       Cell Measures:   area, volume: n/a

      Standard Names:   n/a

              Bounds:   n/a

       Grid Mappings:   n/a

The default xarray plot has some deficiencies

<matplotlib.collections.QuadMesh at 0x7f0df1609990>

cf_xarray can interpret attributes to make two decisions:

  1. That pres should be the Y-Axis

  2. Since pres increases downwards (positive: "down"), the axis should be reversed so that low pressure is at the top of the plot. Now we have a more physically meaningful figure where warmer water is at the top of the water column!
<matplotlib.collections.QuadMesh at 0x7f0df31d88e0>