import numpy as np
import holoviews as hv
from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import HoverTool
hv.extension(‘bokeh’)
renderer = hv.renderer(‘bokeh’).instance(mode=‘server’)
frequencies = [0.5, 1.0]
def sine_curve(phase, freq):
xvals = [0.1* i for i in range(100)]
return hv.Curve((xvals, [np.sin(phase+freq*x) for x in xvals]))
def callback1():
print(‘Curve “0.5” is turn off’)
def callback2():
print(‘Curve “0.5” is turn on’)
curve_dict = {f:sine_curve(0,f) for f in frequencies}
I need to make sure that when you turn off the curve “0.5” in the legend, the callback1 () function is called, and when you turn on the curve “0.5” callback2 () function is called.
How to implement this?
I don’t know if it’s a bokeh or a holoview question but
something like that should do the trick:
import numpy as np
import holoviews as hv
from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import HoverTool
hv.extension('bokeh')
renderer = hv.renderer('bokeh').instance(mode='server')
frequencies = [0.5, 1.0]
def callback(attr,old,new):
if new:
print('Curve “0.5” is turn off')
else:
print('Curve “0.5” is turn on')
def sine_curve(phase, freq):
xvals = [0.1* i for i in range(100)]
return hv.Curve((xvals, [np.sin(phase+freq*x) for x in xvals]))
curve_dict = {f:sine_curve(0,f) for f in frequencies}
ndoverlay = hv.NdOverlay(curve_dict, kdims='frequency')
doc=curdoc()
hvplot = renderer.get_plot(ndoverlay, doc)
legend_items = hvplot.handles['legend_items']
for li in legend_items:
if li.label['value'] == '0.5':
li.renderers[0].on_change('muted',callback)
plot=layout(hvplot.state)
doc.add_root(plot)