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 0x7f03d744d750>
_images/d9f9aefdfb99237399d6a9e392e4a10d213da3342e7e4f974a1c629babdf8c1b.png
air.cf.isel(T=1, Y=[0, 1, 2]).cf.plot(x="longitude", hue="latitude")
[<matplotlib.lines.Line2D at 0x7f03cf39be80>,
 <matplotlib.lines.Line2D at 0x7f03cf39beb0>,
 <matplotlib.lines.Line2D at 0x7f03cf3d00d0>]
_images/ff39d8f8ce1b5779f4f2961fa69bbfe32ba723ba7ff238c054d91179c394cd56.png
air.cf.plot(x="longitude", y="latitude", col="T")
<xarray.plot.facetgrid.FacetGrid at 0x7f03d75ec940>
_images/a3a4aad5ccdb2f939b7b2e1e7b7068e784d416b3fe0d7cbfbe35a8e443eb1ad6.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 0x7f03cd87de40>
_images/0ddcf463c2bb7fc1fac6fe0d27938f03c91b4943ff4f0d9f085d6dc2189d7492.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 0x7f03cd770100>
_images/b93221b71962e5d4192883d916007b6ac609245801b37e29a2818a4b695f2ac5.png