nesticot commited on
Commit
aa03a67
·
1 Parent(s): 6a56958

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +369 -881
app.py CHANGED
@@ -1,992 +1,480 @@
1
- print('Running')
2
- import time
3
- import requests
4
  import pandas as pd
5
  import seaborn as sns
6
  import matplotlib.pyplot as plt
7
  from matplotlib.pyplot import figure
8
  from matplotlib.offsetbox import OffsetImage, AnnotationBbox
9
- import matplotlib.lines as mlines
10
- import matplotlib.transforms as mtransforms
11
- import numpy as np
12
- import time
13
- from urllib.error import HTTPError
14
- #import plotly.express as px
15
- #!pip install chart_studio
16
- #import chart_studio.tools as tls
17
- from bs4 import BeautifulSoup
18
- import matplotlib.pyplot as plt
19
- import numpy as np
20
- import matplotlib.font_manager as font_manager
21
  from datetime import datetime
22
  import pytz
 
23
  from matplotlib.ticker import MaxNLocator
24
- from matplotlib.patches import Ellipse
25
- import matplotlib.transforms as transforms
26
- from matplotlib.gridspec import GridSpec
27
- datetime.now(pytz.timezone('US/Pacific')).strftime('%B %d, %Y')
28
- # Configure Notebook
29
- #%matplotlib inline
30
- plt.style.use('fivethirtyeight')
31
- sns.set_context("notebook")
32
- import warnings
33
- warnings.filterwarnings('ignore')
34
-
35
- # import dataframe_image as dfi
36
- # from google.colab import drive
37
- def percentile(n):
38
- def percentile_(x):
39
- return np.percentile(x, n)
40
- percentile_.__name__ = 'percentile_%s' % n
41
- return percentile_
42
-
43
- # import os
44
- # import praw
45
- # import matplotlib.pyplot as plt
46
- # import matplotlib.colors
47
- # import matplotlib.colors as mcolors
48
- # cmap_sum = matplotlib.colors.LinearSegmentedColormap.from_list("", ["#4285f4","#FFFFFF","#F0E442"])
49
- #import pybaseball
50
- import math
51
- # import matplotlib.ticker as mtick
52
- # import matplotlib.ticker as ticker
53
-
54
- # colour_palette = ['#FFB000','#648FFF','#785EF0',
55
- # '#DC267F','#FE6100','#3D1EB2','#894D80','#16AA02','#B5592B','#A3C1ED']
56
- # import matplotlib.colors as mcolors
57
- # from matplotlib.ticker import FuncFormatter
58
- # from matplotlib.font_manager import FontProperties
59
- import matplotlib.ticker as mtick
60
  import numpy as np
61
- # import matplotlib.pyplot as plt
62
- import matplotlib.colors
63
-
64
- import matplotlib.pyplot as plt
65
- from matplotlib.colors import Normalize
66
- from matplotlib import cm
67
- from datetime import date
68
-
69
- def show_values(axs, orient="v", space=-1,stat = [],rank_n=[]):
70
- def _single(ax):
71
- if orient == "v":
72
- i = 0
73
- for p in ax.patches:
74
-
75
- _x = p.get_x() + p.get_width() / 2
76
- _y = p.get_y() + p.get_height() + (p.get_height()*0.01)
77
- value = '{:.0f}'.format(p.get_height())
78
- if isinstance(stat[i], float):
79
- value_stat = '{:.2f}'.format(stat[i])
80
- else:
81
- value_stat = stat[i]
82
- rank = rank_n[i]
83
- ax.text(_x, -3.8, value_stat, ha="center",fontstyle='italic')
84
- ax.text(_x, -4.2, rank, ha="center",fontstyle='italic')
85
- i = i+1
86
- elif orient == "h":
87
- for p in ax.patches:
88
- _x = p.get_x() + p.get_width() + float(space)
89
- _y = p.get_y() + p.get_height() - (p.get_height()*0.5)
90
- value = '{:.0f}'.format(p.get_width())
91
- ax.text(_x, _y, value, ha="left")
92
-
93
- if isinstance(axs, np.ndarray):
94
- for idx, ax in np.ndenumerate(axs):
95
- _single(ax)
96
- else:
97
- _single(axs)
98
-
99
-
100
-
101
- data_r = requests.get("https://pub-api-ro.fantasysports.yahoo.com/fantasy/v2/league/427.l.public;out=settings/players;position=ALL;start=0;count=1000;sort=rank_season;search=;out=percent_owned;out=auction_values,ranks;ranks=season;ranks_by_position=season;out=expert_ranks;expert_ranks.rank_type=projected_season_remaining/draft_analysis;cut_types=diamond;slices=last7days?format=json_f").json()
102
-
103
- total_list = []
104
-
105
- for x in data_r['fantasy_content']['league']['players']:
106
- single_list = []
107
-
108
- single_list.append(int(x['player']['player_id']))
109
- single_list.append(int(x['player']['player_ranks'][0]['player_rank']['rank_value']))
110
- single_list.append(x['player']['name']['full'])
111
- single_list.append(x['player']['name']['first'])
112
- single_list.append(x['player']['name']['last'])
113
- single_list.append(x['player']['draft_analysis']['average_pick'])
114
- single_list.append(x['player']['average_auction_cost'])
115
- single_list.append(x['player']['display_position'])
116
- single_list.append(x['player']['editorial_team_abbr'])
117
- if 'value' in x['player']['percent_owned']:
118
- single_list.append(x['player']['percent_owned']['value']/100)
119
- else:
120
- single_list.append(0)
121
- total_list.append(single_list)
122
-
123
- df_2023 = pd.DataFrame(data=total_list,columns=['player_id','rank_value','full','first','last','average_pick', 'average_cost','display_position','editorial_team_abbr','percent_owned'])
124
-
125
- df_2023['pos_new'] = ['D' if "D" in x else 'F' for x in df_2023['display_position']]
126
-
127
- player_games = pd.read_csv('Drive/player_games_cards.csv',index_col=[0]).sort_values(by='date')
128
- summary_2023 = pd.read_csv('Drive/summary_2024.csv',index_col=[0])
129
- summary_2022 = pd.read_csv('Drive/2022-23/summary_2023.csv',index_col=[0])
130
- team_games = pd.read_csv('Drive/team_games.csv',index_col=[0])
131
- nhl_logos = pd.read_csv("NHL Logos - NHL Logos.csv")
132
- team_games = team_games.merge(right=nhl_logos[['Team Name','Team']],left_on=['team'],right_on=['Team Name'],how='left')
133
-
134
- yahoo_to_nhl_df = pd.read_csv('yahoo_to_nhl.csv')
135
-
136
- yahoo_to_nhl_df = yahoo_to_nhl_df.merge(df_2023)
137
- summary_2023 = summary_2023.merge(yahoo_to_nhl_df,left_on='player_id',right_on='nhl_id',suffixes=['','_yahoo'],how='left')
138
-
139
- summary_2023 = summary_2023.merge(right=player_games.drop_duplicates(subset=['player_id'],keep='last')[['player_id','Position','Team']],left_on=['player_id'],right_on=['player_id'],how='left')
140
-
141
- summary_2023.loc[summary_2023.display_position.isna(),'display_position'] = summary_2023.loc[summary_2023.display_position.isna(),'Position']
142
- summary_2023.display_position = summary_2023.display_position.replace({'L':'LW','R':'RW'})
143
- summary_2023.percent_owned = summary_2023.percent_owned.fillna(0)
144
-
145
- player_games['game'] = player_games.groupby(['player_id']).cumcount() + 1
146
-
147
- skater_dict = summary_2023.set_index('player_id').sort_values(by='Player')
148
- skater_dict['skater_team'] = skater_dict.Player + ' - ' + skater_dict.Team
149
- skater_dict = skater_dict['skater_team'].to_dict()
150
-
151
- from shiny import ui, render, App
152
- import matplotlib.image as mpimg
153
- app_ui = ui.page_fluid(
154
- #ui.panel_title("Simulate a normal distribution"),
155
-
156
- ui.layout_sidebar(
157
 
158
- ui.panel_sidebar(
159
- #ui.input_date_range("date_range_id", "Date range input",start = statcast_df.game_date.min(), end = statcast_df.game_date.max()),
160
- ui.input_select("id", "Select Skater",skater_dict,width=1,selected=8480069,selectize=True),
161
- #ui.input_select("id", "Select Skater",skater_dict,width=1),
162
- ui.input_numeric("last_game_id", "Select Last 'n' Games",value=1,width=1),
163
- #ui.input_select("ignore_id", "Remove Columns",['Position','Roster%'],multiple=True,selectize=True),
164
- width=2),
165
- ui.panel_main(
166
- ui.output_plot("plot",height = "1350px",width="2400px")
167
- )
168
- ),
169
- )
170
 
 
 
171
 
 
 
 
172
 
 
173
 
174
- from urllib.request import Request, urlopen
175
- from shiny import App, reactive, ui
176
- from shiny.ui import h2, tags
177
- # importing OpenCV(cv2) module
178
 
 
 
 
 
 
 
179
 
 
 
 
 
180
 
181
 
182
- #print(app_ui)
183
- def server(input, output, session):
184
 
185
- @output
186
- @render.plot(alt="A histogram")
187
- def plot():
188
- # from matplotlib import rc, font_manager
189
- # rc('text', usetex=True)
190
- # rc('font',**{'family':'sans-serif','sans-serif':['Century Gothic']})
191
- #fig.set_facecolor('white')
192
- sns.set_theme(style="whitegrid", palette="pastel")
193
- cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["#4285F4","white","#FBBC04"])
194
 
195
- try:
196
- if sum(summary_2023.player_id.isin([int(input.id())]))<1:
197
- fig, ax = plt.subplots(figsize=(10,16))
198
- fig.set_facecolor('white')
199
- fig.text(s=f'Select Player From List',x=0.5,y=0.5,fontsize=28,ha='center')
200
- return
201
- except ValueError:
202
- fig, ax = plt.subplots(figsize=(10,16))
203
- fig.set_facecolor('white')
204
- fig.text(s=f'Select Player From List',x=0.5,y=0.5,fontsize=28,ha='center')
205
- return
206
 
207
- player_id = int(input.id())
208
 
209
- last_games = input.last_game_id()
210
 
 
 
 
 
 
 
 
 
211
 
 
 
 
 
212
 
213
- player_games_one = player_games[player_games['player_id']==player_id].sort_values(by='game',ascending=False)
214
-
215
- print(player_games_one)
216
- summary_2023_one = summary_2023[summary_2023['player_id']==player_id]
217
- summary_2022_one = summary_2022[summary_2022['player_id']==player_id]
218
-
219
- last_games = min(last_games,max(summary_2023_one.GP.max()-1,1))
220
-
221
- name = player_games_one.Player.values[0]
222
-
223
- df_last_games = pd.DataFrame(data={'Games':[last_games]})
224
- df_last_games['TOI'] = player_games_one['TOI'][:last_games].sum()
225
- df_last_games['TOI/G'] = player_games_one['TOI'][:last_games].sum()/df_last_games.Games
226
- df_last_games['PP TOI/G'] = player_games_one['TOI_pp'][:last_games].sum()/df_last_games.Games
227
- df_last_games['Points/60'] = player_games_one['Total Points'][:last_games].sum()/df_last_games.TOI*60
228
- df_last_games['Shots/60'] = player_games_one['Shots'][:last_games].sum()/df_last_games.TOI*60
229
- df_last_games['Hits/60'] = player_games_one['Hits'][:last_games].sum()/df_last_games.TOI*60
230
- df_last_games['Blocks/60'] = player_games_one['Shots Blocked'][:last_games].sum()/df_last_games.TOI*60
231
- df_last_games['Goals/60'] = player_games_one['Goals'][:last_games].sum()/df_last_games.TOI*60
232
- df_last_games['ixG/60'] = player_games_one['ixG'][:last_games].sum()/df_last_games.TOI*60
233
- df_last_games['G-ixG/60'] = df_last_games['Goals/60'] - df_last_games['ixG/60']
234
- df_last_games['iCF/60'] = player_games_one['iCF'][:last_games].sum()/df_last_games.TOI*60
235
- df_last_games['iSCF/60'] = player_games_one['iSCF'][:last_games].sum()/df_last_games.TOI*60
236
- df_last_games['iHDCF/60'] = player_games_one['iHDCF'][:last_games].sum()/df_last_games.TOI*60
237
- df_last_games['GF/60'] = player_games_one['GF'][:last_games].sum()/df_last_games.TOI*60
238
- df_last_games['xGF/60'] = player_games_one['xGF'][:last_games].sum()/df_last_games.TOI*60
239
- df_last_games['IPP'] = player_games_one['Total Points'][:last_games].sum()/player_games_one['GF'][:last_games].sum()
240
- df_last_games['S%'] = player_games_one['Goals'][:last_games].sum()/player_games_one['Shots'][:last_games].sum()
241
- df_last_games['xS%'] = player_games_one['ixG'][:last_games].sum()/player_games_one['Shots'][:last_games].sum()
242
- df_last_games['1st Assist%'] = player_games_one['First Assists'][:last_games].sum()/player_games_one['Total Assists'][:last_games].sum()
243
- df_last_games['Off. Zone Start%'] = player_games_one['Off.\xa0Zone Starts'][:last_games].sum()/(player_games_one['Off.\xa0Zone Starts'][:last_games].sum()+player_games_one['Def.\xa0Zone Starts'][:last_games].sum())
244
- df_last_games['oiSH%'] = player_games_one['GF'][:last_games].sum()/player_games_one['SF'][:last_games].sum()
245
- df_season = pd.DataFrame(data={'Games':[len(player_games_one)]})
246
- df_season['TOI'] = player_games_one['TOI'][:len(player_games_one)].sum()
247
- df_season['TOI/G'] = player_games_one['TOI'][:len(player_games_one)].sum()/df_season.Games
248
- df_season['PP TOI/G'] = player_games_one['TOI_pp'][:len(player_games_one)].sum()/df_season.Games
249
- df_season['Points/60'] = player_games_one['Total Points'][:len(player_games_one)].sum()/df_season.TOI*60
250
- df_season['Shots/60'] = player_games_one['Shots'][:len(player_games_one)].sum()/df_season.TOI*60
251
- df_season['Hits/60'] = player_games_one['Hits'][:len(player_games_one)].sum()/df_season.TOI*60
252
- df_season['Blocks/60'] = player_games_one['Shots Blocked'][:len(player_games_one)].sum()/df_season.TOI*60
253
- df_season['Goals/60'] = player_games_one['Goals'][:len(player_games_one)].sum()/df_season.TOI*60
254
- df_season['ixG/60'] = player_games_one['ixG'][:len(player_games_one)].sum()/df_season.TOI*60
255
- df_season['G-ixG/60'] = df_season['Goals/60'] - df_season['ixG/60']
256
- df_season['iCF/60'] = player_games_one['iCF'][:len(player_games_one)].sum()/df_season.TOI*60
257
- df_season['iSCF/60'] = player_games_one['iSCF'][:len(player_games_one)].sum()/df_season.TOI*60
258
- df_season['iHDCF/60'] = player_games_one['iHDCF'][:len(player_games_one)].sum()/df_season.TOI*60
259
- df_season['GF/60'] = player_games_one['GF'][:len(player_games_one)].sum()/df_season.TOI*60
260
- df_season['xGF/60'] = player_games_one['xGF'][:len(player_games_one)].sum()/df_season.TOI*60
261
- df_season['IPP'] = player_games_one['Total Points'][:len(player_games_one)].sum()/player_games_one['GF'][:len(player_games_one)].sum()
262
- df_season['S%'] = player_games_one['Goals'][:len(player_games_one)].sum()/player_games_one['Shots'][:len(player_games_one)].sum()
263
- df_season['xS%'] = player_games_one['ixG'][:len(player_games_one)].sum()/player_games_one['Shots'][:len(player_games_one)].sum()
264
- df_season['1st Assist%'] = player_games_one['First Assists'][:len(player_games_one)].sum()/player_games_one['Total Assists'][:len(player_games_one)].sum()
265
- df_season['Off. Zone Start%'] = player_games_one['Off.\xa0Zone Starts'][:len(player_games_one)].sum()/(player_games_one['Off.\xa0Zone Starts'][:len(player_games_one)].sum()+player_games_one['Def.\xa0Zone Starts'][:len(player_games_one)].sum())
266
- df_season['oiSH%'] = player_games_one['GF'][:len(player_games_one)].sum()/player_games_one['SF'][:len(player_games_one)].sum()
267
- df_last_games.rename(index={0:'Last '+str(df_last_games.Games[0])+' GP'},inplace=True)
268
- df_season.rename(index={0:'2023-24 ('+str(df_season.Games[0])+'GP)'},inplace=True)
269
-
270
- # cols_to_use = summary_all.columns.difference(summary_all_on_ice.columns)
271
-
272
-
273
- # df_career_total = summary_all.merge(summary_all_on_ice, left_index=True, right_index=True,
274
- # how='outer', suffixes=('', '_y'))
275
-
276
- # df_career_total.drop(df_career_total.filter(regex='_y$').columns, axis=1, inplace=True)
277
-
278
- # df_career_total = df_career_total.merge(summary_pp, left_index=True, right_index=True,
279
- # how='outer', suffixes=('', '_y'))
280
-
281
- # df_career_total.drop(df_career_total.filter(regex='_y$').columns, axis=1, inplace=True)
282
-
283
- # df_career_total = df_career_total.merge(summary_pp_on_ice, left_index=True, right_index=True,
284
- # how='outer', suffixes=('', '_y'))
285
-
286
- # df_career_total.drop(df_career_total.filter(regex='_y$').columns, axis=1, inplace=True)
287
- df_career_total = summary_2022_one.sort_index(ascending=False)
288
- df_career_total.columns = [c.replace(u"\xa0", u" ") for c in list(df_career_total.columns)]
289
- df_career = pd.DataFrame(data={'Games':[df_career_total.GP.sum()]})
290
- df_career['TOI'] = df_career_total['TOI'].sum()
291
- df_career['TOI/G'] = df_career_total['TOI'].sum()/df_career.Games
292
- df_career['PP TOI/G'] = df_career_total['TOI_pp'].sum()/df_career.Games
293
- df_career['Points/60'] = df_career_total['Total_Points'].sum()/df_career_total.TOI.sum()*60
294
- df_career['Shots/60'] = df_career_total['Shots'].sum()/df_career_total.TOI.sum()*60
295
- df_career['Hits/60'] = df_career_total['Hits'].sum()/df_career_total.TOI.sum()*60
296
- df_career['Blocks/60'] = df_career_total['Shots_Blocked'].sum()/df_career_total.TOI.sum()*60
297
- df_career['Goals/60'] = df_career_total['Goals'].sum()/df_career_total.TOI.sum()*60
298
- df_career['ixG/60'] = df_career_total['ixG'].sum()/df_career_total.TOI.sum()*60
299
- df_career['G-ixG/60'] = df_career['Goals/60'] - df_career['ixG/60']
300
- df_career['iCF/60'] = df_career_total['iCF'].sum()/df_career_total.TOI.sum()*60
301
- df_career['iSCF/60'] = df_career_total['iSCF'].sum()/df_career_total.TOI.sum()*60
302
- df_career['iHDCF/60'] = df_career_total['iHDCF'].sum()/df_career_total.TOI.sum()*60
303
- df_career['GF/60'] = df_career_total['GF'].sum()/df_career_total.TOI.sum()*60
304
- df_career['xGF/60'] = df_career_total['xGF'].sum()/df_career_total.TOI.sum()*60
305
- df_career['IPP'] = df_career_total['Total_Points'].sum()/df_career_total['GF'].sum()
306
- df_career['S%'] = df_career_total['Goals'].sum()/df_career_total['Shots'].sum()
307
- df_career['xS%'] = df_career_total['ixG'].sum()/df_career_total['Shots'].sum()
308
- df_career['1st Assist%'] = df_career_total['First_Assists'].sum()/df_career_total['Assists'].sum()
309
- df_career['Off. Zone Start%'] = df_career_total['OZ Start%'].sum()
310
- df_career['oiSH%'] = df_career_total['GF'].sum()/df_career_total['SF'].sum()
311
- df_career.rename(index={0:'2022-23 ('+str(df_career.Games[0])+'GP)'},inplace=True)
312
- df_combined = df_last_games.append([df_season,df_career])
313
- df_combined_t = round(df_combined,3).transpose()
314
- df_combined.style.format(formatter={"IPP": "{:.1%}","S%": "{:.1%}","1st Assist%": "{:.1%}","Off. Zone Start%": "{:.1%}","oiSH%": "{:.1%}"}).set_precision(2)
315
- rows = [idx for idx in df_combined_t.index if '/' not in idx]
316
-
317
-
318
- df_combined_t = df_combined_t.astype(float).fillna(0)
319
- # df_combined_t_style = df_combined_t.style.set_table_styles([{
320
- # 'selector': 'caption',
321
- # 'props': [
322
- # ('color', ''),
323
- # ('fontname', 'Century Gothic'),
324
- # ('font-size', '12px'),
325
- # ('font-style', 'italic'),
326
- # ('font-weight', ''),
327
- # ('text-align', 'centre'),
328
- # ]
329
-
330
- # },{'selector' :'th', 'props':[('text-align', 'center'),('Height','8px')]},{'selector' :'td', 'props':[('text-align', 'center'),('font-size', '10px')]}],overwrite=False).set_properties(
331
- # **{'background-color':'White','index':'White','min-width':'60px'},overwrite=False).set_table_styles(
332
- # [{'selector': 'th:first-child', 'props': [('background-color', 'white')]}],overwrite=False).set_table_styles(
333
- # [{'selector': 'tr:first-child', 'props': [('background-color', 'white')]}],overwrite=False).set_table_styles(
334
- # [{'selector': 'tr', 'props': [('line-height', '18px')]}],overwrite=False).set_properties(
335
- # **{'Height': '8px'},**{'text-align': 'center'},overwrite=False).apply(
336
-
337
- # lambda x: ["background: #FEEFC1" if (i < 1 and v > x.iloc[1]*1.05) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
338
- # lambda x: ["background: #FEEFC1" if (i == 1 and v > x.iloc[2]*1.05) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
339
- # lambda x: ["background: #D0E1FD" if (i < 1 and v < x.iloc[1]*0.95) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
340
- # lambda x: ["background: #D0E1FD" if (i == 1 and v < x.iloc[2]*0.95) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
341
- # lambda x: ["background: #FDDE82" if (i < 1 and v > x.iloc[1]*1.1) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
342
- # lambda x: ["background: #FDDE82" if (i == 1 and v > x.iloc[2]*1.1) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
343
- # lambda x: ["background: #A1C2FA" if (i < 1 and v < x.iloc[1]*0.9) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
344
- # lambda x: ["background: #A1C2FA" if (i == 1 and v < x.iloc[2]*0.9) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
345
- # lambda x: ["background: #FCCD43" if (i < 1 and v > x.iloc[1]*1.15) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
346
- # lambda x: ["background: #FCCD43" if (i == 1 and v > x.iloc[2]*1.15) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
347
- # lambda x: ["background: #72A4F7" if (i < 1 and v < x.iloc[1]*0.85) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
348
- # lambda x: ["background: #72A4F7" if (i == 1 and v < x.iloc[2]*0.85) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
349
- # lambda x: ["background: #FBBC04" if (i < 1 and v > x.iloc[1]*1.2) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
350
- # lambda x: ["background: #FBBC04" if (i == 1 and v > x.iloc[2]*1.2) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
351
- # lambda x: ["color: #ffffff" if (i < 1 and v < x.iloc[1]*0.8) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
352
- # lambda x: ["color: #ffffff" if (i == 1 and v < x.iloc[2]*0.8) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
353
- # lambda x: ["background: #4285F4" if (i == 0 and v < x.iloc[1]*0.8) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
354
- # lambda x: ["background: #4285F4" if (i == 1 and v < x.iloc[2]*0.8) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
355
- # lambda x: ["background: #ffffff" if (i == 0 and v == x.iloc[1]) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
356
- # lambda x: ["background: #ffffff" if (i == 1 and v == x.iloc[2]) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
357
- # lambda x: ["color: #000000" if (i < 1 and v == x.iloc[1]) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).apply(
358
- # lambda x: ["color: #000000" if (i == 1 and v == x.iloc[2]) else "" for i,v in enumerate(x)], axis = 1,subset = (list(df_combined_t.index[2:]), list(df_combined_t.columns))).format(
359
- # "{:.1%}",subset=(rows[2:],df_combined_t.columns)).format(
360
- # '{:.0f}',subset=(rows[0],df_combined_t.columns)).set_precision(2).set_properties(
361
- # **{'index':'White','min-width':'60px'},overwrite=False)
362
-
363
-
364
-
365
- # #.set_table_styles([index_names, headers])
366
-
367
- # df_combined_t_style = df_combined_t_style.format(
368
- # {df_combined_t_style.columns[0]: '{:,.1%}'.format,
369
- # df_combined_t_style.columns[1]: '{:,.1%}'.format,
370
- # df_combined_t_style.columns[2]: '{:,.1%}'.format},subset=(rows[2:],df_combined_t.columns)
371
- # )
372
- # df_combined_t_style = df_combined_t_style.format(
373
- # '{:.0f}',subset=(rows[0],df_combined_t.columns))
374
 
 
 
 
 
375
 
376
- # df_combined_t_style
377
 
 
 
 
 
 
 
 
 
 
 
378
 
379
-
380
 
