I noticed something new1 while poking around stats.nba.com (the NBA's official stats portal). While looking at a team's field goal attempts for the season, a Hex Map now appears alongside the shot plot and shot zones figures (see an example here). I've loved these Kirk Goldsberry-style hex maps since they first appeared on Grantland. In fact, I don't think there's anything more responsible for my interest in NBA analytics than seeing how much information this design could pack into such a clean and beautiful graphic. There are several good blog posts out there about creating similar graphics using the NBA Stats API and Python, most notably from Savvas Tjortjoglou and Eyal Shafran. I'd recommend reading Eyal Shafran's post if you're unfamiliar with these figures.

In this post, I'll demonstrate how to re-create these Hex Maps for entire team's rather than individual players with Python. I'll also flip the traditional approach and create Hex Maps that compare a team's allowed Opponent Field Goal percentage to league average for each shot location, giving some insight into a team's particular defensive strengths and weaknesses (ex: protecting the rim, defending the three point line, etc.)

Modifying NBAapi¶

To generate the Hex Map shot charts, I'll use matplotlib . Thankfully, most of the work required for generating a basketball court and overlying a hexbin has already been done by the two individuals I link to above. Eyal Shafran's NBAapi Python package provides functions for creating Hex Maps for individual players. I've forked NBAapi and updated it to Python 32. Additionally, I've added another module, plot_team.py , which is just a modified version of plot.py for working with entire teams rather than individual players.