I'm going to plot the shot chart for every MVP winner since the 96-97 season (data is not available earlier than that):
In [14]:
import pandas as pd
import NBAapi as nba
import numpy as np
import seaborn
import matplotlib.pyplot as plt
%matplotlib inline
In [15]:
def seasons_string(start,end):
'''
creates a list of NBA seasons from start-end
'''
years = np.arange(start,end+1)
seasons = []
for year in years:
string1 = str(year)
string2 = str(year+1)
season = '{}-{}'.format(string1,string2[-2:])
seasons.append(season)
return seasons
Let's load the list of players from the NBA API:
In [16]:
player_list = nba.player.commonallplayers(currentseason=0) # load a list of NBA players including retired players
And now let's create a list of names and a list of seasons:
In [17]:
players = ['Malone, Karl',
'Jordan, Michael',
'Malone, Karl',
'O\'Neal, Shaquille',
'Iverson, Allen',
'Duncan, Tim',
'Duncan, Tim',
'Garnett, Kevin',
'Nash, Steve',
'Nash, Steve',
'Nowitzki, Dirk',
'Bryant, Kobe',
'James, LeBron',
'James, LeBron',
'Rose, Derrick',
'James, LeBron',
'James, LeBron',
'Durant, Kevin',
'Curry, Stephen',
'Curry, Stephen']
# create season string based on the seasons Steph Curry played in the league
seasons = seasons_string(1996,2016)
for season,player in zip(seasons,players):
print(season + ': ' + player)
And now we can easily create the shot charts following the guidelines from this post:
In [18]:
for season,player in zip(seasons,players):
plt.figure(figsize=(15,12),facecolor='white')
player_id = player_list[player_list['DISPLAY_LAST_COMMA_FIRST']== player].PERSON_ID # get players id
shotchart,leagueavergae = nba.shotchart.shotchartdetail(playerid=player_id,season=season) # get shot chart data from NBA.stats
if season=='1996-97':
nba.plot.grantland_shotchart(shotchart,leagueavergae,short_three=True)
else:
nba.plot.grantland_shotchart(shotchart,leagueavergae)
plt.text(0,38,season,fontsize=20,horizontalalignment='center',verticalalignment='center')
plt.show()
Comments
comments powered by Disqus