How can I specify separate y-limits for subplots in hvplot?

799 views Asked by At

I am plotting several variables with different magnitude in subplots using hvplot. I would like to specify y-limits for each subplot individually. However, with hvplot, I can only specify a single ylim for all subplots, as far as I see. The background of the question is that I want to create a plot where an extra dimension is represented using a widget. I don't want the axis limits to change with the different values of the widget parameter.

A simplified version of my problem looks like this:

import numpy as np
import xarray as xr
import hvplot.xarray

n_x = 100
n_time = 200
ds = xr.Dataset(
    data_vars={
        "a": (("x", "time"), np.random.rand(n_x, n_time)),
        "b": (("x", "time"), np.random.rand(n_x, n_time) * 10 + np.arange(n_time)[None, :]),
    },
    coords={
        'x': ('x', np.arange(n_x)),
        'time': ('time', np.arange(n_time))
    }
)

plot = ds.hvplot.line(
    x="x",
    subplots=True,
    shared_axes=False,
    width=350,
    height=300,
)

The axis ranges of the right plot will change over time and I want to prevent that. I have tried working on the Holoviews object directly like this, but it does not seem to work.

bounds = {'a': (0, 1), 'b': (0, n_time)}
plot.redim.range(**bounds)

I guess that the problem is that 'a' and 'b' are not the actual dimension on the y-axis. Does anyone know how to go about this?

Edit

I have found a workaround, taking the subplots apart, changing the axis range on each individually and then glueing them together again in a new Layout:

import holoviews as hv

container = plot.collate()
subplots = {key: dmap.redim.range(value=bounds[key]) for key, dmap in container.items()}
new_layout = hv.NdLayout(subplots, kdims='Variable')
new_layout

However, I still feel that there should be a more elegant way…

0

There are 0 answers