381
- # dfi.export(df_combined_t_style, 'players/'+name+'_'+str(last_games)+'.png',fontsize=9,dpi=600,table_conversion='chrome')
382
-
383
-
384
- percent_owned = summary_2023_one['percent_owned'].reset_index(drop=True)[0]
385
- yahoo_position = summary_2023_one['display_position'].reset_index(drop=True)[0]
386
-
387
-
388
-
389
- # image = "https://cms.nhl.bamgrid.com/images/headshots/current/168x168/"+str(player_id)+".png"
390
- # logo = nhl_logos[nhl_logos.Team==list(player_games_one['Team'])[0]].reset_index().URL[0]
391
- # fig, ax = plt.subplots(figsize=(10,16))
392
- # fig.set_facecolor('white')
393
-
394
- # # img = mpimg.imread('players/'+name+'_'+str(last_games)+'.png')
395
- # # ax.imshow(img)
396
- # # ax.axis('off')
397
- # # fig.tight_layout()
398
-
399
- # ax.axis('off')
400
- # im = plt.imread('players/'+name+'_'+str(last_games)+'.png')
401
- # ax = fig.add_axes([0,0,1,0.85], anchor='C', zorder=1)
402
- # ax.imshow(im)
403
- # ax.axis('off')
404
- # fig.tight_layout()
405
-
406
- # fig.text(x=0.5,y=-0.05,s='Note: Last Games compares to 2023-24. 2023-24 compares to 2022-23.',horizontalalignment='center',fontsize=16,fontname='Century Gothic')
407
- # # fig.text(x=0.05,y=-0.1,s='Created By: @TJStats',horizontalalignment='left',fontsize=24,fontname='Century Gothic')
408
- # # fig.text(x=0.95,y=-0.1,s='Data: Natural Stat Trick',horizontalalignment='right',fontsize=24,fontname='Century Gothic')
409
- # fig.text(x=0.5,y=1.13,s='NHL Player Summary',horizontalalignment='center',fontsize=52, fontweight='bold')
410
- # fig.text(x=0.5,y=1.08,s='2023-24 Season',horizontalalignment='center',fontsize=36,fontname='Century Gothic', fontstyle='italic')
411
- # fig.text(x=0.12,y=1.03,s='Player',horizontalalignment='center',fontsize=22,fontname='Century Gothic', fontweight='bold')
412
- # fig.text(x=0.12,y=1.0,s='Team',horizontalalignment='center',fontsize=22,fontname='Century Gothic', fontweight='bold')
413
- # fig.text(x=0.12,y=0.97,s='Position',horizontalalignment='center',fontsize=22,fontname='Century Gothic', fontweight='bold')
414
- # #fig.text(x=0.12,y=0.94,s='Age',horizontalalignment='center',fontsize=22,fontname='Century Gothic', fontweight='bold')
415
- # # fig.text(x=0.12,y=0.91,s='Cap Hit',horizontalalignment='center',fontsize=22,fontname='Century Gothic', fontweight='bold')
416
- # #fig.text(x=0.12,y=0.88,s='Roster%',horizontalalignment='center',fontsize=22,fontname='Century Gothic', fontweight='bold')
417
-
418
- # fig.text(x=0.25,y=1.03,s=name,horizontalalignment='left',fontsize=22,fontname='Century Gothic')
419
- # fig.text(x=0.25,y=1.0,s=list(player_games_one['Team'])[0],horizontalalignment='left',fontsize=22,fontname='Century Gothic')
420
- # fig.text(x=0.25,y=0.97,s=yahoo_position,horizontalalignment='left',fontsize=22,fontname='Century Gothic')
421
- # #fig.text(x=0.25,y=0.94,s=str(int(summary_2023_one.reset_index().AGE[0])),horizontalalignment='left',fontsize=22,fontname='Century Gothic')
422
- # # fig.text(x=0.25,y=0.91,s=summary_2023_one.loc[summary_2023_one['player_id']==player_id].reset_index()['sheets'][0],horizontalalignment='left',fontsize=22,fontname='Century Gothic')
423
- # #fig.text(x=0.25,y=0.88,s=str(int(percent_owned*100))+'%',horizontalalignment='left',fontsize=22,fontname='Century Gothic')
424
-
425
-
426
- # im = plt.imread(image)
427
- # newax = fig.add_axes([0.5,0.8,0.22,0.22], anchor='NW', zorder=1)
428
- # newax.imshow(im)
429
-
430
- # imr = plt.imread(logo)
431
- # newerax = fig.add_axes([0.75,0.8,0.22,0.22], anchor='NW', zorder=1)
432
- # newerax.imshow(imr)
433
-
434
- # newax.axis('off')
435
- # newerax.axis('off')
436
-
437
- # plt.savefig('players/'+name+"_"+str(last_games)+' int.png',bbox_inches="tight")
438
- # plt.close()
439
-
440
- #test.loc[name == test.Player].reset_index().TOI[0]/1.5
441
- min_time = min(math.floor(summary_2023.loc[player_id == summary_2023.player_id].reset_index().TOI[0]/100)*100,summary_2023.GP.max()*10)
442
- # min_time = min(math.floor(test.loc[name == test.Player].reset_index().TOI[0]/100)*100,test.GP.max()*10)
443
- test_filter = summary_2023[(summary_2023.TOI >= min_time)&(summary_2023.pos == summary_2023.loc[name == summary_2023.Player].reset_index().pos[0])]
444
- test_filter['Goals/GP'] = test_filter['Goals']/test_filter['GP']
445
- test_filter['Total Assists/GP'] = test_filter['Assists']/test_filter['GP']
446
- test_filter['Total Points/GP'] = test_filter['Total_Points']/test_filter['GP']
447
- test_filter['PP Points/GP'] = test_filter['Total_Points_pp']/test_filter['GP']
448
- test_filter['Shots/GP'] = test_filter['Shots']/test_filter['GP']
449
- test_filter['Hits/GP'] = test_filter['Hits']/test_filter['GP']
450
- test_filter['Shots Blocked/GP'] = test_filter['Shots_Blocked']/test_filter['GP']
451
- test_filter['ixG/60'] = test_filter['ixG']/test_filter['TOI']*60
452
- test_filter['Goals/60'] = test_filter['Goals']/test_filter['TOI']*60
453
- test_filter['G-xG/60'] = test_filter['G-ixG']/test_filter['TOI']*60
454
- test_filter['iCF/60'] = test_filter['iCF']/test_filter['TOI']*60
455
- test_filter['iSCF/60'] = test_filter['iSCF']/test_filter['TOI']*60
456
- test_filter['First Assists/60'] = test_filter['First_Assists']/test_filter['TOI']*60
457
- test_filter['Total Points/60'] = test_filter['Total_Points']/test_filter['TOI']*60
458
-
459
-
460
- test_filter['G-xG/60'] = test_filter['Goals/60'] - test_filter['ixG/60']
461
- test_filter['Goals Z-Score'] = (test_filter['Goals/GP']-test_filter['Goals/GP'].mean())/test_filter['Goals/GP'].std()
462
- test_filter['Assists Z-Score'] = (test_filter['Total Assists/GP']-test_filter['Total Assists/GP'].mean())/test_filter['Total Assists/GP'].std()
463
- test_filter['Points Z-Score'] = (test_filter['Total Points/GP']-test_filter['Total Points/GP'].mean())/test_filter['Total Points/GP'].std()
464
- test_filter['PP Points Z-Score'] = (test_filter['PP Points/GP']-test_filter['PP Points/GP'].mean())/test_filter['PP Points/GP'].std()
465
- test_filter['Shots Z-Score'] = (test_filter['Shots/GP']-test_filter['Shots/GP'].mean())/test_filter['Shots/GP'].std()
466
- test_filter['Hits Z-Score'] = (test_filter['Hits/GP']-test_filter['Hits/GP'].mean())/test_filter['Hits/GP'].std()
467
- test_filter['Blocks Z-Score'] = (test_filter['Shots Blocked/GP']-test_filter['Shots Blocked/GP'].mean())/test_filter['Shots Blocked/GP'].std()
468
-
469
-
470
- test_filter['ixG Z-Score'] = (test_filter['ixG/60']-test_filter['ixG/60'].mean())/test_filter['ixG/60'].std()
471
- test_filter['G Z-Score'] = (test_filter['Goals/60']-test_filter['Goals/60'].mean())/test_filter['Goals/60'].std()
472
- test_filter['G-xG Z-Score'] = (test_filter['G-xG/60']-test_filter['G-xG/60'].mean())/test_filter['G-xG/60'].std()
473
- test_filter['iCF Z-Score'] = (test_filter['iCF/60']-test_filter['iCF/60'].mean())/test_filter['iCF/60'].std()
474
- test_filter['iSCF Z-Score'] = (test_filter['iSCF/60']-test_filter['iSCF/60'].mean())/test_filter['iSCF/60'].std()
475
- test_filter['First Assists Z-Score'] = (test_filter['First Assists/60']-test_filter['First Assists/60'].mean())/test_filter['First Assists/60'].std()
476
- test_filter['P Z-Score'] = (test_filter['Total Points/60']-test_filter['Total Points/60'].mean())/test_filter['Total Points/60'].std()
477
- values_1 = test_filter.loc[name == test_filter.Player][['Goals/GP','Total Assists/GP','Total Points/GP','PP Points/GP','Shots/GP','Hits/GP','Shots Blocked/GP']].reset_index(drop=True).loc[0]
478
- values_2 = test_filter.loc[name == test_filter.Player][['ixG/60','Goals/60','G-xG/60','iCF/60','iSCF/60','First Assists/60','Total Points/60']].reset_index(drop=True).loc[0]
479
- categories = [i[:-11]+'/GP' for i in test_filter.columns[-14:-7]]
480
-
481
- #categories = [*categories]
482
-
483
- player = list(np.around(test_filter.loc[name == test_filter.Player][test_filter.columns[-14:-7]].values.flatten().tolist()))
484
-
485
- #player = [*player, player[0]]
486
-
487
-
488
- label_loc = np.linspace(start=0, stop=2 * np.pi, num=len(player))
489
-
490
- players_stats_all_on_filter = test_filter.copy()
491
- players_stats_all_on_filter['GF/60'] = (players_stats_all_on_filter['GF'])/players_stats_all_on_filter['TOI']*60
492
- players_stats_all_on_filter['xGF/60'] = (players_stats_all_on_filter['xGF'])/players_stats_all_on_filter['TOI']*60
493
- players_stats_all_on_filter['CF/60'] = (players_stats_all_on_filter['CF'])/players_stats_all_on_filter['TOI']*60
494
- # players_stats_all_on_filter['oiSH%'] = (players_stats_all_on_filter['On-Ice SH%']-players_stats_all_on_filter['On-Ice SH%'].mean())/players_stats_all_on_filter['On-Ice SH%'].std()
495
- #players_stats_all_on_filter['OZ Start%'] = (players_stats_all_on_filter['CF']-players_stats_all_on_filter['CF'])/players_stats_all_on_filter['CF']
496
- players_stats_all_on_filter['GF Z-Score'] = (players_stats_all_on_filter['GF/60']-players_stats_all_on_filter['GF/60'].mean())/players_stats_all_on_filter['GF/60'].std()
497
- players_stats_all_on_filter['xGF Z-Score'] = (players_stats_all_on_filter['xGF/60']-players_stats_all_on_filter['xGF/60'].mean())/players_stats_all_on_filter['xGF/60'].std()
498
- players_stats_all_on_filter['CF Z-Score'] = (players_stats_all_on_filter['CF/60']-players_stats_all_on_filter['CF/60'].mean())/players_stats_all_on_filter['CF/60'].std()
499
- players_stats_all_on_filter['oiSH% Z-Score'] = (players_stats_all_on_filter['oiSH']-players_stats_all_on_filter['oiSH'].mean())/players_stats_all_on_filter['oiSH'].std()
500
- players_stats_all_on_filter['OZ Start% Z-Score'] = (players_stats_all_on_filter['OZ Start%']-players_stats_all_on_filter['OZ Start%'].mean())/players_stats_all_on_filter['OZ Start%'].std()
501
- categories_on = [i[:-8]+'/60' for i in players_stats_all_on_filter.columns[-5:]]
502
- categories_on = ['GF/60', 'xGF/60', 'CF/60', 'oiSH', 'OZ Start%']
503
- player_on = list(np.around(players_stats_all_on_filter.loc[name == test_filter.Player][players_stats_all_on_filter.columns[-5:]].values.flatten().tolist(),2))
504
- categories_1 = [i[:-8]+'/GP' for i in test_filter.columns[-14:-7]]
505
- categories_2 = [i[:-8]+'/60' for i in test_filter.columns[-7:]]
506
- categories = categories_1+categories_2
507
- categories[12] = '1A/60'
508
- player = list(np.around(test_filter.loc[name == test_filter.Player][test_filter.columns[-14:]].values.flatten().tolist(),2))
509
-
510
- # color_scheme = []
511
- # for i in player:
512
- # if i <= -2:
513
- # color_scheme.append('#4285F4')
514
- # if i > -2 and i <= -1:
515
- # color_scheme.append('#A1C2FA')
516
- # if i > -1 and i <= 0:
517
- # color_scheme.append('#D0E1FD')
518
- # if i > 0 and i <= 1:
519
- # color_scheme.append('#FEEFC1')
520
- # if i > 1 and i <= 2:
521
- # color_scheme.append('#FDDE82')
522
- # if i > 2:
523
- # color_scheme.append('#FBBC04')
524
-
525
 
