散景粘十字线

问题描述 投票:0回答:1

Bokeh中,是否有类似于plotly中的尖峰线的工具? 十字准线工具与此接近,但是否可以使其坚持绘图?

python bokeh
1个回答
0
投票

我不知道

bokeh
中实现了粘性工具,但可以使用
HoverTool
CustomJS
对其进行原型设计。

下面的代码需要一些解释。

  1. 我将范围设置为轴以获得
    p.x_range.start
    p.y_range.start
    。默认情况下这些不存在。
  2. 我必须收集每种颜色的渲染器以将其注册到正确的回调。如果有多种颜色,我必须添加多个HoverTools
  3. 目前 JS 代码要求提供数据源的正确名称
    a
    。这可以改进。

最小示例

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, CustomJS, HoverTool

source = ColumnDataSource(dict(a=[1,2,3,4,5], x=[1,2,3,4,5]))

p = figure(x_range=(0.9,5.1), y_range=(0.9,5.1))

p.line('a', 'x', source=source, color='red')
cr = p.circle('a', 'x', source=source, color='red')

extra_source = ColumnDataSource({'x0': [], 'y0': [], 'x1': [], 'y1': []})
sb = p.segment(
    x0='x0',
    y0='y0',
    x1='x1',
    y1='y1',
    color='red',
    alpha=0.6,
    line_width=2,
    line_dash="dashed",
    source=extra_source
)

# add a hover tool that sets the link data for a hovered circle
code = """
const data = {'x0': [],'y0': [],'x1': [],'y1': []}
const indices = cb_data.index.indices
console.log(cb_data)
for (let i of indices) {
    // horizontal dashed line
    data['x0'].push(p.x_range.start)
    data['y0'].push(circle.data.a[i])
    data['x1'].push(circle.data.x[i])
    data['y1'].push(circle.data.a[i])

    // vertical dashed line
    data['x0'].push(circle.data.x[i])
    data['y0'].push(p.y_range.start)
    data['x1'].push(circle.data.x[i])
    data['y1'].push(circle.data.a[i])
}
segment.data = data
"""

callback = CustomJS(args={'circle': cr.data_source, 'segment': sb.data_source, 'p':p}, code=code)
p.add_tools(HoverTool(tooltips=None, callback=callback, renderers=[cr]))

show(p)

输出

© www.soinside.com 2019 - 2024. All rights reserved.