如何获取bokeh悬停工具来捕捉数据? [英] How can I get the bokeh hover tool to snap to the data?

查看:103
本文介绍了如何获取bokeh悬停工具来捕捉数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望bokeh悬停工具能够捕捉到数据点,而不是在行上插入鼠标位置.这是我认为可以执行的代码,但是我仍然在显示中获得插值数据.

I would like the bokeh hover tool to snap to the data points instead of interpolating the mouse position on the line. Here's the code that I thought would do it, but I'm still getting interpolated data in the display.

from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool

# prepare some data
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y0 = [i**2 for i in x]
y1 = [10**i for i in x]
y2 = [10**(i**2) for i in x]

# output to static HTML file
output_file("log_lines.html")

hover = HoverTool(tooltips=[
    ("index", "$index"),
    ("(x,y)", "(@x, @y)"),
    ("desc", "@desc"),
])
hover.point_policy='snap_to_data'
hover.line_policy='none'

# create a new plot
p = figure(
   tools="pan,box_zoom,reset,save,hover",
   y_axis_type="log", y_range=[0.001, 10**11], title="log axis example",
   x_axis_label='sections', y_axis_label='particles'
)

# add some renderers
p.line(x, x, legend="y=x")
p.circle(x, x, legend="y=x", fill_color="white", size=8)
p.line(x, y0, legend="y=x^2", line_width=3)
p.line(x, y1, legend="y=10^x", line_color="red")
p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6)
p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4")

# show the results
show(p)

推荐答案

您没有将自定义的HoverTool实例传递给绘图.您需要这样做:

You're not passing your custom HoverTool instance to the plot. You need to do:

...
p = figure(tools="pan,box_zoom,reset,save",
           y_axis_type="log", y_range=[0.001, 10**11], title="log axis example",
           x_axis_label='sections', y_axis_label='particles')
p.add_tools(hover) # this is your custom HoverTool
...

这篇关于如何获取bokeh悬停工具来捕捉数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