526
- goals_above_expected = test_filter.loc[name == test_filter.Player]['Goals'] - test_filter.loc[name == test_filter.Player]['ixG']
527
- goals_above_expected = test_filter.loc[name == test_filter.Player]['Goals'] - test_filter.loc[name == test_filter.Player]['ixG']
528
-
529
- # color_scheme_on= []
530
- # for i in player_on:
531
- # if i <= -2:
532
- # color_scheme_on.append('#4285F4')
533
- # if i > -2 and i <= -1:
534
- # color_scheme_on.append('#A1C2FA')
535
- # if i > -1 and i <= 0:
536
- # color_scheme_on.append('#D0E1FD')
537
- # if i > 0 and i <= 1:
538
- # color_scheme_on.append('#FEEFC1')
539
- # if i > 1 and i <= 2:
540
- # color_scheme_on.append('#FDDE82')
541
- # if i > 2:
542
- # color_scheme_on.append('#FBBC04')
543
-
544
- fig = plt.figure(figsize=(24, 13.5),dpi=300)
545
- fig.set_facecolor('white')
546
-
547
- cmap_new = matplotlib.colors.LinearSegmentedColormap.from_list("", ["#4285F4","white",'#FBBC04'])
548
- colormap_new = plt.get_cmap(cmap_new)
549
- norm_new = Normalize(vmin=-3, vmax=3)
550
-
551
- color_scheme = [colormap_new(norm_new(x)) for x in player]
552
- color_scheme_on = [colormap_new(norm_new(x)) for x in player_on]
553
-
554
- players_stats_all_on_filter = players_stats_all_on_filter.rename(columns={'On-Ice SH%':'oiSH%','Off.\xa0Zone Start %':'OZ Start%'})
555
- values_on = players_stats_all_on_filter.loc[name == test_filter.Player][['GF/60', 'xGF/60', 'CF/60', 'oiSH', 'OZ Start%']].reset_index(drop=True).loc[0]
556
- position_player = summary_2023_one['pos'].reset_index().pos[0]
557
- rank_1 = list(test_filter[values_1.index].rank(method='min',ascending=False).loc[name == test_filter.Player].reset_index(drop=True).astype(int).iloc[0])
558
- rank_2 = list(test_filter[values_2.index].rank(method='min',ascending=False).loc[name == test_filter.Player].reset_index(drop=True).astype(int).iloc[0])
559
- rank_3 = list(players_stats_all_on_filter[values_on.index].rank(method='min',ascending=False).loc[name == test_filter.Player].reset_index(drop=True).astype(int).iloc[0][categories_on])
560
- #rank_3 = rank_3 + list(players_stats_all_on_filter[values_on.index].rank(method='first',ascending=True).loc[name == test.Player].reset_index(drop=False).astype(int).iloc[0][categories_on[3:5]])
561
 
562
- values_on = [float(x) for x in values_on]
563
 
564
- player_games_one = player_games_one.merge(right=team_games[['Team','pp_toi','date']],left_on=['Team','date'],right_on=['Team','date'],how='left').fillna(0)
565
- y=(player_games_one.rolling(last_games).sum()['TOI_pp']/player_games_one.rolling(last_games).sum()['pp_toi'])[last_games:]
566
- player_games_one = player_games_one.sort_values(by='date').reset_index(drop=True)
567
 
568
- # fig, ([ax1,ax2],[ax3,ax4])= plt.subplots(nrows=2, ncols=2,figsize=(16,10))
569
 
 
 
 
570
 
571
 
572
- colormap = plt.get_cmap(cmap)
 
 
 
573
 
574
- value = 1
575
- # Normalize the value
576
- norm = Normalize(vmin=0.8, vmax=1.2)
577
- normalized_value = norm(value)
 
 
 
 
 
 
 
 
 
 
 
 
 
578
 
579
- col_3_colour = ['white']*len(df_combined_t)
580
- col_2_colour = [colormap(norm(x)) for x in list(((df_combined_t[df_combined_t.columns[1]].values) / (df_combined_t[df_combined_t.columns[2]].values)))]
581
- col_1_colour = [colormap(norm(x)) for x in list(((df_combined_t[df_combined_t.columns[0]].values) / (df_combined_t[df_combined_t.columns[1]].values)))]
582
- colour_df = pd.DataFrame(data=[col_1_colour,col_2_colour,col_3_colour]).T.values
583
 
584
- colour_df[[0],[0]] = 'white'
585
- colour_df[[1],[0]] = 'white'
586
- colour_df[[0],[1]] = 'white'
587
- colour_df[[1],[1]] = 'white'
588
 
 
 
589
 
590
- if df_combined_t.values[[10],[0]] < 0 and df_combined_t.values[[10],[1]] < 0:
591
- cmap_flip = matplotlib.colors.LinearSegmentedColormap.from_list("", ["#FBBC04","white","#4285F4"])
592
- norm = Normalize(vmin=0.8, vmax=1.2)
593
- colour_df[[10],[0]] = tuple(cmap_flip(norm(df_combined_t.values[[10],[0]] / df_combined_t.values[[10],[1]])))
594
 
595
- elif df_combined_t.values[[10],[0]] < 0 and df_combined_t.values[[10],[1]] > 0:
596
- #cmap_flip = matplotlib.colors.LinearSegmentedColormap.from_list("", ["#FBBC04","white","#4285F4"])
597
- cmap_y = matplotlib.colors.LinearSegmentedColormap.from_list("", ["white","#4285F4"])
598
- norm = Normalize(vmin=-1, vmax=0)
599
- colour_df[[10],[0]] = tuple(cmap_y(norm(df_combined_t.values[[10],[0]] - df_combined_t.values[[10],[1]])))
600
 
601
- elif df_combined_t.values[[10],[0]] > 0 and df_combined_t.values[[10],[1]] < 0:
602
- cmap_y = matplotlib.colors.LinearSegmentedColormap.from_list("", ["white","#FBBC04"])
603
- norm = Normalize(vmin=0, vmax=1)
604
- colour_df[[10],[0]] = tuple(cmap_y(norm(df_combined_t.values[[10],[0]] - df_combined_t.values[[10],[1]])))
605
 
 
 
 
 
606
 
607
- if df_combined_t.values[[10],[1]] < 0 and df_combined_t.values[[10],[2]] < 0:
608
- cmap_flip = matplotlib.colors.LinearSegmentedColormap.from_list("", ["#FBBC04","white","#4285F4"])
609
- norm = Normalize(vmin=0.8, vmax=1.2)
610
- colour_df[[10],[1]] = tuple(cmap_flip(norm(df_combined_t.values[[10],[1]] / df_combined_t.values[[10],[2]])))
611
 
612
- elif df_combined_t.values[[10],[1]] < 0 and df_combined_t.values[[10],[2]] > 0:
613
- #cmap_flip = matplotlib.colors.LinearSegmentedColormap.from_list("", ["#FBBC04","white","#4285F4"])
614
- cmap_y = matplotlib.colors.LinearSegmentedColormap.from_list("", ["white","#4285F4"])
615
- norm = Normalize(vmin=-1, vmax=0)
616
- colour_df[[10],[1]] = tuple(cmap_y(norm(df_combined_t.values[[10],[1]] - df_combined_t.values[[10],[2]])))
617
 
618
- elif df_combined_t.values[[10],[1]] > 0 and df_combined_t.values[[10],[2]] < 0:
619
- cmap_y = matplotlib.colors.LinearSegmentedColormap.from_list("", ["white","#FBBC04"])
620
- norm = Normalize(vmin=0, vmax=1)
621
- colour_df[[10],[1]] = tuple(cmap_y(norm(df_combined_t.values[[10],[1]] - df_combined_t.values[[10],[2]])))
622
 
 
 
 
 
 
 
 
 
 
 
 
 
623
 
624
- ax1 = plt.subplot(1,3,1)
625
- ax2 = plt.subplot(3,3,2)
626
- ax3 = plt.subplot(3,3,5)
627
- ax4 = plt.subplot(3,3,8)
628
- ax5 = plt.subplot(3,3,3)
629
- ax6 = plt.subplot(3,3,6)
630
- ax7 = plt.subplot(3,3,9)
631
- #axbot = plt.subplot(3,1,1)
632
- axes = [ax1, ax2, ax3, ax4, ax5,ax6,ax7]
633
- # ax[0][0].axis('off')
634
- # im = plt.imread('players/'+name+'_'+str(last_games)+' int.png')
635
- # ax[0][0] = fig.add_axes([0,0,1,1], anchor='W', zorder=1)
636
- # ax[0][0].imshow(im)
637
-
638
 
639
- try:
640
- image = "https://cms.nhl.bamgrid.com/images/headshots/current/168x168/"+str(player_id)+".png"
641
- im = plt.imread(image)
642
-
643
- except HTTPError as err:
644
- if err.code == 403:
645
- image = "https://cms.nhl.bamgrid.com/images/headshots/current/168x168/skater.png"
646
- im = plt.imread(image)
647
- else:
648
- raise
649
 
650
- logo = nhl_logos[nhl_logos.Team==list(player_games_one['Team'])[0]].reset_index().URL[0]
651
- #im = plt.imread('players/'+name+'_'+str(last_games)+'.png')
652
- #ax = fig.add_axes([0,0,1,0.85], anchor='C', zorder=1)
653
- #ax.imshow(im)
654
-
655
-
656
- im = plt.imread(image)
657
- im = OffsetImage(im, zoom=.75)
658
- ab = AnnotationBbox(im, (0.69, 0.765), xycoords='axes fraction', box_alignment=(0.0,0.0),bboxprops={'edgecolor':'white'})
659
- ax1.add_artist(ab)
660
- # axim = fig.add_axes([0.25,0.76,0.12,0.12], anchor='NW', zorder=1)
661
- # axim.imshow(im)
662
- # axim.axis('off')
663
-
664
- imr = plt.imread(logo)
665
- imr = OffsetImage(imr, zoom=.1)
666
- ab = AnnotationBbox(imr, (0.45, 0.765), xycoords='axes fraction', box_alignment=(0.0,0.0),bboxprops={'edgecolor':'white'})
667
- ax1.add_artist(ab)
668
- # axim = fig.add_axes([0.18,0.76,0.75,0.075], anchor='NW', zorder=1)
669
- # axim.imshow(imr)
670
- # axim.axis('off')
671
-
672
- sub_value = 0.16
673
- ax1.text(x=0.5,y=1.13-sub_value,s='NHL Player Summary',horizontalalignment='center',fontsize=36, fontweight='bold')
674
- ax1.text(x=0.5,y=1.08-sub_value,s='2023-24 Season',horizontalalignment='center',fontsize=28,fontname='Century Gothic', fontstyle='italic')
675
- ax1.text(x=0.05,y=1.04-sub_value,s='Player',horizontalalignment='center',fontsize=18,fontname='Century Gothic', fontweight='bold')
676
- ax1.text(x=0.05,y=1.005-sub_value,s='Team',horizontalalignment='center',fontsize=18,fontname='Century Gothic', fontweight='bold')
677
- ax1.text(x=0.05,y=0.97-sub_value,s='Position',horizontalalignment='center',fontsize=18,fontname='Century Gothic', fontweight='bold')
678
- #ax1.text(x=0.1,y=0.94-sub_value,s='Age',horizontalalignment='center',fontsize=18,fontname='Century Gothic', fontweight='bold')
679
- #ax1.text(x=0.12,y=0.91-sub_value,s='Cap Hit',horizontalalignment='center',fontsize=22,fontname='Century Gothic', fontweight='bold')
680
- ax1.text(x=0.05,y=0.935-sub_value,s='Roster%',horizontalalignment='center',fontsize=18,fontname='Century Gothic', fontweight='bold')
681
-
682
- ax1.text(x=0.175,y=1.04-sub_value,s=name,horizontalalignment='left',fontsize=18,fontname='Century Gothic')
683
- ax1.text(x=0.175,y=1.005-sub_value,s=list(player_games_one['Team'])[0],horizontalalignment='left',fontsize=18,fontname='Century Gothic')
684
- ax1.text(x=0.175,y=0.97-sub_value,s=yahoo_position,horizontalalignment='left',fontsize=18,fontname='Century Gothic')
685
- #ax1.text(x=0.25,y=0.94-sub_value,s=str(summary_2023_one.reset_index().AGE[0]),horizontalalignment='left',fontsize=22,fontname='Century Gothic')
686
- #ax1.text(x=0.25,y=0.91-sub_value,s=summary_2023_one.loc[summary_2023_one['player_id']==player_id].reset_index()['sheets'][0],horizontalalignment='left',fontsize=22,fontname='Century Gothic')
687
- ax1.text(x=0.175,y=0.935-sub_value,s=str(int(percent_owned*100))+'%',horizontalalignment='left',fontsize=18,fontname='Century Gothic')
688
-
689
-
 
 
 
 
 
 
 
 
 
 
 
 
690
 
691
-
692
- ax1.axis("off")
693
-
694
-
695
-
696
-
697
-
698
- table = ax1.table(cellText=df_combined_t.values, colLabels=df_combined_t.columns,rowLabels=df_combined_t.index
699
- , cellLoc='center',rowLoc='center', bbox=[0.15, 0.05, 0.8, 0.7],cellColours=colour_df)
700
- #table.auto_set_font_size(True)
701
- table.set_fontsize(20)
702
- table.scale(1, 1.5)
703
 
