When creating interactive visualizations with Bokeh, controlling what users see is just as important as displaying the data itself. Axis ranges determine which portion of your dataset is visible and how users interact with charts through zooming and panning.
Bokeh provides flexible range objects that allow developers to define fixed boundaries, automatically calculate ranges from data, synchronize multiple plots, and build highly interactive dashboards. Understanding how ranges work is essential for creating professional, user-friendly visualizations.
In this tutorial, you'll learn how to configure axis ranges, use different range types, link multiple plots, and apply best practices for responsive data visualization.
What Are Ranges in Bokeh?
A range defines the visible coordinate space of a plot.
Every Bokeh figure contains two primary ranges:
- X Range – Controls the horizontal axis.
- Y Range – Controls the vertical axis.
Ranges determine:
- Which data points are visible
- Zoom limits
- Pan boundaries
- Initial viewport
- Plot synchronization
- Interactive navigation
Without properly configured ranges, charts may not display data in the most useful way.
Types of Ranges
Bokeh supports several range types.
The most commonly used are:
- DataRange1D
- Range1D
- FactorRange
- Linked Ranges
Each serves different visualization needs.
Automatic Data Ranges
By default, Bokeh automatically determines the axis limits using the data you provide.
Example:
from bokeh.plotting import figure, show
plot = figure(
title="Automatic Range Example",
width=700,
height=400
)
plot.line([1,2,3,4],[4,6,5,8])
show(plot)Bokeh analyzes the dataset and selects suitable minimum and maximum values for both axes.
Automatic ranges are convenient for exploratory analysis and rapidly changing datasets.
Using Range1D
When you need complete control over the visible area, use Range1D.
Example:
from bokeh.models import Range1D
plot = figure(
x_range=Range1D(0,10),
y_range=Range1D(0,100)
)The chart will always display values between the specified boundaries regardless of the data.
This is useful for:
- Dashboards
- Scientific measurements
- Financial reports
- Standardized comparisons
Updating a Range
Ranges can be modified after a figure is created.
Example:
plot.x_range.start = 0
plot.x_range.end = 50
plot.y_range.start = -10
plot.y_range.end = 120This approach allows applications to update the viewport dynamically.
Using DataRange1D
DataRange1D automatically adjusts whenever the underlying data changes.
Example:
from bokeh.models import DataRange1D
plot = figure(
x_range=DataRange1D(),
y_range=DataRange1D()
)This range type is ideal for:
- Live dashboards
- Streaming data
- Real-time monitoring
- Sensor visualization
The viewport expands or contracts as new data arrives.
FactorRange for Categories
Categorical charts use FactorRange.
Example:
from bokeh.models import FactorRange
plot = figure(
x_range=FactorRange(
factors=[
"January",
"February",
"March"
]
)
)FactorRange is commonly used in:
- Bar charts
- Grouped bar charts
- Category comparisons
- Business dashboards
Setting Range Padding
Padding adds extra space around plotted data.
Example:
plot.x_range.range_padding = 0.1Benefits include:
- Improved readability
- Less crowded charts
- Better marker visibility
- Professional appearance
Restricting Zoom Limits
You can limit how far users may zoom.
Example:
plot.x_range.min_interval = 1
plot.x_range.max_interval = 100This prevents excessive zooming that could make charts difficult to interpret.
Restricting Panning
Ranges can also restrict panning.
Example:
plot.x_range.bounds = (0,100)
plot.y_range.bounds = (0,500)Users cannot pan outside the specified boundaries.
This is useful when visualizing known measurement ranges.
Linking Multiple Plots
One of Bokeh's most powerful features is linked ranges.
Example:
plot2 = figure(
x_range=plot1.x_range,
y_range=plot1.y_range
)Both figures now share identical ranges.
When one chart is zoomed or panned, the other updates automatically.
Linked ranges are widely used in dashboards containing multiple synchronized charts.
Working with Time Series
Datetime data works seamlessly with Bokeh ranges.
Example:
plot = figure(
x_axis_type="datetime"
)Ranges automatically adapt to:
- Dates
- Months
- Years
- Hours
- Minutes
This makes Bokeh an excellent choice for financial and scientific time-series analysis.
Interactive Navigation
Ranges work together with Bokeh's interactive tools.
Popular tools include:
- Pan
- Wheel Zoom
- Box Zoom
- Reset
- Save
- Hover
- Crosshair
These tools modify the current range without changing the underlying data.
Styling Your Plot
Improve readability with thoughtful styling.
plot.title.text_font_size = "18pt"
plot.xaxis.axis_label = "Time"
plot.yaxis.axis_label = "Value"
plot.background_fill_color = "#f8f9fa"
plot.grid.grid_line_alpha = 0.4A clean layout helps users focus on the data.
Best Practices
For effective range management:
- Use automatic ranges during exploration.
- Use
Range1Dfor consistent dashboards. - Apply
FactorRangeto categorical data. - Add range padding for readability.
- Limit zoom levels when appropriate.
- Link related plots.
- Restrict panning if data boundaries are fixed.
- Test charts with different screen sizes.
These practices improve both usability and performance.
Common Mistakes
Avoid these common issues:
- Choosing ranges that hide important data.
- Forgetting to update ranges after modifying data.
- Excessive zoom limits.
- Using categorical ranges with numeric data.
- Ignoring linked ranges in dashboards.
- Overcrowding the visible area.
Well-designed ranges make visualizations easier to understand and navigate.
Real-World Applications
Range management is widely used in:
- Business intelligence dashboards
- Stock market analysis
- Scientific research
- Geographic mapping
- Manufacturing monitoring
- Healthcare analytics
- IoT dashboards
- Machine learning visualization
Proper viewport control enhances the overall user experience.
Conclusion
Setting ranges is a fundamental skill for creating effective interactive visualizations with Bokeh. By understanding automatic ranges, Range1D, DataRange1D, FactorRange, linked ranges, zoom restrictions, and panning controls, you can build charts that are both informative and easy to explore.
As your projects become more advanced, thoughtful range management will help you create professional dashboards, responsive analytics applications, and real-time monitoring systems that provide users with a clear and intuitive view of their data.


0 Comments