VOOZH about

URL: https://www.geeksforgeeks.org/data-visualization/how-to-show-integer-not-float-with-hover-tooltip-in-bokeh/

⇱ How to Show Integer, Not Float, with Hover Tooltip in Bokeh? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Show Integer, Not Float, with Hover Tooltip in Bokeh?

Last Updated : 23 Jul, 2025

Bokeh is a powerful Python library that enables the creation of interactive and visually appealing plots. One of its key features is the ability to add tooltips, which provide additional information when a user hovers over a plot element. However, by default, Bokeh may display numerical data as floats, which can be undesirable if you want to show integer values. This article will guide you through the process of configuring Bokeh tooltips to display integers instead of floats.

Understanding Bokeh Tooltips

Tooltips in Bokeh are configured using the HoverTool, which allows you to display additional information about a plot's data points. The tooltips can be customized to show various data attributes, and you can format these attributes to display in a specific way.

Basic Tooltip Configuration

To add a tooltip to a Bokeh plot, you need to:

  1. Import Necessary Modules: Import HoverTool from bokeh.models and other relevant modules from Bokeh.
  2. Create a Plot: Use figure() to create a plot.
  3. Add Glyphs: Add glyphs (e.g., circles, lines) to the plot.
  4. Configure Tooltips: Create a HoverTool object and specify the tooltips.
  5. Add HoverTool to Plot: Use add_tools() to add the HoverTool to the plot.

Displaying Integers in Tooltips with Bokeh

By default, Bokeh may display numerical values as floats, even if they are integers. To ensure that integers are displayed without decimal places, you need to format the tooltip values appropriately.

Step 1: Install and Import Bokeh

First, ensure you have Bokeh installed. If not, you can install it using pip:

Next, import the necessary modules:

Step 2: Create a Sample Plot

Let’s create a simple scatter plot to demonstrate how to format tooltip values as integers:

Output:

πŸ‘ Screenshot-2024-08-28-224639
Displaying Integers in Tooltips with Bokeh

Step 3: Configure Hover Tool to Show Integers

To format the hover tooltips to show integers instead of floats, you need to specify a custom format for the tooltip. This is done by creating a HoverTool object and configuring it with the desired format.

Output:

πŸ‘ Screenshot-2024-08-28-224655
Displaying Integers in Tooltips with Bokeh

Explanation:

  • HoverTool: This Bokeh model allows you to customize the tooltips that appear when hovering over data points.
  • hover.tooltips: Defines the content and format of the tooltip. The format {0} specifies that values should be displayed as integers.

Common Issues and Troubleshooting

1. Floats Displayed Instead of Integers

If you still see floats instead of integers, ensure that:

  • The data source contains integer values.
  • The format specifier {0} is correctly applied in the tooltip configuration.

2. Tooltip Not Displaying

If the tooltip is not displaying, check:

  • The HoverTool is added to the plot using add_tools().
  • The data source and field names in the tooltips match those in the ColumnDataSource

Conclusion

Customizing tooltips in Bokeh to display integers instead of floats enhances the readability of your plots. By using the HoverTool model and formatting tooltips, you can ensure that numeric values are presented clearly and appropriately for your audience.

Comment