704
 
705
 
706
- format_col = df_combined_t[df_combined_t.columns[0]]
707
- n_c = 0
708
- for cell in table.get_celld().values():
709
- if n_c < 1*3:
710
- if cell.get_text().get_text() in format_col.astype(str).values:
711
- cell.get_text().set_text('{:,.0f}'.format(float(cell.get_text().get_text())))
712
- elif n_c < 16*3:
713
- if cell.get_text().get_text() in format_col.astype(str).values:
714
- cell.get_text().set_text('{:,.2f}'.format(float(cell.get_text().get_text())))
715
- else:
716
- if cell.get_text().get_text() in format_col.astype(str).values:
717
- cell.get_text().set_text('{:,.1%}'.format(float(cell.get_text().get_text())))
718
- n_c = n_c + 1
719
-
720
-
721
- format_col = df_combined_t[df_combined_t.columns[1]]
722
- n_c = 0
723
- for cell in table.get_celld().values():
724
- if n_c < 1*3:
725
- if cell.get_text().get_text() in format_col.astype(str).values:
726
- cell.get_text().set_text('{:,.0f}'.format(float(cell.get_text().get_text())))
727
- elif n_c < 16*3:
728
- if cell.get_text().get_text() in format_col.astype(str).values:
729
- cell.get_text().set_text('{:,.2f}'.format(float(cell.get_text().get_text())))
730
- else:
731
- if cell.get_text().get_text() in format_col.astype(str).values:
732
- cell.get_text().set_text('{:,.1%}'.format(float(cell.get_text().get_text())))
733
- n_c = n_c + 1
734
-
735
- format_col = df_combined_t[df_combined_t.columns[2]]
736
- n_c = 0
737
- for cell in table.get_celld().values():
738
- if n_c < 1*3:
739
- if cell.get_text().get_text() in format_col.astype(str).values:
740
- cell.get_text().set_text('{:,.0f}'.format(float(cell.get_text().get_text())))
741
- elif n_c < 16*3:
742
- if cell.get_text().get_text() in format_col.astype(str).values:
743
- cell.get_text().set_text('{:,.2f}'.format(float(cell.get_text().get_text())))
744
- else:
745
- if cell.get_text().get_text() in format_col.astype(str).values:
746
- cell.get_text().set_text('{:,.1%}'.format(float(cell.get_text().get_text())))
747
- n_c = n_c + 1
748
 
749
- #ax1.text('')
750
 
751
 
 
 
 
752
 
753
- # ax1.axis('off')
754
- # img = mpimg.imread('players/'+name+'_'+str(last_games)+' int.png')
755
 
756
- # im = plt.imread('players/'+name+'_'+str(last_games)+'.png')
757
- # ax1 = fig.add_axes([0.03,0.,1,1], anchor='SW')
758
- # ax1.imshow(im)
759
 
760
- # ax1.imshow(img)
761
- # ax1.axis('off')
762
- # fig.tight_layout()
763
 
 
 
764
 
 
 
 
 
765
 
766
- categories[0:7] = ['G/GP','A/GP','P/GP','PPP/GP','S/GP','Hits/GP','Blk/GP']
767
- _ = sns.barplot(data=test_filter[test_filter.columns[56:63]],x=categories[0:7],y=player[0:7],palette=color_scheme[0:7],edgecolor='black',ax=ax2)
768
 
769
- ax2.set_title("Individual Per Game Z-Score (vs "+position_player+", min. "+str(min_time)+' TOI)',fontsize=14,fontname='Century Gothic')
770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
771
 
772
- #plt.rcParams['xtick.color']='#333F4B'
773
- #ax[0][1].set_prop_cycle(ytick.color='#333F4B')
 
 
 
 
 
 
 
 
 
 
 
 
 
774
 
775
 
776
- ax2.set_ylabel('Z-Score', fontsize=15,fontname='Century Gothic')
777
- #plt.ylabel('Z-Score', fontsize=15,fontname='Century Gothic')
 
778
 
779
- #plt.xlabel('Percentile', fontsize=12, color = '#000000',fontname='Century Gothic')
780
- #plt.tick_params(axis='x', which='both', labelsize=10,bottom=False,top=False)
781
- ax2.set_ylim([-3, 3])
782
- plt.style.use('classic')
783
- show_values(ax2,stat = values_1,rank_n=rank_1)
784
- ax2.grid(axis = 'y',linestyle = '-', linewidth = 0.5,alpha=0.3)
 
 
785
 
786
- ax2.set_axisbelow(True)
787
- #ax[1].hlines(y=0,xmin=0,xmax=len(player),color='black')
788
 
789
 
790
- #plt.savefig('players/'+name+"_"+str(last_games)+'_Z.png')#,bbox_inches="tight")
791
- # ax[1][0] = sns.barplot(data=test,x=categories,y=player,palette=color_scheme,edgecolor='black')
792
- # show_values(ax2,stat = values)
793
- # data_input = df_shots[(df_shots.shooterName==name) & (df_shots.event!='MISS')]
794
- # sns.kdeplot(data=data_input, x=data_input["yCordAdjusted"]*-1, y=data_input["xCordAdjusted"],fill=True,thresh=0.4,cmap=cmap,ax=ax3)
795
- # #sns.scatterplot(data=data_input, x=data_input["yCordAdjusted"]*-1, y=data_input["xCordAdjusted"], alpha=1,zorder=25,cmap='Blues',s=400,hue='shotType',palette="Set2")
796
- # rink = NHLRink(rotation=270)
797
- # #x, y = rink.convert_xy(x, y)
798
- # rink.draw(ax=ax3,display_range='dzone')
799
- # ax3.set_title(name+" Shooting Heat Map",fontsize=16,fontname='Century Gothic')
800
- #plt.suptitle('NHL Shot Locations ', fontsize=32, y = 0.91)
801
- #plt.title('All Shots', fontsize=16, y=1.02)
802
- sns.barplot(x=categories[7:14],y=player[7:14],palette=color_scheme[7:14],edgecolor='black',ax=ax3)
803
- ax3.set_title("Individual Rate Z-Score (vs "+position_player+", min. "+str(min_time)+' TOI)',fontsize=14,fontname='Century Gothic')
804
- ax3.grid(axis = 'y',linestyle = '-', linewidth = 0.5,alpha=0.3)
805
- ax3.set_ylim([-3, 3])
806
- ax3.set_axisbelow(True)
807
- ax3.set_ylabel('Z-Score', fontsize=15,fontname='Century Gothic')
808
- #plt.rcParams['xtick.color']='#333F4B'
809
- #ax[0][1].set_prop_cycle(ytick.color='#333F4B')
810
-
811
-
812
- sns.barplot(x=categories_on,y=player_on,palette=color_scheme_on,edgecolor='black',ax=ax4)
813
- ax4.set_ylim([-3, 3])
814
- ax4.set_title("On-Ice All Situations Rate Z-Score (vs "+position_player+", min. "+str(min_time)+' TOI)',fontsize=14,fontname='Century Gothic')
815
- ax4.set_axisbelow(True)
816
- ax4.set_ylabel('Z-Score', fontsize=15,fontname='Century Gothic')
817
- ax4.grid(axis = 'y',linestyle = '-', linewidth = 0.5,alpha=0.3)
818
 
