我在TapTool上使用CustomJS回调时遇到问题 . 我想在点击之后强制选择50个点 . 因此,我做了一个javascript回调,修改了数据源中选择的索引列表,并应更新绘图 . 我可以通过控制台看到数据源已更新,但情节不是 .
我从文档示例(https://bokeh.pydata.org/en/latest/docs/user_guide/interaction/callbacks.html)中创建了一个测试版本,但它既不起作用 . 是否因为更改选择时更新绘图有不同的方法?
这是我制作的测试版本:
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import Figure, output_file, show
output_notebook()
x = [x*0.005 for x in range(0, 200)]
y = x
source = ColumnDataSource(data=dict(x=x, y=y))
plot = Figure(plot_width=400, plot_height=400)
plot.circle('x', 'y', source=source, line_width=3, line_alpha=0.6)
callback = CustomJS(args=dict(source=source), code="""
var l_selected=source.selected;
var indices = l_selected['1d'].indices;
if(indices.length <= 1) {
var new_indices = Array.from(new Array(50), (x,i) => i + indices[0]);
l_selected['1d'].indices=new_indices;
}
source.selected=l_selected;
console.log(source.selected)
source.change.emit();
""")
slider = Slider(start=0.1, end=4, value=1, step=.1, title="power")
slider.js_on_change('value', callback)
plot.add_tools(TapTool(callback=callback))
layout = column(slider, plot)
show(layout)
我不知道它是否有用,但我使用的是0.12.16版本的Bokeh,我试图让它在Jupyter笔记本中运行
1 回答
Seb在评论中给出了答案 . 因为散景0.12.15
source.selected['1d']
成了source.selected.indices