Is there any way to add a title to legends, e.g. current legend on the left, legend with title on the right. This particular legend is placed outside the plot are so I don't know if I can fake it with a text glyph.
Bokeh creates the HTML file when you call the show() function. This function also automatically opens a web browser to display the HTML file. If you want Bokeh to only generate the file but not open it in a web browser, use the save() function instead.
As of the latest version of Bokeh (1.2) you can add a title to your legend as follows:
p.legend.title = 'Example Title'
The complete example:
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))
This code was taken from the official docs
Update: This method is no longer necessary. See the accepted answer.
I found that still in the Bokeh library they do not implement a way to add a title to the legend but a solution that I implement is the following: Add a very small white dot before adding any other chart.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With