diff --git a/intro-to-bokeh/README.md b/intro-to-bokeh/README.md index fcda9e3a51..5d0eeb21e3 100644 --- a/intro-to-bokeh/README.md +++ b/intro-to-bokeh/README.md @@ -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. diff --git a/intro-to-bokeh/code-snippets/bokeh-template.py b/intro-to-bokeh/code-snippets/bokeh-template.py index 35391023f1..c39284cab5 100644 --- a/intro-to-bokeh/code-snippets/bokeh-template.py +++ b/intro-to-bokeh/code-snippets/bokeh-template.py @@ -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 diff --git a/intro-to-bokeh/code-snippets/east-top-2-race-v0.py b/intro-to-bokeh/code-snippets/east-top-2-race-v0.py index a50c191f2b..461f22f50d 100644 --- a/intro-to-bokeh/code-snippets/east-top-2-race-v0.py +++ b/intro-to-bokeh/code-snippets/east-top-2-race-v0.py @@ -1,7 +1,7 @@ # 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( @@ -9,24 +9,15 @@ 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", @@ -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 diff --git a/intro-to-bokeh/code-snippets/east-west-column-layout.py b/intro-to-bokeh/code-snippets/east-west-column-layout.py index 17146aec48..d08cc28b7f 100644 --- a/intro-to-bokeh/code-snippets/east-west-column-layout.py +++ b/intro-to-bokeh/code-snippets/east-west-column-layout.py @@ -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( @@ -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 diff --git a/intro-to-bokeh/code-snippets/east-west-gridplot-layout-with-nones.py b/intro-to-bokeh/code-snippets/east-west-gridplot-layout-with-nones.py index ee458b7065..e229793d09 100644 --- a/intro-to-bokeh/code-snippets/east-west-gridplot-layout-with-nones.py +++ b/intro-to-bokeh/code-snippets/east-west-gridplot-layout-with-nones.py @@ -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( @@ -21,4 +21,4 @@ ) # Plot the two visualizations in a horizontal configuration -show(east_west_gridplot) # noqa +show(east_west_gridplot) # noqa: F821 diff --git a/intro-to-bokeh/code-snippets/east-west-gridplot-layout.py b/intro-to-bokeh/code-snippets/east-west-gridplot-layout.py index ed9badc27f..fda1d384a4 100644 --- a/intro-to-bokeh/code-snippets/east-west-gridplot-layout.py +++ b/intro-to-bokeh/code-snippets/east-west-gridplot-layout.py @@ -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 diff --git a/intro-to-bokeh/code-snippets/east-west-layout-prep.py b/intro-to-bokeh/code-snippets/east-west-layout-prep.py index 6e07bc9de8..e054d131ee 100644 --- a/intro-to-bokeh/code-snippets/east-west-layout-prep.py +++ b/intro-to-bokeh/code-snippets/east-west-layout-prep.py @@ -1,34 +1,17 @@ # 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, @@ -36,7 +19,7 @@ west_fig = figure( x_axis_type="datetime", - plot_height=300, + height=300, x_axis_label="Date", y_axis_label="Wins", toolbar_location=None, @@ -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 diff --git a/intro-to-bokeh/code-snippets/east-west-tabbed-layout.py b/intro-to-bokeh/code-snippets/east-west-tabbed-layout.py index 6e3b87dfe9..8796eef708 100644 --- a/intro-to-bokeh/code-snippets/east-west-tabbed-layout.py +++ b/intro-to-bokeh/code-snippets/east-west-tabbed-layout.py @@ -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( @@ -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 diff --git a/intro-to-bokeh/code-snippets/first-figure-no-gridlines.py b/intro-to-bokeh/code-snippets/first-figure-no-gridlines.py index 532fecb1ee..fc38aacdb0 100644 --- a/intro-to-bokeh/code-snippets/first-figure-no-gridlines.py +++ b/intro-to-bokeh/code-snippets/first-figure-no-gridlines.py @@ -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", @@ -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 diff --git a/intro-to-bokeh/code-snippets/first-figure.py b/intro-to-bokeh/code-snippets/first-figure.py index 18c771277e..1eda1fa839 100644 --- a/intro-to-bokeh/code-snippets/first-figure.py +++ b/intro-to-bokeh/code-snippets/first-figure.py @@ -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", diff --git a/intro-to-bokeh/code-snippets/first-glyphs.py b/intro-to-bokeh/code-snippets/first-glyphs.py index f910ec02f2..305a8b7873 100644 --- a/intro-to-bokeh/code-snippets/first-glyphs.py +++ b/intro-to-bokeh/code-snippets/first-glyphs.py @@ -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) diff --git a/intro-to-bokeh/code-snippets/hover-tooltip-example0.py b/intro-to-bokeh/code-snippets/hover-tooltip-example0.py index ad60fc3a6f..29cc86e0fb 100644 --- a/intro-to-bokeh/code-snippets/hover-tooltip-example0.py +++ b/intro-to-bokeh/code-snippets/hover-tooltip-example0.py @@ -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 diff --git a/intro-to-bokeh/code-snippets/hover-tooltip-example1.py b/intro-to-bokeh/code-snippets/hover-tooltip-example1.py index 54e074f1cc..5a187eb961 100644 --- a/intro-to-bokeh/code-snippets/hover-tooltip-example1.py +++ b/intro-to-bokeh/code-snippets/hover-tooltip-example1.py @@ -7,10 +7,10 @@ ] # Configure a renderer to be used upon hover -hover_glyph = fig.circle( # noqa +hover_glyph = fig.scatter( # noqa: F821 x="play3PA", y="pct3PM", - source=three_takers_cds, # noqa + source=three_takers_cds, # noqa: F821 size=15, alpha=0, hover_fill_color="black", @@ -18,7 +18,7 @@ ) # Add the HoverTool to the figure -fig.add_tools(HoverTool(tooltips=tooltips, renderers=[hover_glyph])) # noqa +fig.add_tools(HoverTool(tooltips=tooltips, renderers=[hover_glyph])) # noqa: F821 # Visualize -show(fig) # noqa +show(fig) # noqa: F821 diff --git a/intro-to-bokeh/code-snippets/interactive-legend.py b/intro-to-bokeh/code-snippets/interactive-legend.py index 178a339bc5..8210cdaa9a 100644 --- a/intro-to-bokeh/code-snippets/interactive-legend.py +++ b/intro-to-bokeh/code-snippets/interactive-legend.py @@ -1,31 +1,29 @@ # Bokeh Libraries +from bokeh.plotting import figure, show from bokeh.io import output_file +from bokeh.models import ColumnDataSource, CDSView, GroupFilter from bokeh.layouts import row -from bokeh.models import CDSView, ColumnDataSource, GroupFilter -from bokeh.plotting import figure, show -# Output inline in the notebook +# Output to file output_file("lebron-vs-durant.html", title="LeBron James vs. Kevin Durant") # Store the data in a ColumnDataSource -player_gm_stats = ColumnDataSource(player_stats) # noqa +player_gm_stats = ColumnDataSource(player_stats) # noqa: F821 # Create a view for each player -lebron_filters = [ - GroupFilter(column_name="playFNm", group="LeBron"), - GroupFilter(column_name="playLNm", group="James"), -] -lebron_view = CDSView(source=player_gm_stats, filters=lebron_filters) +lebron_filter = GroupFilter( + column_name="playFNm", group="LeBron" +) & GroupFilter(column_name="playLNm", group="James") +lebron_view = CDSView(filter=lebron_filter) -durant_filters = [ - GroupFilter(column_name="playFNm", group="Kevin"), - GroupFilter(column_name="playLNm", group="Durant"), -] -durant_view = CDSView(source=player_gm_stats, filters=durant_filters) +durant_filter = GroupFilter( + column_name="playFNm", group="Kevin" +) & GroupFilter(column_name="playLNm", group="Durant") +durant_view = CDSView(filter=durant_filter) # Consolidate the common keyword arguments in dicts common_figure_kwargs = { - "plot_width": 400, + "width": 400, "x_axis_label": "Points", "toolbar_location": None, } @@ -39,12 +37,12 @@ common_lebron_kwargs = { "view": lebron_view, "color": "#002859", - "legend": "LeBron James", + "legend_label": "LeBron James", } common_durant_kwargs = { "view": durant_view, "color": "#FFC324", - "legend": "Kevin Durant", + "legend_label": "Kevin Durant", } # Create the two figures and draw the data @@ -53,14 +51,14 @@ title="Click Legend to HIDE Data", y_axis_label="Rebounds", ) -hide_fig.circle(**common_circle_kwargs, **common_lebron_kwargs) -hide_fig.circle(**common_circle_kwargs, **common_durant_kwargs) +hide_fig.scatter(**common_circle_kwargs, **common_lebron_kwargs) +hide_fig.scatter(**common_circle_kwargs, **common_durant_kwargs) mute_fig = figure(**common_figure_kwargs, title="Click Legend to MUTE Data") -mute_fig.circle( +mute_fig.scatter( **common_circle_kwargs, **common_lebron_kwargs, muted_alpha=0.1 ) -mute_fig.circle( +mute_fig.scatter( **common_circle_kwargs, **common_durant_kwargs, muted_alpha=0.1 ) diff --git a/intro-to-bokeh/code-snippets/linked-axes.py b/intro-to-bokeh/code-snippets/linked-axes.py index bb7f992dfd..48c6944624 100644 --- a/intro-to-bokeh/code-snippets/linked-axes.py +++ b/intro-to-bokeh/code-snippets/linked-axes.py @@ -1,14 +1,14 @@ # Bokeh Libraries -from bokeh.io import output_file -from bokeh.layouts import column, gridplot -from bokeh.models import CategoricalColorMapper, ColumnDataSource, Div from bokeh.plotting import figure, show +from bokeh.io import output_file +from bokeh.models import ColumnDataSource, CategoricalColorMapper, Div +from bokeh.layouts import gridplot, column # Output to file output_file("phi-gm-linked-stats.html", title="76ers Game Log") # Store the data in a ColumnDataSource -gm_stats_cds = ColumnDataSource(phi_gm_stats) # noqa +gm_stats_cds = ColumnDataSource(phi_gm_stats) # noqa: F821 # Create a CategoricalColorMapper that assigns a color to wins and losses win_loss_mapper = CategoricalColorMapper( @@ -31,8 +31,8 @@ # Create a figure fig = figure( y_axis_label=stat_label, - plot_height=200, - plot_width=400, + height=200, + width=400, x_range=(1, 10), tools=["xpan", "reset", "save"], ) diff --git a/intro-to-bokeh/code-snippets/linked-selection.py b/intro-to-bokeh/code-snippets/linked-selection.py index 8aac73a6a1..a011777fb6 100644 --- a/intro-to-bokeh/code-snippets/linked-selection.py +++ b/intro-to-bokeh/code-snippets/linked-selection.py @@ -1,70 +1,71 @@ # Bokeh Libraries +from bokeh.plotting import figure, show from bokeh.io import output_file -from bokeh.layouts import gridplot from bokeh.models import ( - CategoricalColorMapper, ColumnDataSource, + CategoricalColorMapper, NumeralTickFormatter, ) -from bokeh.plotting import figure, show +from bokeh.layouts import gridplot -# Output inline in the notebook +# Output to file output_file( "phi-gm-linked-selections.html", title="76ers Percentages vs. Win-Loss" ) # Store the data in a ColumnDataSource -gm_stats_cds = ColumnDataSource(phi_gm_stats_2) # noqa +gm_stats_cds = ColumnDataSource(phi_gm_stats_2) # noqa: F821 -# Create a CategoricalColorMapper that assigns specific -# colors to wins and losses +# Create a CategoricalColorMapper that assigns specific colors to wins +# and losses win_loss_mapper = CategoricalColorMapper( factors=["W", "L"], palette=["Green", "Red"] ) # Specify the tools -toolList = ["lasso_select", "tap", "reset", "save"] +tool_list = ["lasso_select", "tap", "reset", "save"] # Create a figure relating the percentages -pctFig = figure( +pct_fig = figure( title="2PT FG % vs 3PT FG %, 2017-18 Regular Season", - plot_height=400, - plot_width=400, - tools=toolList, + height=400, + width=400, + tools=tool_list, x_axis_label="2PT FG%", y_axis_label="3PT FG%", ) # Draw with circle markers -pctFig.circle( +pct_fig.scatter( x="team2P%", y="team3P%", source=gm_stats_cds, size=12, color="black" ) -# Format the y-axis tick labels as percenages -pctFig.xaxis[0].formatter = NumeralTickFormatter(format="00.0%") -pctFig.yaxis[0].formatter = NumeralTickFormatter(format="00.0%") +# Format the axis tick labels as percentages +pct_fig.xaxis[0].formatter = NumeralTickFormatter(format="00.0%") +pct_fig.yaxis[0].formatter = NumeralTickFormatter(format="00.0%") # Create a figure relating the totals -totFig = figure( +tot_fig = figure( title="Team Points vs Opponent Points, 2017-18 Regular Season", - plot_height=400, - plot_width=400, - tools=toolList, + height=400, + width=400, + tools=tool_list, x_axis_label="Team Points", y_axis_label="Opponent Points", ) # Draw with square markers -totFig.square( +tot_fig.scatter( x="teamPTS", y="opptPTS", source=gm_stats_cds, size=10, + marker="square", color=dict(field="winLoss", transform=win_loss_mapper), ) # Create layout -grid = gridplot([[pctFig, totFig]]) +grid = gridplot([[pct_fig, tot_fig]]) # Visualize show(grid) diff --git a/intro-to-bokeh/code-snippets/multiply-glyphs.py b/intro-to-bokeh/code-snippets/multiply-glyphs.py index b2b1f140b0..f85e349c09 100644 --- a/intro-to-bokeh/code-snippets/multiply-glyphs.py +++ b/intro-to-bokeh/code-snippets/multiply-glyphs.py @@ -15,8 +15,8 @@ # Create a figure with a datetime type x-axis fig = figure( title="My Tutorial Progress", - plot_height=400, - plot_width=700, + height=400, + width=700, x_axis_label="Day Number", y_axis_label="Words Written", x_minor_ticks=2, @@ -31,7 +31,7 @@ top=daily_words, color="blue", width=0.75, - legend="Daily", + legend_label="Daily", ) # The cumulative sum will be a trend line @@ -40,7 +40,7 @@ y=cumulative_words, color="gray", line_width=1, - legend="Cumulative", + legend_label="Cumulative", ) # Put the legend in the upper left corner diff --git a/intro-to-bokeh/code-snippets/output-file-example.py b/intro-to-bokeh/code-snippets/output-file-example.py index 6364d1ff6f..d2895f7758 100644 --- a/intro-to-bokeh/code-snippets/output-file-example.py +++ b/intro-to-bokeh/code-snippets/output-file-example.py @@ -2,8 +2,7 @@ from bokeh.io import output_file from bokeh.plotting import figure, show -# The figure will be rendered in a static HTML file -# called output_file_test.html +# The figure will be rendered in a static HTML file called output_file_test.html output_file("output_file_test.html", title="Empty Bokeh Figure") # Set up a generic figure() object diff --git a/intro-to-bokeh/code-snippets/read-nba-data.py b/intro-to-bokeh/code-snippets/read-nba-data.py index 6e8d3e7292..d0f66ef5eb 100644 --- a/intro-to-bokeh/code-snippets/read-nba-data.py +++ b/intro-to-bokeh/code-snippets/read-nba-data.py @@ -34,9 +34,8 @@ # Aggregate the total three-point attempts and makes for each player three_takers = ( - three_takers.groupby("name") + three_takers.groupby("name")[["play3PA", "play3PM"]] .sum() - .loc[:, ["play3PA", "play3PM"]] .sort_values("play3PA", ascending=False) ) @@ -48,7 +47,7 @@ #################################################################### -# phi_gm_stats: Used in Linking Axes and Selections +# phi_gm_stats: Used in Linking Axes # Isolate relevant data phi_gm_stats = ( @@ -56,7 +55,17 @@ (team_stats["teamAbbr"] == "PHI") & (team_stats["seasTyp"] == "Regular") ] - .loc[:, ["gmDate", "teamPTS", "teamTRB", "teamAST", "teamTO", "opptPTS"]] + .loc[ + :, + [ + "gmDate", + "teamPTS", + "teamTRB", + "teamAST", + "teamTO", + "opptPTS", + ], + ] .sort_values("gmDate") ) @@ -76,7 +85,7 @@ phi_gm_stats["winLoss"] = win_loss #################################################################### -# phi_gm_stats_2: Used in Linking Axes and Selections +# phi_gm_stats_2: Used in Linking Selections # Isolate relevant data phi_gm_stats_2 = ( diff --git a/intro-to-bokeh/code-snippets/selection-example.py b/intro-to-bokeh/code-snippets/selection-example.py index c13a771bdd..ac664062f4 100644 --- a/intro-to-bokeh/code-snippets/selection-example.py +++ b/intro-to-bokeh/code-snippets/selection-example.py @@ -1,7 +1,7 @@ # Bokeh Libraries +from bokeh.plotting import figure, show from bokeh.io import output_file from bokeh.models import ColumnDataSource, NumeralTickFormatter -from bokeh.plotting import figure, show # Output to file output_file( @@ -9,15 +9,15 @@ ) # Store the data in a ColumnDataSource -three_takers_cds = ColumnDataSource(three_takers) # noqa +three_takers_cds = ColumnDataSource(three_takers) # noqa: F821 # Specify the selection tools to be made available select_tools = ["box_select", "lasso_select", "poly_select", "tap", "reset"] # Create the figure fig = figure( - plot_height=400, - plot_width=600, + height=400, + width=600, x_axis_label="Three-Point Shots Attempted", y_axis_label="Percentage Made", title="3PT Shots Attempted vs. Percentage Made (min. 100 3PA), 2017-18", @@ -29,10 +29,11 @@ fig.yaxis[0].formatter = NumeralTickFormatter(format="00.0%") # Add square representing each player -fig.square( +fig.scatter( x="play3PA", y="pct3PM", source=three_takers_cds, + marker="square", color="royalblue", selection_color="deepskyblue", nonselection_color="lightgray", diff --git a/intro-to-bokeh/code-snippets/west-top-2-race-v0.py b/intro-to-bokeh/code-snippets/west-top-2-race-v0.py index ada0c88007..4cba7d983f 100644 --- a/intro-to-bokeh/code-snippets/west-top-2-race-v0.py +++ b/intro-to-bokeh/code-snippets/west-top-2-race-v0.py @@ -1,7 +1,7 @@ # Bokeh libraries +from bokeh.plotting import figure, show from bokeh.io import output_file from bokeh.models import ColumnDataSource -from bokeh.plotting import figure, show # Output to file output_file( @@ -10,8 +10,8 @@ ) # Isolate the data for the Rockets and Warriors -rockets_data = west_top_2[west_top_2["teamAbbr"] == "HOU"] # noqa -warriors_data = west_top_2[west_top_2["teamAbbr"] == "GS"] # noqa +rockets_data = west_top_2[west_top_2["teamAbbr"] == "HOU"] # noqa: F821 +warriors_data = west_top_2[west_top_2["teamAbbr"] == "GS"] # noqa: F821 # Create a ColumnDataSource object for each team rockets_cds = ColumnDataSource(rockets_data) @@ -20,8 +20,8 @@ # Create and configure the figure fig = figure( x_axis_type="datetime", - plot_height=300, - plot_width=600, + height=300, + width=600, title="Western Conference Top 2 Teams Wins Race, 2017-18", x_axis_label="Date", y_axis_label="Wins", @@ -30,13 +30,17 @@ # Render the race as step lines fig.step( - "stDate", "gameWon", color="#CE1141", legend="Rockets", source=rockets_cds + "stDate", + "gameWon", + color="#CE1141", + legend_label="Rockets", + source=rockets_cds, ) fig.step( "stDate", "gameWon", color="#006BB6", - legend="Warriors", + legend_label="Warriors", source=warriors_cds, ) diff --git a/intro-to-bokeh/code-snippets/west-top-2-race-v1.py b/intro-to-bokeh/code-snippets/west-top-2-race-v1.py index 50a5ba01c5..1ee9e07a2c 100644 --- a/intro-to-bokeh/code-snippets/west-top-2-race-v1.py +++ b/intro-to-bokeh/code-snippets/west-top-2-race-v1.py @@ -1,7 +1,7 @@ # 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, CDSView, GroupFilter # Output to file output_file( @@ -10,43 +10,41 @@ ) # Create a ColumnDataSource -west_cds = ColumnDataSource(west_top_2) # noqa +west_cds = ColumnDataSource(west_top_2) # noqa: F821 # Create views 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")] -) +rockets_view = CDSView(filter=GroupFilter(column_name="teamAbbr", group="HOU")) +warriors_view = CDSView(filter=GroupFilter(column_name="teamAbbr", group="GS")) # Create and configure the figure west_fig = figure( x_axis_type="datetime", - plot_height=300, - plot_width=600, + height=300, + 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( +# Render the race as markers, one per day +west_fig.scatter( "stDate", "gameWon", source=west_cds, view=rockets_view, color="#CE1141", - legend="Rockets", + legend_label="Rockets", + size=4, ) -west_fig.step( +west_fig.scatter( "stDate", "gameWon", source=west_cds, view=warriors_view, color="#006BB6", - legend="Warriors", + legend_label="Warriors", + size=4, ) # Move the legend to the upper left corner diff --git a/intro-to-bokeh/requirements.txt b/intro-to-bokeh/requirements.txt new file mode 100644 index 0000000000..1883de87a9 --- /dev/null +++ b/intro-to-bokeh/requirements.txt @@ -0,0 +1,3 @@ +bokeh==3.10.2 +numpy==2.5.5 +pandas==3.1.2