Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position of the legend in a Bokeh plot

Does anyone know how to carry the legend in bokeh outside of the graph? The only manipulation I could do was to choose a position among:

top_right, top_left, bottom_left or bottom_right

using:

legend()[0].orientation = "bottom_left"

and when I try different ones I get the error message:

ValueError: invalid value for orientation: 'outside'; allowed values are top_right, top_left, bottom_left or bottom_right
like image 319
MajorYel Avatar asked Oct 08 '14 10:10

MajorYel


People also ask

How do I hide my legend in bokeh?

Legends added to Bokeh plots can be made interactive so that clicking or tapping on the legend entries will hide or mute the corresponding glyph in a plot. These modes are activated by setting the click_policy property on a Legend to either "hide" or "mute" .

What is bokeh plots?

The bokeh. plotting API is Bokeh's primary interface, and lets you focus on relating glyphs to data. It automatically assembles plots with default elements such as axes, grids, and tools for you.

What is bokeh data visualization?

Bokeh is a Python library for creating interactive visualizations for modern web browsers. It helps you build beautiful graphics, ranging from simple plots to complex dashboards with streaming datasets. With Bokeh, you can create JavaScript-powered visualizations without writing any JavaScript yourself.


2 Answers

As of Bokeh 0.12.4 it is possible to position legends outside the central plot area. Here is a short example from the user's guide:

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)

To adjust the position, change dx and dy in location=(dx, dy).

enter image description here

like image 63
bigreddot Avatar answered Oct 16 '22 17:10

bigreddot


According to the Bokeh documentation and bigreddot, one way is to use the Legend command. I found another way.

If you use the legend_label argument in the plotting functions such as quad() or line(), the plot labels are appended to the p.legend. The location of the legend is defined by p.legend.location = "center" as a normal way.

To put the legend outside, you should use p.add_layout(p.legend[0], 'right').

Here is the above code in a different way.

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)
like image 2
Mehdi Avatar answered Oct 16 '22 19:10

Mehdi