我不知道
bokeh
中实现了粘性工具,但可以使用 HoverTool
和 CustomJS
对其进行原型设计。
下面的代码需要一些解释。
p.x_range.start
和 p.y_range.start
。默认情况下这些不存在。a
。这可以改进。from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, CustomJS, HoverTool
source = ColumnDataSource(dict(a=[1,2,3,4,5], x=[1,2,3,4,5]))
p = figure(x_range=(0.9,5.1), y_range=(0.9,5.1))
p.line('a', 'x', source=source, color='red')
cr = p.circle('a', 'x', source=source, color='red')
extra_source = ColumnDataSource({'x0': [], 'y0': [], 'x1': [], 'y1': []})
sb = p.segment(
x0='x0',
y0='y0',
x1='x1',
y1='y1',
color='red',
alpha=0.6,
line_width=2,
line_dash="dashed",
source=extra_source
)
# add a hover tool that sets the link data for a hovered circle
code = """
const data = {'x0': [],'y0': [],'x1': [],'y1': []}
const indices = cb_data.index.indices
console.log(cb_data)
for (let i of indices) {
// horizontal dashed line
data['x0'].push(p.x_range.start)
data['y0'].push(circle.data.a[i])
data['x1'].push(circle.data.x[i])
data['y1'].push(circle.data.a[i])
// vertical dashed line
data['x0'].push(circle.data.x[i])
data['y0'].push(p.y_range.start)
data['x1'].push(circle.data.x[i])
data['y1'].push(circle.data.a[i])
}
segment.data = data
"""
callback = CustomJS(args={'circle': cr.data_source, 'segment': sb.data_source, 'p':p}, code=code)
p.add_tools(HoverTool(tooltips=None, callback=callback, renderers=[cr]))
show(p)