Bokeh悬停提示未显示所有数据 - Ipython笔记本

4

我正在尝试使用 Bokeh,并混合代码。我从 Pandas DataFrame 创建了下面这个图表,它正确地显示了我想要的所有工具元素。然而,工具提示只显示了部分数据。

以下是该图表:

带有工具提示的 Bokeh 图表

以下是我的代码:

from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import HoverTool
from collections import OrderedDict

x  = yearly_DF.index
y0 = yearly_DF.weight.values
y1 = yearly_DF.muscle_weight.values
y2 = yearly_DF.bodyfat_p.values

#output_notebook()

p = figure(plot_width=1000, plot_height=600,
           tools="pan,box_zoom,reset,resize,save,crosshair,hover", 
           title="Annual Weight Change",
           x_axis_label='Year', 
           y_axis_label='Weight',
           toolbar_location="left"
          )

hover = p.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([('Year', '@x'),('Total Weight', '@y0'), ('Muscle Mass', '$y1'), ('BodyFat','$y2')])

output_notebook()

p.line(x, y0, legend="Weight")
p.line(x, y1, legend="Muscle Mass", line_color="red")

show(p)  

我已经测试过Firefox 39.0、Chrome 43.0.2357.130(64位)和Safari Version 8.0.7。我已经清除了缓存,在所有浏览器中都遇到了同样的错误。此外,我执行了pip install bokeh --upgrade以确保我正在运行最新版本。
2个回答

15

尝试使用ColumnDataSource

悬停工具需要访问数据源才能显示信息。 @x@y 是以数据单位表示的 x-y 值。 (@前缀是特殊的,只能跟随有限的变量,@y2 不是其中之一)。通常情况下,我会使用$+列名来显示我感兴趣的值,例如$weight。有关更多信息,请参见此处

此外,我很惊讶悬停工具会出现。正如这里所指出的那样,我认为悬停工具无法与线条标志一起使用。

尝试以下内容: (我还没有测试,可能有拼写错误)。

df = yearly_DF.reset_index() # move index to column.
source = ColumnDataSource(ColumnDataSource.from_df(df)

hover.tooltips = OrderedDict([('x', '@x'),('y', '@y'), ('year', '$index'), ('weight','$weight'), ('muscle_weight','$muscle_weight'), ('body_fat','$bodyfat_p')])

p.line(x='index', y='weight', source=source, legend="Weight")
p.line(x='index', y='muscle_weight', source=source, legend="Muscle Mass", line_color="red")

2
当你说“@”时,是不是指的“$”,而当你说“$”时,是不是指的“@”? - jf328
@jf328 我认为他确实搞反了,至少根据当前的版本12.5、12.6来衡量。 - jxramos

1

2
抱歉,我只看到了细节而忽略了整体。你需要按照Colin在下面所描述的方式设置一个列数据源,并确保列名与悬停模板中的标识符匹配。 - bigreddot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接