[英]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)
dx
和dy
。
根據 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.