Plotting fields, grids and sections#

Introduction#

The xoa.plot module has functions that draw maps, grids, sections and series from xarray objects, and finds what to draw with xoa.meta. The lower level xoa.core.plot module draws from plain numpy arrays and matplotlib axes.

This guide explains how the functions find their inputs, what their options mean, and how to use the low level routines. The tutorials show them at work, with figures:

In [1]: import numpy as np

In [2]: import xarray as xr

In [3]: import matplotlib.pyplot as plt

In [4]: import xoa

In [5]: from xoa import plot as xplot

In [6]: from xoa.core import plot as cplot

The main xoa accessors give access to the high level functions through the plot subaccessor, with the array or dataset as the first argument: da.xoa.plot.field(), grid(), section(), stick(), rose() and taylor(ref).

Two layers#

High level (xarray)

Low level (numpy)

Role

plot_field()

(xarray plotting)

A field on a map, with contour overlays

plot_grid()

plot_mesh()

Edges and centers, bathymetry or resolution of a grid

plot_section()

plot_depth_section()

A vertical section with a variable depth

plot_stick()

plot_sticks()

A current time series as sticks

plot_vectors()

plot_vectors()

Currents on a map as under-sampled arrows, with a reference arrow and optional uncertainty ellipses

plot_taylor()

plot_taylor()

A Taylor diagram of arrays against a reference

plot_rose()

plot_rose()

A wind or wave rose of direction and value frequencies

add_colorbar()

add_colorbar()

A shrunk and labelled colorbar

get_label()

"Long name [units]" from the meta-data

(re-exported)

create_map(), decorate_map(), add_land(), add_layer()

Decorated cartopy maps

The high level functions find the data and the labels, then delegate the drawing to the low level ones, which makes the latter usable on any array, with a plain matplotlib axes when no map is needed.

How inputs are found#

Names are never needed: variables and coordinates are identified from their names and attributes with the specifications of xoa.meta.

Function

What is searched

plot_field()

Longitude and latitude, with xoa.coords.get_lon() and xoa.coords.get_lat()

plot_grid()

Longitude and latitude, and the bathy and mask variables of a dataset

plot_section()

The vertical dimension, the depth (with its positive attribute), longitude and latitude

plot_stick()

The u and v variables of a dataset, and the time

plot_vectors()

Longitude and latitude of the grid of u

overlays of plot_field()

Any generic name that is given as a string, like "bathy"

The generic names are the ones of the current specifications, which can be tuned for your own files with xoa.meta.set_meta_specs(). This is how to see what a function will use:

In [7]: from xoa.meta import get_meta_specs

In [8]: ds = xr.Dataset(
   ...:     {
   ...:         "h": (("y", "x"), np.full((3, 4), 100.0),
   ...:               {"standard_name": "model_sea_floor_depth_below_geoid"}),
   ...:         "land": (("y", "x"), np.ones((3, 4)), {"standard_name": "land_binary_mask"}),
   ...:     },
   ...:     coords={
   ...:         "lon": ("x", np.arange(4.0), {"standard_name": "longitude"}),
   ...:         "lat": ("y", np.arange(3.0), {"standard_name": "latitude"}),
   ...:     },
   ...: )
   ...: 

In [9]: specs = get_meta_specs(ds)

In [10]: specs.search(ds, "bathy").name, specs.search(ds, "mask").name
Out[10]: ('h', 'land')

In [11]: xoa.coords.get_lon(ds).name
Out[11]: 'lon'

When a name is ambiguous, or not recognised, pass the arrays yourself: a data array for an overlay, or an array for the horizontal axis of a section.

Labels#

get_label() builds "Long name [units]": it takes long_name, then standard_name and then the name of the array, capitalizes the first letter and appends the units. Missing attributes are first completed from the meta specifications, so a bare temp array is labelled properly:

In [12]: xplot.get_label(xr.DataArray([1.0], dims="x", name="temp"))
Out[12]: 'Sea water in situ temperature [°C]'

In [13]: xplot.get_label(xr.DataArray([1.0], dims="x", attrs={"long_name": "my field", "units": "m"}))
Out[13]: 'My field [m]'

In [14]: xplot.get_label(xr.DataArray([1.0], dims="x", name="temp"), units=False)
Out[14]: 'Sea water in situ temperature'

