Skip to content

Commit 1ba7f31

Browse files
authored
Merge pull request #17 from leondangio/intro-to-bokeh
Intro to bokeh
2 parents 9cda6be + a5c5335 commit 1ba7f31

26 files changed

+34403
-0
lines changed

intro-to-bokeh/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Getting Started with Interactive Visualization Using Bokeh
2+
3+
Data and code snippets from tutorial.
4+
5+
## Data
6+
7+
Found in the [`data\`](https://github.com/LeonSD09/materials/tree/intro-to-bokeh/intro-to-bokeh/data) directory.
8+
9+
The tutorial utilizes the following three files:
10+
11+
- **2017-18_standings.csv**
12+
- Contains daily team standing information for the 2017-18 season
13+
- [Original Kaggle Link](https://www.kaggle.com/pablote/nba-enhanced-stats/downloads/2017-18_standings.csv/26)
14+
- **2017-18_playerBoxScore.csv**
15+
- Contains per-game box score player statistics for the 2017-18 season
16+
- [Original Kaggle Link](https://www.kaggle.com/pablote/nba-enhanced-stats/downloads/2017-18_playerBoxScore.csv/26)
17+
- **2017-18_teamBoxScore.csv**
18+
- Contains per-game box score team statistics for the 2017-18 season
19+
- [Original Kaggle Link](https://www.kaggle.com/pablote/nba-enhanced-stats/downloads/2017-18_teamBoxScore.csv/26)
20+
21+
The original Kaggle link for this data is: [**NBA Enhanced Box Score and Standings Stats**](https://www.kaggle.com/pablote/nba-enhanced-stats/home)
22+
23+
This data is licensed under [**CC BY-SA 4.0**](https://creativecommons.org/licenses/by-sa/4.0/).
24+
25+
## Code Snippets
26+
27+
Found in the [`code-snippets\`](https://github.com/LeonSD09/materials/tree/intro-to-bokeh/intro-to-bokeh/code-snippets) directory.
28+
29+
The code snippets found in the tutorial can be found here in individual files.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Bokeh Visualization Template
2+
3+
This template is a general outline for turning your data into a
4+
visualization using Bokeh.
5+
"""
6+
# Data handling
7+
import pandas as pd
8+
import numpy as np
9+
10+
# Bokeh libraries
11+
from bokeh.io import output_file, output_notebook
12+
from bokeh.plotting import figure, show
13+
from bokeh.models import ColumnDataSource
14+
from bokeh.layouts import row, column, gridplot
15+
from bokeh.models.widgets import Tabs, Panel
16+
17+
# Prepare the data
18+
19+
# Determine where the visualization will be rendered
20+
output_file('filename.html') # Render to static HTML, or
21+
output_notebook() # Render inline in a Jupyter Notebook
22+
23+
# Set up the figure(s)
24+
fig = figure() # Instantiate a figure() object
25+
26+
# Connect to and draw the data
27+
28+
# Organize the layout
29+
30+
# Preview and save
31+
show(fig) # See what I made, and save if I like it
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Bokeh libraries
2+
from bokeh.plotting import figure, show
3+
from bokeh.io import output_file
4+
from bokeh.models import ColumnDataSource, CDSView, GroupFilter
5+
6+
# Output to file
7+
output_file('east-top-2-standings-race.html',
8+
title='Eastern Conference Top 2 Teams Wins Race')
9+
10+
# Create a ColumnDataSource
11+
standings_cds = ColumnDataSource(standings)
12+
13+
# Create views for each team
14+
celtics_view = CDSView(source=standings_cds,
15+
filters=[GroupFilter(column_name='teamAbbr',
16+
group='BOS')])
17+
raptors_view = CDSView(source=standings_cds,
18+
filters=[GroupFilter(column_name='teamAbbr',
19+
group='TOR')])
20+
21+
# Create and configure the figure
22+
east_fig = figure(x_axis_type='datetime',
23+
plot_height=300, plot_width=600,
24+
title='Eastern Conference Top 2 Teams Wins Race, 2017-18',
25+
x_axis_label='Date', y_axis_label='Wins',
26+
toolbar_location=None)
27+
28+
# Render the race as step lines
29+
east_fig.step('stDate', 'gameWon',
30+
color='#007A33', legend='Celtics',
31+
source=standings_cds, view=celtics_view)
32+
east_fig.step('stDate', 'gameWon',
33+
color='#CE1141', legend='Raptors',
34+
source=standings_cds, view=raptors_view)
35+
36+
# Move the legend to the upper left corner
37+
east_fig.legend.location = 'top_left'
38+
39+
# Show the plot
40+
show(east_fig)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Bokeh library
2+
from bokeh.plotting import figure, show
3+
from bokeh.io import output_file
4+
from bokeh.layouts import column
5+
6+
# Output to file
7+
output_file('east-west-top-2-standings-race.html',
8+
title='Conference Top 2 Teams Wins Race')
9+
10+
# Plot the two visualizations in a vertical configuration
11+
show(column(west_fig, east_fig))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Bokeh libraries
2+
from bokeh.io import output_file
3+
from bokeh.layouts import gridplot
4+
5+
# Output to file
6+
output_file('east-west-top-2-gridplot.html',
7+
title='Conference Top 2 Teams Wins Race')
8+
9+
# Reduce the width of both figures
10+
east_fig.plot_width = west_fig.plot_width = 300
11+
12+
# Edit the titles
13+
east_fig.title.text = 'Eastern Conference'
14+
west_fig.title.text = 'Western Conference'
15+
16+
# Plot the two visualizations with placeholders
17+
east_west_gridplot = gridplot([[west_fig, None], [None, east_fig]],
18+
toolbar_location='right')
19+
20+
# Plot the two visualizations in a horizontal configuration
21+
show(east_west_gridplot)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Bokeh libraries
2+
from bokeh.io import output_file
3+
from bokeh.layouts import gridplot
4+
5+
# Output to file
6+
output_file('east-west-top-2-gridplot.html',
7+
title='Conference Top 2 Teams Wins Race')
8+
9+
# Reduce the width of both figures
10+
east_fig.plot_width = west_fig.plot_width = 300
11+
12+
# Edit the titles
13+
east_fig.title.text = 'Eastern Conference'
14+
west_fig.title.text = 'Western Conference'
15+
16+
# Configure the gridplot
17+
east_west_gridplot = gridplot([[west_fig, east_fig]],
18+
toolbar_location='right')
19+
20+
# Plot the two visualizations in a horizontal configuration
21+
show(east_west_gridplot)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Bokeh libraries
2+
from bokeh.plotting import figure, show
3+
from bokeh.models import ColumnDataSource, CDSView, GroupFilter
4+
5+
# Create a ColumnDataSource
6+
standings_cds = ColumnDataSource(standings)
7+
8+
# Create the views for each team
9+
celtics_view = CDSView(source=standings_cds,
10+
filters=[GroupFilter(column_name='teamAbbr',
11+
group='BOS')])
12+
13+
raptors_view = CDSView(source=standings_cds,
14+
filters=[GroupFilter(column_name='teamAbbr',
15+
group='TOR')])
16+
17+
rockets_view = CDSView(source=standings_cds,
18+
filters=[GroupFilter(column_name='teamAbbr',
19+
group='HOU')])
20+
warriors_view = CDSView(source=standings_cds,
21+
filters=[GroupFilter(column_name='teamAbbr',
22+
group='GS')])
23+
24+
# Create and configure the figure
25+
east_fig = figure(x_axis_type='datetime',
26+
plot_height=300,
27+
x_axis_label='Date',
28+
y_axis_label='Wins',
29+
toolbar_location=None)
30+
31+
west_fig = figure(x_axis_type='datetime',
32+
plot_height=300,
33+
x_axis_label='Date',
34+
y_axis_label='Wins',
35+
toolbar_location=None)
36+
37+
# Configure the figures for each conference
38+
east_fig.step('stDate', 'gameWon',
39+
color='#007A33', legend='Celtics',
40+
source=standings_cds, view=celtics_view)
41+
east_fig.step('stDate', 'gameWon',
42+
color='#CE1141', legend='Raptors',
43+
source=standings_cds, view=raptors_view)
44+
45+
west_fig.step('stDate', 'gameWon', color='#CE1141', legend='Rockets',
46+
source=standings_cds, view=rockets_view)
47+
west_fig.step('stDate', 'gameWon', color='#006BB6', legend='Warriors',
48+
source=standings_cds, view=warriors_view)
49+
50+
# Move the legend to the upper left corner
51+
east_fig.legend.location = 'top_left'
52+
west_fig.legend.location = 'top_left'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Bokeh Library
2+
from bokeh.io import output_file
3+
from bokeh.models.widgets import Tabs, Panel
4+
5+
# Output to file
6+
output_file('east-west-top-2-tabbed_layout.html',
7+
title='Conference Top 2 Teams Wins Race')
8+
9+
# Increase the plot widths
10+
east_fig.plot_width = west_fig.plot_width = 800
11+
12+
# Create two panels, one for each conference
13+
east_panel = Panel(child=east_fig, title='Eastern Conference')
14+
west_panel = Panel(child=west_fig, title='Western Conference')
15+
16+
# Assign the panels to Tabs
17+
tabs = Tabs(tabs=[west_panel, east_panel])
18+
19+
# Show the tabbed layout
20+
show(tabs)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Bokeh Libraries
2+
from bokeh.io import output_notebook
3+
from bokeh.plotting import figure, show
4+
5+
# The figure will be rendered inline in my Jupyter Notebook
6+
output_notebook()
7+
8+
# Example figure
9+
fig = figure(background_fill_color='gray',
10+
background_fill_alpha=0.5,
11+
border_fill_color='blue',
12+
border_fill_alpha=0.25,
13+
plot_height=300,
14+
plot_width=500,
15+
h_symmetry=True,
16+
x_axis_label='X Label',
17+
x_axis_type='datetime',
18+
x_axis_location='above',
19+
x_range=('2018-01-01', '2018-06-30'),
20+
y_axis_label='Y Label',
21+
y_axis_type='linear',
22+
y_axis_location='left',
23+
y_range=(0, 100),
24+
title='Example Figure',
25+
title_location='right',
26+
toolbar_location='below',
27+
tools='save')
28+
29+
# Remove the gridlines from the figure() object
30+
fig.grid.grid_line_color = None
31+
32+
# See what it looks like
33+
show(fig)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Bokeh Libraries
2+
from bokeh.io import output_notebook
3+
from bokeh.plotting import figure, show
4+
5+
# The figure will be rendered inline in my Jupyter Notebook
6+
output_notebook()
7+
8+
# Example figure
9+
fig = figure(background_fill_color='gray',
10+
background_fill_alpha=0.5,
11+
border_fill_color='blue',
12+
border_fill_alpha=0.25,
13+
plot_height=300,
14+
plot_width=500,
15+
h_symmetry=True,
16+
x_axis_label='X Label',
17+
x_axis_type='datetime',
18+
x_axis_location='above',
19+
x_range=('2018-01-01', '2018-06-30'),
20+
y_axis_label='Y Label',
21+
y_axis_type='linear',
22+
y_axis_location='left',
23+
y_range=(0, 100),
24+
title='Example Figure',
25+
title_location='right',
26+
toolbar_location='below',
27+
tools='save')
28+
29+
# See what it looks like
30+
show(fig)

0 commit comments

Comments
 (0)