Sometimes we want to add visual cues (boundary lines, shaded regions, labels, and arrows, etc.) to our plots to call out some feature or other. Bokeh has several annotation types available for uses like this. Typically to add annotations we create the "low level" annotation object directly, and add it to our plot using add_layout.
1. Title
There are three ways to add and customize a title.
When using bokeh.plotting , the quickest way to add a basic title is to pass the text as the title parameter to Figure. The default title is normally on the top of a plot, aligned to the left. But which side of the plot the default title appears on can be controlled by the title_location parameter.
Visual properties for font, border, background, and others can be set directly on .title.Here is an example that sets font, background properties, and others using
In addition to the default title, it is possible to create and add additionalTitleobjects to plots using theadd_layoutmethod. You just need to add a new layout to the graph.
from bokeh.models import Title
p3 = figure(plot_width=300, plot_height=300)
p3.circle([2,2], [2,3],size=100, color="salmon")
p3.add_layout(Title(text="Bottom Title", align="center"), "below")
In the end, use show() and row() to combine the three graphs in alignment.
show(row(p1,p2,p3))
2. Legend
Basic Legend Label
To provide a simple explicit label for a glyph, pass the legend_label keyword argument
If multiple glyphs are given the same label, they will all be combined into a single legend item with that label.
Here is an example. Firstly, we download the stock data and process the DateTime format using Pandas and Numpy.
from bokeh.sampledata.stocks import AAPL,GOOG, IBM
import pandas as pd
import numpy as np
def datetime(x):
return np.array(x, dtype=np.datetime64)
Then, we plot the three different lines together and give them legends, respectively. At last, we relocate the legend to the top left.
It is also possible to not specify any of the legend arguments, and manually build a Legend by hand. In particular, if you want to draw multiple legend items for “multi” glyphs, you can specify anindexfor the legend item.
Color bars are especially useful if we vary the color of a glyph according to some color mapping. Bokeh color bars are configured with a color mapper and added to plots with the add_layout method.
The example below shows a complete example that also uses the color mapper to transform the glyph color.
The Arrow annotation allows you to "point" at different things on your plot and can be especially useful in conjunction with labels. This arrow has three types that are OpenHead , NormalHead , and VeeHead.
A BoxAnnotation can be linked to either data or screen coordinates to emphasize specific plot regions. By default, box annotation dimensions (e.g. left or top) default will extend the annotation to the edge of the plot area.
Labels are text elements that can be used to annotate either glyphs or plot regions. The position and text to display are configured as x, y, and text.
With the name label, we can easily figure out the height and weight of each student.
7. Span
Span annotations are lines that have a single dimension (width or height) and extend to the edge of the plot area. You need to specify the dimension that should be spanned (i.e., width or height), any visual line properties for the appearance, and the location along the dimension where the line should be drawn.
from bokeh.models.annotations import Span
x = np.linspace(0, 20, 200)
y = np.sin(x)
p = figure(plot_width=400, plot_height=400,y_range=(-2, 2))
p.line(x, y, color = 'hotpink')
upper = Span(location=1, dimension='width', line_color='dodgerblue', line_width=4)
p.add_layout(upper)
lower = Span(location=-1, dimension='width', line_color='forestgreen', line_width=4)
p.add_layout(lower)
show(p)