Last modified: Jan 02, 2025 By Alexander Williams
Python Bokeh Pan and Zoom Tools Tutorial Guide
Interactive visualizations are crucial for exploring data effectively. Bokeh's PanTool()
and WheelZoomTool()
provide powerful ways to navigate through your plots dynamically.
Understanding Pan and Zoom Tools
The pan and zoom tools in Bokeh allow users to explore different regions of a plot and adjust the view level. These tools are essential for interactive data exploration.
Let's start with a simple example that showcases how to implement these tools in a basic line plot. For more advanced plot types, you might want to check out our Python Bokeh Scatter Plot Tutorial.
Basic Implementation
from bokeh.plotting import figure, show
from bokeh.layouts import column
import numpy as np
# Create sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Create figure with both tools
p = figure(width=600, height=300, tools=['pan', 'wheel_zoom'])
# Add line plot
p.line(x, y, line_width=2)
# Show the plot
show(p)
Advanced Tool Configuration
For more precise control, you can explicitly add tools using tool objects. This approach offers greater flexibility in configuring tool behavior.
from bokeh.plotting import figure, show
from bokeh.models import PanTool, WheelZoomTool
# Create figure
p = figure(width=600, height=300)
# Add tools explicitly
pan = PanTool(dimensions='both') # 'both', 'width', or 'height'
wheel_zoom = WheelZoomTool(zoom_on_axis=False) # True for axis-specific zoom
p.add_tools(pan, wheel_zoom)
p.line(x, y, line_width=2)
show(p)
Combining with Other Interactive Tools
Pan and zoom tools work seamlessly with other Bokeh interactive features. Consider combining them with HoverTool for enhanced interactivity.
from bokeh.models import HoverTool
from bokeh.plotting import figure, show
# Create figure with multiple tools
p = figure(width=600, height=300, tools=['pan', 'wheel_zoom', 'reset', 'save'])
# Add hover tool
hover = HoverTool(tooltips=[
('x', '@x'),
('y', '@y')
])
p.add_tools(hover)
# Add data
p.line(x, y, line_width=2)
show(p)
Customizing Tool Behavior
You can customize how the pan and zoom tools behave by setting specific parameters. This allows for fine-tuned control over the user interaction experience.
from bokeh.plotting import figure, show
from bokeh.models import PanTool, WheelZoomTool
# Create figure
p = figure(width=600, height=300)
# Configure custom pan tool
custom_pan = PanTool(
dimensions='width', # Only pan horizontally
)
# Configure custom wheel zoom
custom_zoom = WheelZoomTool(
zoom_on_axis=True, # Zoom only affects the axis being scrolled
maintain_focus=True # Keep focus point stable while zooming
)
p.add_tools(custom_pan, custom_zoom)
p.line(x, y, line_width=2)
show(p)
Managing Tool Active States
You can control which tools are active by default when the plot loads. This is particularly useful when creating multiple coordinated views.
from bokeh.plotting import figure, show
# Create figure with tools
p = figure(width=600, height=300, tools=['pan', 'wheel_zoom', 'reset'])
# Set active tools
p.toolbar.active_drag = 'pan' # Make pan tool active by default
p.toolbar.active_scroll = 'wheel_zoom' # Make wheel zoom active by default
p.line(x, y, line_width=2)
show(p)
Performance Considerations
When working with large datasets, pan and zoom operations can affect performance. Consider using data downsampling or implementing view-dependent data loading.
Conclusion
The PanTool and WheelZoomTool are essential components for creating interactive Bokeh visualizations. They provide users with intuitive ways to explore data at different scales and positions.
Remember to consider your specific use case when configuring these tools, and don't hesitate to combine them with other interactive features for a richer user experience.