The units are written in a nicer way by default (pretty=True): degC, Celsius and the like become °C, m s-1 becomes m s⁻¹, 1e-3 becomes psu, and the unit 1 of the dimensionless numbers is skipped.

In [15]: attrs = {"long_name": "speed", "units": "m s-1"}

In [16]: xplot.get_label(xr.DataArray([1.0], dims="x", attrs=attrs))
Out[16]: 'Speed [m s⁻¹]'

In [17]: xplot.get_label(xr.DataArray([1.0], dims="x", attrs=attrs), pretty=False)
Out[17]: 'Speed [m s-1]'

Maps#

Map functions need cartopy, which is only imported when a map is drawn.

  • transform is the coordinate system of the data and defaults to PlateCarree. The projection of the map, which defaults to Mercator, is the one of the axes. When a map is created, the projection can be a cartopy projection or its lower case name, like "mercator", "stereographic" or "ortho" (see get_projection()). plot_field() defaults to Mercator, and plot_grid() to the orthographic projection centered on the grid, which limits the deformation of the cells.

  • plot_field() and plot_grid() create the map when no axes is provided, from the extent of the data computed with xoa.geo.get_extent() (a margin can be added to the field). Their map_kw parameter holds the options of decorate_map() (gridlines, gridlines_labels_on, land, land_scale, coastlines, bbox), plus figsize and projection for the creation of the figure.

  • On axes that you provide, only the decoration keys are applied, so that panels of multi-panel figures can be created with their own projection.

  • A field must only have its horizontal dimensions: select the time and the level first.

Contour overlays#

The overlay_contours parameter of plot_field() draws contours over the field, for instance the coast or isobaths. True contours the field itself. A list gives one dictionary per layer, with the parameters of matplotlib.axes.Axes.contour() and a field key that is a data array, which may be on another grid, or a generic name that is searched in the dataset given by ds:

In [18]: import cmocean

In [19]: from xoa.core.grid import create_rotated_grid

In [20]: rgrid = create_rotated_grid(41, 31, -4.0, 47.5, 20.0, 4.0, 3.0)

In [21]: lon2d, lat2d = rgrid["lon"], rgrid["lat"]

In [22]: depth = 100 + 1500 * (lon2d - lon2d.min()) / np.ptp(lon2d)

In [23]: ds = xr.Dataset(
   ....:     {
   ....:         "temp": (("y", "x"), 12 + 3 * np.sin(lon2d) * np.cos(lat2d),
   ....:                  {"standard_name": "sea_water_temperature", "units": "degC"}),
   ....:         "h": (("y", "x"), depth, {"standard_name": "model_sea_floor_depth_below_geoid"}),
   ....:     },
   ....:     coords={
   ....:         "lon": (("y", "x"), lon2d, {"standard_name": "longitude"}),
   ....:         "lat": (("y", "x"), lat2d, {"standard_name": "latitude"}),
   ....:     },
   ....: )
   ....: 

In [24]: xplot.plot_field(
   ....:     ds.temp,
   ....:     cmap=cmocean.cm.thermal,
   ....:     overlay_contours=[dict(field="bathy", levels=[200, 1000], colors="0.4", linestyles="--")],
   ....:     ds=ds,
   ....: )
   ....: 
Out[24]: <cartopy.mpl.geocollection.GeoQuadMesh at 0x738be88f3110>
_images/indepth.plot.overlay.png

Background layers#

add_layer() adds a layer of a web map service (WMS or WMTS) under the data, and layer=True or layer="name" does it in decorate_map(). The layers are defined in the layers section of the xoa config file, one subsection per layer, and the plot.layer option sets the default one. The National Geographic map of Esri, natgeo, is available out of the box.

To add your own layer, which is a WMS here, edit your xoa.cfg file (see xoa.show_paths()) or load another one with xoa.load_options():

[plot]
layer = mylayer

[layers]
    [[mylayer]]
    kind = wms  # or wmts, for which url is the capabilities document
    url = https://my.server/wms?
    layer = name_of_the_layer
    attribution = "My credits"

