【问题标题】:Bokeh hover tooltip not displaying all data - Ipython notebook散景悬停工具提示未显示所有数据 - Ipython 笔记本
【发布时间】:2015-09-22 10:26:55
【问题描述】:

我正在尝试使用 Bokeh 和混合代码片段。我从 Pandas DataFrame 创建了下面的图表,它使用我想要的所有工具元素正确显示图表。但是,工具提示部分显示了数据。

这是图表:

这是我的代码:

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 版本 8.0.7 进行了测试。我已经清除了缓存,并且在所有浏览器中都出现了相同的错误。我还做了 pip install bokeh --upgrade 以确保我运行的是最新版本。

【问题讨论】:

    标签: python pandas ipython-notebook bokeh


    【解决方案1】:

    尝试使用ColumnDataSource

    悬停工具需要访问数据源才能显示信息。 @x@y 是数据单元中的 x-y 值。 (@前缀比较特殊,后面只能跟有限的一组变量,@y2不是其中之一),一般我会用$+ column_name来显示我感兴趣的值,比如@987654330 @。请参阅here 了解更多信息。

    此外,我很惊讶悬停会出现。正如我认为 hoverTool 不适用于线条字形,如 here

    所述

    尝试以下方法:(我没有测试过,可能有错别字)。

    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")
    

    【讨论】:

    • 你说“@”是指“$”,说“$”是指“@”吗?
    • @jf328 我认为他确实倒退了,至少以当前版本 12.5,12.6 衡量
    【解决方案2】:

    您使用的是火狐吗?这是一些旧版本的 FF 报告的问题:

    https://github.com/bokeh/bokeh/issues/1981

    https://github.com/bokeh/bokeh/issues/2122

    升级 FF 解决了这个问题。

    【讨论】:

    • 对不起,我只见树木不见森林。您需要使用与悬停模板中的标识符匹配的列名来设置列数据源,如下科林所述。
    猜你喜欢
    • 2016-07-19
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    相关资源
    最近更新 更多