不显示所有数据的散景悬停工具提示

编程入门 行业动态 更新时间:2024-10-27 22:23:13
不显示所有数据的散景悬停工具提示 - Ipython笔记本(Bokeh hover tooltip not displaying all data - Ipython notebook)

我正在试验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。 我清除了缓存,并在所有浏览器中收到相同的错误。 此外,我做了点安装散景 - 升级,以确保我有最新版本运行。

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.

Here is the graph:

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.

最满意答案

尝试使用ColumnDataSource 。

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

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

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

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

Try using ColumnDataSource.

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

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

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

更多推荐

本文发布于:2023-04-29 05:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335282.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:提示   工具   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!