如何在散景中为点添加标签?

时间:2015-04-27 13:58:59

标签: bokeh

所以我想做的是一个简单的数字,有线条和圆圈之类的 http://bokeh.pydata.org/en/latest/docs/quickstart.html#getting-started 但是鼠标悬停在圆圈上后会显示标签。

这可能吗?

1 个答案:

答案 0 :(得分:5)

根据我的理解HoverTool正是您所寻找的。您可以看到它用于矩形字形而不是圆(和线)的示例,但这应该是最终结果。

这是line example的修改版本,带有圆圈字形和悬停工具:

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)