Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add labels to dots in bokeh?

Tags:

bokeh

So what I'd like to do is a simple figure with lines and circles like http://docs.bokeh.org/en/latest/docs/quickstart.html#getting-started but with labels which show after mouseover over the circles.

Would that be possible?

like image 260
ditoslav Avatar asked Feb 11 '23 07:02

ditoslav


1 Answers

From what I understand HoverTool is what you are looking for. You can see an example of it being used on rect glyphs instead of circles (and lines) but that should be the final result.

Here's a modified version of the line example with circle glyphs and a hover tool:

from collections import OrderedDict
import numpy as np

from bokeh.plotting import *
from bokeh.models import HoverTool

x = np.linspace(0, 4*np.pi, 200)
y = np.sin(x)

output_file("line_dots.html", title="line.py example")

source = ColumnDataSource(
    data=dict(
        x=x,
        y=y,
        label=["%s X %s" % (x_, y_) for x_, y_ in zip(x, y)]
    )
)
TOOLS="crosshair,pan,wheel_zoom,box_zoom,reset,hover,previewsave"
p = figure(title="simple line example", tools=TOOLS)
p.line('x', 'y', color="#2222aa", line_width=2, source=source)
p.circle('x', 'y', color="#2222aa", line_width=2, source=source)

hover =p.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([
    ("index", "$index"),
    ("(xx,yy)", "(@x, @y)"),
    ("label", "@label"),
])

show(p)
like image 163
Fabio Pliger Avatar answered May 16 '23 02:05

Fabio Pliger