Bokeh – Annotations and Legends

Bokeh - Annotations and Legends

Bokeh – Annotations and Legends

NumFocus sponsored by Bokeh project https://numfocus.org/. In this chapter, we will discuss Bokeh annotations and legends. Plot title, as well as x and y-axis labels, can be provided in the Figure constructor itself.

fig = figure(title, x_axis_label, y_axis_label)

In the following plot, these properties are set as shown below −

from bokeh.plotting import figure, output_file, show
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
y = np.sin(x)
fig = figure(title = "sine wave example", x_axis_label = 'angle', y_axis_label = 'sin')
fig.line(x, y,line_width = 2)
show(p)

Output

Bokeh - Annotations and Legends

The title’s text and axis labels can also be specified by assigning appropriate string values to corresponding properties of the figure object.

fig.title.text = "sine wave example"
fig.xaxis.axis_label = 'angle'
fig.yaxis.axis_label = 'sin'

It is also possible to specify the location, alignment, font, and color of the title.

fig.title.align = "right"
fig.title.text_color = "orange"
fig.title.text_font_size = "25px"
fig.title.background_fill_color = "blue"

Adding legends to the plot figure is very easy. We have to use the legend property of any glyph method.

Below we have three glyph curves in the plot with three different legends −

from bokeh.plotting import figure, output_file, show
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
fig = figure()
fig.line(x, np.sin(x),line_width = 2, line_color = 'navy', legend = 'sine')
fig.circle(x,np.cos(x), line_width = 2, line_color = 'orange', legend = 'cosine')
fig.square(x,-np.sin(x),line_width = 2, line_color = 'grey', legend = '-sine')
show(fig)

Output

Next Topic – Click Here

This Post Has One Comment

Leave a Reply