I'm trying to create a bokeh plot where, with a slider, it updates multiple labels. The code I have below is a combination from two questions posed on stackoverflow here and here.
In the code i have static x and y coordinates, its just the text labels that i want to manipulate with the slider. With code below it doesnt seem to show or even update. Why is it not showing and/or updating?
from bokeh.plotting import figure, show, output_file
from bokeh.models import ColumnDataSource, Range1d, LabelSet, Label, Slider, TextInput, CustomJS
from bokeh.layouts import column, row
output_file("image.html")
plot = figure(x_range=(0, 100), y_range=(0, 100))
source = ColumnDataSource(
data=dict(
x=[55, 28, 18, 74, 76, 28, 32, 18, 60, 84, 44, 56, 56, 76],
y=[8, 8, 33, 14, 72, 64, 46, 20, 52, 56, 84, 22, 36, 32],
v1=prices_final["1"].values,
v2=prices_final["2"].values,
v3=prices_final["3"].values,
v4=prices_final["4"].values,
)
)
labels = ColumnDataSource(data=dict(x=[], y=[], t=[], ind=[]))
plot.add_layout(
LabelSet(
x="x",
y="y",
text="t",
source=labels,
level="overlay",
x_offset=0,
y_offset=0,
render_mode="canvas",
text_font_size="10pt",
text_color="black",
background_fill_color="white",
border_line_color="black",
)
)
# Set up widgets
slider = Slider(title="Hour of day", value=1.0, start=1.0, step=1.0, end=24.0)
code = """
labels.data = {'x':[],'y':[],'t':[]}
source.selected.indices = [slider.value]
labels.data = {'ind':[slider.value],
'x':[source.data.x],
'y':[source.data.y],
't':[source.data.v[slider.value]]}
labels.change.emit()
source.change.emit()
"""
callback = CustomJS(args=dict(source=source, slider=slider, labels=labels), code=code)
slider.js_on_change("value", callback)
layout = column(slider, plot)
show(layout)
prices final looks like:
prices_final['1'].values = array([ -5.25, 2.67, 10.67, -0.95, -9.54,
-4.22, -5.2 , -5.53, -9.33, -3.49, -0.47, -8.96, -17.88, -5.49])
all other prices_final have similar data structure.