Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a two line legend in a bokeh plot

Tags:

python

bokeh

I'm trying to create a Bokeh chart with alot of individual lines on it, 14 to be exact (not my idea, was told to do it this way). Problem is that the requested formatting has the legend horizontal along the bottom of the chart and it's to long to fit so I need it to be two lines and I can't seem to find any indication on how to do this. Hoping someone has had to tackle this before.

Current code with a single line legend.

#graph data to chart
r0  = p1.line(data['Time'], data['Signal 1'], line_color="MediumBlue")
r1  = p1.line(data['Time'], data['Signal 2'], line_color="MidnightBlue")
r2  = p1.line(data['Time'], data['Signal 3'], line_color="red", y_range_name="Temp")
r3  = p1.line(data['Time'], data['Signal 4']*10, line_color="gray", y_range_name="Temp")
r4  = p1.line(data['Time'], data['Signal 5']*3.281, line_color="gold")
r5  = p1.line(data['Time'], data['Signal 6'], line_color="brown", y_range_name="Temp")
r6  = p1.line(data['Time'], data['Signal 7'], line_color="tomato", y_range_name="Temp")
r7  = p1.line(data['Time'], data['Signal 8'], line_color="blue", line_dash="4 4", y_range_name="Temp")
r8  = p1.line(data['Time'], data['Signal 9'], line_color="cyan", y_range_name="Temp")
r9  = p1.line(data['Time'], data['Signal 10']*57.3, line_color="plum", y_range_name="Temp")
r10 = p1.line(data['Time'], data['Signal 11']*1.94, line_color="green", y_range_name="Temp")

#create html file
output_file("Engine_Disp_Test.html", title="Engine Disp Test")

#Configure Legend
legend=Legend(items=[
                     ("Signal 1" , [r0]),
                 ("Signal 2" , [r1]),
                 ("Signal 3" , [r2]),
                 ("Signal 4" , [r3]),
                 ("Signal 5", [r4]),
                 ("Signal 6", [r5]),
                 ("Signal 7", [r6]),
                 ("Signal 8", [r7]),
                 ("Signal 9", [r8]),
                 ("Signal 10", [r9]),
                 ("Signal 11", [r10])
                 ], location=(70,-10), orientation="horizontal")
like image 396
BikeControl Avatar asked Jan 10 '17 16:01

BikeControl


People also ask

How do you plot multiple lines on bokeh?

With Bokeh's bokeh. plotting interface, you can add more glyphs to your plot: To add more line graphs to your plot, all you need to do is call the line() function multiple times.

What is legend in line plot?

A legend or key is drawn with sample lines labeled with an automatically generated string that describes the variable and the context. Thus if the plot shows the same variable at different times or depths, the different lines have unique labels.

How do I save a bokeh plot as a PNG?

Exporting PNG images Bokeh can generate RGBA-format Portable Network Graphics (PNG) images from layouts using the export_png() function. This functionality renders the layout in memory and then captures a screenshot. The output image will have the same dimensions as the source layout.


1 Answers

As of Bokeh 0.12.4 there is nothing that will automatically let you split legends into multiple row (or columns). But you can work around it by adding two legends. Note that I tweaked the positions and also added a min_border_bottom value since since the bottom legend seemed to be cut off otherwise.

from bokeh.io import output_file, show
from bokeh.models import Legend
from bokeh.plotting import figure

p = figure(min_border_bottom=130)

r1 = p.line(x=[0, 1], y=1, line_width=2, color="red")
r2 = p.line(x=[0, 1], y=2, line_width=2, color="blue")
r3 = p.line(x=[0, 1], y=3, line_width=2, color="green")
r4 = p.line(x=[0, 1], y=4, line_width=2, color="orange")

legend1 = Legend([items=[("r1" , [r1]), ("r2", [r2])],
                 location=(70,20), orientation="horizontal")

legend2 = Legend(items=[("r3" , [r3]), ("r4", [r4])],
                 location=(70,10), orientation="horizontal")

p.add_layout(legend1, 'below')
p.add_layout(legend2, 'below')

output_file("foo.html")

show(p)

enter image description here

like image 82
bigreddot Avatar answered Oct 06 '22 03:10

bigreddot