-
Notifications
You must be signed in to change notification settings - Fork 0
/
harden.py
123 lines (95 loc) · 4.53 KB
/
harden.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import requests
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# James Harden's 2014-2015 shot chart data URL
shot_chart_url = 'http://stats.nba.com/stats/shotchartdetail?CFID=33&CFPAR'\
'AMS=2014-15&ContextFilter=&ContextMeasure=FGA&DateFrom=&D'\
'ateTo=&GameID=&GameSegment=&LastNGames=0&LeagueID=00&Loca'\
'tion=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&'\
'PaceAdjust=N&PerMode=PerGame&Period=0&PlayerID=201935&Plu'\
'sMinus=N&Position=&Rank=N&RookieYear=&Season=2014-15&Seas'\
'onSegment=&SeasonType=Regular+Season&TeamID=0&VsConferenc'\
'e=&VsDivision=&mode=Advanced&showDetails=0&showShots=1&sh'\
'owZones=0'
# Get data from URL
response = requests.get(shot_chart_url)
headers = response.json()['resultSets'][0]['headers']
shots = response.json()['resultSets'][0]['rowSet']
# Create pandas DataFrame from shots
shot_df = pd.DataFrame(shots, columns=headers)
# from IPython.display import display
# with pd.option_context('display.max_columns', None):
# display(shot_df.head())
from matplotlib.patches import Circle, Rectangle, Arc
# Draw a basketball court
def draw_court(ax=None, color='black', lw=2, outer_lines=False):
# Plot onto current axes
if ax is None:
ax = plt.gca()
hoop = Circle((0, 0), radius=7.5, linewidth=lw, color=color, fill=False)
backboard = Rectangle((-30, -7.5), 60, -1, linewidth=lw, color=color)
key_outer_box = Rectangle((-80, -47.5), 160, 190, linewidth=lw, color=color, fill=False)
key_inner_box = Rectangle((-60, -47.5), 120, 190, linewidth=lw, color=color, fill=False)
free_throw_top_arc = Arc((0, 142.5), 120, 120, theta1=0, theta2=180, linewidth=lw, color=color, fill=False)
free_throw_bottom_arc = Arc((0, 142.5), 120, 120, theta1=180, theta2=0, linewidth=lw, color=color, linestyle='dashed')
restricted_area = Arc((0, 0), 80, 80, theta1=0, theta2=180, linewidth=lw, color=color)
right_corner_three_line = Rectangle((-220, -47.5), 0, 140, linewidth=lw, color=color)
left_corner_three_line = Rectangle((220, -47.5), 0, 140, linewidth=lw, color=color)
three_arc = Arc((0, 0), 475, 475, theta1=22, theta2=158, linewidth=lw, color=color)
center_court_outer_arc = Arc((0, 395), 120, 120, theta1=180, theta2=0,
linewidth=lw, color=color)
center_court_inner_arc = Arc((0, 395), 40, 40, theta1=180, theta2=0,
linewidth=lw, color=color)
court_elements = [
hoop,
backboard,
key_outer_box,
key_inner_box,
free_throw_top_arc,
free_throw_bottom_arc,
restricted_area,
right_corner_three_line,
left_corner_three_line,
three_arc,
center_court_outer_arc,
center_court_inner_arc
]
if outer_lines:
# Draw half-court line, baseline, sidelines
boundaries = Rectangle((-250, -47.5), 500, 442.5, linewidth=lw, color=color, fill=False)
court_elements.append(boundaries)
# Draw elements
for element in court_elements:
ax.add_patch(element)
return ax
import urllib.request
# Scrape and store James Harden profile image
pic = urllib.request.urlretrieve("http://stats.nba.com/media/players/230x185/201935.png",
"201935.png")
harden_pic = plt.imread(pic[0])
from matplotlib.offsetbox import OffsetImage
# Create joint plot
warm_color_map = plt.cm.YlOrRd_r
joint_shot_chart = sns.jointplot(shot_df.LOC_X, shot_df.LOC_Y, stat_func=None,
kind='kde', space=0, color=warm_color_map(0.1),
cmap=warm_color_map, n_levels=50)
joint_shot_chart.fig.set_size_inches(12, 11)
# Draw court onto ax_joint of joint plot
ax = joint_shot_chart.ax_joint
draw_court(ax)
ax.set_xlim(250, -250)
ax.set_ylim(395, -47.5)
# Remove labels and ticks
ax.set_xlabel('')
ax.set_ylabel('')
ax.tick_params(labelbottom='off', labelleft='off')
# Add title and text
ax.set_title('James Harden FGA \n2014-2015 Reg. Season', y=1.2, fontsize=18)
ax.text(-250, 420, 'Data Source: stats.nba.com'
'\nAuthor: Tiger Shen', fontsize=12)
# Plot Harden image
img = OffsetImage(harden_pic, zoom=0.6)
img.set_offset((625,621)) # Top right corner
ax.add_artist(img)
plt.show()