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 the ColumnDataSource Object

Give Feedback

This video covers Bokeh’s ColumnDataSource object. The ColumnDataSource is foundational in passing the data to the glyphs you are using to visualize. Its primary functionality is to map names to the columns of your data, making it easier for you to reference data elements when building your visualization.

For information about integrating data sources, check out the Bokeh user guide’s post on the ColumnDataSource and other source objects available.

Bokeh provides a helpful list of CSS color names categorized by their general hue. Also, is a great site for finding CSS, hex, and RGB color codes.


import pandas as pd 

# Read the csv files
player_stats = pd.read_csv('data/2017-18_playerBoxScore.csv',
team_stats = pd.read_csv('data/2017-18_teamBoxScore.csv',
standings = pd.read_csv('data/2017-18_standings.csv',

# Create west_top_2
west_top_2 = (standings[(standings['teamAbbr'] == 'HOU') | 
              (standings['teamAbbr'] == 'GS')]
              .loc[:, ['stDate', 'teamAbbr', 'gameWon']]
              .sort_values(['teamAbbr', 'stDate']))


# Bokeh libraries
from 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
            title='Western Conference Top 2 Teams Wins Race')

# Isolate the data for the Rockets and Warriors
rockets_data = west_top_2[west_top_2['teamAbbr'] == 'HOU']
warriors_data = west_top_2[west_top_2['teamAbbr'] == 'GS']

# Create a ColumnDataSource object for each team
rockets_cds = ColumnDataSource(rockets_data)
warriors_cds = ColumnDataSource(warriors_data)

# Create and configure the figure
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',

# Render the race as step lines
fig.step('stDate', 'gameWon', 
         color='#CE1141', legend='Rockets', 
fig.step('stDate', 'gameWon', 
         color='#006BB6', legend='Warriors', 

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

# Show the plot

Pygator on Aug. 18, 2019

I get the following error when trying to build the dataframe:

FileNotFoundError Traceback (most recent call last) <ipython-input-1-90fbea3810d4> in <module> 3 # Read the csv files 4 player_stats = pd.read_csv(‘data/2017-18_playerBoxScore.csv’, ----> 5 parse_dates=[‘gmDate’]) 6 team_stats = pd.read_csv(‘data/2017-18_teamBoxScore.csv’, 7 parse_dates=[‘gmDate’])

~/Bokeh/venv/lib/python3.7/site-packages/pandas/io/ in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision) 683 ) 684 –> 685 return _read(filepath_or_buffer, kwds) 686 687 = name

~/Bokeh/venv/lib/python3.7/site-packages/pandas/io/ in _read(filepath_or_buffer, kwds) 455 456 # Create the parser. –> 457 parser = TextFileReader(fp_or_buf, **kwds) 458 459 if chunksize or iterator:

~/Bokeh/venv/lib/python3.7/site-packages/pandas/io/ in init(self, f, engine, **kwds) 893 self.options[“has_index_names”] = kwds[“has_index_names”] 894 –> 895 self._make_engine(self.engine) 896 897 def close(self):

~/Bokeh/venv/lib/python3.7/site-packages/pandas/io/ in _make_engine(self, engine) 1133 def _make_engine(self, engine=”c”): 1134 if engine == “c”: -> 1135 self._engine = CParserWrapper(self.f, **self.options) 1136 else: 1137 if engine == “python”:

~/Bokeh/venv/lib/python3.7/site-packages/pandas/io/ in init(self, src, kwds) 1904 kwds[“usecols”] = self.usecols 1905 -> 1906 self._reader = parsers.TextReader(src, kwds) 1907 self.unnamed_cols = self._reader.unnamed_cols 1908

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.cinit()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

FileNotFoundError: [Errno 2] File b’data/2017-18_playerBoxScore.csv’ does not exist: b’data/2017-18_playerBoxScore.csv’

Pygator on Aug. 21, 2019

I downloaded the data from the link dan gave and moved the three csvs into the data/ subfolder . Datetimes aren’t working for me, i don’t understand how to format them at all.

Chris Bailey RP Team on Aug. 21, 2019

Hi Pygator, Are you getting the same error as you posted above? I just tried the link that Dan provided for the data, and it downloaded the CSVs with incorrect names. Each file name should start with 2017-18 and what I got was 017-18. That would cause the error above of file not found. I will work with Dan on how to fix the issue, but you can rename the files, and add a “2” to the front of each. If the issue is past the first error you got, and is more specific to datetimes, can you send me more info, as to where it is failing. Thanks for taking the time to comment, and I hope I can help you solve this.

mcng4570 on March 26, 2020

You use fig for the figure but at the end you use west_fig twice. Also I am getting the warning: BokehDeprecationWarning: ‘legend’ keyword is deprecated, use explicit ‘legend_label’, ‘legend_field’, or ‘legend_group’ keywords instead

Nice work through so far! Thanks

andresgtn on March 30, 2020

theres a typo on the file

you declare fig but then call west_fig

Chris Bailey RP Team on March 30, 2020

Hi @andresgtn, Thanks for spotting that typo. The video is correct, using only fig. I have updated the text below the video to just use fig.

ellefore on June 1, 2020

I am working within spyder.

The rockets_data and warriors_data are populated:

rockets_data.head() Out[13]: stDate teamAbbr gameWon 21 2017-10-17 HOU 1.0 81 2017-10-18 HOU 2.0 141 2017-10-19 HOU 2.0 201 2017-10-20 HOU 2.0 261 2017-10-21 HOU 3.0

warriors_data.head() Out[14]: stDate teamAbbr gameWon 19 2017-10-17 GS 0.0 79 2017-10-18 GS 0.0 139 2017-10-19 GS 0.0 199 2017-10-20 GS 1.0 259 2017-10-21 GS 1.0

When I run the script, I get the chart with the legend (line colors are present in the legend) but the actual step lines are not in the chart.

Is there a way to print out rockets_cds and warrior_cds to see what they look like? I assume the .step call is what is not working, have there been changes to the inputs to the .step call? This is the code as I have it:

fig.step(‘stDate’, ‘gameWon’, color=’#CE1141’, legend_label=’Rockets’, source=rockets_cds) fig.step(‘stDate’, ‘gameWon’, color=’#006BB6’, legend_label=’Warriors’, source=warriors_cds)

ellefore on June 1, 2020

The code is separated into 2 lines, copy and paste crammed them together in the previous post.

fig.step(‘stDate’, ‘gameWon’, color=’#CE1141’, legend_label=’Rockets’, source=rockets_cds)

fig.step(‘stDate’, ‘gameWon’, color=’#006BB6’, legend_label=’Warriors’, source=warriors_cds)

Become a Member to join the conversation.