Last updated
Last updated
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
.
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 additionalTitle
objects to plots using theadd_layout
method. You just need to add a new layout to the graph.
In the end, use show()
and row()
to combine the three graphs in alignment.
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.
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 anindex
for 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.
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.
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.
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 , , and .