The same layer can be defined from a dict with xoa.set_options("layers", mylayer={"kind": "wms", "url": ..., "layer": ...}). The plot.layer_* options set the opacity, the drawing order and the light grey attribution drawn in the lower right corner.

Warning

Quote the values that contain commas, like an attribution, or they are read as lists. A service that is not reachable raises a warning and the land is drawn instead, unless fallback=False. Check the terms of use of a service before publishing a figure.

Scale bar and compass#

add_scalebar() and add_compass() decorate a cartopy map, and plot_field(), plot_fields() and plot_grid() add them with scalebar=True and compass=True, or dicts of parameters.

  • The scale bar has a round length, 1, 2 or 5 times a power of ten, that is at most a fifth of the width of the axes, unless length is given. It is measured on the ground along the horizontal direction of the axes at the position of the bar, so that it accounts for the distortion of the projection.

  • The compass points to the north of the meridian at its position, which is not the vertical with a conic or orthographic projection. The "simple" style is an arrow and the "fancy" one a rose.

  • Both are placed in a corner with loc. effect draws a "shadow", a "glow" or both (["shadow", "glow"]) below them, and effect_kw sets the blur width, the alpha, the color and the shadow offset, for each effect if it is a dict of dicts.

  • The length and the angle are computed when they are added: set the extent before.

In [25]: xplot.plot_field(
   ....:     ds.temp,
   ....:     cmap=cmocean.cm.thermal,
   ....:     scalebar=dict(loc="lower right", effect="shadow"),
   ....:     compass=dict(style="fancy", effect="shadow"),
   ....: )
   ....: 
Out[25]: <cartopy.mpl.geocollection.GeoQuadMesh at 0x738be71c2210>
_images/indepth.plot.decorations.png

Fancy frame#

add_fancy_map_frame() adds a GMT-like frame of alternating black and white segments around a map. It is requested at the creation of a map with fancy=True or a dict of parameters, like map_kw=dict(fancy=dict(width=8)) with the plot functions.

  • The segments are cut at the longitudes and latitudes of the gridlines, with the cartopy longitude and latitude locators for all projections. Gridlines without lines nor labels are added if there are none. The tick labels are moved away from the map by the width of the frame.

  • It needs a projection whose meridians and parallels are parallel to the edges of the map, like PlateCarree or Mercator, and raises an error otherwise.

In [26]: xplot.plot_field(ds.temp, cmap=cmocean.cm.thermal, map_kw=dict(fancy=True))
Out[26]: <cartopy.mpl.geocollection.GeoQuadMesh at 0x738be70aed50>
_images/indepth.plot.fancy.png

Colorbars#

All the colorbars of the module are shrunk (shrink=0.7, option plot.colorbar_shrink) and labelled with get_label(), so that they do not dwarf the maps.

  • The functions that make a colorbar accept cbar_kwargs (plot_field(), plot_section()) or colorbar_kwargs (plot_ts()), which override the defaults, and add_colorbar=False to skip it.

  • For several panels, plot_fields() makes the figure of maps from a dict of titles and fields, or a list of fields, and colorbar="shared" adds a single colorbar. Per-field parameters are given as (field, kwargs) tuples:

In [27]: fields = {f"{d:+d}": ds.temp + d for d in (-2, 0, 2)}

In [28]: fig, axes = xplot.plot_fields(fields, colorbar="shared", figsize=(11, 3.6), vmin=8, vmax=18,
   ....:                               cmap=cmocean.cm.thermal)
   ....: 
_images/indepth.plot.colorbar.png
  • cmap="auto" chooses the colormap from the sign of the data, among the plot.cmapdiv, plot.cmappos and plot.cmapneg options, with symmetric limits for the diverging one. See the tutorial Set the options of xoa.

  • To lay out the axes yourself, skip the colorbars of plot_field() and add a shared one with add_colorbar(), which takes the axes and the array that gives the label.

  • For publications, panel_labels=True of plot_fields() labels the maps “a)”, “b)”… A string sets the style, like "({A})", and a dict the other parameters of PanelLabeler. add_panel_labels() does the same on any axes, and the PanelLabeler instance goes on with its sequence from one figure to the next.

Colormaps and effects#

xoa.color.CmapAdapter adapts a colormap to the data: "piv0" centers a diverging colormap on zero, whatever the limits of the data, and "cyc360" is for circular quantities like directions. xoa.plot.add_shadow() and xoa.plot.add_glow() make lines and text stand out. See the tutorial Colormaps adapted to the data and graphical effects.

Minimaps#

plot_minimap() and plot_double_minimap() show where the data are. With the parent argument, the minimap is an inset of the axes at loc ("upper right", "lower left"…) with a width size relative to the one of the parent, instead of a fixed place in the figure that may enlarge it or hide its content. See Plot fields and currents on maps.

The adaptive grid stride#

plot_grid() draws the edges and the centers of the cells. Edges are computed from the centers by xoa.core.grid.centers2edges(), and are extrapolated at the ends. Drawing every line of a large grid would only give a solid color, so the grid is under-sampled.

The stride is either "auto" (default), an integer or a (y, x) tuple:

  • In the "auto" mode, the spacing of adjacent centers is measured on the screen, once projected on the axes, and the stride is the smallest one that leaves min_spacing pixels (12 by default) between the lines drawn. It follows the size of the figure, the zoom, the projection and the rotation of the grid, and it is independent for each direction. The spacing is measured on a bounded number of lines, so that it is cheap for any grid.

  • The first and last edges are always drawn, and the others are evenly spread between them, so that no coarse cell is thin when the stride does not divide the number of cells.

  • When the grid is under-sampled, the centers that are drawn are the ones of the coarse cells that are delimited by the lines, and not a subset of the true centers, that would sit on the lines.

The low level get_strides() does the measure. On a plain axes whose data coordinates are pixels, the rule is easy to follow:

In [29]: lon, lat = np.meshgrid(np.linspace(0, 100, 101), np.linspace(0, 100, 101))

In [30]: fig = plt.figure(figsize=(5, 5), dpi=100)

In [31]: ax = fig.add_axes([0, 0, 1, 1])

In [32]: ax.set_xlim(0, 100)
Out[32]: (0.0, 100.0)

In [33]: ax.set_ylim(0, 100)
Out[33]: (0.0, 100.0)

# 5 pixels between points: 3 points are needed to get at least 12 pixels
In [34]: cplot.get_strides("auto", lon, lat, ax, min_spacing=12)
Out[34]: (3, 3)

In [35]: cplot.get_strides("auto", lon, lat, ax, min_spacing=5)
Out[35]: (1, 1)

In [36]: cplot.get_strides((2, 4), lon, lat)
Out[36]: (2, 4)

Without axes, the spacing can be measured on a web map, like those of Leaflet or OpenLayers, at a zoom level: the points are placed with get_webmercator_pixels(). This is how a web application undersamples the arrows of the currents for the view:

In [37]: lon, lat = np.meshgrid(np.arange(-5, -4.5, 0.01), np.arange(48, 48.3, 0.01))

In [38]: cplot.get_strides("auto", lon, lat, zoom=10, min_spacing=20)
Out[38]: (2, 3)

In [39]: cplot.get_strides("auto", lon, lat, zoom=13, min_spacing=20)
Out[39]: (1, 1)

The same grid is drawn with plot_grid(), with a stride that suits the figure:

In [40]: xplot.plot_grid(ds.temp, stride="auto", cmap=cmocean.cm.thermal)
Out[40]: <GeoAxes: >
_images/indepth.plot.grid.png

Other options of plot_grid():

  • kind: "mesh" (edges and centers, the default), "bathy" (the bathymetry found in a dataset, with the land masked, and the mesh if there is none) or "resolution" (the compute_center_resolution(), in km).

  • edges and centers switch the lines and points on or off. They are drawn by default with the mesh and not with the fields, but can be added over them.

  • strips, with n_cells, outlines the strips along the edges that xoa.grid.get_edge_extents() returns.

  • edge_kw and center_kw style the lines and the points.

  • On a staggered dataset, pass a data array, since the longitudes and latitudes are ambiguous. The "bathy" kind uses the grid of the bathymetry.

Sections#

plot_section() draws an array that has a vertical dimension and a single horizontal one. The depth may be 1D or vary along the section, as with terrain-following coordinates; when no depth coordinate is found, the vertical coordinate is used, so decode sigma coordinates first (see Sigma coordinates with xoa.sigma). The vertical axis is inverted when depths are positive down.

The x parameter selects the horizontal axis: None picks longitude or latitude from the one with the largest extent, "lon" and "lat" force one, "distance" is the distance in km along the section, and any array that broadcasts to the horizontal dimension is accepted.

In [41]: depths = xr.DataArray(
   ....:     np.linspace(0, 200, 30), dims="z", attrs={"standard_name": "ocean_depth", "positive": "down", "units": "m"}
   ....: )
   ....: 

In [42]: lons1d = xr.DataArray(
   ....:     np.linspace(-6, -2, 20), dims="x", attrs={"standard_name": "longitude", "units": "degrees_east"}
   ....: )
   ....: 

In [43]: sec = xr.DataArray(
   ....:     20 - depths.values[:, None] / 20 * (1 + 0.5 * np.sin(lons1d.values[None])),
   ....:     dims=("z", "x"),
   ....:     coords={"depth": depths, "lon": lons1d},
   ....:     attrs={"standard_name": "sea_water_temperature", "units": "degC"},
   ....: )
   ....: 

In [44]: fig, ax = plt.subplots(figsize=(7, 3.5), constrained_layout=True)

In [45]: xplot.plot_section(sec, ax=ax, cmap=cmocean.cm.thermal)
Out[45]: <matplotlib.collections.QuadMesh at 0x738be701a710>
_images/indepth.plot.section.png

Sticks#

plot_stick() shows a time series of currents, as sticks that start from a line, are oriented along the current and are as long as its speed. This is a compact way to show the rotation of tidal currents. Pass u and v, or a dataset that holds them, and use scale to change the lengths (the smaller, the longer). By default, the scale is computed by xoa.core.plot.get_stick_scale() so that the strong sticks, at the 99th percentile of the speed, span the fraction of the axes width given by the plot.stick_length option. A reference stick is drawn above the left corner of the axes, like the reference arrow of plot_vectors(): its value is rounded to 1, 2 or 5 times a power of ten by xoa.core.plot.get_quiverkey_value() and it is added with xoa.core.plot.add_quiverkey(), that you can use with any quiver. Pass key=False to remove it.

Uncertainty ellipses#

Pass ustd to plot_vectors(), with optionally vstd (ustd by default) and uvcorr (zero by default), to draw an ellipse at the tip of each arrow. The ellipse is the one of the covariance matrix of the components, with a radius of nsigma standard deviations. In 2D, the default nsigma=2.45 includes 95% of a gaussian distribution, while nsigma=2 only includes 86%. The standard deviations are under-sampled like the arrows, in the same units, and the ellipses are rotated like them when the projection of the map requires it. They have the color of the arrows, with a transparent face, unless the arrows are colored by speed. They are the ellipses attribute of the returned quiver, and ellipse_kwargs sets their style and a label for the legend.

At a lower level, xoa.core.plot.plot_vector_ellipses() adds them to any quiver whose angles are "uv" (the default) and whose scale is given with a scale_units among "width", "height", "dots", "inches", "x" and "y". All the values of pivot are supported. See Plot fields and currents on maps.

Taylor diagrams#

plot_taylor() summarizes how well arrays match a reference: the angle is the correlation, the radius is the standard deviation and the distance to the reference point is the centered root mean square difference (dashed contours). The ref argument is mandatory.

  • dim lists the dimensions where the statistics are computed. By default, all of them, which gives a single point per variable. The other dimensions create the points, labelled with their coordinates, and each variable of a dataset adds its own points.

  • normalize=True divides the standard deviations by the one of the reference, which is then at 1. It is needed when the points do not share the same reference.

  • labels are listed in the legend. With values, the markers are colored instead and a colorbar is added, and the labels are written next to the points.

  • markers is one marker, or one per point (cycled).

  • The diagram is a quarter of circle, and a half circle if some correlations are negative.

The tuning parameters are the ones of TaylorDiagram: rmax, rms_levels, corr_ticks, grid_kwargs, contour_kwargs and ref_kwargs. The returned diagram can receive more points with add_points(), using the diagram argument.

In [46]: rng = np.random.default_rng(0)

In [47]: obs = xr.DataArray(rng.normal(size=200), dims="time", name="obs")

