我有一个在Bokeh中创建的散点图,我非常希望能够根据您在下拉菜单中的选择来更改Hovertool上显示的工具提示。如果选择了“统计数据集1”,我想显示统计数据1&2。如果选择了“统计数据集2”,我想显示统计数据3、4、5。
我希望最终结果是一个html文件,因此使用CustomJS进行回调可能是必须的。这是我设法获得代码的地方。回调可能会引起问题,因为我不确定如何通过回调弄乱工具提示。
import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import CustomJS, ColumnDataSource, HoverTool, ColumnDataSource, Select
from bokeh.layouts import row
#Create a dataframe with x and y coordinates and 4 different statistics
df = pd.DataFrame({
'x':[1,2,3],
'y':[1,2,3],
'stat1':[1,2,3],
'stat2':[4,5,6],
'stat3':[7,8,9],
'stat4':[10,11,12],
'stat5':[13,14,15]
})
#Create Bokeh's ColumnDataSource
source=ColumnDataSource(data=df)
#Create the different options for the Hovertool tooltips
option1=[('Stat1','@stat1'),
('Stat2','@stat2')]
option2=[('Stat3','@stat3'),
('Stat4','@stat4'),
('Stat5','@stat5')]
#Set the default option for the Hovertool tooltips
hover=HoverTool(tooltips=option1)
#Create the figure
plot = figure(tools=[hover])
plot.circle('x', 'y', source=source)
#Create the callback that will update the tooltips
callback = CustomJS (args=dict(tt=plot.hover), code="""
if (cb_obj.value='Stat Set 1') {
tt.tooltips=option1
} else {
tt.tooltips=option2
}
""")
#Create a dropdown menu that allows you to change which set of stats will populate the tooltips
stat_select=Select(options=['Stat Set 1', 'Stat Set 2'],
value='Stat Set 1',
title='What stats set do you want to see when you hover?', callback=callback)
show(row(stat_select,plot))