22

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

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
4

2 回答 2

24

从散景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)

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

在此处输入图像描述

于 2016-07-01T13:08:08.730 回答
1

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

如果在绘图函数(例如 quad() 或 line())中使用legend_labelp.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)
于 2021-06-02T20:40:06.317 回答