-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplots_3year.py
More file actions
226 lines (202 loc) · 9.89 KB
/
plots_3year.py
File metadata and controls
226 lines (202 loc) · 9.89 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import numpy as np
import pandas as pd
import sqlite3
from bokeh.plotting import figure, output_file, output_notebook, show
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.models.widgets import Panel, Tabs
#Create database connection
con = sqlite3.connect('data/nflPPdb.sqlite')
#Create RB and WR dataframes
def make_dfs():
"""
Creates dataframes for plotting using SQL queries.
Returns a dataframe for each positional group (dfRB, dfWR, dfQB).
"""
df1 = pd.read_sql_query('SELECT combine.name, combine.fortyyd, combine.heightinchestotal,\
combine.weight, combine.twentyss, combine.vertical, combine.year\
FROM combine\
WHERE combine.year < 2009 AND combine.pickround != 0', con)
df1['speedscore'] = (df1['weight']*200)/(df1['fortyyd']**4)
df2 = pd.read_sql_query('SELECT combine.name, combine.year, players.position\
FROM combine, players\
WHERE combine.name = players.name AND combine.year = players.draft_year', con)
df3 = pd.merge(df1, df2, on=['name', 'year'], how='inner', suffixes=('df1','df2'))
df3 = df3.drop_duplicates(subset='name', keep=False)
df4 = pd.read_sql_query('SELECT DISTINCT combine.name, rr.rushing_yards, rr.receiving_yards\
FROM combine, rr\
WHERE combine.name = rr.name AND combine.year < 2009', con)
df4 = df4.fillna(0)
df4['totYds'] = (df4['receiving_yards'] + df4['rushing_yards']).astype(int)
df5 = pd.merge(df3,df4, on='name', how='inner', suffixes=('df3','df4'))
dfRB = df5[df5.position == 'RB']
dfRB = dfRB.groupby('name').head(3).reset_index(drop=True)
dfRB = dfRB.groupby('name').mean()
dfRB = dfRB[dfRB.fortyyd < 5] #remove outliers
dfWR = df5[df5.position == 'WR']
dfWR = dfWR.groupby('name').head(3).reset_index(drop=True)
dfWR = dfWR.groupby('name').mean()
dfWR = dfWR[dfWR.fortyyd < 5] #remove outliers
#Create QB data frame
dfQB = pd.read_sql_query('SELECT DISTINCT combine.name, combine.fortyyd, combine.heightinchestotal,\
combine.weight, combine.twentyss, combine.vertical, passing.passing_yards\
FROM combine, passing\
WHERE combine.name = passing.name AND combine.year < 2009', con)
dfQB['count'] = 1 #use to get 40 yard time back after aggregating
#dfQB = pd.pivot_table(dfQB,index=['name'],aggfunc=np.sum).reset_index()
dfQB = dfQB.groupby('name').head(3).reset_index(drop=True)
dfQB = dfQB.groupby('name').sum()
dfQB['fortyyd'] = dfQB['fortyyd']/dfQB['count']
dfQB['heightinchestotal'] = dfQB['heightinchestotal']/dfQB['count']
dfQB['twentyss'] = dfQB['twentyss']/dfQB['count']
dfQB['vertical'] = dfQB['vertical']/dfQB['count']
dfQB['weight'] = dfQB['weight']/dfQB['count']
dfQB['speedscore'] = (dfQB['weight']*200)/(dfQB['fortyyd']**4)
dfQB = dfQB.drop('count', 1)
dfQB = dfQB[dfQB.passing_yards > 175] #remove outliers
return (dfRB, dfWR, dfQB)
def rb(xvar, hover_lab, title, x_lab, dfRB, dfWR, dfQB):
"""
Defines plot within RB tab.
Parameters:
xvar x-axis variable (string)
hover_lab hover label for x-axis variable (string)
title title of plot (string)
x_lab x-axis label (string)
dfRB running back dataframe (dataframe)
dfWR wide receiver dataframe (dataframe)
dfQB quarterback dataframe (dataframe)
"""
source = ColumnDataSource(data=dict(x=dfRB[xvar], y=dfRB['totYds'],
rush=dfRB['rushing_yards'],
rec=dfRB['receiving_yards'], name=dfRB.index,))
hover = HoverTool(tooltips=[('Player', '@name'),(hover_lab, '$x{1.11}'),
('Career Rushing Yards', '@rush'),('Career Receiving Yards', '@rec'),
('Total Yards', '@y'),])
p1 = figure(plot_width=600, plot_height=700, tools='pan,wheel_zoom,box_zoom,reset,resize,save',
title=title, x_axis_label =x_lab, y_axis_label ='Career Rushing and Receiving Yards')
p1.add_tools(hover)
p1.circle('x', 'y', size=7, color='cyan', source=source)
tab1 = Panel(child=p1, title='RB')
return tab1
def wr(xvar, hover_lab, title, x_lab, dfRB, dfWR, dfQB):
"""
Defines plot within WR tab.
Parameters:
xvar x-axis variable (string)
hover_lab hover label for x-axis variable (string)
title title of plot (string)
x_lab x-axis label (string)
dfRB running back dataframe (dataframe)
dfWR wide receiver dataframe (dataframe)
dfQB quarterback dataframe (dataframe)
"""
source = ColumnDataSource(data=dict(x=dfWR[xvar], y=dfWR['totYds'], rush=dfWR['rushing_yards'],
rec=dfWR['receiving_yards'], name=dfWR.index,))
hover = HoverTool(tooltips=[('Player', '@name'),(hover_lab, '$x{1.11}'),
('Career Rushing Yards', '@rush'),('Career Receiving Yards', '@rec'),
('Total Yards', '@y'),])
p2 = figure(plot_width=600, plot_height=700, tools="pan,wheel_zoom,box_zoom,reset,resize,save",
title=title,
x_axis_label =x_lab, y_axis_label ='Career Rushing and Receiving Yards')
p2.add_tools(hover)
p2.circle('x', 'y', size=7, color='cyan', source=source)
tab2 = Panel(child=p2, title='WR')
return tab2
def qb(xvar, hover_lab, title, x_lab, dfRB, dfWR, dfQB):
"""
Defines plot within QB tab.
Parameters:
xvar x-axis variable (string)
hover_lab hover label for x-axis variable (string)
title title of plot (string)
x_lab x-axis label (string)
dfRB running back dataframe (dataframe)
dfWR wide receiver dataframe (dataframe)
dfQB quarterback dataframe (dataframe)
"""
source = ColumnDataSource(data=dict(x=dfQB[xvar], y=dfQB['passing_yards'], name=dfQB.index,))
hover = HoverTool(
tooltips=[('Player', '@name'),(hover_lab, '$x{1.11}'),('Career Passing Yds', '@y'),])
p3 = figure(plot_width=600, plot_height=700, tools="pan,wheel_zoom,box_zoom,reset,resize,save",
title=title,
x_axis_label =x_lab, y_axis_label ='Career Passing Yards')
p3.add_tools(hover)
p3.circle('x', 'y', size=7, color='cyan', source=source)
tab3 = Panel(child=p3, title='QB')
return tab3
def plot_40dash():
"""
Plot of 40 yard times by yardage with tabs for each position.
Calls:
make_dfs(), rb(), wr(), qb()
"""
dfRB, dfWR, dfQB = make_dfs()
output_notebook()
# output_file('40yd.html')
tab1 = rb('fortyyd', '40 Yard Dash', 'RB: Total Yards by 40 Yard Dash', '40 Yard Dash', dfRB, dfWR, dfQB)
tab2 = wr('fortyyd', '40 Yard Dash', 'WR: Total Yards by 40 Yard Dash', '40 Yard Dash', dfRB, dfWR, dfQB)
tab3 = qb('fortyyd', '40 Yard Dash', 'QB: Total Yards by 40 Yard Dash', '40 Yard Dash', dfRB, dfWR, dfQB)
tabs = Tabs(tabs=[ tab1, tab2, tab3 ])
show(tabs)
def plot_twentyss():
"""
Plot of 20 yard shuttle times by yardage with tabs for each position.
Calls:
make_dfs(), rb(), wr(), qb()
"""
dfRB, dfWR, dfQB = make_dfs()
dfRB = dfRB[dfRB.twentyss > 0]
dfWR = dfWR[dfWR.twentyss > 0]
dfQB = dfQB[dfQB.twentyss > 2] #Remove Alex Smith (incorrect time)
output_notebook()
# output_file('40yd.html')
tab1 = rb('twentyss', '20 Yd Shuttle', 'RB: Total Yards by Short Shuttle', '20 Yard Short Shuttle', dfRB, dfWR, dfQB)
tab2 = wr('twentyss', '20 Yd Shuttle', 'WR: Total Yards by Short Shuttle', '20 Yard Short Shuttle', dfRB, dfWR, dfQB)
tab3 = qb('twentyss', '20 Yd Shuttle', 'QB: Total Yards by Short Shuttle', '20 Yard Short Shuttle', dfRB, dfWR, dfQB)
tabs = Tabs(tabs=[ tab1, tab2, tab3 ])
show(tabs)
def plot_vertical():
"""
Plot of vertical jump by yardage with tabs for each position.
Calls:
make_dfs(), rb(), wr(), qb()
"""
dfRB, dfWR, dfQB = make_dfs()
dfRB = dfRB[dfRB.vertical > 0]
dfWR = dfWR[dfWR.vertical > 0]
dfQB = dfQB[dfQB.vertical > 20] #Remove Alex Smith (incorrect measurement)
output_notebook()
# output_file('40yd.html')
tab1 = rb('vertical', 'Vertical Jump (in)', 'RB: Total Yards by Vertical Jump', 'Vertical Jump (in)', dfRB, dfWR, dfQB)
tab2 = wr('vertical', 'Vertical Jump (in)', 'WR: Total Yards by Vertical Jump', 'Vertical Jump (in)', dfRB, dfWR, dfQB)
tab3 = qb('vertical', 'Vertical Jump (in)', 'QB: Total Yards by Vertical Jump', 'Vertical Jump (in)', dfRB, dfWR, dfQB)
tabs = Tabs(tabs=[ tab1, tab2, tab3 ])
show(tabs)
def plot_height():
"""
Plot of height by yardage with tabs for each position.
Calls:
make_dfs(), rb(), wr(), qb()
"""
dfRB, dfWR, dfQB = make_dfs()
output_notebook()
# output_file('40yd.html')
tab1 = rb('heightinchestotal', 'Height (in)', 'RB: Total Yards by Height', 'Height (in)', dfRB, dfWR, dfQB)
tab3 = qb('heightinchestotal', 'Height (in)', 'QB: Total Yards by Height', 'Height (in)', dfRB, dfWR, dfQB)
tab2 = wr('heightinchestotal', 'Height (in)', 'WR: Total Yards by Height', 'Height (in)', dfRB, dfWR, dfQB)
tabs = Tabs(tabs=[ tab1, tab2, tab3 ])
show(tabs)
def plot_speedscore():
"""
Plot of speedscore by yardage with tabs for each position.
Calls:
make_dfs(), rb(), wr(), qb()
"""
dfRB, dfWR, dfQB = make_dfs()
output_notebook()
# output_file('40yd.html')
tab1 = rb('speedscore', 'Speed Score', 'RB: Total Yards by Speed Score', 'Speed Score', dfRB, dfWR, dfQB)
tab3 = qb('speedscore', 'Speed Score', 'QB: Total Yards by Speed Score', 'Speed Score', dfRB, dfWR, dfQB)
tab2 = wr('speedscore', 'Speed Score', 'WR: Total Yards by Speed Score', 'Speed Score', dfRB, dfWR, dfQB)
tabs = Tabs(tabs=[ tab1, tab2, tab3 ])
show(tabs)