我有一个包含多条线的图表。我想在悬停时以粗体显示一行。其他线路应该保持不变。
在下面的示例中,工具提示工作正常,但 hover_glyph 没有。如果我将鼠标悬停在一条线上,那么两条线都会变宽。如果我将鼠标悬停在第二行,两行都不会发生任何事情。如何让它工作?
x = np.linspace(0, 4 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
output_notebook()
source = ColumnDataSource(pd.DataFrame(data={'L1': y1, 'L2': y2}, index=x))
f = figure(tools='xwheel_zoom, wheel_zoom, pan', active_scroll='xwheel_zoom', active_drag='pan')
g1 = f.line(x='index', y='L1', source=source, color=Spectral11[1], legend_label='L1', name='L1')
gg1 = g1.hover_glyph = Line(line_color=Spectral11[1], line_width=5)
g2 = f.line(x='index', y='L2', source=source, color=Spectral11[3], legend_label='L2', name='L2')
gg2 = g2.hover_glyph = Line(line_color=Spectral11[3], line_width=4)
hover = HoverTool(renderers=[g2])
hover.tooltips=[
('both fields', '@L1'+':'+'@L2'),
('name', '$y'),
('Number', '$name')
]
f.add_tools(hover)
hover2 = HoverTool(renderers=[g1])
hover2.tooltips=[
('name', '$y'),
('Number', '$name')
]
f.add_tools(hover2)
show(f)
可能的解决方法是使用
multi_line
.
为了完整起见,这里有一个稍微简单、独立、有效的问题演示。当 L1 悬停时,此代码段应突出显示 L1 而不是 L2。相反,当 L1 悬停时,两条线都会突出显示。
from bokeh.io import output_notebook, show
from bokeh.models import HoverTool
from bokeh.palettes import Spectral11
from bokeh.plotting import figure, ColumnDataSource
import numpy as np
output_notebook()
x = np.linspace(0, 4 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
source = ColumnDataSource({'L1': y1, 'L2': y2, 'index': x})
f = figure(tools='')
g1 = f.line(x='index', y='L1', source=source, color=Spectral11[1], legend_label='L1', name='L1',
hover_line_width=4)
g2 = f.line(x='index', y='L2', source=source, color=Spectral11[3], legend_label='L2', name='L2',
hover_line_width=4)
hover = HoverTool(renderers=[g1]) # Activate hover only for L1.
f.add_tools(hover)
show(f)
编辑:添加了可能的解决方法
如果您乐于使用多行,行突出显示功能似乎对他们有用,所以这可能是一种解决方法。不过,我还没有尝试重现原始问题中显示的工具提示。
from bokeh.io import output_notebook, show
from bokeh.models import HoverTool
from bokeh.palettes import Spectral11
from bokeh.plotting import figure, ColumnDataSource
import numpy as np
output_notebook()
x = np.linspace(0, 4 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
source = ColumnDataSource({
"index": [x, x],
"y": [y1, y2],
"color": [Spectral11[1], Spectral11[3]],
"label": ["L1", "L2"]
})
f = figure(tools='')
g = f.multi_line(xs="index", ys="y", source=source, color="color", legend_group="label",
hover_line_width=4)
hover = HoverTool(renderers=[g],
tooltips="@label: $data_x, $data_y")
f.add_tools(hover)
show(f)
两个渲染器是连接的,因为它们共享相同的
source
.
如果你想拆分悬停动作,你必须将
source
分成两个sources
.
import numpy as np
from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.palettes import Spectral11
output_notebook()
x = np.linspace(0, 4 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
source1 = ColumnDataSource({'L1': y1, "index":x})
source2 = ColumnDataSource({'L1': y1,'L2': y2, "index":x})
p = figure(tools='xwheel_zoom, wheel_zoom, pan', active_scroll='xwheel_zoom', active_drag='pan')
g1 = p.line(x='index', y='L1', source=source1, color=Spectral11[1], legend_label='L1', name='L1',
hover_line_color=Spectral11[1], hover_line_width=5)
# workaround for the hover information "both fields"
p.line(x='index', y='L1', source=source2, color=Spectral11[1], legend_label='L1', name='L2', line_width=0)
g2 = p.line(x='index', y='L2', source=source2, color=Spectral11[3], legend_label='L2', name='L2',
hover_line_color=Spectral11[3], hover_line_width=4)
hover = HoverTool(renderers=[g2])
hover.tooltips=[
('both fields', '@L1:@L2'),
('name', '$y'),
('Number', '$name')
]
p.add_tools(hover)
hover2 = HoverTool(renderers=[g1])
hover2.tooltips=[
('name', '$y'),
('Number', '$name')
]
p.add_tools(hover2)
show(p)
点评:你一个悬停工具信息有点棘手。解决方法可能没问题,但远非完美。