在bokeh应用程序中,我希望重用HoverTool创建的工具提示的呈现。你知道吗
特别是,我想通过某种方式在数据源中选择一个数据点,然后显示该点的进一步信息。例如,可以从滑块进行选择。 我可以添加一个自制的标签(参见代码示例),但是如果能够显示由HoverTool生成的工具提示,那就更好了,因为它们的格式已经很好了。你知道吗
示例代码显示了一个选择数据点并设置自定义标签的滑块。我想避免使用自定义标签,但以某种方式触发悬停工具提示。你知道吗
from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import HoverTool, ColumnDataSource, Slider, CustomJS, LabelSet
from bokeh.plotting import figure
import numpy as np
x = np.linspace(0,1)
y = np.linspace(0,2)
ds = ColumnDataSource(data=dict(x=x,y=y))
fig = figure()
fig.scatter(x='x', y='y', source=ds)
# a datasource to show labels at a x/y position, set in the JS callback
labels = ColumnDataSource(data=dict(x=[], y=[], t=[], ind=[]))
fig.add_layout(LabelSet(x='x', y='y', text='t', source=labels))
# slider that selects a datapoint and creates the label for the point
slider = Slider(start=0, end=len(x), value=0, step=1)
code = """
labels.data = {'x':[],'y':[],'t':[]}
source.selected.indices = [slider.value]
labels.data = {'ind':[slider.value],
'x':[source.data.x[slider.value]],
'y':[source.data.y[slider.value]],
't':[source.data.x[slider.value]]}
labels.change.emit()
source.change.emit()
"""
callback = CustomJS(args=dict(source=ds, slider=slider, labels=labels), code=code)
slider.js_on_change('value', callback)
# hover to show default tooltips, can those be triggered?
hover = HoverTool(mode='vline')
fig.add_tools(hover)
show(column(slider, fig))
调用
HoverToolView._inspect(x, y)
,其中(x,y)是点的画布坐标。我们需要使用xscale.compute()
和yscale.compute()
将数据坐标转换为画布坐标。你知道吗相关问题 更多 >
编程相关推荐