Change/autoscale y-limit after updating dynamic map

I have the following reproducible notebook:

import pandas as pd
import numpy as np
from holoviews.streams import Pipe
import holoviews as hv
hv.extension("bokeh")

import panel as pn
pn.extension()

apple_df = pd.DataFrame({'data': np.random.normal(0.0, 100_000.0, 10000), 'fruit': 'apple'}) 
banana_df = pd.DataFrame({'data': np.random.uniform(0.0, 1.0, 1000), 'fruit': 'banana'})
df = pd.concat([apple_df, banana_df])

pipe = Pipe(data=[])
dmap = hv.DynamicMap(hv.Histogram, streams=[pipe])
dmap.opts(width=1000, default_tools=['reset'])
frequencies, edges = frequencies, edges = np.histogram(df.data, 20)
pipe.send((edges, frequencies))

button = button = pn.widgets.Button(name="Button", button_type="primary")

def filter_plot(event):
    filter_df = df.query('fruit == "banana"')
    frequencies, edges = frequencies, edges = np.histogram(filter_df.data, 20)
    pipe.send((edges, frequencies))

button.on_click(filter_plot)

pn.Column(button, dmap).servable()

which I then serve up with:

panel serve dashboard.ipynb

In this simple example, once the button is clicked, it will filter out any data that is not “banana” and then it plots the histogram for “banana”. However, since the y-range on the figure is too large since it is based on the original full data set, which contains both “apple” and “banana”.

Can somebody tell me how I can rescale/autoscale the y-range after the data has been filtered?

Try adding framewise to opts like this: dmap.opts(width=1000, default_tools=['reset'], framewise=True)

2 Likes

Thanks @Hoxbro! That did the trick

1 Like