Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions intro-to-bokeh/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
# Getting Started with Interactive Visualization Using Bokeh
# Interactive Data Visualization in Python With Bokeh

Data and code snippets from https://realpython.com/python-data-visualization-bokeh/
Data and code snippets for the Real Python tutorial [Interactive Data Visualization in Python With Bokeh](https://realpython.com/python-data-visualization-bokeh/).

## Setup

You need Python 3.12 or later. Create and activate a virtual environment, then install the pinned dependencies:

```console
$ python -m pip install -r requirements.txt
```

To render the plots inside a notebook, also install Jupyter with `python -m pip install jupyterlab==4.6.7`.

## Usage

The snippets mirror the tutorial's code blocks and build on each other the same way. Run them in order in one Python session or Jupyter Notebook, started from the `data/` directory so that pandas finds the CSV files. Start with `read-nba-data.py`, which loads the data and prepares the DataFrames that later snippets use, and run `east-west-layout-prep.py` before each `east-west-*-layout.py` snippet.

## Data

Found in the [`data\`](https://github.com/realpython/materials/tree/master/intro-to-bokeh/data) directory.
Found in the [`data/`](https://github.com/realpython/materials/tree/master/intro-to-bokeh/data) directory.

The tutorial utilizes the following three files:

- **2017-18_standings.csv**
- Contains daily team standing information for the 2017-18 season
- [Original Kaggle Link](https://www.kaggle.com/pablote/nba-enhanced-stats/downloads/2017-18_standings.csv/26)
- [Original Kaggle Link](https://www.kaggle.com/datasets/pablote/nba-enhanced-stats?select=2017-18_standings.csv)
- **2017-18_playerBoxScore.csv**
- Contains per-game box score player statistics for the 2017-18 season
- [Original Kaggle Link](https://www.kaggle.com/pablote/nba-enhanced-stats/downloads/2017-18_playerBoxScore.csv/26)
- [Original Kaggle Link](https://www.kaggle.com/datasets/pablote/nba-enhanced-stats?select=2017-18_playerBoxScore.csv)
- **2017-18_teamBoxScore.csv**
- Contains per-game box score team statistics for the 2017-18 season
- [Original Kaggle Link](https://www.kaggle.com/pablote/nba-enhanced-stats/downloads/2017-18_teamBoxScore.csv/26)
- [Original Kaggle Link](https://www.kaggle.com/datasets/pablote/nba-enhanced-stats?select=2017-18_teamBoxScore.csv)

The original Kaggle link for this data is: [**NBA Enhanced Box Score and Standings Stats**](https://www.kaggle.com/pablote/nba-enhanced-stats/home)
The original Kaggle link for this data is: [**NBA Enhanced Box Score and Standings Stats**](https://www.kaggle.com/datasets/pablote/nba-enhanced-stats)

This data is licensed under [**CC BY-SA 4.0**](https://creativecommons.org/licenses/by-sa/4.0/).

## Code Snippets

Found in the [`code-snippets\`](https://github.com/realpython/materials/tree/master/intro-to-bokeh/code-snippets) directory.
Found in the [`code-snippets/`](https://github.com/realpython/materials/tree/master/intro-to-bokeh/code-snippets) directory.

The code snippets found in the tutorial can be found here in individual files.
11 changes: 5 additions & 6 deletions intro-to-bokeh/code-snippets/bokeh-template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
visualization using Bokeh.
"""

import numpy as np # noqa

# Data handling
import pandas as pd # noqa
import pandas as pd # noqa: F401
import numpy as np # noqa: F401

# Bokeh libraries
from bokeh.io import output_file, output_notebook
from bokeh.layouts import column, gridplot, row # noqa
from bokeh.models import ColumnDataSource # noqa
from bokeh.models.widgets import Panel, Tabs # noqa
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource # noqa: F401
from bokeh.layouts import row, column, gridplot # noqa: F401
from bokeh.models import TabPanel, Tabs # noqa: F401

# Prepare the data

Expand Down
33 changes: 11 additions & 22 deletions intro-to-bokeh/code-snippets/east-top-2-race-v0.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
# Bokeh libraries
from bokeh.io import output_file
from bokeh.models import CDSView, ColumnDataSource, GroupFilter
from bokeh.plotting import figure, show
from bokeh.io import output_file
from bokeh.models import ColumnDataSource

# Output to file
output_file(
"east-top-2-standings-race.html",
title="Eastern Conference Top 2 Teams Wins Race",
)

# Create a ColumnDataSource
standings_cds = ColumnDataSource(standings) # noqa

# Create views for each team
celtics_view = CDSView(
source=standings_cds,
filters=[GroupFilter(column_name="teamAbbr", group="BOS")],
)
raptors_view = CDSView(
source=standings_cds,
filters=[GroupFilter(column_name="teamAbbr", group="TOR")],
)
# Create a ColumnDataSource for each team
celtics_cds = ColumnDataSource(standings[standings["teamAbbr"] == "BOS"]) # noqa: F821
raptors_cds = ColumnDataSource(standings[standings["teamAbbr"] == "TOR"]) # noqa: F821

# Create and configure the figure
east_fig = figure(
x_axis_type="datetime",
plot_height=300,
plot_width=600,
height=300,
width=600,
title="Eastern Conference Top 2 Teams Wins Race, 2017-18",
x_axis_label="Date",
y_axis_label="Wins",
Expand All @@ -38,17 +29,15 @@
"stDate",
"gameWon",
color="#007A33",
legend="Celtics",
source=standings_cds,
view=celtics_view,
legend_label="Celtics",
source=celtics_cds,
)
east_fig.step(
"stDate",
"gameWon",
color="#CE1141",
legend="Raptors",
source=standings_cds,
view=raptors_view,
legend_label="Raptors",
source=raptors_cds,
)

# Move the legend to the upper left corner
Expand Down
4 changes: 2 additions & 2 deletions intro-to-bokeh/code-snippets/east-west-column-layout.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Bokeh library
from bokeh.plotting import figure, show # noqa: F401
from bokeh.io import output_file
from bokeh.layouts import column
from bokeh.plotting import show

# Output to file
output_file(
Expand All @@ -10,4 +10,4 @@
)

# Plot the two visualizations in a vertical configuration
show(column(west_fig, east_fig)) # noqa
show(column(west_fig, east_fig)) # noqa: F821
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
)

# Reduce the width of both figures
east_fig.plot_width = west_fig.plot_width = 300 # noqa
east_fig.width = west_fig.width = 300 # noqa: F821

# Edit the titles
east_fig.title.text = "Eastern Conference" # noqa
west_fig.title.text = "Western Conference" # noqa
east_fig.title.text = "Eastern Conference" # noqa: F821
west_fig.title.text = "Western Conference" # noqa: F821

# Plot the two visualizations with placeholders
east_west_gridplot = gridplot(
Expand All @@ -21,4 +21,4 @@
)

# Plot the two visualizations in a horizontal configuration
show(east_west_gridplot) # noqa
show(east_west_gridplot) # noqa: F821
13 changes: 5 additions & 8 deletions intro-to-bokeh/code-snippets/east-west-gridplot-layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
)

# Reduce the width of both figures
east_fig.plot_width = west_fig.plot_width = 300 # noqa
east_fig.width = west_fig.width = 300 # noqa: F821

# Edit the titles
east_fig.title.text = "Eastern Conference" # noqa
west_fig.title.text = "Western Conference" # noqa
east_fig.title.text = "Eastern Conference" # noqa: F821
west_fig.title.text = "Western Conference" # noqa: F821

# Configure the gridplot
east_west_gridplot = gridplot(
[[west_fig, east_fig]], # noqa: F821
toolbar_location="right",
)
east_west_gridplot = gridplot([[west_fig, east_fig]], toolbar_location="right") # noqa: F821

# Plot the two visualizations in a horizontal configuration
show(east_west_gridplot) # noqa
show(east_west_gridplot) # noqa: F821
55 changes: 17 additions & 38 deletions intro-to-bokeh/code-snippets/east-west-layout-prep.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
# Bokeh libraries
from bokeh.models import CDSView, ColumnDataSource, GroupFilter
from bokeh.plotting import figure
from bokeh.plotting import figure, show # noqa: F401
from bokeh.models import ColumnDataSource

# Create a ColumnDataSource
standings_cds = ColumnDataSource(standings) # noqa

# Create the views for each team
celtics_view = CDSView(
source=standings_cds,
filters=[GroupFilter(column_name="teamAbbr", group="BOS")],
)

raptors_view = CDSView(
source=standings_cds,
filters=[GroupFilter(column_name="teamAbbr", group="TOR")],
)

rockets_view = CDSView(
source=standings_cds,
filters=[GroupFilter(column_name="teamAbbr", group="HOU")],
)
warriors_view = CDSView(
source=standings_cds,
filters=[GroupFilter(column_name="teamAbbr", group="GS")],
)
# Create a ColumnDataSource for each team
celtics_cds = ColumnDataSource(standings[standings["teamAbbr"] == "BOS"]) # noqa: F821
raptors_cds = ColumnDataSource(standings[standings["teamAbbr"] == "TOR"]) # noqa: F821
rockets_cds = ColumnDataSource(standings[standings["teamAbbr"] == "HOU"]) # noqa: F821
warriors_cds = ColumnDataSource(standings[standings["teamAbbr"] == "GS"]) # noqa: F821

# Create and configure the figure
east_fig = figure(
x_axis_type="datetime",
plot_height=300,
height=300,
x_axis_label="Date",
y_axis_label="Wins",
toolbar_location=None,
)

west_fig = figure(
x_axis_type="datetime",
plot_height=300,
height=300,
x_axis_label="Date",
y_axis_label="Wins",
toolbar_location=None,
Expand All @@ -47,34 +30,30 @@
"stDate",
"gameWon",
color="#007A33",
legend="Celtics",
source=standings_cds,
view=celtics_view,
legend_label="Celtics",
source=celtics_cds,
)
east_fig.step(
"stDate",
"gameWon",
color="#CE1141",
legend="Raptors",
source=standings_cds,
view=raptors_view,
legend_label="Raptors",
source=raptors_cds,
)

west_fig.step(
"stDate",
"gameWon",
color="#CE1141",
legend="Rockets",
source=standings_cds,
view=rockets_view,
legend_label="Rockets",
source=rockets_cds,
)
west_fig.step(
"stDate",
"gameWon",
color="#006BB6",
legend="Warriors",
source=standings_cds,
view=warriors_view,
legend_label="Warriors",
source=warriors_cds,
)

# Move the legend to the upper left corner
Expand Down
10 changes: 5 additions & 5 deletions intro-to-bokeh/code-snippets/east-west-tabbed-layout.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Bokeh Library
from bokeh.io import output_file
from bokeh.models.widgets import Panel, Tabs
from bokeh.models import TabPanel, Tabs

# Output to file
output_file(
Expand All @@ -9,14 +9,14 @@
)

# Increase the plot widths
east_fig.plot_width = west_fig.plot_width = 800 # noqa
east_fig.width = west_fig.width = 800 # noqa: F821

# Create two panels, one for each conference
east_panel = Panel(child=east_fig, title="Eastern Conference") # noqa
west_panel = Panel(child=west_fig, title="Western Conference") # noqa
east_panel = TabPanel(child=east_fig, title="Eastern Conference") # noqa: F821
west_panel = TabPanel(child=west_fig, title="Western Conference") # noqa: F821

# Assign the panels to Tabs
tabs = Tabs(tabs=[west_panel, east_panel])

# Show the tabbed layout
show(tabs) # noqa
show(tabs) # noqa: F821
8 changes: 5 additions & 3 deletions intro-to-bokeh/code-snippets/first-figure-no-gridlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
background_fill_alpha=0.5,
border_fill_color="blue",
border_fill_alpha=0.25,
plot_height=300,
plot_width=500,
h_symmetry=True,
height=300,
width=500,
x_axis_label="X Label",
x_axis_type="datetime",
x_axis_location="above",
Expand All @@ -28,6 +27,9 @@
tools="save",
)

# See what it looks like
show(fig)

# Remove the gridlines from the figure() object
fig.grid.grid_line_color = None

Expand Down
5 changes: 2 additions & 3 deletions intro-to-bokeh/code-snippets/first-figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
background_fill_alpha=0.5,
border_fill_color="blue",
border_fill_alpha=0.25,
plot_height=300,
plot_width=500,
h_symmetry=True,
height=300,
width=500,
x_axis_label="X Label",
x_axis_type="datetime",
x_axis_location="above",
Expand Down
6 changes: 3 additions & 3 deletions intro-to-bokeh/code-snippets/first-glyphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
# Create a figure with no toolbar and axis ranges of [0,3]
fig = figure(
title="My Coordinates",
plot_height=300,
plot_width=300,
height=300,
width=300,
x_range=(0, 3),
y_range=(0, 3),
toolbar_location=None,
)

# Draw the coordinates as circles
fig.circle(x=x, y=y, color="green", size=10, alpha=0.5)
fig.scatter(x=x, y=y, color="green", size=10, alpha=0.5)

# Show plot
show(fig)
4 changes: 2 additions & 2 deletions intro-to-bokeh/code-snippets/hover-tooltip-example0.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
]

# Add the HoverTool to the figure
fig.add_tools(HoverTool(tooltips=tooltips)) # noqa
fig.add_tools(HoverTool(tooltips=tooltips)) # noqa: F821

# Visualize
show(fig) # noqa
show(fig) # noqa: F821
Loading
Loading