我绘制数据框某些列的线条。我希望悬停工具显示产生该数据的列的名称以及其他未绘制的列的一些信息。
例如,在下面的代码中,当鼠标悬停在 A 行的中心点上时,我希望悬停工具显示“Name = A; Aux = 0.1”。该值存储在 A1 列中。相反,当越过 B 线的中心点时,工具应显示“Name = B; Aux = 0.3”
from bokeh.io import show
from bokeh.models import HoverTool, ColumnDataSource
from bokeh.plotting import figure
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3], 'A' : [1, 5, 3], 'A1': [0.2, 0.1, 0.2],
'B' : [2, 4, 3], 'B1':[0.1, 0.3, 0.2]})
tools_to_show = 'box_zoom,save,hover,reset'
p = figure(plot_height =300, plot_width = 1200,
toolbar_location='above',
tools=tools_to_show)
columns = ['A', 'B']
source = ColumnDataSource(df)
for col in columns:
p.line('x', col, source=source)
hover = p.select(dict(type=HoverTool))
hover.tooltips = [("Name","@col"), ("Aux", "@col1")]
hover.mode = 'mouse'
show(p)
谢谢!
请您参考如下方法:
最近有一项功能支持这一点。使用 Bokeh 0.13.0 或更新版本,您可以在字形上设置 name 并在工具提示中使用 $name 引用该名称。此外,您可以使用 @$name 引用具有该名称的列。但是,“间接”列必须是 name 中指定的列,因此您必须根据此期望重新排列列名:
from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3], 'A_y' : [1, 5, 3], 'A': [0.2, 0.1, 0.2],
'B_y' : [2, 4, 3], 'B':[0.1, 0.3, 0.2]})
tools_to_show = 'box_zoom,save,hover,reset'
p = figure(plot_height =300, plot_width = 1200,
toolbar_location='above', tools=tools_to_show,
# "easy" tooltips in Bokeh 0.13.0 or newer
tooltips=[("Name","$name"), ("Aux", "@$name")])
columns = ['A', 'B']
source = ColumnDataSource(df)
for col in columns:
# have to use different colnames for y-coords so tooltip can refer to @$name
p.line('x', col + "_y", source=source, name=col)
show(p)
