A physics experiment I had worked with showed effects depending on the direction of change of a independent variable affecting the system. To compare measured data under a condition of either negative or positive change of the independent variable x
, the measured data y
will need to be plotted with some style indicating this direction information.
I mainly use xarray to store a number of dimensions such as voltage, current, repeats and direction.
Holoviews offer a number of ways to do visualize the data with direction indications.
I went with obj.apply
to apply a function that modified the opts of the obj
.
def compute_marker(scatter):
direction_markers = {
'positive': 'circle',
'negative': 'triangle'
}
sel_data = scatter.data
direction = sel_data['direction'].iloc[0]
return scatter.opts(marker=direction_markers[direction])
obj = obj.apply(compute_marker)
The full code making use of the function
import numpy as np
import pandas as pd
import xarray as xr
import scipy.stats as stats
import holoviews as hv
from holoviews import opts
import hvplot.pandas
hv.extension('bokeh')
# Normal distribution parameters
variance = 1
sigma = np.sqrt(variance)
mu = xr.DataArray([-1, 1], coords={'direction': ['negative', 'positive']}, dims='direction')
# Independent variable
x = xr.DataArray(np.linspace(0, 1, len(idx)), dims='x', name='x')
x = (x * sigma * 2 - sigma) * 3
# Generate normal distribution
y = xr.apply_ufunc(stats.norm.pdf, x, mu, sigma)
# Add noise
noise = xr.DataArray(np.random.randn(len(V), len(I), len(direction), len(x)),
coords=[V, I, direction, x],
name='noise')
y = y + noise * 0.03
y.name = 'y'
def compute_marker(scatter):
direction_markers = {
'positive': 'circle',
'negative': 'triangle'
}
sel_data = scatter.data
direction = sel_data['direction'].iloc[0]
return scatter.opts(marker=direction_markers[direction])
plot = (y.to_dataframe()
.hvplot.scatter(x='x', y='y', groupby=['V', 'I', 'direction'], dynamic=False)
.apply(compute_marker))
plot
However, with the bokeh backend I find that the plot
is not rendered properly in either classic jupyter notebook or jupyterlab when I attempt to overlay the direction dimension plot.overlay('direction')
. The plot do not display at all. With the matplotlib backend this issue does not arise.
I have created this thread to explore issues I have encountered for this situation and create individual GitHub issues to tackle underlying issues.