pandas 散景悬停工具提示未显示所有数据 - Ipython notebook

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31226119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:34:01  来源:igfitidea点击:

Bokeh hover tooltip not displaying all data - Ipython notebook

pythonpandasipython-notebookbokeh

提问by Luis Miguel

I am experimenting with Bokeh and mixing pieces of code. I created the graph below from a Pandas DataFrame, which displays the graph correctly with all the tool elements I want. However, the tooltip is partially displaying the data.

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

Here is the graph:

这是图表:

bokeh chart with tooltip

带有工具提示的散景图

Here is my code:

这是我的代码:

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)  

I have tested with Firefox 39.0, Chrome 43.0.2357.130 (64-bit) and Safari Version 8.0.7. I have cleared the cache and I get the same error in all browsers. Also I did pip install bokeh --upgrade to make sure I have the latest version running.

我已经使用 Firefox 39.0、Chrome 43.0.2357.130(64 位)和 Safari 版本 8.0.7 进行了测试。我已清除缓存,但在所有浏览器中都出现相同的错误。我也做了 pip install bokeh --upgrade 以确保我运行的是最新版本。

回答by colinfang

Try using ColumnDataSource.

尝试使用ColumnDataSource.

Hover tool needs to have access to the data source so that it can display info. @x, @yare the x-y values in data unit. (@prefix is special, can only followed by a limited set of variable, @y2is not one of them)., Normally I would use $+ column_name to display the value of my interest, such as $weight. See herefor more info.

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

Besides, I am surprised that the hover would appear at all. As I thought hoverTool doesn't work with line glyph, as noted here

此外,我很惊讶会出现悬停。因为我认为hoverTool不与线雕文工作,因为注意到这里

Try the following : (I haven't tested, might have typos).

请尝试以下操作:(我还没有测试过,可能有错别字)。

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

回答by bigreddot

Are you using Firefox? This was a reported issue with some older versions of FF:

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

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

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

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

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

Upgrading FF resolved the issue.

升级FF解决了这个问题。