您可以使用HoverTool和CustomJS,如下面的代碼示例所示。這個例子畫出從光標位置到(1,1)點的一條線。打開JavaScript控制檯時,您可以在移動鼠標時看到x和y的值。
from bokeh.plotting import figure,show, ColumnDataSource
from bokeh.models import CustomJS, HoverTool
import numpy as np
s = ColumnDataSource(data = dict(x=[0,1],y=[0,1])) #points of the line
callback = CustomJS(args=dict(s=s), code="""
var geometry = cb_data['geometry'];
var x_data = geometry.x; // current mouse x position in plot coordinates
var y_data = geometry.y; // current mouse y position in plot coordinates
console.log("(x,y)=" + x_data+","+y_data); //monitors values in Javascript console
var x = s.get('data')['x'];
var y = s.get('data')['y'];
x[0] = x_data;
y[0] = y_data;
s.trigger('change');
""")
hover_tool = HoverTool(callback=callback)
p = figure(x_range=(0,1), y_range=(0,1), tools= [hover_tool,
"crosshair,box_zoom,wheel_zoom,pan,reset"])
p.line(x='x',y='y',source=s)
show(p)
的JavaScript控制檯輸出:
...
VM615:7 (x,y)=0.37494791666666666,0.37447916666666664
VM615:7 (x,y)=0.37494791666666666,0.37114583333333334
VM615:7 (x,y)=0.37161458333333336,0.37114583333333334
VM615:7 (x,y)=0.38828125,0.37114583333333334
VM615:7 (x,y)=0.43161458333333336,0.3878125
VM615:7 (x,y)=0.7216145833333333,0.4878125
...
這非常適用於鼠標的位置,但可以在CustomJS適應的點擊?謝謝! –
您可以使用TapTool來對點擊字形上的事件做出反應。在我的答案嘗試在這裏看到一個例子:http://stackoverflow.com/questions/41640536/bokeh-how-to-make-hovertool-tooltips-stick-to-points-on-click –