multi_line 的 Bokeh HoverTool 工具提示 - 显示所有点的所有坐标
Bokeh HoverTool tooltips for multi_line - displaying all coordinates at all points
我修改了官方散景使用指南页面上提供的 multi_line 绘图示例,以添加带有工具提示的 HoverTool。 (Usage guide example)
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, HoverTool
output_file("patch.html")
plot_data=dict(
xs=[[1, 3, 2], [3, 4, 6, 6]],
ys=[[2, 1, 4], [4, 7, 8, 5]],
colors=["firebrick", "navy"],
alphas=[0.8, 0.3])
hover=HoverTool(tooltips=[
('X-Coordinate','@xs'),
('Y-Coordinate','@ys')])
dsource = ColumnDataSource(plot_data)
p = figure(plot_width=400, plot_height=400, tools=[hover, 'wheel_zoom', ])
p.multi_line('xs', 'ys', color='colors', alpha='alphas', line_width=4,
source=dsource)
show(p)
我希望工具提示显示我将指针悬停(靠近)的点的 x 和 y 坐标。但是,工具提示包含指针悬停的所有线点的 x 和 y 坐标。
有没有埋在某个地方的way/option(我费了好大劲才找到它)来让HoverTool-工具显示单个点的坐标?
P.S。 - 我知道 $x, $y 和 $sx, $sy 可用于显示屏幕和 canvas x, y 坐标分别但在我的情况下 x 轴也可以是日期时间轴,其中如果我想要工具提示中的一个日期而不是所有日期。
制作一条不可见的线,并将其设置到hover.renderers
:
import numpy as np
from itertools import chain
nan = itertools.repeat([np.nan])
xs, ys = (np.concatenate(list(chain(*zip(plot_data[name], nan)))) for name in ["xs", "ys"])
dsource2 = ColumnDataSource(dict(xs=xs, ys=ys))
line = p.line('xs', 'ys', source=dsource2, alpha=0)
hover.renderers = [line]
发布完整代码的答案,包括 HYRY 提供的解决方案:
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, HoverTool
import numpy as np
import itertools
from itertools import chain
output_file("patch.html")
plot_data=dict(
xs=[[1, 3, 2], [3, 4, 6, 6]],
ys=[[2, 1, 4], [4, 7, 8, 5]],
colors=["firebrick", "navy"],
alphas=[0.8, 0.3])
hover=HoverTool()
dsource = ColumnDataSource(plot_data)
p = figure(plot_width=400, plot_height=400, tools=[hover, 'wheel_zoom', ])
p.multi_line('xs', 'ys', color='colors', alpha='alphas', line_width=4, source=dsource)
nan = itertools.repeat([np.nan])
xs, ys = (np.concatenate(list(chain(*zip(plot_data[name], nan)))) for name in ["xs", "ys"])
dsource2 = ColumnDataSource(dict(xs=xs, ys=ys))
line = p.line('xs', 'ys', source=dsource2, color='white', line_width=1, alpha=1)
hover.renderers = [line]
show(p)
我已经修改了 'invisible' 线的属性,以指示它是如何在现有线上绘制的,从而提供坐标。输出现在看起来像:
我修改了官方散景使用指南页面上提供的 multi_line 绘图示例,以添加带有工具提示的 HoverTool。 (Usage guide example)
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, HoverTool
output_file("patch.html")
plot_data=dict(
xs=[[1, 3, 2], [3, 4, 6, 6]],
ys=[[2, 1, 4], [4, 7, 8, 5]],
colors=["firebrick", "navy"],
alphas=[0.8, 0.3])
hover=HoverTool(tooltips=[
('X-Coordinate','@xs'),
('Y-Coordinate','@ys')])
dsource = ColumnDataSource(plot_data)
p = figure(plot_width=400, plot_height=400, tools=[hover, 'wheel_zoom', ])
p.multi_line('xs', 'ys', color='colors', alpha='alphas', line_width=4,
source=dsource)
show(p)
我希望工具提示显示我将指针悬停(靠近)的点的 x 和 y 坐标。但是,工具提示包含指针悬停的所有线点的 x 和 y 坐标。
有没有埋在某个地方的way/option(我费了好大劲才找到它)来让HoverTool-工具显示单个点的坐标?
P.S。 - 我知道 $x, $y 和 $sx, $sy 可用于显示屏幕和 canvas x, y 坐标分别但在我的情况下 x 轴也可以是日期时间轴,其中如果我想要工具提示中的一个日期而不是所有日期。
制作一条不可见的线,并将其设置到hover.renderers
:
import numpy as np
from itertools import chain
nan = itertools.repeat([np.nan])
xs, ys = (np.concatenate(list(chain(*zip(plot_data[name], nan)))) for name in ["xs", "ys"])
dsource2 = ColumnDataSource(dict(xs=xs, ys=ys))
line = p.line('xs', 'ys', source=dsource2, alpha=0)
hover.renderers = [line]
发布完整代码的答案,包括 HYRY 提供的解决方案:
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, HoverTool
import numpy as np
import itertools
from itertools import chain
output_file("patch.html")
plot_data=dict(
xs=[[1, 3, 2], [3, 4, 6, 6]],
ys=[[2, 1, 4], [4, 7, 8, 5]],
colors=["firebrick", "navy"],
alphas=[0.8, 0.3])
hover=HoverTool()
dsource = ColumnDataSource(plot_data)
p = figure(plot_width=400, plot_height=400, tools=[hover, 'wheel_zoom', ])
p.multi_line('xs', 'ys', color='colors', alpha='alphas', line_width=4, source=dsource)
nan = itertools.repeat([np.nan])
xs, ys = (np.concatenate(list(chain(*zip(plot_data[name], nan)))) for name in ["xs", "ys"])
dsource2 = ColumnDataSource(dict(xs=xs, ys=ys))
line = p.line('xs', 'ys', source=dsource2, color='white', line_width=1, alpha=1)
hover.renderers = [line]
show(p)
我已经修改了 'invisible' 线的属性,以指示它是如何在现有线上绘制的,从而提供坐标。输出现在看起来像: