Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Using GroupFilter and CDSView

Give Feedback

This video expands on Bokeh’s ColumnDataSource object, by exploring GroupFilter and CDSView. These features of the ColumnDataSource allow you to filter your data and make multiple views of a single ColumnDataSource. Allowing you to do much of your data wrangling using Bokeh’s own tools.

File: WestConfTop2.py

# Bokeh libraries
from bokeh.io import output_file
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, CDSView, GroupFilter

# Import the data
from read_nba_data import west_top_2

# Output to static HTML file
output_file('west_top_2_standings_race.html',
            title='Western Conference Top 2 Teams Wins Race')

# Create a ColumnDataSource
west_cds = ColumnDataSource(west_top_2)

# Create view for each team
rockets_view = CDSView(source=west_cds,
                       filters=[GroupFilter(column_name='teamAbbr', group='HOU')])
warriors_view = CDSView(source=west_cds,
                        filters=[GroupFilter(column_name='teamAbbr', group='GS')])

# Create and configure the figure
west_fig = figure(x_axis_type='datetime',
                  plot_height=300, plot_width=600,
                  title='Western Conference Top 2 Teams Wins Race, 2017-18',
                  x_axis_label='Date', y_axis_label='Wins',
                  toolbar_location=None)

# Render the race as step lines
west_fig.step('stDate', 'gameWon',
              source=west_cds, view=rockets_view,
              color='#CE1141', legend='Rockets')
west_fig.step('stDate', 'gameWon',
              source=west_cds, view=warriors_view,
              color='#006BB6', legend='Warriors')

# Move the legend to the upper left corner
west_fig.legend.location = 'top_left'

# Show the plot
show(west_fig)

andresgtn on March 30, 2020

what is the rationale behind using CDSViews with group filters over creating multiple CDS - its not clear what is the benefit of one over the other. Its nice to understand the idea behind using two different ways of seemingly achieving the same outcome. E.g. one is more memory-efficient than the other

Chris Bailey RP Team on March 30, 2020

Hi @andresgtn, One of the key reasons for using a single CDS (Column Data Source) is to have interactivity, which is covered in much more detail coming up. With one source, if you make selections in one view it can be also represented on another view. I think you are correct that it is also memory efficient. Link to the docs for more.Bokeh Column Data Source

Become a Member to join the conversation.