How To Create NBA Shot Charts in Python

Learn how to create NBA shot charts in Python using the Pandas library. We’ll go over how to clean and manipulate data, and then create visualizations using the Seaborn library.

Introduction

Welcome to my guide on how to create NBA shot charts in Python. This guide will show you how to use the python package pandas-profiling to create shot charts for all 30 NBA teams. In addition, I will also show you how to create custom shot charts for specific players.

I have broken this guide down into 3 sections:

1. How to create NBA team shot charts
2. How to create custom shot charts for specific players
3. How to make your shot charts more visually appealing

At the end of this guide, you will have a strong understanding of how to create your own NBA shot charts in Python. Let’s get started!

Data Acquisition

Web Scraping

Web scraping is a process of extracting data from websites. It’s a technique employed in order to gather specific types of information from the web in an automated fashion.

For our purposes, we will focus on scraping NBA shot data from the basketball statistics website, basketball-reference.com. The website contains detailed information on every shot attempted in every NBA game since the 1986-1987 season. We will use Python’s Beautiful Soup library to extract the relevant information from the website and then use the Matplotlib library to create shot charts for specific players.

Data Cleaning

Once you have your data scraped, it is time for some quick data cleaning. For this tutorial, I will be using the 2017-2018 NBA regular season shot chart data from stats.nba.com.

The first thing we need to do is get rid of all of the null values in the dataframe. A null value is a value that is unknown or uninitialized. For our purposes, we are interested in null values that represent missed shots, so we will only drop rows where the SHOT_MADE_FLAG is null.

Next, we need to make sure that all of our numeric columns are represented as numeric values and not strings. We can do this by using the to_numeric function from pandas.

Now that our data is cleaned, we can start to visualize it!

Data Visualization

Plotly

Created by Plotly, an open-source graphing library, Plotly is a Python graphing library that makes it simple to create interactive, publication-quality charts and graphs.

With Plotly, you can create line charts, bar charts, scatter plots, heatmaps, and more. You can also use Plotly to style your charts and make them more interactive with features like hover text and annotations.

To get started with Plotly, check out our how-to guide.

Seaborn

Seaborn is a data visualization library in Python that provides a high-level interface for drawing attractive and informative statistical graphics. It is built on top of the matplotlib library and adds support for plotting categorical data, statistical estimation, and regression analysis.

Seaborn’s syntax is very similar to matplotlib’s, so if you are familiar with the latter, you will find Seaborn easy to use. In this tutorial, we will walk you through the basics of using Seaborn to create NBA shot charts.

First, let’s import the necessary libraries:

import pandas as pd
import seaborn as sns
sns.set_style(“white”)
sns.set_color_codes()
Next, we load the data into a pandas DataFrame:

df = pd.read_csv(“nba_shots.csv”)
Now, let’s take a look at the first five rows of our data:

GAME_ID MATCHUP LOCATION W … CLOSE_DEF_DIST FGM PTS player_id
0 21800001 WASHINGTON WIZARDS vs. ATL Hawks A … 6.7 1 2 johnwall-1
1 21800001 WASHINGTON WIZARDS vs. ATL Hawks A … 16.3 0 0 johnwall-1

Conclusion

In conclusion, NBA shot charts are a great way to visualize where players are taking and making shots on the court. They can be used to compare players, teams, or even specific lineups. Python makes it easy to create these charts using the seaborn library.

Similar Posts