python - 从 ColumnDataSource 中选取 Bokeh HoverTool 输出的数据

标签 python python-3.x bokeh

我对 Bokeh 中的 HoverTool 有点烦恼。我有一个具有多个数据“列”的 ColumnDataSource,并且我使用各个列在图表上绘制线条。当我将鼠标悬停在线上的一个点上时,我想显示该线上该点的“x”(CDS 中的“rpm”数据)和“y”(CDS 中的其他列之一)数据。

由于所有行都使用相同的 CDS(“y”值的不同列),对于我来说,我无法弄清楚如何区分悬停在其上的行的“y”值,并显示仅该行的“y”值。目前,我只是显示悬停在“x”值上的所有“y”值。

我不想使用“$y”参数,因为那只是图表上光标的位置,而不是悬停点的数据。

非常感谢您的想法。谢谢。

请注意,该程序绘制给定发动机 RPM 的车辆速度,每个系列均按变速箱档位排列。

import itertools
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.palettes import Category20_20 as palette

data={'rpm': range(2000, 4000, 500),
     '1st': [15.00040961597459,
             18.750512019968237,
             22.50061442396188,
             26.25071682795553],
     '2nd': [28.241511931310182,
             35.301889914137725,
             42.36226789696527,
             49.422645879792825],
     '3rd': [45.751249328722494,
             57.18906166090312,
             68.62687399308373,
             80.06468632526436],
     '4th': [66.30615844742391,
             82.8826980592799,
             99.45923767113587,
             116.03577728299183]}

cds = ColumnDataSource(data=data)

fig = figure(width=800, height= 600, title='Speed', x_axis_label='RPM', y_axis_label='Speed(mph)')
hovertools= HoverTool(tooltips=[('Gear', '$name'),
                                ('4th', '@4th{0.0}'),
                                ('3rd', '@3rd{0.0}'),
                                ('2nd', '@2nd{0.0}'),
                                ('1st', '@1st{0.0}'),
                                ('RPM', '@rpm')],)

colors = itertools.cycle(palette)
fig.add_tools(hovertools)
for gear in ('1st', '2nd', '3rd', '4th'):
    color = colors.__next__()
    fig.line(x='rpm',
             y=gear,
             source=cds,
             color=color,
             legend=gear + ' gear',
             muted_color=color,
             muted_alpha=0.2,
             line_width=1,
             name=gear + ' gear')
    fig.circle(x='rpm',
               y=gear,
               source=cds,
               color=color,
               legend=gear + ' gear',
               muted_color=color,
               muted_alpha=0.2,
               line_width=1,
               name=gear + ' gear')

fig.legend.click_policy = "mute"
fig.legend.background_fill_alpha = 0.5

show(fig)

最佳答案

您可以使用@$name查找具有 $name 的列的值作为列名称:

hovertools= HoverTool(tooltips=[('Gear', '$name'),
                                ('data', '@$name{0.0}'),
                                ('RPM', '@rpm')],)

但请注意,要使其起作用,您为 name 设置的值字形方法中的内容必须与列名称匹配。例如。我必须改变:

for gear in ('1st', '2nd', '3rd', '4th'):
    color = colors.__next__()
    fig.line(x='rpm',
             y=gear,
             ...
             name=gear)   # CHANGED to match column name
    fig.circle(x='rpm',
               y=gear,
               ...
               name=gear) # CHANGED to match column name

这会产生:

enter image description here

enter image description here

请注意,在工具提示的“元组”格式中,第一个标签值不会扩展,因此如果您想要例如类似 4th gear: <value>在每行的基础上,那么您将需要使用 Custom Tooltip

关于python - 从 ColumnDataSource 中选取 Bokeh HoverTool 输出的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55942765/

相关文章:

python - 如何在类中使用 python csv 模块

python - 如何在python中使用\K作为正则表达式?

python - 从 python 中的文件路径列表加载文件

python-3.x - 如何从 AWS S3 导入 jupyter notebook 中的 .py 文件

python - Bokeh 服务器 - 如何在回调函数中操作选择

python - Bokeh :对多个图形使用相同的工具效果

python - Django错误: ModuleNotFoundError: No module named 'ofac'

python - 使用 Google API 获取用户电子邮件的最佳方式是什么?

python - 带有词典字典的 Bokeh 表

python - python中两个或多个base64字符串的连接