如何使用HoverTool在Bokeh中显示来自两个不同字形的两个点之间的差异?

时间:2019-04-18 22:09:29

标签: python plot hover bokeh

我在图中有两条线,我想使用HoverTool来移动悬停时每对点的差。

我尝试了'@y' - '@yhat'float('@y') - float('@yhat')@y - @yhat,但没有任何效果。

1 个答案:

答案 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)

结果:

enter image description here