0

我试图使用 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)

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

这是我的散景图:

在此处输入图像描述

但是,我想通过适当的标签来实现这样的目标:

在此处输入图像描述

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

4

1 回答 1

1

需要更多微调 -

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)

在此处输入图像描述

于 2020-04-18T22:43:40.470 回答