Bokeh悬停工具提示不显示所有数据-Ipython noteb

2024-04-26 03:12:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是图表:

bokeh chart with tooltip

这是我的代码:

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)  

我已经测试了Firefox39.0、Chrome43.0.2357.130(64位)和Safari8.0.7版。我已经清除了缓存,并且在所有浏览器中都出现了相同的错误。我还安装了pip-install-bokeh——升级以确保运行最新版本。


Tags: 数据代码fromimportdfoutputlinebokeh
2条回答

尝试使用^{}

悬停工具需要访问数据源,以便显示信息。 @x@y是数据单元中的x-y值。(@前缀是特殊的,只能跟有限的一组变量,@y2不是其中之一),通常我会使用$+列名来显示我感兴趣的值,例如$weight。有关详细信息,请参见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")

你在用火狐吗?据报道,某些旧版本的FF存在此问题:

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

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

升级FF解决了这个问题。

相关问题 更多 >

    热门问题