首页 > 解决方案 > 如何在散景图中的同一行上放置多个注释?

问题描述

所以我正在制作一些散景图,我想在绘图区域之外添加一些文本。假设我想将一个注释放在右上角上方,另一个放在左上角上方。我遇到的问题是散景将这两个注释垂直堆叠,但我希望它们水平位于同一条线上。有没有办法做到这一点?

代码:

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', border_line_color=None, margin=0)
plot.add_layout(resource_label, 'above')

输出示例

标签: bokehmultiline

解决方案


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

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, 2, 4, 5], 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, x_units='screen', y_units='screen',
    text='Bottom Right Label', render_mode='css', background_fill_color='white')

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

show(p)

在此处输入图像描述

来自文档的注释:

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


推荐阅读