HoverTool for multiple data series in bokeh scatter plot

后端 未结 2 660
无人共我
无人共我 2020-12-09 11:13

I have the following small example script making use of numpy and bokeh:

import numpy as np
import bokeh.plotting as bp
from bokeh.objects import HoverTool 
         


        
相关标签:
2条回答
  • 2020-12-09 11:49

    The original answer was ancient and outdated, here is how to accomplish this with any modern version of Bokeh:

    from bokeh.plotting import figure, show
    import numpy as np
    
    x = np.linspace(0, 2*np.pi)
    y1 = np.sin(x)
    y2 = np.cos(x)
    
    fig = figure(tools="reset", tooltips=[("x", "$x"), ("y", "$y")])
    s1 = fig.scatter(x, y1, color='#0000ff', size=10, legend_label='sine')
    s2 = fig.scatter(x, y2, color='#ff0000', size=10, legend_label='cosine')
    
    show(fig)
    
    0 讨论(0)
  • 2020-12-09 12:07

    Edit: Note that the approach below is necessary only if you want different tooltips for different glyphs. If you want the same tooltips for all glyphs, see the answer above.


    If you want to have multiple hover tools, you need to add multiple hover tools, each configured for a different renderer. You can add them this way:

    p = figure()
    
    r1 = p.circle([1,2,3], [4,5,6], color="blue")
    p.add_tools(HoverTool(renderers=[r1], tooltips=TIPS))
    
    r2 = p.square([4,5,6], [1,2,3], color="red")
    p.add_tools(HoverTool(renderers=[r2], tooltips=TIPS))
    
    0 讨论(0)
提交回复
热议问题