Use `groupby` with `hv.DynamicMap`

This tutorial shows an example of generating multiple curves from a single DataFrame, using a column to group them. I am using hv.DynamicMap to work with streaming data (per this tutorial) and I would like to generate multiple curves using a similar grouping mechanism.

My current invocation looks like this:

def random_datapoint(**kwargs):
   return pd.DataFrame({'returns': np.random.random(1), 'category': np.random.choice(4)}, index=[pd.Timestamp.now()])

sdf = streamz.dataframe.PeriodicDataFrame(random_datapoint, interval='300ms')
raw_dmap = hv.DynamicMap(hv.Curve, streams=[Buffer(sdf)], groupby='category')
(raw_dmap).opts(
    opts.Curve(width=500, show_grid=True))

but it produces only one curve, not one for each distinct category in the ‘category’ column of the DataFrame. Thanks for your help.

This is what I am currently using:

vdim = hv.Dimension('returns', range=(0, 90))
hv.DynamicMap(
    lambda data: hv.Dataset(data, vdims=vdims).to(hv.Curve, step, groupby='category').overlay(),
    streams=[
        Buffer(sdf),
    ],
)

I certainly welcome any corrections, as I am very new to this library.

1 Like