答案 0 :(得分:3)
我发现仍然在Bokeh库中,他们没有实现 为图例添加标题,但我实现的解决方案如下: 在添加任何其他图表之前,添加一个非常小的白点。
Example_Code:
import numpy as np
from bokeh.plotting import output_file, figure, show
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
legend_title = "Math Functions" #Legend Title
output_file("legend_labels.html")
p = figure()
p.circle(0, 0, size=0.00000001, color= "#ffffff", legend=legend_title)
p.circle(x, y, legend="sin(x)")
p.line(x, y, legend="sin(x)")
p.line(x, 2*y, legend="2*sin(x)",
line_dash=[4, 4], line_color="orange", line_width=2)
p.square(x, 3*y, legend="3*sin(x)", fill_color=None, line_color="green")
p.line(x, 3*y, legend="3*sin(x)", line_color="green")
p.legend.label_text_font = "times"
p.legend.label_text_font_style = "italic"
p.legend.label_text_color = "navy"
show(p)
答案 1 :(得分:1)
从Bokeh(1.2)的最新版本开始,您可以按如下方式在图例中添加标题:
import numpy as np
from bokeh.layouts import gridplot
from bokeh.plotting import figure, show, output_file
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select"
p1 = figure(title="Legend Example", tools=TOOLS)
p1.circle(x, y, legend="sin(x)")
p1.circle(x, 2*y, legend="2*sin(x)", color="orange")
p1.circle(x, 3*y, legend="3*sin(x)", color="green")
p1.legend.title = 'Example Title'
p2 = figure(title="Another Legend Example", tools=TOOLS)
p2.circle(x, y, legend="sin(x)")
p2.line(x, y, legend="sin(x)")
p2.line(x, 2*y, legend="2*sin(x)", line_dash=(4, 4), line_color="orange", line_width=2)
p2.square(x, 3*y, legend="3*sin(x)", fill_color=None, line_color="green")
p2.line(x, 3*y, legend="3*sin(x)", line_color="green")
output_file("legend.html", title="legend.py example")
show(gridplot([p1, p2], ncols=2, plot_width=400, plot_height=400))
此代码取自official docs
答案 2 :(得分:0)
从Bokeh 0.13.0
开始,没有建立这样做的方法。有一个开放的GH问题可以请求legen标题,您可以按照这些标题来查看进度: