如何在Bokeh中为点添加标签?

4
1个回答

8
据我理解,您需要的工具是HoverTool。您可以在矩形图形上使用它进行示例,而不是圆圈(和线条),但那应该是最终结果。
以下是一个修改后的折线图示例,其中包括圆圈图形和悬停工具。
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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接