如何在bokeh图的同一行上放置多个注释?

问题描述

因此,我正在制作一些散景图,我想在绘图区域外添加一些文本。假设我要在右上角放置一个注释,在左上角放置另一个注释。我遇到的问题是bokeh是垂直堆叠这两个注解,但我希望它们在水平方向上处于同一行。有什么办法吗?

代码

from bokeh.plotting import figure
from bokeh.models import Legend

plot = figure(**plot_params)    
date_label = Legend(items=[('label1',[])],location='center_left',border_line_color=None,margin=0)
plot.add_layout(date_label,'above')

resource_label = Legend(items=[('label2',location='center_right',margin=0)
plot.add_layout(resource_label,'above')

output example

解决方法

您可以使用min_border属性来扩展绘图尺寸,并使用Labelrender_mode=css通过在xy的任意位置添加注释。设置为screen个单位。

from bokeh.plotting import figure,output_notebook,show
from bokeh.models import Label

output_notebook()

p = figure(plot_width=400,plot_height=400)

p.circle([1,2,3,4,5],[6,7,size=20,color="navy",alpha=0.5)
p.border_fill_color = "whitesmoke"
p.min_border = 50

top_left_label = Label(
    x=-35,y=320,x_units='screen',y_units='screen',text='Top Left Label',render_mode='css',background_fill_color='white')

bottom_right_label = Label(
    x=200,y=-45,text='Bottom Right Label',background_fill_color='white')

p.add_layout(top_left_label)
p.add_layout(bottom_right_label)

show(p)

enter image description here

文档中的注释:

使用“保存”工具不会在输出中显示CSS标签。