如何在 Bokeh 中为跨度注释添加标签?到目前为止,我已经看到了标签本身,有没有更好的方法将标签绑定到跨度?
如果你想要一个标签附加到 Span,你只需要将位置设置为相同。
from bokeh.models import Span, Label
from bokeh.plotting import figure
p = figure(plot_height=400, plot_width=400)
# Initialize your span and label
my_span = Span(location=0, dimension='height')
p.renderers.extend([my_span,])
my_label = Label(x=0, y=200, y_units='screen', text='Test label')
p.add_layout(my_label)
Run Code Online (Sandbox Code Playgroud)
注意在此示例中,此标签的y坐标是通过使用y_units=screen参数以像素坐标指定的。它也可以在情节坐标中,只是不要传递screen参数。
然后,您可以像这样更新他们的位置:
def update():
my_span.set(location=my_slider.value)
my_label.set(x=my_slider.value)
Run Code Online (Sandbox Code Playgroud)
以供参考: