Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can Bokeh be used to create a color scatter plot that shows data when hovering over points in Python?
Bokeh is a Python package that helps in data visualization. It renders plots using HTML and JavaScript, making it ideal for web-based dashboards and interactive applications. Unlike Matplotlib and Seaborn which produce static plots, Bokeh creates interactive plots that respond to user interactions.
Bokeh integrates seamlessly with NumPy, Pandas, and other Python packages. It can produce interactive plots, dashboards, and can be embedded in Flask or Django web applications or rendered in Jupyter notebooks.
Installation
Install Bokeh using pip or conda:
pip3 install bokeh
Or with Anaconda:
conda install bokeh
Creating a Color Scatter Plot with Hover Tooltips
Here's how to create an interactive scatter plot where hovering over points displays additional information:
import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool
# Generate sample data
N = 1000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
# Create colors based on x and y coordinates
colors = [
"#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(40+2*x, 30+2*y)
]
# Define hover tool with custom tooltips
hover = HoverTool(tooltips=[
("Index", "$index"),
("(X,Y)", "($x, $y)"),
("Radius", "@radius")
])
# Create figure with tools including hover
p = figure(width=600, height=400, tools=[hover, "pan,wheel_zoom,box_zoom,reset,save"])
# Add scatter plot with hover data
p.scatter(x, y, radius=radii, fill_color=colors, fill_alpha=0.6,
line_color=None, radius_units='data')
# Output to HTML file
output_file("interactive_scatter.html", title="Interactive Color Scatter Plot")
show(p)
Enhanced Version with Custom Data
For more meaningful hover information, you can include custom data:
import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool, ColumnDataSource
# Generate sample data with labels
N = 500
data = {
'x': np.random.random(size=N) * 100,
'y': np.random.random(size=N) * 100,
'size': np.random.randint(10, 30, size=N),
'category': np.random.choice(['A', 'B', 'C'], size=N),
'value': np.random.randint(1, 100, size=N)
}
# Color mapping for categories
color_map = {'A': 'red', 'B': 'green', 'C': 'blue'}
data['color'] = [color_map[cat] for cat in data['category']]
# Create ColumnDataSource
source = ColumnDataSource(data)
# Enhanced hover tool
hover = HoverTool(tooltips=[
("Category", "@category"),
("Position", "(@x{0.0}, @y{0.0})"),
("Value", "@value"),
("Size", "@size")
])
# Create figure
p = figure(width=700, height=500, tools=[hover, "pan,wheel_zoom,reset,save"],
title="Enhanced Interactive Scatter Plot")
# Add scatter plot
p.scatter('x', 'y', size='size', color='color', alpha=0.6, source=source)
output_file("enhanced_scatter.html")
show(p)
Key Features
| Feature | Description | Usage |
|---|---|---|
| HoverTool | Shows data on mouse hover | tooltips=[("Label", "@field")] |
| ColumnDataSource | Stores data for plots | source=ColumnDataSource(data) |
| Interactive Tools | Pan, zoom, reset functions | tools=["pan", "wheel_zoom"] |
Tooltip Formatting Options
Customize tooltip display with these format specifiers:
@field- Display field value from data source$x, $y- Display cursor coordinates{0.00}- Number formatting (2 decimal places)$index- Display data point index
Conclusion
Bokeh's interactive scatter plots with hover tooltips provide an excellent way to explore data relationships. The HoverTool and ColumnDataSource make it easy to display detailed information when users interact with plot points, enhancing the data visualization experience.
