Bokeh:绘制多条单独的线

2024-09-27 23:15:51 发布

您现在位置:Python中文网/ 问答频道 /正文

第1期:

我想不出如何画出多条不同的线,除了这张图:

main_time_line = p.line(x=(start, stop), y=(0, 0))

g1 = p.square(source=source, x='examination__date', y=0, size=4,
              color='black', name='g1')

hover_tool.renderers.append(g1)

g2 = p.circle(source=source, x='examination__date', y='level', size=15)

for i, (idate, ilevel, iname) in enumerate(zip(source.data['examination__date'],
                                               source.data['level'],
                                               source.data['examination__name'])):
    vert = 'top' if ilevel < 0 else 'bottom'
    horizontal = 'right' if ilevel < 0 else 'left'
    yoff = -10 if ilevel < 0 else 10
    p.line(x=idate, y=(0, ilevel), color='black', line_width=3)
    my_txt = Label(x=idate, 
                   y=ilevel, 
                   text=iname, 
                   text_align=horizontal,
                   text_baseline=vert, 
                   text_font_size='13px', 
                   y_offset=yoff)
    p.add_layout(my_txt)

上述结果是: enter image description here

视觉效果或多或少和我所说的一样,但是垂直线是用for循环绘制的,这给小部件带来了问题,即:复选框只对正方形和圆形起作用。你知道吗

我猜这是因为垂直线是在没有源参数的情况下绘制的,因此通过JS回调发出更改不会更新它们的数据(源是来自pandas dataframe的ColumDataSource) 在使用source arg时,我无法按原样绘制它们。你知道吗

第2期: 假设我放弃for循环,我就无法操作文本的放置

有什么建议吗?你知道吗


Tags: textsourcefordatasizedateifline
1条回答
网友
1楼 · 发布于 2024-09-27 23:15:51

对文档的进一步检查使我发现了片段和标签集,因此为了达到我想要的效果,我只需向ColumnDataSource添加一个由0组成的“零”列表。你知道吗

我相信有很多其他的方法来处理它。你知道吗

main_time_line = p.line(x=(start, stop), y=(0, 0), color='blue')

g1 = p.square(source=source, x='examination__date', y=0, size=4, 
              color='black', name='g1')

hover_tool.renderers.append(g1)

g2 = p.circle(source=source, x='examination__date', y='level', size=15)

g3 = p.segment(source=source, 
               x0='examination__date', 
               y0='zeroes',
               x1='examination__date', 
               y1='level',
               color="#F4A582", 
               line_width=3)

labels = LabelSet(x='examination__date', y='level', text='examination__name', 
                  level='glyph', x_offset=5, y_offset=5, source=source, 
                  render_mode='canvas')

p.add_layout(labels)

相关问题 更多 >

    热门问题