I’d like to update the color of a selection of bars (ListSelection) in multiple HoloViews
barplots (one per criteria). So far I’ve come up with this (code below gif):
- each criterium bar plot is an
hv.Overlay
of the all bars and the selection of bars - changing the selection triggers a function that updates the selection of bars plot
- a show_layout method returns an
hv.Layout
of all above overlays - a class attribute layout displays the panel (Layout and List param)
Changng a selection takes about 5 seconds, which is too slow to use interactively. The hv.Layout
seems to be renewed with every new selection. Is there a faster way e.g. is there a way to simply change elements in the HoloViews
object and have the updates take place?
import numpy as np
import panel as pn
import pandas as pd
import param
import holoviews as hv
pn.extension()
import hvplot.pandas
criteria =[f'criterium_{x}' for x in range(20)]
parameters =[f'parameter_{x}' for x in range(8)]
df = pd.DataFrame(columns=parameters,
index=criteria,
data=np.random.rand(len(criteria),
len(parameters)))
class Update_HoloViews_callback(param.Parameterized):
"""Plots all layout with overlays of 2 barplots"""
pars = param.ListSelector(default=parameters[:1],
objects=parameters)
def __init__(self,**params):
for criterium in criteria:
barplot_all = df.loc[criterium,:].hvplot.bar(legend=False,rot=90).opts(width=200,height=250)
barplot_selection = barplot_all[self.pars]
barplot_combined = barplot_all*barplot_selection
setattr(self,criterium+'_plot', barplot_combined)
super().__init__(**params)
self.layout = pn.Row(self.show_layout,
pn.Param(self))
@pn.depends('pars',watch=True)
def update_layout(self):
for i,attr in enumerate(criteria):
exec(f'self.{attr}_plot.Bars.{attr}.II=self.{attr}_plot.Bars.{attr}.I[self.pars]')
def show_layout(self):
barplot_layout = []
for criterium in criteria:
barplot_layout.append(getattr(self,f'{criterium}_plot'))
return hv.Layout(barplot_layout)
p = Update_HoloViews_callback()
p.layout