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.
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.
To add a tooltip to a Bokeh plot, you need to:
- Import Necessary Modules: Import
HoverTool
from bokeh.models
and other relevant modules from Bokeh. - Create a Plot: Use
figure()
to create a plot. - Add Glyphs: Add glyphs (e.g., circles, lines) to the plot.
- Configure Tooltips: Create a
HoverTool
object and specify the tooltips. - Add HoverTool to Plot: Use
add_tools()
to add the HoverTool
to the plot.
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:
Python
Next, import the necessary modules:
Python
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import HoverTool
# Display plots inline in Jupyter Notebook
output_notebook()
Step 2: Create a Sample Plot
Let’s create a simple scatter plot to demonstrate how to format tooltip values as integers:
Python
# Sample data
x = [1, 2, 3, 4, 5]
y = [6, 7, 8, 9, 10]
# Create a new plot
p = figure(width=400, height=400, title="Scatter Plot with Integer Tooltip")
# Add a scatter renderer
p.scatter(x=x, y=y, size=10)
# Show the plot with default tooltip (float)
show(p)
Output:
Displaying Integers in Tooltips with BokehTo 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.
Python
# Create a new plot
p = figure(width=400, height=400, title="Scatter Plot with Integer Tooltip")
# Add a scatter renderer
p.scatter(x=x, y=y, size=10)
# Define the hover tool with integer formatting
hover = HoverTool()
hover.tooltips = [("X", "@x{0}"), ("Y", "@y{0}")]
# Add the hover tool to the plot
p.add_tools(hover)
# Show the plot with integer tooltips
show(p)
Output:
Displaying Integers in Tooltips with BokehExplanation:
- 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.
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.
Similar Reads
Python - Data visualization tutorial Data visualization is a crucial aspect of data analysis, helping to transform analyzed data into meaningful insights through graphical representations. This comprehensive tutorial will guide you through the fundamentals of data visualization using Python. We'll explore various libraries, including M
7 min read
What is Data Visualization and Why is It Important? Data visualization uses charts, graphs and maps to present information clearly and simply. It turns complex data into visuals that are easy to understand.With large amounts of data in every industry, visualization helps spot patterns and trends quickly, leading to faster and smarter decisions.Common
4 min read
Data Visualization using Matplotlib in Python Matplotlib is a widely-used Python library used for creating static, animated and interactive data visualizations. It is built on the top of NumPy and it can easily handles large datasets for creating various types of plots such as line charts, bar charts, scatter plots, etc. These visualizations he
11 min read
Data Visualization with Seaborn - Python Seaborn is a popular Python library for creating attractive statistical visualizations. Built on Matplotlib and integrated with Pandas, it simplifies complex plots like line charts, heatmaps and violin plots with minimal code.Creating Plots with SeabornSeaborn makes it easy to create clear and infor
9 min read
Data Visualization with Pandas Pandas is a powerful open-source data analysis and manipulation library for Python. The library is particularly well-suited for handling labeled data such as tables with rows and columns. Pandas allows to create various graphs directly from your data using built-in functions. This tutorial covers Pa
6 min read
Plotly for Data Visualization in Python Plotly is an open-source Python library designed to create interactive, visually appealing charts and graphs. It helps users to explore data through features like zooming, additional details and clicking for deeper insights. It handles the interactivity with JavaScript behind the scenes so that we c
12 min read
Data Visualization using Plotnine and ggplot2 in Python Plotnine is a Python data visualization library built on the principles of the Grammar of Graphics, the same philosophy that powers ggplot2 in R. It allows users to create complex plots by layering components such as data, aesthetics and geometric objects.Installing Plotnine in PythonThe plotnine is
6 min read
Introduction to Altair in Python Altair is a declarative statistical visualization library in Python, designed to make it easy to create clear and informative graphics with minimal code. Built on top of Vega-Lite, Altair focuses on simplicity, readability and efficiency, making it a favorite among data scientists and analysts.Why U
4 min read
Python - Data visualization using Bokeh Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots. Bokeh output can be obtained in various mediums like notebook, html and server. It is possible to embed bokeh plots in Django and flask apps. Bokeh provides two visualization interfaces to us
4 min read
Pygal Introduction Python has become one of the most popular programming languages for data science because of its vast collection of libraries. In data science, data visualization plays a crucial role that helps us to make it easier to identify trends, patterns, and outliers in large data sets. Pygal is best suited f
5 min read