Python 将多个bokeh HoverTool实例与models API一起使用

Python 将多个bokeh HoverTool实例与models API一起使用,python,python-3.x,plot,hover,bokeh,Python,Python 3.x,Plot,Hover,Bokeh,我想在一个绘图中使用多个Hovertools以及Hovertool的names属性来选择应用每个工具。比如说 hover1 = HoverTool(tooltips=[("group", "1")], names = ['line1']) hover2 = HoverTool(tooltips=[("group", "2")], names = ['lines2']) 以及两个数据来源: source1 = ColumnDataSource(data=dict( xs=[[1, 3, 2],

我想在一个绘图中使用多个Hovertools以及Hovertool的names属性来选择应用每个工具。比如说

hover1 = HoverTool(tooltips=[("group", "1")], names = ['line1'])
hover2 = HoverTool(tooltips=[("group", "2")], names = ['lines2'])
以及两个数据来源:

source1 = ColumnDataSource(data=dict(
xs=[[1, 3, 2], [3, 4, 6, 6]],
ys=[[2, 1, 4], [4, 7, 8, 5]], 
))

source2 = ColumnDataSource(data=dict(
xs=[[1, 3, 2], [6, 7, 9, 8]],
ys=[[-1, 0, 1], [1, 1, 2, 1]]
))
我认为下面使用bokeh.models API应该做我想做的事情

p = figure(plot_width=400, plot_height=400)
l1 = MultiLine(xs='xs', ys='ys', name='lines1')
l2 = MultiLine(xs='xs', ys='ys', name='lines2')
p.add_tools(hover)
p.add_tools(hover2)
p.add_glyph(source1, l1)
p.add_glyph(source2, l2)
show(p)
遗憾的是,结果图中的鼠标悬停工具不起作用,即不显示工具提示。使用bokeh.plotting API如下,一切正常:

p = figure(plot_width=400, plot_height=400, tools=[hover, hover2])
p.multi_line(xs='xs', ys='ys', source=source1, name='lines1')
p.multi_line(xs='xs', ys='ys', source=source2, name='lines2')
show(p)
问题:如何使用bokeh.models API复制bokeh.plotting API的结果?

中HoverTool模型的名称属性:

名称:属性类型:列表字符串

要查询的名称的列表。如果已设置,则仅使用名称属性具有匹配值的渲染器

用这个

l1 = MultiLine(xs='xs', ys='ys', name='lines1')
将名称指定给多行对象,该对象是一个图示符,而不是渲染器。所以试试这个吧

from bokeh.io import output_notebook, show
output_notebook()

import numpy as np
from bokeh.plotting import figure
from bokeh.models.sources import ColumnDataSource
from bokeh.models.glyphs import MultiLine
from bokeh.layouts import row
from bokeh.models.tools import HoverTool

source = ColumnDataSource(data=dict(
        xs1=[[1, 2, 3], [5, 6, 7]],
        ys1=[[1, 2, 3], [6, 5, 7]],
        xs2=[[7, 8, 9], [1, 2, 3]],
        ys2=[[4, 5, 7], [6, 7, 2]],
    )
)

hover1 = HoverTool(tooltips=[("group", "1")], names = ['lines1'])
hover2 = HoverTool(tooltips=[("group", "2")], names = ['lines2'])

p = figure(plot_width=400, plot_height=400)
l1 = MultiLine(xs='xs1', ys='ys1')
l2 = MultiLine(xs='xs2', ys='ys2')
r1 = p.add_glyph(source, l1, name='lines1')  # the name is assigned to the renderer
r2 = p.add_glyph(source, l2, name='lines2')
# r1.name = 'lines1'  # or you could assign the name like this as well
# r2.name = 'lines2'

p.add_tools(hover1)
p.add_tools(hover2)

# p = figure(plot_width=400, plot_height=400, tools=[hover1, hover2])
# p.multi_line(xs='xs1', ys='ys1', source=source, name='lines1')
# p.multi_line(xs='xs2', ys='ys2', source=source, name='lines2')


show(p)
中悬停工具模型的“名称”属性:

名称:属性类型:列表字符串

要查询的名称的列表。如果已设置,则仅使用名称属性具有匹配值的渲染器

用这个

l1 = MultiLine(xs='xs', ys='ys', name='lines1')
将名称指定给多行对象,该对象是一个图示符,而不是渲染器。所以试试这个吧

from bokeh.io import output_notebook, show
output_notebook()

import numpy as np
from bokeh.plotting import figure
from bokeh.models.sources import ColumnDataSource
from bokeh.models.glyphs import MultiLine
from bokeh.layouts import row
from bokeh.models.tools import HoverTool

source = ColumnDataSource(data=dict(
        xs1=[[1, 2, 3], [5, 6, 7]],
        ys1=[[1, 2, 3], [6, 5, 7]],
        xs2=[[7, 8, 9], [1, 2, 3]],
        ys2=[[4, 5, 7], [6, 7, 2]],
    )
)

hover1 = HoverTool(tooltips=[("group", "1")], names = ['lines1'])
hover2 = HoverTool(tooltips=[("group", "2")], names = ['lines2'])

p = figure(plot_width=400, plot_height=400)
l1 = MultiLine(xs='xs1', ys='ys1')
l2 = MultiLine(xs='xs2', ys='ys2')
r1 = p.add_glyph(source, l1, name='lines1')  # the name is assigned to the renderer
r2 = p.add_glyph(source, l2, name='lines2')
# r1.name = 'lines1'  # or you could assign the name like this as well
# r2.name = 'lines2'

p.add_tools(hover1)
p.add_tools(hover2)

# p = figure(plot_width=400, plot_height=400, tools=[hover1, hover2])
# p.multi_line(xs='xs1', ys='ys1', source=source, name='lines1')
# p.multi_line(xs='xs2', ys='ys2', source=source, name='lines2')


show(p)

一个悬停工具上可以有多个渲染器。但据我所知,在同一个绘图上不能使用多个鼠标悬停工具。您可以使用名为CustomJSHover的回调自定义悬停工具提示。检查documentation@ChesuCR使用plotting API时,会出现关于重复工具的警告,但使用多个悬停工具似乎可以正常工作。每个悬停工具的工具栏中甚至有一个单独的图标,允许单独启用/禁用它。有关多个悬停工具的使用,请参阅。啊!!很高兴知道。我很久以前就尝试过这样做,但没有成功,也许我使用了不同的工具。无论如何,请检查我写的答案是否对您有用。您可以在一个工具上使用多个渲染器。但据我所知,在同一个绘图上不能使用多个鼠标悬停工具。您可以使用名为CustomJSHover的回调自定义悬停工具提示。检查documentation@ChesuCR使用plotting API时,会出现关于重复工具的警告,但使用多个悬停工具似乎可以正常工作。每个悬停工具的工具栏中甚至有一个单独的图标,允许单独启用/禁用它。有关多个悬停工具的使用,请参阅。啊!!很高兴知道。我很久以前就尝试过这样做,但没有成功,也许我使用了不同的工具。不管怎样,检查一下我写的答案是否对你有用!非常感谢您不仅花时间寻找解决方案,而且还解释了解决方案:很好!!如果有效,请将帖子标记为正确答案。谢谢你,你的工作真有魅力!非常感谢您不仅花时间寻找解决方案,而且还解释了解决方案:很好!!如果有效,请将帖子标记为正确答案。非常感谢。