In [48]: models = xr.Dataset({
   ....:     name: obs * a + rng.normal(scale=b, size=200)
   ....:     for name, a, b in [("m1", 1.0, 0.3), ("m2", 0.7, 0.6), ("m3", 1.3, 0.9)]
   ....: })
   ....: 

In [49]: xplot.plot_taylor(models, obs, normalize=True, markers=["o", "s", "^"])
Out[49]: <xoa.core.plot.TaylorDiagram at 0x738be74c6780>
_images/indepth.plot.taylor.png

Without a reference array, already normalized statistics are drawn with the low level plot_taylor(), whose ref_std is 1 by default:

In [50]: cplot.plot_taylor([0.9, 1.2, 0.7], [0.95, 0.8, -0.3], labels=["a", "b", "c"])
Out[50]: <xoa.core.plot.TaylorDiagram at 0x738be765fd10>
_images/indepth.plot.taylor_low.png

The statistics themselves come from xoa.core.stats.taylor_stats().

Wind and wave roses#

plot_rose() shows how often the wind or the waves come from each direction, with stacked bars for the bins of speed or height. The north is at the top and the angles are clockwise.

  • The sectors are centered on the cardinal directions, so that a northerly wind is not split in two sectors at 0°. Use nsector to change their number, 16 by default.

  • The frequencies are in percent of the samples that are valid in both the value and the direction. The ones that are masked with NaN are ignored, so mask the bad quality data before: flags may miss spikes.

  • edges are the lower edges of the bins of value. The last bin is open. They are rounded values chosen from the data by default.

  • Winds and waves usually come from a direction, while currents go to it. This does not change the plot: only the label of the direction does, so check the convention of your data.

  • With a dataset, the value and the direction are searched with xoa.meta, for instance hs and direction. When there are none, the components of the currents or of the wind are searched with xoa.dyn.get_u() and xoa.dyn.get_v().

  • plot_rose(u, v) makes the rose of the speed and of the direction that the flow goes to. The second argument is taken as a northward component when xoa.meta identifies it as such and it has no angle units, and cartesian=True or False forces the choice. Use from_=True for the direction it comes from, like the wind.

The tuning parameters are the ones of Rose: rmax, rlabel_angle and grid_kwargs, and of add_bars(): cmap, colors, label and legend. The binning alone is done by xoa.core.stats.rose_hist().

In [51]: rng = np.random.default_rng(0)

In [52]: wdir = rng.normal(250, 40, 2000) % 360

In [53]: wspd = xr.DataArray(rng.weibull(2, 2000) * 8, dims="time", attrs={"units": "m s-1"})

In [54]: xplot.plot_rose(wspd, xr.DataArray(wdir, dims="time"), nsector=12)
Out[54]: <xoa.core.plot.Rose at 0x738be74c68b0>
_images/indepth.plot.rose.png

Low level routines#

The routines of xoa.core.plot need no xarray and no map. The mesh of a grid is computed on its own, which is useful to check what will be drawn:

In [55]: lons, lats = np.meshgrid(np.arange(4.0), np.arange(3.0))

In [56]: segments, clon, clat = cplot.get_mesh(lons, lats)

In [57]: len(segments), segments[0]
Out[57]: 
(9,
 array([[-0.5, -0.5],
        [ 0.5, -0.5],
        [ 1.5, -0.5],
        [ 2.5, -0.5],
        [ 3.5, -0.5]]))

In [58]: segments, clon, clat = cplot.get_mesh(lons, lats, stride=(2, 2))

In [59]: len(segments), clon
Out[59]: 
(6,
 array([[0.5, 2.5],
        [0.5, 2.5]]))

and then drawn on any axes:

In [60]: fig, ax = plt.subplots(figsize=(3, 2.5))

In [61]: cplot.plot_mesh(ax, lons, lats, stride=1)
Out[61]: 
(<matplotlib.collections.LineCollection at 0x738be6dc2210>,
 <matplotlib.lines.Line2D at 0x738be6ec9010>)

In [62]: ax.set_aspect("equal")
_images/indepth.plot.mesh.png

The other routines are the same: plot_depth_section() takes the 2D arrays of the horizontal coordinates, depths and values, and plot_sticks() takes the positions and the components.