RMe*_*elo 1 python python-3.x pandas bokeh
我从数据框的某些列绘制线。我希望悬停工具显示源自该数据的列的名称,以及其他未绘制的列的一些信息。
例如,在下面的代码中,当鼠标悬停在A行的中心点上时,我希望悬停工具显示“ Name = A; Aux = 0.1”。此值存储在A1列中。相反,在B线上的中心点上方时,工具应显示“名称= B;辅助= 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)
Run Code Online (Sandbox Code Playgroud)
谢谢!
最近有一个功能来支持这一点。使用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)
Run Code Online (Sandbox Code Playgroud)