819
- ax2.set_ylabel('Z-Score', fontsize=15,fontname='Century Gothic')
820
- show_values(ax3,stat = values_2,rank_n=rank_2)
821
- values_on[3] = str(round(values_on[3]*100,1))+'%'
822
- values_on[4] = str(round(values_on[4]*100,1))+'%'
823
- show_values(ax4,stat = values_on,rank_n=rank_3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
824
 
 
825
 
826
- ax2.text(-0.5, -3.8, '2023-24', ha="right",fontstyle='italic',zorder=1)
827
- ax3.text(-0.5, -3.8, '2023-24', ha="right",fontstyle='italic')
828
- ax4.text(-0.5, -3.8, '2023-24', ha="right",fontstyle='italic')
829
 
 
 
 
830
 
831
- ax2.text(-0.5, -4.2, 'Rank (of '+str(len(test_filter))+")", ha="right",fontstyle='italic',zorder=100)
832
- ax3.text(-0.5, -4.2, 'Rank(of '+str(len(test_filter))+")", ha="right",fontstyle='italic',zorder=100)
833
- ax4.text(-0.5, -4.2, 'Rank (of '+str(len(test_filter))+")", ha="right",fontstyle='italic',zorder=100)
834
 
 
 
 
 
835
 
836
- ax2.tick_params(axis='x', which='both', labelsize=12,bottom=False,top=False)
837
- ax3.tick_params(axis='x', which='both', labelsize=12,bottom=False,top=False)
838
- ax4.tick_params(axis='x', which='both', labelsize=12,bottom=False,top=False)
 
839
 
840
- if summary_2023_one.GP.values[0] < 2:
841
- line_text_value = 1
842
- else:
843
- line_text_value = last_games+0.5
844
-
845
- if position_player == 'F':
846
- ax5.hlines(y=18,xmin=0, xmax=100, color='black',linewidth=1,linestyles='--',alpha=0.5)
847
- ax5.text(s="1st",y=18,x=line_text_value, color='black',fontsize=8,bbox=dict(facecolor='white', alpha=0.5, pad=1.0))
848
- ax5.hlines(y=16,xmin=0, xmax=100, color='black',linewidth=1,linestyles='--',alpha=0.5)
849
- ax5.text(s="2nd",y=16,x=line_text_value, color='black',fontsize=8,bbox=dict(facecolor='white', alpha=0.5, pad=1.0))
850
- ax5.hlines(y=14,xmin=0, xmax=100, color='black',linewidth=1,linestyles='--',alpha=0.5)
851
- ax5.text(s="3rd",y=14,x=line_text_value, color='black',fontsize=8,bbox=dict(facecolor='white', alpha=0.5, pad=1.0))
852
- ax5.hlines(y=12,xmin=0, xmax=100, color='black',linewidth=1,linestyles='--',alpha=0.5)
853
- ax5.text(s="4th",y=12,x=line_text_value, color='black',fontsize=8,bbox=dict(facecolor='white', alpha=0.5, pad=1.0))
854
-
855
- if position_player == 'D':
856
- ax5.hlines(y=23,xmin=0, xmax=100, color='black',linewidth=1,linestyles='--',alpha=0.5)
857
- ax5.text(s="1st",y=23,x=line_text_value, color='black',fontsize=8,bbox=dict(facecolor='white', alpha=0.5, pad=1.0))
858
- ax5.hlines(y=20,xmin=0, xmax=100, color='black',linewidth=1,linestyles='--',alpha=0.5)
859
- ax5.text(s="2nd",y=20,x=line_text_value, color='black',fontsize=8,bbox=dict(facecolor='white', alpha=0.5, pad=1.0))
860
- ax5.hlines(y=17,xmin=0, xmax=100, color='black',linewidth=1,linestyles='--',alpha=0.5)
861
- ax5.text(s="3rd",y=17,x=line_text_value, color='black',fontsize=8,bbox=dict(facecolor='white', alpha=0.5, pad=1.0))
862
-
863
-
864
- sns.lineplot(x=player_games_one.game,y=player_games_one.rolling(last_games,min_periods=last_games).mean()['TOI'],ax=ax5,color='#FFB000',linewidth = 2,label='TOI/GP',legend=False)
865
-
866
-
867
-
868
-
869
- ax5.xaxis.set_major_locator(MaxNLocator(integer=True))
870
- ax5.set_xlim([last_games, np.max(player_games_one.game)])
871
- axn = ax5.twinx()
872
- #sns.lineplot(x=game_log_pp.Game,y=game_log_pp.rolling(last_games).mean()['PP_TOI'],ax=axn,color='#DCAD23',linewidth = 2,label='PP TOI/GP',legend=False)
873
- #ax5.set_ylim([min(math.floor(min(player_games_one.TOI)/5)*5,10), math.ceil(max((player_games_one.rolling(last_games).mean().fillna(0)['TOI']))/5)*5])
874
- ax5.set_ylim([10,30])
875
- ax5.grid(axis = 'x',linestyle = '-', linewidth = 0.5,alpha=0.3)
876
- ax5.grid(axis = 'y',linestyle = '-', linewidth = 0.5,alpha=0.3)
877
- ax5.set_title(str(last_games)+" Game Rolling Average - TOI and PP%",fontsize=14,fontname='Century Gothic')
878
- ax5.set(xlabel='Game', ylabel='TOI/GP')
879
- axn.set(xlabel='Game', ylabel='PP%')
880
- axn.yaxis.set_major_formatter(mtick.PercentFormatter(1.0))
881
- axn.set_ylim([0, 1])
882
-
883
-
884
- sns.lineplot(x=player_games_one.game,y=(player_games_one.rolling(last_games).sum()['TOI_pp']/player_games_one.rolling(last_games).sum()['pp_toi']),ax=axn,color='#648FFF',linewidth = 2,label='PP%',legend=False)
885
- #
886
- handles, labels = [ax5.get_legend_handles_labels()[0]+axn.get_legend_handles_labels()[0]]+[ax5.get_legend_handles_labels()[1]+axn.get_legend_handles_labels()[1]]
887
- ax5.legend(handles, labels, fontsize=10,ncol=2,loc=9)
888
 
889
- sns.lineplot(x=player_games_one.game,y=player_games_one.rolling(last_games).mean()['Total Points'],ax=ax6,color='#FFB000',label='Points',linewidth = 2)
890
- sns.lineplot(x=player_games_one.game,y=player_games_one.rolling(last_games).mean()['Total Points_pp'],ax=ax6,color='#648FFF',label='PP Points',linewidth = 2)
891
- sns.lineplot(x=player_games_one.game,y=player_games_one.rolling(last_games).mean()['Goals'],ax=ax6,color='#DC267F',label='Goals',linewidth = 2)
892
- sns.lineplot(x=player_games_one.game,y=player_games_one.rolling(last_games).mean()['Total Assists'],ax=ax6,color='#FE6100',label='Assists',linewidth = 2)
893
- ax6.legend(fontsize=10,ncol=4,loc=9)
894
- ax6.xaxis.set_major_locator(MaxNLocator(integer=True))
895
- ax6.set_xlim([last_games, np.max(player_games_one.game)])
896
- ax6.set_ylim([0, round(max((player_games_one.rolling(last_games).mean().fillna(0)['Total Points'])+1)*2)/2])
897
- ax6.grid(axis = 'x',linestyle = '-', linewidth = 0.5,alpha=0.3)
898
- ax6.grid(axis = 'y',linestyle = '-', linewidth = 0.5,alpha=0.3)
899
- ax6.set_title(str(last_games)+" Game Rolling Average - Points",fontsize=14)
900
- ax6.set(xlabel='Game', ylabel='Value Per Game')
901
 
902
 
903
- sns.lineplot(x=player_games_one.game,y=player_games_one.rolling(last_games).mean()['Shots'],ax=ax7,color='#FFB000',linewidth = 2,label='Shots')
904
- sns.lineplot(x=player_games_one.game,y=player_games_one.rolling(last_games).mean()['iCF'],ax=ax7,color='#648FFF',linewidth = 2,label='iCF')
905
- # sns.lineplot(x=player_games_one.game,y=player_games_one.rolling(last_games).mean()['iSCF'],ax=ax7,color='#DC267F',linewidth = 2,label='iSCF')
906
- sns.lineplot(x=player_games_one.game,y=player_games_one.rolling(last_games).mean()['Hits'],ax=ax7,color='#DC267F',linewidth = 2,label='Hits')
907
- sns.lineplot(x=player_games_one.game,y=player_games_one.rolling(last_games).mean()['Shots Blocked'],ax=ax7,color="#FE6100",linewidth = 2,label='Blocks')
908
 
909
- ax7.legend(fontsize=10,ncol=5,loc=9)
910
- ax7.xaxis.set_major_locator(MaxNLocator(integer=True))
911
- ax7.set_xlim([last_games, np.max(player_games_one.game)])
912
- ax7.set_ylim([0, max(round(max((player_games_one.rolling(last_games).mean().fillna(0)['iCF'])+1)*2)/2+1,round(max((player_games_one.rolling(last_games).mean().fillna(0)['Shots'])+1)*2)/2+1,round(max((player_games_one.rolling(last_games).mean().fillna(0)['Hits'])+1)*2)/2+1,round(max((player_games_one.rolling(last_games).mean().fillna(0)['Shots Blocked'])+1)*2)/2+1)])
913
- ax7.grid(axis = 'x',linestyle = '-', linewidth = 0.5,alpha=0.3)
914
- ax7.grid(axis = 'y',linestyle = '-', linewidth = 0.5,alpha=0.3)
915
- ax7.set_title(str(last_games)+" Game Rolling Average - Shots and Bangers",fontsize=14,fontname='Century Gothic')
916
- ax7.set(xlabel='Game', ylabel='Value Per Game')
917
- ax2.hlines(y=0,xmin=-0.5, xmax=len(ax2.patches)-0.5, color='black',linewidth=1)
918
- ax3.hlines(y=0,xmin=-0.5, xmax=len(ax3.patches)-0.5, color='black',linewidth=1)
919
- ax4.hlines(y=0,xmin=-0.5, xmax=len(ax4.patches)-0.5, color='black',linewidth=1)
920
 
921
- ax2.set_xlim([-0.5, len(ax2.patches)-0.5])
922
- ax3.set_xlim([-0.5, len(ax3.patches)-0.5])
923
- ax4.set_xlim([-0.5, len(ax4.patches)-0.5])
924
 
925
- #plt.tight_layout(pad=5, w_pad=2, h_pad=2)
926
 
927
- #abot = fig.add_axes([0.075,0.025,0.9,0.025], anchor='NW', zorder=1)
928
 
929
- print('here')
930
 
931
- ax1.text(x=0.0,y=-0.025,s='Created By: @TJStats',horizontalalignment='left',fontsize=14)#,fontname='Century Gothic')
932
- ax1.text(x=0.0,y=-0.05,s='Data: Natural Stat Trick, CapFriendly, Yahoo Fantasy',horizontalalignment='left',fontsize=14)#,fontname='Century Gothic')
933
- ax1.text(x=0.725,y=-0.025,s=f'Generated: {str(date.today())}',horizontalalignment='right',fontsize=14)#,fontname='Century Gothic')
 
934
 
935
- from matplotlib.font_manager import FontProperties
936
- font_properties_label = FontProperties(family='century gothic', size=16)
937
- ax5.set_xlabel(xlabel=ax5.get_xlabel(),fontproperties=font_properties_label)
938
- ax6.set_xlabel(xlabel=ax6.get_xlabel(),fontproperties=font_properties_label)
939
- ax7.set_xlabel(xlabel=ax7.get_xlabel(),fontproperties=font_properties_label)
940
- ax5.set_ylabel(ylabel=ax5.get_ylabel(),fontproperties=font_properties_label)
941
- ax6.set_ylabel(ylabel=ax6.get_ylabel(),fontproperties=font_properties_label)
942
- ax7.set_ylabel(ylabel=ax7.get_ylabel(),fontproperties=font_properties_label)
943
 
944
- font_properties_title = FontProperties(family='century gothic', size=16)
945
- ax2.set_title(label=ax2.get_title(),fontproperties=font_properties_title)
946
- ax3.set_title(label=ax3.get_title(),fontproperties=font_properties_title)
947
- ax4.set_title(label=ax4.get_title(),fontproperties=font_properties_title)
948
- ax5.set_title(label=ax5.get_title(),fontproperties=font_properties_title)
949
- ax6.set_title(label=ax6.get_title(),fontproperties=font_properties_title)
950
- ax7.set_title(label=ax7.get_title(),fontproperties=font_properties_title)
951
 
952
- ax1.text(x=0.43,y=0.025,s='Note: Last Games compares to 2023-24. 2023-24 compares to 2022-23.',horizontalalignment='center',fontsize=12,fontname='Century Gothic')
953
- # font_properties_ticks = FontProperties(family='century gothic', size=12)
954
- # ax5.set_xticklabels(ax5.get_xticks(),fontproperties=font_properties_label)
955
 
956
- #plt.xticks(fontname = 'century gothic')
957
- for key, cell in table.get_celld().items():
958
- cell.set_linewidth(0)
959
 
960
 
961
- #abot.axis('off')
962
- #ax1.set_zorder(2)
963
- fig.tight_layout()
964
 
965
- # for i in range(0,len(df_combined_t.index)):
966
- # ax1.text(s=df_combined_t.index[i],x=10,y=700+(10*i),fontsize=10,bbox=dict(facecolor='red', alpha=1))
 
 
967
 
968
 
969
- # #plt.figure(dpi=300)
970
- # if not os.path.exists('player_cards/skaters/'+name):
971
- # os.makedirs('player_cards/skaters/'+name)
972
 
973
- # dir = 'players/'
974
- # for f in os.listdir(dir):
975
- # os.remove(os.path.join(dir, f))
976
 
 
977
 
978
- #plt.savefig('player_cards/skaters/'+name+'/'+name+"_"+str(last_games)+'_Znew.png',dpi=600,bbox_inches="tight")
979
 
980
- #matplotlib.rcParams["figure.dpi"] = 600
981
 
982
- #plt.savefig('players/'+name+"_"+str(last_games)+'_Znew.png',dpi=600,bbox_inches="tight")
 
 
 
 
 
983
 
984
 
985
 
986
- # test = test.fillna(0)
987
- #test['PP TOI'] = ["%d:%02d" % (int(x),(x*60)%60) if x>0 else '0:00' for x in test['PP TOI']]
 
 
 
 
 
988
 
 
 
 
989
 
 
 
 
 
 
990
 
991
- app = App(app_ui, server)
992
- #time.sleep(60)
 
 
 
 
1
  import pandas as pd
2
  import seaborn as sns
3
  import matplotlib.pyplot as plt
4
  from matplotlib.pyplot import figure
5
  from matplotlib.offsetbox import OffsetImage, AnnotationBbox
6
+ from scipy import stats
7
+ import pickle
8
+ import json
9
+ from datetime import timedelta
10
+ from urllib.request import urlopen
11
+ from datetime import date
 
 
 
 
 
 
12
  from datetime import datetime
13
  import pytz
14
+ import json
15
  from matplotlib.ticker import MaxNLocator
16
+ import matplotlib.font_manager as font_manager
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # team_games_df = pd.read_csv('data/team_games_all.csv',index_col=[0])
20
+ # player_games_df = pd.read_csv('data/player_games_cards.csv',index_col=[0]).sort_values(by='date').reset_index(drop=True)
21
+ team_abv_nst = pd.read_csv('data/team_abv_nst.csv')
22
+ #player_games_df = player_games_df.loc[:, ~player_games_df.columns.str.contains('^Unnamed')]
23
+ #team_abv = pd.read_csv('team_abv.csv')
24
+ #team_games_df = team_games_df.merge(right=team_abv,left_on='team',right_on='team_name',how='left').drop(columns='team_name')
25
+ team_abv = pd.read_csv('data/team_abv.csv')
 
 
 
 
 
26
 
27
+ import pickle
28
+ from datetime import timedelta
29
 
30
+ # # Loop over the counter and format the API call
31
+ # r = requests.get('https://statsapi.web.nhl.com/api/v1/schedule?startDate=2022-10-01&endDate=2023-06-01')
32
+ # schedule = r.json()
33
 
34
+ schedule = json.loads(urlopen('https://statsapi.web.nhl.com/api/v1/schedule?startDate=2023-10-07&endDate=2024-04-19').read())
35
 
36
+ def flatten(t):
37
+ return [item for sublist in t for item in sublist]
 
 
38
 
39
+ game_id = flatten([[x['gamePk'] for x in schedule['dates'][y]['games']] for y in range(0,len(schedule['dates']))])
40
+ game_type = flatten([[x['gameType'] for x in schedule['dates'][y]['games']] for y in range(0,len(schedule['dates']))])
41
+ game_date = flatten([[(pd.to_datetime(x['gameDate']) - timedelta(hours=8)) for x in schedule['dates'][y]['games']] for y in range(0,len(schedule['dates']))])
42
+ game_final = flatten([[x['status']['detailedState'] for x in schedule['dates'][y]['games']] for y in range(0,len(schedule['dates']))])
43
+ game_home = flatten([[x['teams']['home']['team']['name'] for x in schedule['dates'][y]['games']] for y in range(0,len(schedule['dates']))])
44
+ game_away = flatten([[x['teams']['away']['team']['name'] for x in schedule['dates'][y]['games']] for y in range(0,len(schedule['dates']))])
45
 
46
+ schedule_df = pd.DataFrame(data={'game_id': game_id, 'game_type':game_type,'game_date' : game_date, 'game_home' : game_home, 'game_away' : game_away,'status' : game_final})
47
+ schedule_df = schedule_df[schedule_df.game_type == 'R'].reset_index(drop=True)
48
+ schedule_df = schedule_df[schedule_df.status != 'Postponed']
49
+ schedule_df = schedule_df.replace('Montréal Canadiens','Montreal Canadiens')
50
 
51
 
 
 
52
 
53
+ schedule_df_merge = schedule_df.merge(right=team_abv,left_on='game_home',right_on='team_name',how='left')
54
+ schedule_df_merge = schedule_df_merge.merge(right=team_abv,left_on='game_away',right_on='team_name',how='left')
55
+ schedule_df_merge = schedule_df_merge.drop(columns={'team_name_x','team_name_y'})
56
+ schedule_df_merge = schedule_df_merge.rename(columns={'team_abv_x' : 'team_abv_home','team_abv_y' : 'team_abv_away'})
 
 
 
 
 
57
 
 
 
 
 
 
 
 
 
 
 
 
58
 
 
59
 
 
60
 
61
+ #schedule_df_merge.game_date = pd.to_datetime(schedule_df_merge['game_date']).dt.tz_convert(tz='US/Eastern').dt.date
62
+ # schedule_df_merge = schedule_df_merge.set_index(pd.DatetimeIndex(schedule_df_merge.game_date).strftime('%Y-%m-%d'))
63
+ schedule_df_merge.index = pd.to_datetime(schedule_df_merge.game_date)
64
+ schedule_df_merge = schedule_df_merge.drop(columns='game_date')
65
+ #schedule_df_merge.index = schedule_df_merge.index.tz_convert('US/Pacific')
66
+ schedule_df_merge.index = schedule_df_merge.index.date
67
+ schedule_df_merge = schedule_df_merge.sort_index()
68
+ schedule_df_merge = schedule_df_merge[schedule_df_merge.index <= date(2024,5,1)]
69
 
70
+ schedule_df_merge_final = schedule_df_merge[schedule_df_merge['status']=='Final']
71
+ schedule_ccount_df = pd.DataFrame(data={'date':list(schedule_df_merge_final.index)*2,'team':list(schedule_df_merge_final.team_abv_away)+list(schedule_df_merge_final.team_abv_home)}).sort_values(by='date').reset_index(drop=True)
72
+ schedule_ccount_df['team_game'] = schedule_ccount_df.groupby('team').cumcount()+1
73
+ schedule_ccount_df.date = pd.to_datetime(schedule_ccount_df.date)
74
 
75
+ today = pd.to_datetime(datetime.now(pytz.timezone('US/Pacific')).strftime('%Y-%m-%d'))
76
+ team_schdule = schedule_df_merge[(schedule_df_merge['team_abv_home']=='EDM')|(schedule_df_merge['team_abv_away']=='EDM')]
77
+ team_schdule_live = team_schdule[team_schdule.index <= today]
78
+ team_schdule_live.head()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ team_games_df = pd.read_csv('data/team_games_all.csv',index_col=[0])
81
+ player_games_df = pd.read_csv('data/player_games.csv',index_col=[0]).sort_values(by='date').reset_index(drop=True)
82
+ team_abv_df = pd.read_csv('data/team_abv.csv')
83
+ player_games_df = player_games_df.loc[:, ~player_games_df.columns.str.contains('^Unnamed')]
84
 
85
+ team_games_df = team_games_df.merge(right=team_abv_df,left_on='team',right_on='team_name',how='left').drop(columns='team_name')
86
 
87
+ player_games_df = player_games_df.drop_duplicates(subset=['player_id','date'],keep='last').reset_index(drop=True)
88
+ player_games_df.date = pd.to_datetime(player_games_df.date)
89
+ team_games_df = team_games_df[team_games_df['date']=='Final']
90
+ schedule_df_merge_final = schedule_df_merge[schedule_df_merge['status']=='Final']
91
+ schedule_ccount_df = pd.DataFrame(data={'date':list(schedule_df_merge_final.index)*2,'team':list(schedule_df_merge_final.team_abv_away)+list(schedule_df_merge_final.team_abv_home)}).sort_values(by='date').reset_index(drop=True)
92
+ schedule_ccount_df['team_game'] = schedule_ccount_df.groupby('team').cumcount()+1
93
+ schedule_ccount_df.date = pd.to_datetime(schedule_ccount_df.date)
94
+ team_games_df['team_game'] = team_games_df.groupby('team').cumcount()+1
95
+ player_games_df = player_games_df.merge(right=schedule_ccount_df,left_on=['Team','date'],right_on=['team','date'],how='left')
96
+ player_games_df['player_game'] = player_games_df.groupby('player_id').cumcount()+1
97
 
98
+ date_range_list = pd.date_range(start=player_games_df.date.min()+timedelta(days=6),end=player_games_df.date.max())
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
+ team_abv_nst_dict = {'All':''} | team_abv_nst.set_index('team_abv')['team_name'].to_dict()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
+ position_dict = {'All':'','F':'Forwards','D':'Defense'}
104
 
 
 
 
105
 
 
106
 
107
+ stat_input_list = ['TOI', 'Goals', 'Total Assists',
108
+ 'First Assists', 'Second Assists', 'Total Points', 'Shots','PIM', 'Hits',
109
+ 'Hits Taken', 'Shots Blocked']
110
 
111
 
112
+ df_cum_stat_total = player_games_df.groupby(['player_id','Player','Position']).agg(
113
+ GP = ('GP','count'),
114
+ Total_Points = ('Total Points','sum')
115
+ ).reset_index()
116
 
117
+ df_all_sort = df_cum_stat_total.copy()
118
+ stat_pick = 'Total_Points'
119
+ count=11
120
+ not_position = ''
121
+ team = ''
122
+ df_all_sort = df_all_sort[(df_all_sort['Position']!=not_position)]
123
+ df_all_sort[stat_pick+' per game'] = df_all_sort[stat_pick]/df_all_sort['GP']
124
+ df_all_sort[stat_pick+' Rank'] = df_all_sort[stat_pick].rank(ascending=False,method='min')
125
+ df_all_sort = df_all_sort[df_all_sort[stat_pick+' Rank']<=count]
126
+ df_all_sort[stat_pick+' per game Rank'] = df_all_sort[stat_pick+' per game'].rank(ascending=False,method='min')
127
+ # #df_all_sort.sort_values(by=[stat_pick,stat_pick+' per game','Total Points'],ascending = (False, False,False))
128
+ df_all_sort_list = df_all_sort[df_all_sort[stat_pick+' Rank']<max(df_all_sort[stat_pick+' Rank'])].sort_values(by=[stat_pick,stat_pick+' per game','Total_Points'],ascending = (False, False,False))
129
+ # # df_all_sort = df_all_sort.sort_values(by=[stat_pick,stat_pick+' per game','Total Points'],ascending = (False, False,False))[(df_all_sort['Position']!=not_position)&(df_all_sort['Team']!=team)].head(count)['Player']
130
+ temp_df = df_all_sort[df_all_sort[stat_pick+' Rank']==max(df_all_sort[stat_pick+' Rank'])]#[stat_pick+' per game Rank'].rank().sort_values(ascending=True).reset_index(drop=True)[count-len(df_all_sort_list)-1]
131
+ temp_df['temp'] = temp_df[stat_pick+' per game Rank'].rank()#.sort_values(ascending=True)#.reset_index(drop=True)
132
+ temp_df = temp_df.sort_values(by='temp',ascending=True)#.reset_index(drop=True)
133
+ temp = temp_df[temp_df['temp']<=(count-len(df_all_sort_list))]
134
 
135
+ players_list = list(pd.concat([df_all_sort_list,temp]).reset_index(drop=True)['player_id'])
 
 
 
136
 
 
 
 
 
137
 
138
+ rookie_df = pd.read_csv('data/player_rookies.csv',index_col=[0])
139
+ rookie_list = rookie_df.player_id.values
140
 
 
 
 
 
141
 
142
+ skater_dict = df_cum_stat_total.sort_values(by=['Total_Points','GP'],ascending=[False,True]).drop_duplicates(subset='player_id').set_index('player_id')#.sort_values(by='Player')
143
+ #skater_dict['skater_team'] = skater_dict.Player + ' - ' + skater_dict.Team
144
+ skater_dict = skater_dict['Player'].to_dict()
145
+ # players_list = list(df_all_sort['Player'])
146
+ print(players_list)
147
 
 
 
 
 
148
 
149
+ from shiny import ui, render, App
150
+ from shiny import App, reactive, ui
151
+ from shiny.ui import h2, tags
152
+ import matplotlib.image as mpimg
153
 
154
+ # app_ui = ui.page_fluid(
 
 
 
155
 
156
+ # # ui.output_plot("plot"),
157
+ # #ui.h2('MLB Batter Launch Angle vs Exit Velocity'),
158
+ # ui.layout_sidebar(
159
+ # ui.panel_sidebar(
160
+ # ui.input_select("id", "Select Batter",batter_dict),
161
 
162
+ # ui.input_select("plot_id", "Select Plot",{'scatter':'Scatter Plot','dist':'Distribution Plot'})))
163
+ # ,
 
 
164
 
165
+ # ui.panel_main(ui.output_plot("plot",height = "750px",width="1250px")),
166
+ # #ui.download_button('test','Download'),
167
+ # )
168
+ #import shinyswatch
169
+ app_ui = ui.page_fluid(
170
+ #shinyswatch.theme.cosmo(),
171
+ ui.layout_sidebar(
172
+
173
+ # Available themes:
174
+ # cerulean, cosmo, cyborg, darkly, flatly, journal, litera, lumen, lux,
175
+ # materia, minty, morph, pulse, quartz, sandstone, simplex, sketchy, slate,
176
+ # solar, spacelab, superhero, united, vapor, yeti, zephyr
177
 
178
+ ui.panel_sidebar(
179
+ ui.input_select("id", "Select Skater (max. 10 Skaters)",skater_dict,width=1,selected=list(players_list[0:10]),selectize=True,multiple=True),
180
+ ui.input_select("stat", "Stat Input",stat_input_list,width=1,size=1,selectize=False,selected='Total Points'),
181
+ ui.input_select("team_select", "Team",team_abv_nst_dict,width=1,size=1,selectize=False,selected='All'),
182
+ ui.input_select("position_select", "Position",position_dict,width=1,size=1,selectize=False,selected='All'),
183
+ ui.input_date("date", "Date input",value = datetime.today().date() - timedelta(days=1),min='2023-10-10', max=datetime.today().date() - timedelta(days=1)),
184
+ ui.input_switch("rookie_switch", "Rookies Only"),
185
+ ui.output_table("result"),
186
+ width=3),
187
+
188
+
 
 
 
189
 
190
+ ui.panel_main(
 
 
 
 
 
 
 
 
 
191
 
192
+ ui.navset_tab(
193
+ ui.nav("Chart Races",
194
+ ui.panel_main(
195
+ ui.output_plot("plot",height = "1200px",width="1200px")),
196
+ )
197
+
198
+
199
+
200
+ ))))
201
+ # ui.row(
202
+ # ui.column(
203
+ # 3,
204
+ # ui.input_date("x", "Date input"),),
205
+ # ui.column(
206
+ # 1,
207
+ # ui.input_select("level_id", "Select Level",level_dict,width=1)),
208
+ # ui.column(
209
+ # 3,
210
+ # ui.input_select("stat_id", "Select Stat",plot_dict_small,width=1)),
211
+ # ui.column(
212
+ # 2,
213
+ # ui.input_numeric("n", "Rolling Window Size", value=50)),
214
+ # ),
215
+ # ui.output_table("result_batters")),
216
+
217
+ # ui.nav(
218
+ # "Pitchers",
219
+
220
+ # ui.row(
221
+ # ui.column(
222
+ # 3,
223
+ # ui.input_select("id_pitch", "Select Pitcher",pitcher_dict,width=1,selected=675911),
224
+ # ),
225
+ # ui.column(
226
+ # 1,
227
+ # ui.input_select("level_id_pitch", "Select Level",level_dict,width=1)),
228
+ # ui.column(
229
+ # 3,
230
+ # ui.input_select("stat_id_pitch", "Select Stat",plot_dict_small_pitch,width=1)),
231
+ # ui.column(
232
+ # 2,
233
+ # ui.input_numeric("n_pitch", "Rolling Window Size", value=50)),
234
+ # ),
235
+ # ui.output_table("result_pitchers")),
236
+ # )
237
+ # )
238
+ # )
239
+
240
+
241
+
242
+
243
+ #from urllib.request import Request, urlopen
244
 
245
+ # importing OpenCV(cv2) module
 
 
 
 
 
 
 
 
 
 
 
246
 
247
 
248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
+ def server(input, output, session):
251
 
252
 
253
+ @reactive.Effect
254
+ def _():
255
+
256
 
257
+ team_select_list = [input.team_select()]
258
+ position_select_list = [input.position_select()]
259
 
260
+ if team_select_list[0] == 'All':
261
+ team_select_list = team_abv_nst.team_abv.unique()
 
262
 
263
+ if position_select_list[0] == 'All':
264
+ position_select_list = player_games_df.Position.unique()
 
265
 
266
+ elif position_select_list[0] == 'F':
267
+ position_select_list = player_games_df[player_games_df.Position != 'D'].Position.unique()
268
 
269
+ else:
270
+ position_select_list = ['D']
271
+
272
+ print(team_select_list)
273
 
 
 
274
 
275
+ if input.rookie_switch():
276
 
277
+ df_cum_stat_total = player_games_df[(player_games_df.date <= pd.to_datetime(input.date()))
278
+ &(player_games_df.player_id.isin(rookie_list))
279
+ &(player_games_df.Team.isin(team_select_list))
280
+ &(player_games_df.Position.isin(position_select_list))].groupby(['player_id','Player','Position']).agg(
281
+ GP = ('GP','count'),
282
+ Total_Points = (f'{input.stat()}','sum')
283
+ ).reset_index()
284
+
285
+ else:
286
+ df_cum_stat_total = player_games_df[(player_games_df.date <= pd.to_datetime(input.date()))
287
+ &(player_games_df.Team.isin(team_select_list))
288
+ &(player_games_df.Position.isin(position_select_list))].groupby(['player_id','Player','Position']).agg(
289
+ GP = ('GP','count'),
290
+ Total_Points = (f'{input.stat()}','sum')
291
+ ).reset_index()
292
+
293
+ df_all_sort = df_cum_stat_total.copy()
294
+ stat_pick = 'Total_Points'
295
+ count=6
296
+ not_position = ''
297
+ team = ''
298
+ df_all_sort = df_all_sort[(df_all_sort['Position']!=not_position)]
299
+ df_all_sort[stat_pick+' per game'] = df_all_sort[stat_pick]/df_all_sort['GP']
300
+ df_all_sort[stat_pick+' Rank'] = df_all_sort[stat_pick].rank(ascending=False,method='min')
301
+ df_all_sort = df_all_sort[df_all_sort[stat_pick+' Rank']<=count]
302
+ df_all_sort[stat_pick+' per game Rank'] = df_all_sort[stat_pick+' per game'].rank(ascending=False,method='min')
303
+ # #df_all_sort.sort_values(by=[stat_pick,stat_pick+' per game','Total Points'],ascending = (False, False,False))
304
+ df_all_sort_list = df_all_sort[df_all_sort[stat_pick+' Rank']<max(df_all_sort[stat_pick+' Rank'])].sort_values(by=[stat_pick,stat_pick+' per game','Total_Points'],ascending = (False, False,False))
305
+ # # df_all_sort = df_all_sort.sort_values(by=[stat_pick,stat_pick+' per game','Total Points'],ascending = (False, False,False))[(df_all_sort['Position']!=not_position)&(df_all_sort['Team']!=team)].head(count)['Player']
306
+ temp_df = df_all_sort[df_all_sort[stat_pick+' Rank']==max(df_all_sort[stat_pick+' Rank'])]#[stat_pick+' per game Rank'].rank().sort_values(ascending=True).reset_index(drop=True)[count-len(df_all_sort_list)-1]
307
+ temp_df['temp'] = temp_df[stat_pick+' per game Rank'].rank()#.sort_values(ascending=True)#.reset_index(drop=True)
308
+ temp_df = temp_df.sort_values(by='temp',ascending=True)#.reset_index(drop=True)
309
+ temp = temp_df[temp_df['temp']<=(count-len(df_all_sort_list))]
310
+
311
+ players_list_new = list(pd.concat([df_all_sort_list,temp]).reset_index(drop=True)['player_id'])
312
+
313
+
314
+ skater_dict = df_cum_stat_total.sort_values(by=['Total_Points','GP'],ascending=[False,True]).drop_duplicates(subset='player_id').set_index('player_id')#.sort_values(by='Player')
315
+ #skater_dict['skater_team'] = skater_dict.Player + ' - ' + skater_dict.Team
316
+ skater_dict = skater_dict['Player'].to_dict()
317
+ # players_list = list(df_all_sort['Player'])
318
+
319
+ ui.update_select(
320
+ "id",
321
+ label="Select Skater (max. 10 Skaters)",
322
+ choices=skater_dict,
323
+ selected=list(players_list_new[0:10]))
324
+
325
 
326
+ @output
327
+ @render.table
328
+ def result():
329
+ if input.rookie_switch():
330
+
331
+ return player_games_df[(player_games_df.date <= pd.to_datetime(input.date()))&(player_games_df.player_id.isin(rookie_list))].groupby(['player_id','Player','Position']).agg(
332
+ GP = ('GP','count'),
333
+ Stat = (f'{input.stat()}','sum')
334
+ ).reset_index().sort_values(by=['Stat','GP'],ascending=[False,True]).reset_index(drop=True)
335
+
336
+ else:
337
+ return player_games_df[player_games_df.date <= pd.to_datetime(input.date())].groupby(['player_id','Player','Position']).agg(
338
+ GP = ('GP','count'),
339
+ Stat = (f'{input.stat()}','sum')
340
+ ).reset_index().sort_values(by=['Stat','GP'],ascending=[False,True]).reset_index(drop=True)
341
 
342
 
343
+ @output
344
+ @render.plot(alt="A histogram")
345
+ def plot():
346
 
347
+ rookie = ''
348
+ if input.rookie_switch():
349
+ rookie = 'Rookie '
350
+
351
+ i = 0
352
+ #rookie = ''
353
+ current_season = '2023'
354
+ start_season = '2024'
355
 
356
+ # player_lookup_list = ['Connor McDavid','David Pastrnak','Nathan MacKinnon']
 
357
 
358
 
359
+ type(input.id())
360
+ print(input.id())
361
+ player_lookup_list = list(input.id())[0:10]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
 
363
+ stat = input.stat()
364
+ sns.set_theme(style="whitegrid", palette="pastel")
365
+ #print(type([input.date()))
366
+ date_range_list = [pd.to_datetime(input.date())]
367
+ for k in range(len(date_range_list)):
368
+ print(date_range_list[k])
369
+ stat = input.stat()
370
+ team_schedule_url_merge = []
371
+ max_games_player = []
372
+ max_games_team = []
373
+ max_stat = []
374
+ per_game = False
375
+ for i in range(0,len(player_lookup_list)):
376
+ team_schedule_url_merge.append(player_games_df[(player_games_df.player_id == int(player_lookup_list[i]))&(date_range_list[k] >= player_games_df.date)].reset_index(drop=True))
377
+ #print('touble',i, player_lookup_list[i],len(player_games_df[(player_games_df.player_id == player_lookup_list[i])]))
378
+ team_schedule_url_merge[i].index = team_schedule_url_merge[i].team_game
379
+ team_schedule_url_merge[i] = team_schedule_url_merge[i].reindex(np.arange(team_schedule_url_merge[i].team_game.min(), team_schedule_url_merge[i].team_game.max() + 1)).reset_index(drop=True)
380
+ #team_schedule_url_merge[0]['team_game'] = team_schedule_url_merge[0]['index']
381
+ #team_schedule_url_merge[0]['player_game'] =
382
+ #schedule_ccount_df[schedule_ccount_df['team'].isin(team_schedule_url_merge[0].Team.unique())].merge(right=team_schedule_url_merge[0],left_on=['date','team'],right_on=['date','Team'],how='left')
383
 
384
+ team_schedule_url_merge[i]['stat'] = team_schedule_url_merge[i][stat].cumsum()
385
 
 
 
 
386
 
387
+ #team_schedule_url_merge[i]['stat'] = team_schedule_url_merge[i][stat_pick]
388
+ team_schedule_url_merge[i] = team_schedule_url_merge[i].append(team_schedule_url_merge[i]).sort_index()
389
+ team_schedule_url_merge[i] = team_schedule_url_merge[i].append(team_schedule_url_merge[i].iloc[0]).sort_index().reset_index(drop=True)
390
 
391
+ team_schedule_url_merge[i]['team_game'][0] = 0
392
+ team_schedule_url_merge[i]['player_game'][0] = 0
393
+ team_schedule_url_merge[i]['stat'][0] = 0
394
 
395
+ for j in range(1,len(team_schedule_url_merge[i]),2):
396
+ team_schedule_url_merge[i]['player_game'][j] = team_schedule_url_merge[i]['player_game'][j]-1
397
+ team_schedule_url_merge[i]['team_game'][j] = team_schedule_url_merge[i]['team_game'][j]-1
398
+ team_schedule_url_merge[i]['stat'][j] = team_schedule_url_merge[i]['stat'][j] - team_schedule_url_merge[i][stat][j]
399
 
400
+ if len(team_schedule_url_merge[i]) >3:
401
+ if pd.isna(team_schedule_url_merge[i].iloc[3]['player_game']) and pd.isna(team_schedule_url_merge[i].iloc[1]['player_game']) == True:
402
+ team_schedule_url_merge[i]['player_game'][2] = np.nan
403
+ team_schedule_url_merge[i]['stat'][2] = np.nan
404
 
405
+ if len(team_schedule_url_merge[i]) >3:
406
+ if pd.isna(team_schedule_url_merge[i].iloc[len(team_schedule_url_merge[i])-1]['player_game']) == True:
407
+ team_schedule_url_merge[i]['stat'][len(team_schedule_url_merge[i])-1] = np.nanmax(team_schedule_url_merge[i]['stat'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
 
409
+ if not (team_schedule_url_merge[i]['team_game'].values[1] == team_schedule_url_merge[i]['player_game'].values[0]):
410
+ team_schedule_url_merge[i].loc[0,'team_game'] = np.nan
 
 
 
 
 
 
 
 
 
 
411
 
412
 
413
+ max_games_player.append(np.around(np.nanmax(team_schedule_url_merge[i]['player_game'])))
414
+ max_games_team.append(np.around(np.nanmax(team_schedule_url_merge[i]['team_game'])))
415
+ max_stat.append((np.around(np.nanmax(team_schedule_url_merge[i]['stat']))))
 
 
416
 
 
 
 
 
 
 
 
 
 
 
 
417
 
 
 
 
418
 
 
419
 
 
420
 
 
421
 
422
+ fig, ax = plt.subplots(figsize=(15,15))
423
+ cgfont = {'fontname':'Century Gothic'}
424
+ font = font_manager.FontProperties(family='Century Gothic',
425
+ style='normal', size=18)
426
 
 
 
 
 
 
 
 
 
427
 
428
+ ax.axhline(0,color='black',linestyle ="--",linewidth=2,alpha=1.0,label='Missed Games')
429
+ ax.axhline(0,color='black',linestyle ="-",linewidth=2,alpha=1.0)
 
 
 
 
 
430
 
 
 
 
431
 
432
+ if 'Total' in stat:
433
+ stat = stat.replace('Total ',"")
 
434
 
435
 
436
+ colour_scheme = ['#648FFF','#785EF0','#DC267F','#FE6100','#FFB000','#FAEF3B','#861318','#2ED3BC','#341BBF','#B37E2C']
 
 
437
 
438
+ for i in range(len(team_schedule_url_merge)):
439
+ sns.lineplot(team_schedule_url_merge[i].reset_index()['team_game'],team_schedule_url_merge[i].reset_index()['stat'],linewidth=3-i*.2,color=colour_scheme[i])
440
+ plt.plot(team_schedule_url_merge[i]['team_game'],team_schedule_url_merge[i]['stat'],color=ax.lines[i*2+2].get_color(),label=str(i+1)+'. '+team_schedule_url_merge[i]['Player'][0]+', '+str(int(max_stat[i]))+' '+stat+' in '+str(int(max(team_schedule_url_merge[i]['player_game'])))+' Games',linewidth=6)
441
+ ax.lines[i*2+2].set_linestyle("--")
442
 
443
 
 
 
 
444
 
445
+ fig.set_facecolor('#ffffff')
446
+ ax.set(xlim=(0,max([team_schedule_url_merge[x].team_game.max() for x in range(len(team_schedule_url_merge))])))
447
+ ax.set(ylim=(0,max([team_schedule_url_merge[x].stat.max() for x in range(len(team_schedule_url_merge))])))
448
 
449
+ ax.legend_.remove()
450
 
 
451
 
 
452
 
453
+ if per_game == False:
454
+ fig.suptitle(rookie+stat+' Race, All Situations',y=.99,fontsize=36,color='black',**cgfont)
455
+ ax.set_ylabel(stat,fontsize=20,color='black',**cgfont)
456
+ # else:
457
+ # fig.suptitle(stat+' Per Game, All Situations',y=.99,fontsize=48,color='black',**cgfont)
458
+ # ax.set_ylabel(stat+"/GP",fontsize=20,color='black',**cgfont)
459
 
460
 
461
 
462
+ ax.set_title(str(current_season)[0:4]+'-'+str(start_season)[-4:]+' Season',y=1.01,fontsize=24,color='black',**cgfont)
463
+ ax.set_xlabel('Team Game',fontsize=20,color='black',**cgfont)
464
+ ax.tick_params(axis="x", labelsize=24,colors='black')
465
+ ax.set_facecolor('#ffffff')
466
+ ax.xaxis.set_major_locator(MaxNLocator(integer=True))
467
+ ax.tick_params(axis="y", labelsize=24,colors='black')
468
+ ax.yaxis.set_major_locator(MaxNLocator(integer=True))
469
 
470
+ fig.text(x=0.025,y=0.01,s="Created By: @TJStats",color='black', fontsize=20, horizontalalignment='left',**cgfont)
471
+ fig.text(x=0.975,y=0.01,s="Data: Natural Stat Trick",color='black', fontsize=20, horizontalalignment='right',**cgfont)
472
+ fig.text(x=.975,y=0.91,s='Date: '+input.date().strftime('%B %d, %Y'),color='black', fontsize=18, horizontalalignment='right',**cgfont)
473
 
474
+ fig.legend(prop=font,bbox_to_anchor=(0.07, 0.88),loc='upper left',framealpha=1,frameon=True)
475
+ plt.tight_layout()
476
+ #fig.savefig('gif_race/'+stat+rookie+str(date_range_list[k].date())+'.png', facecolor=fig.get_facecolor(), edgecolor='none',bbox_inches='tight',dpi=100)
477
+ #plt.close()
478
+ #fig.legend(prop=font,loc='best',framealpha=1,frameon=True)
479
 
480
+ app = App(app_ui, server)