我在图中有两条线,我想使用HoverTool
来移动悬停时每对点的差。
我尝试了'@y' - '@yhat'
,float('@y') - float('@yhat')
,@y - @yhat
,但没有任何效果。
答案 0 :(得分:1)
可以在HoverTool上通过这样的JS回调进行安排(散景v1.1.0):
from bokeh.plotting import figure, show
from bokeh.models import CustomJS, HoverTool, ColumnDataSource
p = figure()
l1 = p.line([1, 2, 3, 4, 5], [14, 16, 11, 12, 17], color = 'green')
l2 = p.line([1, 2, 3, 4, 5], [15, 10, 19, 18, 13], color = 'red')
code = ''' var index = cb_data.index['0d'].indices[0];
if (index != null){
v1 = String(l1.data_source.data['y'][index]);
v2 = String(l2.data_source.data['y'][index]);
diff = String(v1 - v2);
hover.tooltips = [["x", "@x"], ["y1", v1], ["y2", v2], ["diff", diff]];
} '''
hover = HoverTool(mode = 'vline', line_policy = 'prev', point_policy = 'snap_to_data', renderers = [l1])
hover.callback = CustomJS(args = dict(l1 = l1, l2 = l2, hover = hover), code = code)
p.add_tools(hover)
show(p)
结果: