Plotting

Plotting is where cf_xarray really shines in our biased opinion.

from cf_xarray.datasets import airds

air = airds.air
air.cf
Coordinates:
             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

Tip

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 0x743304bf1fd0>
_images/840df7be491d10048ff0ce699f98bbeb1f745370d5d01501a077bd5dee148e82.png
air.cf.isel(T=1, Y=[0, 1, 2]).cf.plot(x="longitude", hue="latitude")
[<matplotlib.lines.Line2D at 0x743304927750>,
 <matplotlib.lines.Line2D at 0x743304927890>,
 <matplotlib.lines.Line2D at 0x7433049279d0>]
_images/7015fc1ecee2f6ccbd7a79b785340bea3260dbfd388f15a1eb868f528a4f4fac.png
air.cf.plot(x="longitude", y="latitude", col="T")
<xarray.plot.facetgrid.FacetGrid at 0x7433049bc2f0>
_images/e00db5a713b7cb001a31003a013cdf0ac17d2a80b8c727f228700e7b07108fd9.png

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(
    coords={
        "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)
ds.temp.cf
Coordinates:
             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

ds.temp.plot(cmap=mpl.cm.RdBu_r)
<matplotlib.collections.QuadMesh at 0x7433047e0550>
_images/6dac910fcb0c9e5029d0843f9934054d91dbe495d68192d81b7a66411be7ca23.png

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!

ds.temp.cf.plot(cmap=mpl.cm.RdBu_r)
<matplotlib.collections.QuadMesh at 0x7433046a8b90>
_images/9df9572222291cbe21a086dba41da0d3f02467de343a4d3f1324ed828dac9390.png