如何使用散景在每一行中添加标签

How to add label in each line using Bokeh

我正在尝试使用 Bokeh 创建一个封闭区域。我就是这样做的,

from bokeh.plotting import figure, output_file, show

p = figure(title="line", plot_width=300, plot_height=300)
p.line([1, 2, 3, 4, 5, 1], [6, 7, 8, 7, 3, 6])      
p.scatter([1, 2, 3, 4, 5, 1], [6, 7, 8, 7, 3, 6])


show(p)

现在我想用这个函数找出每一行的长度:

def distance(x1, y1, x2, y2):
    # compute cartesian distance
    return np.sqrt((x1-x2)**2 + (y1-y2)**2)

想将每行中的信息附加为行标签。我有困难。有人可以帮忙吗

这是我的散景图:

但是,我想通过适当的标签实现类似的效果:

如果有人帮助我实现这一目标,我将不胜感激。

需要更多微调 -

import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, Label, LabelSet, Range1d

p = figure(title="line", plot_width=300, plot_height=300)
x = [1, 2, 3, 4, 5, 1]
y = [6, 7, 8, 7, 3, 6]

def distance(x1, y1, x2, y2):
    # compute cartesian distance
    return np.sqrt((x1-x2)**2 + (y1-y2)**2)

dist = []
x_mid = []
y_mid = []

for i in range(0,len(x)-1):
    if (i != len(x)-1):
        dist1 = round(distance(x[i],y[i],x[i+1],y[i+1]),2)
        x_mid1 = (x[i] + x[i+1])/2
        y_mid1 = (y[i] + y[i+1])/2
    else:
        dist1 = round(distance(x[i],y[i],x[0],y[0]),2)
        x_mid1 = (x[i] + x[0])/2
        y_mid1 = (y[i] + y[0])/2
    dist.append(dist1)
    x_mid.append(x_mid1)
    y_mid.append(y_mid1) 


source = ColumnDataSource(data=dict(x_mid = x_mid,y_mid = y_mid,dist = dist))

p.line(x, y)      
p.scatter(x, y)

labels = LabelSet(x= 'x_mid', y= 'y_mid', text='dist', level='glyph',
              x_offset=5, y_offset=5, source= source, render_mode='canvas', text_font_size="8pt")

p.add_layout(labels)
show(p)