Bokeh is a Python library used to visualize data in an interactive and modern look. It facilitates the presentation of useful insights into the data more effectively. We can create different types of plots, such as line, grid, scatter, and box charts.
While working with charts, adding textual information to the chart is very important for better presentation. Annotation in Bokeh allows us to add elements like titles, legends, labels, bars, and arrows.
In this Answer, we’ll learn to add different annotation types to our Bokeh charts. Before moving forward, let’s click the “Run” button in the widget below to draw a simple graph. We’ll use the same graph to add different annotations.
from bokeh.io import output_file, save from bokeh.plotting import figure, show myPlot = figure(width = 680, height = 400) # add a circle renderer with a size, color,dfferf and alpha myPlot.scatter([1, 2, 3, 4, 5], [6, 5, 1, 4, 3], size=20, color="red", alpha=0.5) # show the results output_file("output.html") show(myPlot)
Note: You can either view the plot in the "Output" tab or by clicking the URL next to "Your app can be found at:".
The code above creates a simple scatter plot with five points in a output.html
file and print the graph.
This annotation is used to add descriptive text to the chart. The title
parameter in the figure
function is used to add titles to the graphs. We define the title
parameter in line 4 in the following code block to name our simple graph.
from bokeh.io import output_file, save from bokeh.plotting import figure, show myPlot = figure(title="Sample Graph with Title", width = 680, height = 400) # add a scatter renderer with a size, color, and alpha myPlot.scatter([1, 2, 3, 4, 5], [6, 5, 1, 4, 3], color="red", size=3, alpha=0.5) # show the results output_file("output.html") show(myPlot)
The output of the widget above shows a scatter plot with a title mentioned above the graph.
We can also add a title
to a graph without specifying it in the figure
function but using the .title
property. In the code below, we use the .title
property in lines 13–17 to add a title and make some customizations.
from bokeh.io import output_file, save from bokeh.plotting import figure, show myPlot = figure(width = 680, height = 400) # add a scatter renderer with a size, color, and alpha myPlot.scatter([1, 2, 3, 4, 5], [6, 5, 1, 4, 3], color="red", size=3, alpha=0.5) myPlot.title.text = "Title added using .title property" myPlot.title.align = "left" myPlot.title.text_color = "#00477A" myPlot.title.text_font_size = "25px" myPlot.title.background_fill_color = "#ffffff" # show the results output_file("output.html") show(myPlot)
The code above uses the .title
property to add text and set the alignment, text color, font size, and background color. The execution of the code above shows the same output as the previous one, but this time, we used the .title
property instead of a parameter.
Legend is used to add information about the graph lines on the plot to make the line representation clearer. We can add legends by using any of the legend_field
, legend_group
, or legend_label
parameter. The legend_field
is used to add legend on the browser side, and the legend_group
parameter is used for group columns of ColumnDataSource. Run the code widget below to create a graph with a title and legend.
from bokeh.io import output_file, save from bokeh.plotting import figure, show myPlot = figure(width = 680, height = 400) # add a scatter renderer with a size, color, and alpha myPlot.scatter([1, 2, 3, 4, 5], [6, 5, 1, 4, 3], color="red", alpha=0.5, size=3, marker="square", legend_label="scatter", line_color="green") myPlot.line([1, 2, 3, 4, 5], [6, 5, 1, 4, 3], legend_label="scatter", line_color="green") myPlot.title.text = "Legend in a graph" myPlot.title.align = "left" myPlot.title.text_color = "#00477A" myPlot.title.text_font_size = "25px" myPlot.title.background_fill_color = "#ffffff" # show the results output_file("output.html") show(myPlot)
The code above creates a scatter plot and connects the points using a line plot. The legend_label
parameter in scatter
and line
functions define the legend.
We can hide legends by setting the visible
property of the legendItem
to False
. The following code line will hide the legend from the above graph. If we have more than 1 legend, the value in the items
array can be changed accordingly.
myPlot.legend.items[0].visible = False
Label annotation adds extra information to the graphs as a rectangular box. We can use the Label
method to create a single label annotation or LabelSet
to create multiple labels at once from the ColumnDataSource. Let’s run the code widget below to add a label to our graph. The Label
method takes positions on the x and y-axis, label text, and additionally, we can add some styling as well.
from bokeh.io import output_file, save from bokeh.models import Label from bokeh.plotting import figure, show myPlot = figure(width = 680, height = 400) # add a circle renderer with a size, color, and alpha myPlot.scatter([1, 2, 3, 4, 5], [6, 5, 1, 4, 3], color="red", alpha=0.5, size=3, marker="square", legend_label="scatter", line_color="green") myPlot.line([1, 2, 3, 4, 5], [6, 5, 1, 4, 3], legend_label="scatter", line_color="green") myPlot.title.text = "Legend in a graph" myPlot.title.align = "left" myPlot.title.text_color = "#00477A" myPlot.title.text_font_size = "25px" myPlot.title.background_fill_color = "#ffffff" label = Label(x=1, y=1, text='Scatter plot with label annotation', text_font_style='bold', border_line_color='#c3c99a', background_fill_color='#c3c99a') myPlot.add_layout(label) # show the results output_file("output.html") show(myPlot)
In the code above, we imported Label
from the bokeh.models
in line 2. Then, we defined the Label
object’s properties in lines 22–27 and used the add_layout
function to add the label to the graph.
Lines 22–23: We define the position (coordinates) of the label on the graph plane.
Line 24: We define the text for the label.
Line 25: We set the text_font_style
variable to bold
.
Lines 26–27: We define some styling for the label box.
Line 29: We use the add_layout
function of figure
object to add a label to the graph.
The execution of the code above shows a graph with a label mentioned on it.
In this Answer, we saw some very helpful features of the Bokeh library. We learned to add titles, legends, and labels to our Bokeh graphs.
Free Resources