Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Bokeh Filtering Data: A Complete Guide to Interactive Data Filtering in Python

Interactive data visualization becomes much more valuable when users can focus on specific subsets of information. Instead of displaying an entire dataset at once, filtering allows viewers to narrow the data based on categories, values, dates, or user selections. Bokeh provides several powerful tools for filtering data while maintaining fast, interactive performance.

Whether you're building a business dashboard, a scientific analysis tool, or a real-time monitoring application, understanding data filtering is essential for creating responsive and user-friendly visualizations.

In this tutorial, you'll learn how to filter data using ColumnDataSource, CDSView, BooleanFilter, IndexFilter, GroupFilter, Pandas, and CustomJS callbacks.


What Is Data Filtering?

Data filtering is the process of displaying only the records that meet specific conditions.

Instead of plotting every row in a dataset, you can show only the information that is relevant to a particular analysis.

Common filtering examples include:

  • Displaying sales from a specific year
  • Showing products in one category
  • Viewing data for a selected region
  • Filtering sensor readings above a threshold
  • Displaying records within a date range
  • Highlighting high-performing employees

Filtering improves both readability and performance.


Why Filter Data?

Filtering provides several important benefits:

  • Improves dashboard performance
  • Reduces visual clutter
  • Focuses attention on important data
  • Enables interactive exploration
  • Supports large datasets
  • Creates responsive user interfaces
  • Simplifies data analysis
  • Enhances user experience

Interactive filtering is one of the key strengths of modern web-based visualizations.


Creating a Sample Dataset

Begin with a simple dataset.

from bokeh.models import ColumnDataSource

source = ColumnDataSource(data={
    "month":["Jan","Feb","Mar","Apr","May"],
    "sales":[120,150,180,170,200],
    "region":["North","South","North","East","South"]
})

This dataset contains sales information grouped by month and region.


Using CDSView

CDSView allows glyphs to display only selected rows from a ColumnDataSource.

from bokeh.models import CDSView

A view works together with one or more filters to determine which records appear in the visualization.


BooleanFilter

A BooleanFilter uses a list of True and False values.

from bokeh.models import BooleanFilter

filter = BooleanFilter([
    True,
    False,
    True,
    False,
    True
])

Rows marked as True remain visible, while rows marked as False are hidden.

This approach is useful for simple logical filtering.


Applying a Filter

Create a view using the filter.

from bokeh.models import CDSView

view = CDSView(
    filter=filter
)

Now apply the view to a glyph.

plot.circle(
    x="month",
    y="sales",
    source=source,
    view=view,
    size=10
)

Only the selected rows are displayed.


IndexFilter

An IndexFilter selects rows by their position.

from bokeh.models import IndexFilter

filter = IndexFilter([
    0,
    2,
    4
])

Only rows at indexes 0, 2, and 4 will be shown.

This is useful when row numbers are already known.


GroupFilter

A GroupFilter filters categorical data.

from bokeh.models import GroupFilter

filter = GroupFilter(
    column_name="region",
    group="North"
)

Only records where the region is North appear in the visualization.

Group filters are ideal for business dashboards and categorical reports.


Combining Multiple Filters

Multiple filters can work together.

from bokeh.models import IntersectionFilter

Combining filters allows users to create advanced queries such as:

  • North region
  • Sales above 150
  • Current year
  • Selected product category

Layered filtering enables highly flexible dashboards.


Filtering with Pandas

Many developers filter data before creating the ColumnDataSource.

import pandas as pd

filtered = df[df["Sales"] > 150]

Then create the data source.

source = ColumnDataSource(filtered)

This workflow is common in data science applications.


Interactive Filtering with Widgets

Widgets allow users to change filters dynamically.

Example widgets include:

  • Dropdown menus
  • Checkboxes
  • Radio buttons
  • Multi-select lists
  • Sliders
  • Date pickers

Users can update visualizations without refreshing the page.


Using CustomJS

For client-side interaction, use JavaScript callbacks.

from bokeh.models import CustomJS

CustomJS enables instant filtering directly inside the browser without requiring a Python server.

This approach is excellent for standalone HTML dashboards.


Linked Filtering

Multiple charts can share the same filtered dataset.

For example:

  • Bar chart
  • Scatter plot
  • Line chart
  • Data table

All update automatically when the filter changes.

Linked filtering creates highly interactive analytical dashboards.


Hover Tool Integration

Filtered data works seamlessly with hover tools.

from bokeh.models import HoverTool

hover = HoverTool(
    tooltips=[
        ("Month","@month"),
        ("Sales","@sales"),
        ("Region","@region")
    ]
)

plot.add_tools(hover)

Hover information is displayed only for visible records.


Styling the Visualization

Improve readability with thoughtful styling.

plot.title.text_font_size = "18pt"

plot.xaxis.axis_label = "Month"

plot.yaxis.axis_label = "Sales"

plot.background_fill_color = "#f8f9fa"

plot.grid.grid_line_alpha = 0.4

A clean layout helps users focus on the filtered data.


Best Practices

When filtering data in Bokeh:

  • Use descriptive column names.
  • Filter data as early as possible.
  • Reuse ColumnDataSource objects.
  • Keep filters simple when possible.
  • Test with large datasets.
  • Combine filtering with hover tools.
  • Use widgets for interactive exploration.
  • Validate user input before applying filters.

These practices improve both performance and usability.


Common Mistakes

Avoid these common issues:

  • Filtering after rendering unnecessarily.
  • Creating duplicate data sources.
  • Using inconsistent column names.
  • Ignoring missing values.
  • Overcomplicating filter logic.
  • Displaying too many filtered views simultaneously.

Simple, efficient filtering leads to better dashboards.


Real-World Applications

Data filtering is widely used in:

  • Business intelligence dashboards
  • Financial analytics
  • Marketing reports
  • Healthcare monitoring
  • Manufacturing quality control
  • Geographic information systems
  • Scientific research
  • Machine learning visualization
  • Sales performance dashboards

Filtering helps users quickly find the information that matters most.


Performance Tips

For large datasets:

  • Filter data before plotting whenever possible.
  • Reuse existing ColumnDataSource objects.
  • Use CDSView instead of duplicating data.
  • Minimize unnecessary callbacks.
  • Stream only required records.
  • Cache frequently used filtered datasets.

Efficient filtering ensures responsive and scalable applications.


Conclusion

Data filtering is a fundamental feature of interactive visualization in Bokeh. By combining ColumnDataSource, CDSView, BooleanFilter, IndexFilter, GroupFilter, Pandas, and interactive widgets, you can create dashboards that allow users to explore data quickly and efficiently.

As your projects become more sophisticated, mastering Bokeh's filtering tools will help you build professional applications that are responsive, scalable, and easy to use. Whether you're analyzing business metrics, scientific measurements, financial trends, or real-time sensor data, effective filtering transforms large datasets into meaningful, actionable insights.

A Complete Guide to Interactive Data Filtering in Python


Post a Comment

0 Comments