简体   繁体   English

在散景中将多行字形悬停时如何显示单个值?

[英]How to show a single value when hovering a Multiline glyph in Bokeh?

As for now I see Bokeh supports HoverTool for multi_line glyph. 到目前为止,我看到Bokeh支持HoverTool用于multi_line字形。 But the problem is that if I want to display particular value for point - it shows all the list of values instead of it. 但是问题是,如果我要显示点的特定值-它会显示所有值列表而不是它。

Please, see example below: 请参阅以下示例:

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)

在此处输入图片说明

I know about $x, $y ability, but that shows coordinated under the mouse, and they are changing as you move a mouse, that is not desired behaviour. 我知道$ x,$ y能力,但这在鼠标下显示协调,并且在您移动鼠标时它们在变化,这不是您期望的行为。

It there a way to display exact value for hover point in multi_line glyph? 有没有办法在multi_line字形中显示悬停点的确切值?

ps creating invisible line is not a solution, as I have more advanced plot with filtering and linked plots and so on. ps创建不可见的线不是解决方案,因为我有带有过滤和链接图等更高级的图。

Thanks! 谢谢!

If you update to the bokeh version 0.12.16 you can use the new class CustomJSHover like this: 如果更新到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)

Following on from Dmitriy's comment in the above answer as he said data_x and data_y are now exposed so you just need to change your code to: Dmitriy在上述答案中的评论紧随其后,他说data_x和data_y现在公开了,因此您只需要将代码更改为:

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

But what's nice about this is you can use the Bokeh formatters. 但是,这样做的好处是您可以使用Bokeh格式化程序。 eg this is one I used for values on a date time axis eg: 例如,这是我用于日期时间轴上的值的一个,例如:

plot.add_tools(HoverTool(tooltips=
    [
        ('Date',  '$data_x{%F}'),
        ('Level', '$data_y{0,0.000000}'),
        ('Ticket', '@ticket')
    ],
    formatters={
        '$data_x': 'datetime',
    }
))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM