python - 在 Bokeh 中悬停多行字形时如何显示单个值?

标签 python python-3.x hover bokeh multiline

至于现在,我看到 Bokeh 支持多行字形的 HoverTool。 但问题是,如果我想显示点的特定值 - 它会显示所有值列表而不是它。

请看下面的例子:

from bokeh.plotting import show, figure
from bokeh.models import ColumnDataSource, HoverTool

df = {'X_value': [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], 
      'model': ['m1', 'm1', 'm2', 'm2'], 
      'color': ['red', 'red', 'blue', 'blue'],
      'Y_value': [[0.50, 0.66, 0.70, 0.67], [0.65, 0.68, 0.71, 0.66], [0.80, 0.79, 0.84, 0.80], [0.80, 0.83, 0.76, 0.64]]}

source = ColumnDataSource(df)

p = figure(plot_height=400)
p.multi_line(xs='X_value', ys='Y_value', legend="model", color='color',
             line_width=5, line_alpha=0.6, hover_line_alpha=1.0,
             source=source)

p.add_tools(HoverTool(show_arrow=False, line_policy='next', tooltips=[
    ('X_value', '@X_value'),
    ('Y_value', '@Y_value')
]))

show(p)

enter image description here

我知道 $x, $y 的能力,但是这在鼠标下显示协调,并且当你移动鼠标时它们会发生变化,这不是我们想要的行为。

有没有办法在多行字形中显示悬停点的精确值?

附注创建不可见的线不是解决方案,因为我有更高级的过滤图和链接图等。

谢谢!

最佳答案

如果您更新到 bokeh 版本 0.12.16,您可以使用新类 CustomJSHover像这样:

from bokeh.plotting import show, figure
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.models.tools import CustomJSHover


df = {'X_value': [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], 
      'model': ['m1', 'm1', 'm2', 'm2'], 
      'color': ['red', 'red', 'blue', 'blue'],
      'Y_value': [[0.50, 0.66, 0.70, 0.67], [0.65, 0.68, 0.71, 0.66], [0.80, 0.79, 0.84, 0.80], [0.80, 0.83, 0.76, 0.64]]}

source = ColumnDataSource(df)

p = figure(plot_height=400)
p.multi_line(xs='X_value', ys='Y_value', legend="model", color='color',
             line_width=5, line_alpha=0.6, hover_line_alpha=1.0,
             source=source)

x_custom = CustomJSHover(code="""
    return '' + special_vars.data_x
""")

y_custom = CustomJSHover(code="""
    return '' + special_vars.data_y
""")

p.add_tools(
    HoverTool(
        show_arrow=False, 
        line_policy='next',
        tooltips=[
            ('X_value', '@X_value{custom}'),  # or just ('X_value', '$data_x')
            ('Y_value', '@Y_value{custom}')
        ],
        formatters=dict(
            X_value=x_custom,
            Y_value=y_custom
        )
    )
)

show(p)

关于python - 在 Bokeh 中悬停多行字形时如何显示单个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50332640/

相关文章:

python - 单击输入按钮

python - scikit 如何学习逻辑回归以进行分类或回归

python - 用 Python 编写后更改文件的修改日期

python - 我可以使用 python 进行 CSP 吗?

python - 与 sympy 集成

python-3.x - 从复杂字典生成列表

javascript - 将鼠标悬停在子元素上时如何显示内容并保持其可见性?

python - 在Python中打包字符串的正确方法

html - 如何增加变换: scale radius when hover

css - :hover above img doesnt work properly with display block under IE & Opera