python - 将多个 Bokeh HoverTool 实例与模型 API 一起使用

标签 python python-3.x plot hover bokeh

我想在一个绘图中使用多个 Hovertool 以及 Hovertoolnames 属性来选择性地应用每个工具。举个例子

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)

唉,结果图中的Hovertool不起作用(即没有显示工具提示)。按如下方式使用 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 的结果?

最佳答案

Bokeh DocumentationHoverTool 模型的 names 属性:

names: property type: List ( String )

A list of names to query for. If set, only renderers that have a matching value for their name attribute will be used.

有了这个

l1 = MultiLine(xs='xs', ys='ys', name='lines1')

您正在为 Multiline 对象分配名称,这是一个字形,而不是渲染器。所以试试这个吧

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)

关于python - 将多个 Bokeh HoverTool 实例与模型 API 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50835979/

相关文章:

python - 我怎样才能并行化这个字数统计功能?

python - 管理员尚未同意使用该应用程序 - Azure AD

python - 为什么两个链接的 gtk 切换按钮会出现运行时错误?

Windows 上的 Python CPU 时间

python - 尝试学习 python 类

用于坐标平面上的线的 Javascript 图表库

如果 streams=True,Python Requests 模块不处理超时?

python - 值错误: No data provided for "dense_input"

plot.window(...) 中的 R 错误需要有限的 'xlim' 值

python - 使用 matplotlib,如何打印 "actual size"?