3. Plotting Data#

Hint

There are many other libraries for plotting self-labelled data, especially for pandas.DataFrame objects, which can easily be generated from a xarray.Dataset using e.g.:

ds = harvester.full_ds
df = ds.to_dataframe().reset_index()

Notably:

  • xarray itself has plotting functionality

  • pandas itself has plotting functionality

  • seaborn uses matlotlib for plotting dataframes

  • holoviews uses bokeh for plotting dataframes

  • hvplot builds on holoviews for plotting datasets

  • altair for dataframes

The plotting functionality in xyzpy has a few focuses:

  • Easily show the dependence of quantities on up to 4 parameters at once, including a colormapped third axis into the page , z=coo, even for histograms.

  • Provide the same interface to an interactive plotter for exploring data and a static plotter for creating high quality figures. This is done using bokeh and matplotlib. In general, to switch between the two add/remove the prefix “i” (e.g. iheatmap/heatmap).

  • Supply all options as keywords and just suggest the right one when mis-spelt so you don’t have to remember. Many options are shared between the matplotlib and bokeh backends (but since visually obvious, options do not error if unavailable).

  • Access by name to extra perceptually uniform colormaps from cmocean and colorcet if installed.

The following demonstrations show basic usage of all the plot types. These can be called through the Dataset accessor attribute .xyz.

First we’ll define and label a function to explore, and run some nice combos:

[1]:
%config InlineBackend.figure_formats = ['svg']

import xarray as xr
import xyzpy as xyz
import numpy as np

def singuass(x, a, l, phi):
    return np.sin(x / l - phi) * np.exp(-x**2 / a)

r = xyz.Runner(singuass, 'sg(x)')

combos = {
    'x': np.linspace(-4, 4, 52),
    'a': [1, 2, 3],
    'l': [0.2, 0.3, 0.4, 0.5, 0.6, 0.7],
    'phi': [0, 1],
}

r.run_combos(combos)
100%|##########| 1872/1872 [00:00<00:00, 224309.71it/s]
[1]:
<xarray.Dataset>
Dimensions:  (a: 3, l: 6, phi: 2, x: 52)
Coordinates:
  * x        (x) float64 -4.0 -3.843 -3.686 -3.529 ... 3.529 3.686 3.843 4.0
  * a        (a) int64 1 2 3
  * l        (l) float64 0.2 0.3 0.4 0.5 0.6 0.7
  * phi      (phi) int64 0 1
Data variables:
    sg(x)    (x, a, l, phi) float64 -1.027e-07 -9.415e-08 ... -0.004828

3.1. Line Plots#

Plot a variable (y) as a line. The variable can’t have more dimensions than specified as x, z, row and col (though it should tell you which you need to select values for if there are more dimensions).

[2]:
r.last_ds.xyz.lineplot(x='x', y='sg(x)', z='l', col='a', row='phi',
                       colors=True, figsize=(6, 4))
[2]:
_images/plotting_3_0.svg
[3]:
r.last_ds.xyz.ilineplot(x='x', y='sg(x)', z='l', col='a', row='phi',
                        colors=True, figsize=(6, 4))
Loading BokehJS ...