Maj*_*Yel 19 python visualization bokeh
有谁知道如何在图表外的散景中携带图例?我唯一可以做的操作就是选择一个位置:
top_right, top_left, bottom_left or bottom_right
Run Code Online (Sandbox Code Playgroud)
使用:
legend()[0].orientation = "bottom_left"
Run Code Online (Sandbox Code Playgroud)
当我尝试不同的时候,我收到错误信息:
ValueError: invalid value for orientation: 'outside'; allowed values are top_right, top_left, bottom_left or bottom_right
Run Code Online (Sandbox Code Playgroud)
big*_*dot 19
从Bokeh开始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)
Run Code Online (Sandbox Code Playgroud)
调整位置,改变dx和dy进入location=(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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15039 次 |
| 最近记录: |