是否可以使用js_on_change
withbokeh
动态更新放置在绘图旁边的文本?
例如,使用来自不同问题的此代码段
from random import random
from bokeh.models import CustomJS, ColumnDataSource, Span
from bokeh.plotting import figure, output_file, show
output_file("callback.html")
x = [random() for x in range(500)]
y = [random() for y in range(500)]
color = ["navy"] * len(x)
s = ColumnDataSource(data=dict(x=x, y=y, color=color))
p = figure(plot_width=400,
plot_height=400,
tools="lasso_select",
title="Select Here")
p.circle(x='x', y='y', color='color', size=8, source=s, alpha=0.4)
slope = Span(location=.5,
dimension="width",
line_alpha=.6,
line_width=5)
p.add_layout(slope)
s.selected.js_on_change(
'indices',
CustomJS(args=dict(s=s, slope=slope),
code="""
var inds = cb_obj.indices;
if (inds.length == 0) {
slope.location = 0.5
return
}
var total = 0;
for (var i = 0; i < inds.length; i++) {
total += s.data["y"][inds[i]]
}
var avg = total / inds.length;
slope.location = avg;
"""))
show(p)
我想在图形右侧包含一个文本,显示计算的值,slope.location
并在我选择新点时更新。