Bokeh is a powerful data visualization library in Python that allows you to create interactive and visually appealing plots. The Bokeh plotting module provides several tools that can be used to enhance the functionality of the plots. These tools can be configured to suit your specific needs. In this article, we will learn about the Configuring Plot Tools in Bokeh in Python with some examples.
Tooltips with Python’s Bokeh
One of the key features of Bokeh is its ability to provide interactive tools that allow users to interact with the plots. Bokeh provides a number of tools that can be used to zoom, pan, select, and manipulate data on the plot. The types of gesture tools are:
- Pan/Drag tools
- Click/Tap tools
- Scroll/Pinch tools
For each type of gesture, only one tool can be active at any given time. The active tool is highlighted on the toolbar next to the tool icon.
Adding a Hover Tool to a Scatter Plot
The hover tool allows users to see additional information about the data points on a plot by hovering their mouse over the point. To add a hover tool to a scatter plot in Bokeh, we need to create a HoverTool object and add it to the plot’s tools list.
Example
In this example, we create a scatter plot using the circle method and add a HoverTool object to the plot using the add_tools method. We configure the hover tool to show the labels column in the data source when the user hovers over a data point.
Python3
from bokeh.plotting import figure, output_file, show from bokeh.models import HoverTool import pandas as pd # create some data data = pd.DataFrame({ 'x' : [ 1 , 2 , 3 , 4 , 5 ], 'y' : [ 5 , 4 , 3 , 2 , 1 ], 'labels' : [ 'A' , 'B' , 'C' , 'D' , 'E' ]}) # create a figure p = figure(title = 'Scatter Plot with Hover Tool' ) # add scatter plot p.circle(x = 'x' , y = 'y' , source = data, size = 10 ) # add hover tool hover = HoverTool(tooltips = [( 'Label' , '@labels' )]) p.add_tools(hover) # show the plot show(p) |
Output:
Adding a Box Zoom Tool to a Line Plot
The box zoom tool allows users to zoom in on a specific region of a plot by drawing a box around the area they want to zoom in on. To add a box zoom tool to a line plot in Bokeh, we need to create a BoxZoomTool object and add it to the plot’s tools list.
Example
In this example, we create a line plot using the line method and add a BoxZoomTool object to the plot using the add_tools method.
Python3
from bokeh.plotting import figure, output_file, show from bokeh.models import BoxZoomTool import numpy as np # create some data x = np.linspace( 0 , 2 * np.pi, 100 ) y = np.sin(x) # create a figure p = figure(title = 'Line Plot with Box Zoom Tool' ) # add line plot p.line(x, y) # add box zoom tool box_zoom = BoxZoomTool() p.add_tools(box_zoom) # show the plot show(p) |
Output:
Adding a Tap Tool to a Bar Chart
The tap tool allows users to select a data point on a plot by clicking on it. To add a tap tool to a bar chart in Bokeh, we need to create a TapTool object and add it to the plot’s tools list.
Example
In this example, we create a bar chart using the bar method of the figure object. We then create a HoverTool object and configure it to show the y value of each bar when the user hovers over it. Finally, we add the HoverTool to the figure object using the add_tools method. The resulting chart will be saved to an HTML file named “bar_chart.html” and displayed in a web browser window
Python3
from bokeh.io import output_file, show from bokeh.plotting import figure from bokeh.models import ColumnDataSource, HoverTool # create some sample data x = [ 'A' , 'B' , 'C' , 'D' , 'E' ] y = [ 10 , 20 , 30 , 40 , 50 ] # create a ColumnDataSource with the data source = ColumnDataSource(data = dict (x = x, y = y)) # create the figure p = figure(x_range = x) # add the bars to the figure p.vbar(x = 'x' , top = 'y' , width = 0.9 , source = source) # configure the tooltip hover = HoverTool(tooltips = [( "Value" , "@y" )]) p.add_tools(hover) # show the figure output_file( "bar_chart.html" ) show(p) |
Output: