How do I link the CrossHairTool in bokeh over several plots?

后端 未结 5 1272
逝去的感伤
逝去的感伤 2020-12-30 07:34

When moving the crosshair (dimensions=width) in one plot I want to see the same position in the other plot(s). My plots share the same x-axis.

Here is the plot setup

5条回答
  •  感动是毒
    2020-12-30 08:05

    This answer is for the people who liked Graeme's solution just as I did, but need to apply it to more than two figures just as I did:

    from bokeh.models import CustomJS, CrosshairTool
    
    def add_vlinked_crosshairs(figs):
        js_leave = ''
        js_move = 'if(cb_obj.x >= fig.x_range.start && cb_obj.x <= fig.x_range.end &&\n'
        js_move += 'cb_obj.y >= fig.y_range.start && cb_obj.y <= fig.y_range.end){\n'
        for i in range(len(figs)-1):
            js_move += '\t\t\tother%d.spans.height.computed_location = cb_obj.sx\n' % i
        js_move += '}else{\n'
        for i in range(len(figs)-1):
            js_move += '\t\t\tother%d.spans.height.computed_location = null\n' % i
            js_leave += '\t\t\tother%d.spans.height.computed_location = null\n' % i
        js_move += '}'
        crosses = [CrosshairTool() for fig in figs]
        for i, fig in enumerate(figs):
            fig.add_tools(crosses[i])
            args = {'fig': fig}
            k = 0
            for j in range(len(figs)):
                if i != j:
                    args['other%d'%k] = crosses[j]
                    k += 1
            fig.js_on_event('mousemove', CustomJS(args=args, code=js_move))
            fig.js_on_event('mouseleave', CustomJS(args=args, code=js_leave))
    

提交回复
热议问题