Learn how to use Python by creating an animated bar chart!
In this blog post were going to walk through how to create an animated bar chart that shows how the rushing leaders changed over time throughout the 2021 season. The bar chart will display the season long rushing leaders at each week during the regular season. We can do this in three main steps: data retrieval, data manipulation, and plotting. Let's get straight away into it.
The first phase is the data retrieval phase. We are using play by play data across the 2021 and will pull this from NFLfastpy. Like always, start with the installation (if you don't have NFLfastpy already), and then use the load_pbp_data method to load in the data. Play by play data contains information for every play for the desired time period. So we'll be looking at 2021 data and using every rush from the entire season.
5 rows × 372 columns
We have worked with play by play data in the past and this time around we are going to focus on a few key features. In order to get NFL rushing leaders each week in 2021 we need to grab the rushing yards for each player, grouped by week. To do this we use the groupby method and sum. To understand what this looks like I selected Derrick Henry's rushing yards in each game. While doing this I noticed that our data includes playoff weeks, so we can filter that out in the next step. I always like to check that the data makes sense in the real life context it comes from, and in this case we only see data from week 1 until week 8 for Henry, after which he got hurt, so this checks out.
Next we're going to work on formating the data to be plotted. The bar_chart_race package that we use for plotting requires a specific data format to properly work, so we're going to take a few steps to get it into a format that works well with that package. First we need to make a pivot table so that the index on the left is the NFL week, and the columns are players with their respective rushing yards.
5 rows × 370 columns
Now we have to fill the NaN values with 0. We see NaN values in the dataframe since these are instances when the players did not get rushes in the given week, hence they got 0 rushing yards. Lastly, we have to do a cumulative sum so that we are adding up the rushing yards for each player individually. We can do this with a cumsum() method.
5 rows × 370 columns
Next we clean up the size of the dataframe by only keeping the rows that will show up in the bar chart. To do this we loop through the data and only keep the top 10 at any given week. This step is not entirely necessary, but I thought it was best to clean up our data frame and get it plot ready.
5 rows × 26 columns
Finally were ready to plot our data. To do this I found a package that fits our use case perfectly. It takes in a dataframe that is partitioned over time and allows the user flexibility to change timestamps and transitions. First we install the package and then plot our data. I recommend playing around with some of the features to see how the plot reacts. If you are doing this in Google Colab you should see a MP4 file pop up in the file tab on the left with our final result.
I was really happy with this result, I think visualization is one of the most important parts of data science. I've watched it many times over and every time you can notice something new. Like I mentioned earlier seeing Derrick Henry dominate over the first 8 weeks and then get injured is really cool to see in this type of plot, and he stayed in the top 10!
I hope you like this type of blog where we create a cool visual, and as always let me know if you have any questions.