Plotting Data
Contents
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:
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
- a: 3
- l: 6
- phi: 2
- x: 52
- x(x)float64-4.0 -3.843 -3.686 ... 3.843 4.0
array([-4. , -3.843137, -3.686275, -3.529412, -3.372549, -3.215686, -3.058824, -2.901961, -2.745098, -2.588235, -2.431373, -2.27451 , -2.117647, -1.960784, -1.803922, -1.647059, -1.490196, -1.333333, -1.176471, -1.019608, -0.862745, -0.705882, -0.54902 , -0.392157, -0.235294, -0.078431, 0.078431, 0.235294, 0.392157, 0.54902 , 0.705882, 0.862745, 1.019608, 1.176471, 1.333333, 1.490196, 1.647059, 1.803922, 1.960784, 2.117647, 2.27451 , 2.431373, 2.588235, 2.745098, 2.901961, 3.058824, 3.215686, 3.372549, 3.529412, 3.686275, 3.843137, 4. ])
- a(a)int641 2 3
array([1, 2, 3])
- l(l)float640.2 0.3 0.4 0.5 0.6 0.7
array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7])
- phi(phi)int640 1
array([0, 1])
- sg(x)(x, a, l, phi)float64-1.027e-07 -9.415e-08 ... -0.004828
array([[[[-1.02738453e-07, -9.41531885e-08], [-7.80939572e-08, -1.10376862e-07], [ 6.12215108e-08, 1.12534073e-07], [-1.11337603e-07, -4.63778257e-08], [-4.21051741e-08, -1.10566685e-07], [ 6.06232938e-08, -4.70251615e-08]], [[-3.06259013e-04, -2.80666699e-04], [-2.32794805e-04, -3.29028788e-04], [ 1.82498751e-04, 3.35459343e-04], [-3.31892717e-04, -1.38250350e-04], [-1.25513755e-04, -3.29594644e-04], [ 1.80715492e-04, -1.40180031e-04]], [[-4.40765402e-03, -4.03933158e-03], [-3.35036331e-03, -4.73535471e-03], [ 2.62650672e-03, 4.82790271e-03], [-4.77657214e-03, -1.98968744e-03], [-1.80638343e-03, -4.74349846e-03], [ 2.60084219e-03, -2.01745924e-03]]], ... [[[ 1.02738453e-07, 1.68664580e-08], [ 7.80939572e-08, -2.59881717e-08], [-6.12215108e-08, 4.63778257e-08], [ 1.11337603e-07, 7.39341017e-08], [ 4.21051741e-08, -6.50676402e-08], [-6.06232938e-08, -1.12534972e-07]], [[ 3.06259013e-04, 5.02782026e-05], [ 2.32794805e-04, -7.74696480e-05], [-1.82498751e-04, 1.38250350e-04], [ 3.31892717e-04, 2.20394451e-04], [ 1.25513755e-04, -1.93963902e-04], [-1.80715492e-04, -3.35462024e-04]], [[ 4.40765402e-03, 7.23599673e-04], [ 3.35036331e-03, -1.11493667e-03], [-2.62650672e-03, 1.98968744e-03], [ 4.77657214e-03, 3.17189845e-03], [ 1.80638343e-03, -2.79151220e-03], [-2.60084219e-03, -4.82794131e-03]]]])
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]:
[3]:
r.last_ds.xyz.ilineplot(x='x', y='sg(x)', z='l', col='a', row='phi',
colors=True, figsize=(6, 4))