繁体   English   中英

图例在散景图中的位置

[英]Position of the legend in a Bokeh plot

有谁知道如何在图表的散景中携带图例? 我能做的唯一操作是在以下位置中选择一个位置:

top_right, top_left, bottom_left or bottom_right

使用:

legend()[0].orientation = "bottom_left"

当我尝试不同的时,我收到错误消息:

ValueError: invalid value for orientation: 'outside'; allowed values are top_right, top_left, bottom_left or bottom_right

从 Bokeh 0.12.4 ,可以将图例0.12.4在中心绘图区域之外。 这是用户指南中的一个简短示例:

import numpy as np
from bokeh.models import Legend
from bokeh.plotting import figure, show, output_file

x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)

output_file("legend_labels.html")

p = figure(toolbar_location="above")

r0 = p.circle(x, y)
r1 = p.line(x, y)

r2 = p.line(x, 2*y, line_dash=[4, 4], line_color="orange", line_width=2)

r3 = p.square(x, 3*y, fill_color=None, line_color="green")
r4 = p.line(x, 3*y, line_color="green")

legend = Legend(items=[
    ("sin(x)",   [r0, r1]),
    ("2*sin(x)", [r2]),
    ("3*sin(x)", [r3, r4])
], location=(0, -30))

p.add_layout(legend, 'right')

show(p)

要调整位置,请更改location=(dx, dy) dxdy

在此处输入图片说明

根据 Bokeh 文档和 bigreddot,一种方法是使用Legend命令。 我找到了另一种方法。

如果在诸如 quad() 或 line() 之类的绘图函数中使用legend_label参数,则绘图标签将附加到p.legend 图例的位置由p.legend.location = "center"作为正常方式定义。

要将图例放在外面,您应该使用p.add_layout(p.legend[0], 'right')

这是上面的代码以不同的方式。

import numpy as np

from bokeh.plotting import figure, output_file, show

x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)

output_file("legend_labels.html")

p = figure()

p.circle(x, y, legend_label="sin(x)")
p.line(x, y, legend_label="sin(x)")

p.line(x, 2*y, legend_label="2*sin(x)",
       line_dash=[4, 4], line_color="orange", line_width=2)

p.square(x, 3*y, legend_label="3*sin(x)", fill_color=None, line_color="green")
p.line(x, 3*y, legend_label="3*sin(x)", line_color="green")

p.legend.location = "center"

########################################################
# This line puts the legend outside of the plot area

p.add_layout(p.legend[0], 'right')
########################################################

show(p)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM