How to map NBA player shots using CartoDB

A couple of weeks ago LA Times amazed us with their Kobe’s 30,699 shots visualization. Then they explained us how they made it. In this post, I am going to replicate their process. But in my case, I am going to plot Stephen Curry’s last regular season shots. A little bit of knowledge of the NBA API, few lines of Python and some SQL queries in CartoDB and… BOOM!

First, you have to go to the NBA shooting stats of Stephen Curry.  If you click on the number of field goals attempted, a pop up would appear.  Click on “Shotchart” and you will be able to see burden representation what we want to achieve. So, open your browser developer tools and copy the url as showed in the image below.

nba_api

Open your command line console and use the curl  command to save it in your local machine.

If you copy that url into your browser (and you have a json viewer extension), you can see the array of shots with its attributes. Secondly, you have to transform the json file to a csv file. In order to get this done you need Python:

Congratulations! The techie part is over. Now mapping time! Import the csv file to CartoDB and get your hands dirty with SQL. First, you have to transform “loc_y” and “loc_y” into points (Jorge Sanz did the same trick with NBA players movements some time ago). Create a new dataset from the following query:

Now it is time to edit and clean the dataset. There are a few things that we can improve. Time and points are just two:

There is an important field missing: match up teams. In order to get this information, we need to relate game ids (field that we have) with match up teams. Go to Stephen Curry’s game logs and get the two urls (there are two pages of games). And repeat the same steps in order to get a dataset containing both the match up teams and the game ids. Then create a new dataset from the next query:

A final touch, scale a little bit your points to improve the visualization:

I have chose to visualize made (green) and missed shots (red). So this is my CartoCSS style:

Finally, choose a basketball court image as a basemap. Voilá!

Footnote: Things are starting to get more interesting. Using CartoDB’s Deep Insights new technology you can analyze and visualize this information as never seen before…

GIS Analyst. Working at @CARTO and @ongawa4d.

Leave a Reply

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *