How to Show Integer, Not Float, with Hover Tooltip in Bokeh?
Last Updated :
31 Aug, 2024
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
Adding Tooltips to a Timeseries Chart (Hover Tool) in Python Bokeh
Adding tooltips to a timeseries chart using the Bokeh library in Python can significantly enhance the interactivity and user experience of your data visualizations. Tooltips provide additional information when a user hovers over specific data points, making it easier to understand complex datasets.
4 min read
How to Change the Tooltip Position to Top-Bottom ?
The tooltip is a hint that appears when you hover over an element in the User Interface. You can easily create a tooltip using HTML and CSS by changing the visibility property and hover selector. The tooltip can be positioned top-bottom using the CSS top and CSS bottom properties. ApproachCreate the
2 min read
How to use Tooltip on Social Media Icons in Bootstrap 5 ?
In Bootstrap, Tooltips can be added to the social media icons that can be displayed on the icon's hover. Tooltips enhance the user experience by offering quick hints, tips, or clarifications that help users understand the functionality of interface elements, navigate through a website or application
2 min read
How to Hide the Floating Toolbar in Plotly in Python
In this article, we'll see an easy way to hide the floating toolbar in Python Plotly using Python. When we hover the mouse pointer over the chart, we see a floating toolbar with a set of tools. At times, this toolbar can be a distraction. We just want the chart to appear without a floating toolbar t
2 min read
How to Make a Tooltip With an Right Arrow ?
Tooltips are used to display the additional information along with the links on the web pages. It adds enhancement and increases the overall user experience while using the application. Below are the approaches to make a tooltip with an right arrow: Table of Content Using CSS Pseudo-elementsUsing SV
3 min read
How to Create a Tooltip with only HTML?
A tooltip is a small box that appears when a user hovers over an item on a website or software application. This article discusses the importance of creating simple, accessible tooltips in web design without depending on JavaScript or CSS frameworks. We will use only HTML elements, specifically titl
2 min read
How to make a Tooltip with an Image ?
The tooltip is the styling component that provides additional information or context when hovering over an element. We will explore various examples to make a tooltip with an image including a Tooltip With an Image on Top and Bottom, a Tooltip With an Image on Left and Right, and a Tooltip With an A
4 min read
How to Create Tooltip with Vue.js?
Tooltips are the styling components used to provide informative or interactive hints when hovering over an element, enhancing user experience, and guiding interactions in web applications. Below are the approaches to creating a tooltip with Vue.js: Table of Content Using Vue DirectivesUsing Vue Tool
2 min read
How to line up different widgets horizontally in Bokeh?
Bokeh is a data visualization library that can be used to create beautiful graphics, from simple plots to complex dashboards with streaming datasets. Bokeh provides interactive plots in modern web browsers like Google Chrome etc, using HTML and JavaScript to present clean and highly interactive char
3 min read
What is a tooltip and how to create it in Bootstrap ?
A Tooltip is like a balloon or also a small screen tip that displays text description to any object. A tooltip is displayed when the user hovers over an object using the cursor. It is a very useful part of a website and now can be found in almost all websites including some web applications like Ado
3 min read