Is there a way to update xlim/ylim of DynamicMap dynamically?

I have a Histogram in a DynamicMap whose bounds can change significantly based on widget selections. Here is a small example:

import holoviews as hv
import numpy as np
import panel as pn

hv.extension("bokeh")

def plot_hist(
    negpos: str = "positive",
):
    data = np.random.normal(size=100)
    match negpos:
        case "negative":
            data = data[data < 0]
            # xlim=(min(data),0)
        case "positive":
            data = data[data > 0]
            # xlim=(0, max(data))

    counts, edges = np.histogram(data)

    hist = hv.Histogram((counts,edges)).opts(
        apply_ranges=True,
        # xlim=xlim,
    )
    return hist

negpos_selector = pn.widgets.RadioButtonGroup(
    name="negpos",
    options=["negative", "positive"]
)
widgets = pn.WidgetBox(negpos_selector)
dmap = hv.DynamicMap(
    pn.bind(plot_hist, negpos=negpos_selector)
)

pn.Row(dmap, widgets)

Screenshot 2025-03-03 at 10.59.46 AM

The initial bounds are determined by the data for the initial case, but when I change it, the xlim/ylim bounds do not change to match the data:

Screenshot 2025-03-03 at 11.01.23 AM

, nor does it work if I explicitly set the xlim. Is there some way to force it to update?

I think potentially related to framewise=True or axiswise=True.

Alternatively, you can drop the DynamicMap and it’ll completely update the figure

    hist = hv.Histogram((counts,edges)).opts(
        framewise=True
        # xlim=xlim,
    )


1 Like