Hovering
With multidimensional data sets, interactive elements can provide additional information beyond that which is represented visually. A very useful mechanism for doing that involves showing a text box with additional information if one hovers over a data glyph. In Bokeh, this is supported by the HoverTool, which can be configured to display the desired information in the text box.
The code below and the plots that follow revisit the auto mpg data that we examined previously. We have turned on line numbers for the code to be able to highlight a few points. We can configure hovering by adding hover
to the set of tools passed to the figure constructor, along with a list of hover_tips
(tooltips) that are also specified. (Those operations are carried out on lines 9-13.) The custom Bokeh format for specifying what will be shown upon hover is included in the definition of the hover_tips on line 10, where each hover tip is included in a tuple of strings. The use of the "@" symbol enables one to refer to columns of the ColumnDataSource, e.g., ('mpg', '@mpg'), ('cyl', '@cyl'), ('displ', '@displ'). If we wanted to show just a subset of the data columns, we could explicitly list those. But if we want to include all the columns, we can simply loop over all of the columns stored in the attribute called source.column_names
, and build up the hover tips programmatically using Python list comprehension syntax.
In the plot that follows, try hover over some of the data points to see the associated data for each car.