nesticot commited on
Commit
2876a6e
·
verified ·
1 Parent(s): 54d2aec

Update pitch_summary_functions.py

Browse files
Files changed (1) hide show
  1. pitch_summary_functions.py +44 -43
pitch_summary_functions.py CHANGED
@@ -22,7 +22,7 @@ colour_palette = ['#FFB000','#648FFF','#785EF0',
22
  '#DC267F','#FE6100','#3D1EB2','#894D80','#16AA02','#B5592B','#A3C1ED']
23
  season_start = '2024-03-20'
24
  season_end = '2024-09-29'
25
- season_fg=2024
26
  chad_fg = requests.get(f'https://www.fangraphs.com/api/leaders/major-league/data?age=&pos=all&stats=pit&lg=all&qual=0&season={season_fg}&season={season_fg}&month=1000&season1={season_fg}&ind=0&pageitems=2000000000&pagenum=1&ind=0&rost=0&players=&type=36&postseason=&sortdir=default&sortstat=sp_pitching').json()
27
  cmap_sum = matplotlib.colors.LinearSegmentedColormap.from_list("", ['#648FFF','#FFFFFF','#FFB000',])
28
 
@@ -228,7 +228,6 @@ def percentile(n):
228
  return percentile_
229
 
230
  ### TJ STUFF+ DF CLEAN ###
231
-
232
  def df_clean(df):
233
  df_copy = df.copy()
234
  df_copy.loc[df_copy['pitcher_hand'] == 'L','hb'] *= -1
@@ -237,7 +236,7 @@ def df_clean(df):
237
 
238
  df_copy['pitch_l'] = [1 if x == 'L' else 0 for x in df_copy['pitcher_hand']]
239
  df_copy['bat_l'] = [1 if x == 'L' else 0 for x in df_copy['batter_hand']]
240
- #df_copy = df_copy[~df_copy.pitch_type.isin(["EP", "PO", "KN", "CS", "SC", "FA"])].reset_index(drop=True)
241
  df_copy['pitch_type'] = df_copy['pitch_type'].replace({'FT':'SI'})
242
 
243
  # df_copy['des_new'] = df_copy['play_description'].map(des_dict)
@@ -315,7 +314,6 @@ def df_clean(df):
315
  df_copy = df_copy.dropna(subset=['pitch_type']).fillna(0)
316
  return df_copy
317
 
318
-
319
  ### PITCH COLOURS ###
320
  pitch_colours = {
321
  'Four-Seam Fastball':'#FF007D',#BC136F
@@ -337,7 +335,7 @@ pitch_colours = {
337
  'Slow Curve':'#274BFC',
338
  'Eephus':'#648FFF',
339
 
340
- 'Knuckle Ball':'#867A08',
341
 
342
  'Pitch Out':'#472C30',
343
  'Other':'#9C8975',
@@ -659,10 +657,19 @@ def tj_stuff_roling(df,
659
  ax.xaxis.set_major_locator(MaxNLocator(integer=True))
660
 
661
  ### TJ STUFF+ ROLLING ###
662
- def tj_velocity_roling(df,
663
  window,
664
  ax):
665
  ## Velocity Plot
 
 
 
 
 
 
 
 
 
666
  sorted_value_counts = df['pitch_type'].value_counts().sort_values(ascending=False)
667
 
668
  # Get the list of items ordered from most to least frequent
@@ -670,19 +677,39 @@ def tj_velocity_roling(df,
670
 
671
 
672
  for i in items_in_order:
673
- if max(df[df['pitch_type']==i]['pitch_type_count_each']) >= window:
674
- sns.lineplot(x=range(1,max(df[df['pitch_type']==i]['pitch_type_count_each'])+1),
675
- y=df[df['pitch_type']==i]['start_speed'].rolling(window).sum()/window,
676
- color=pitch_colours[df[df['pitch_type']==i]['pitch_description'].values[0]],
677
- ax=ax,linewidth=3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
678
 
679
  # Adjust x-axis limits to start from 1
680
- ax.set_xlim(window,max(df['pitch_type_count_each']))
681
- ax.set_ylim(65,105)
682
  #ax.get_legend().remove()
683
- ax.set_xlabel('Pitches', fontdict=font_properties_axes)
684
- ax.set_ylabel('Velocity (mph)', fontdict=font_properties_axes)
685
- ax.set_title(f"{window} Pitch Rolling Velocity",fontdict=font_properties_titles)
686
  # ax.axis('square')
687
  # ax.set_xlim(left=1)
688
  ax.xaxis.set_major_locator(MaxNLocator(integer=True))
@@ -1091,30 +1118,4 @@ def location_plot(df,ax,hand):
1091
  if len(pitch_location_group['px'])>0:
1092
  ax.get_legend().remove()
1093
  ax.grid(False)
1094
- ax.set_title(f"Pitch Locations vs {hand}HH\n{pitch_location_group['pitches'].sum()} Pitches",fontdict=font_properties_titles)
1095
-
1096
-
1097
-
1098
-
1099
- ####LHH
1100
- def location_pitch_plot(df,ax,hand):
1101
- #label_labels = df.sort_values(by=['prop','pitch_type'],ascending=[False,True]).pitch_description.unique()
1102
- ## Location Plot
1103
- df_pitch= df[df['batter_hand']==hand]
1104
- sns.scatterplot(ax=ax,x=df_pitch['px'],
1105
- y=df_pitch['pz'],
1106
- hue=df_pitch['pitch_description'],
1107
- palette=pitch_colours,ec='black',
1108
- s=175,
1109
- linewidth=2,
1110
- zorder=2)
1111
-
1112
- ax.axis('square')
1113
- draw_line(ax,alpha_spot=0.75,catcher_p=False)
1114
- ax.axis('off')
1115
- ax.set_xlim((-2.75,2.75))
1116
- ax.set_ylim((-0.5,5))
1117
- if len(df_pitch['px'])>0:
1118
- ax.get_legend().remove()
1119
- ax.grid(False)
1120
- ax.set_title(f"Pitch Locations vs {hand}HH\n{df_pitch['pitches'].sum()} Pitches",fontdict=font_properties_titles)
 
22
  '#DC267F','#FE6100','#3D1EB2','#894D80','#16AA02','#B5592B','#A3C1ED']
23
  season_start = '2024-03-20'
24
  season_end = '2024-09-29'
25
+ season_fg=2023
26
  chad_fg = requests.get(f'https://www.fangraphs.com/api/leaders/major-league/data?age=&pos=all&stats=pit&lg=all&qual=0&season={season_fg}&season={season_fg}&month=1000&season1={season_fg}&ind=0&pageitems=2000000000&pagenum=1&ind=0&rost=0&players=&type=36&postseason=&sortdir=default&sortstat=sp_pitching').json()
27
  cmap_sum = matplotlib.colors.LinearSegmentedColormap.from_list("", ['#648FFF','#FFFFFF','#FFB000',])
28
 
 
228
  return percentile_
229
 
230
  ### TJ STUFF+ DF CLEAN ###
 
231
  def df_clean(df):
232
  df_copy = df.copy()
233
  df_copy.loc[df_copy['pitcher_hand'] == 'L','hb'] *= -1
 
236
 
237
  df_copy['pitch_l'] = [1 if x == 'L' else 0 for x in df_copy['pitcher_hand']]
238
  df_copy['bat_l'] = [1 if x == 'L' else 0 for x in df_copy['batter_hand']]
239
+ df_copy = df_copy[~df_copy.pitch_type.isin(["PO"])].reset_index(drop=True)
240
  df_copy['pitch_type'] = df_copy['pitch_type'].replace({'FT':'SI'})
241
 
242
  # df_copy['des_new'] = df_copy['play_description'].map(des_dict)
 
314
  df_copy = df_copy.dropna(subset=['pitch_type']).fillna(0)
315
  return df_copy
316
 
 
317
  ### PITCH COLOURS ###
318
  pitch_colours = {
319
  'Four-Seam Fastball':'#FF007D',#BC136F
 
335
  'Slow Curve':'#274BFC',
336
  'Eephus':'#648FFF',
337
 
338
+ 'Knuckleball':'#867A08',
339
 
340
  'Pitch Out':'#472C30',
341
  'Other':'#9C8975',
 
657
  ax.xaxis.set_major_locator(MaxNLocator(integer=True))
658
 
659
  ### TJ STUFF+ ROLLING ###
660
+ def tj_stuff_roling_game(df,
661
  window,
662
  ax):
663
  ## Velocity Plot
664
+
665
+ # Create a dictionary to map each unique date to a unique number
666
+ date_to_number = {date: i+1 for i, date in enumerate(df['game_id'].unique())}
667
+
668
+
669
+ df['start_number'] = df['game_id'].map(date_to_number)
670
+
671
+ plot_game_roll = df.groupby(['start_number','game_id','game_date','pitch_type','pitch_description'])['tj_stuff_plus'].mean().reset_index()
672
+
673
  sorted_value_counts = df['pitch_type'].value_counts().sort_values(ascending=False)
674
 
675
  # Get the list of items ordered from most to least frequent
 
677
 
678
 
679
  for i in items_in_order:
680
+ df_item = plot_game_roll[plot_game_roll['pitch_type']==i].set_index(['start_number'])
681
+ df_item = df_item.reindex(range(df['start_number'].min(),df['start_number'].max()+1)).reset_index()
682
+ #df_item['start_number'] = df_item['start_number']
683
+ #df_item['tj_stuff_plus'] = df_item['tj_stuff_plus'].
684
+
685
+ df_item['tj_stuff_plus'] = df_item['tj_stuff_plus'].ffill()
686
+ df_item['pitch_type'] = df_item['pitch_type'].ffill()
687
+ df_item['pitch_description'] = df_item['pitch_description'].ffill()
688
+ print(df_item)
689
+ sns.lineplot(x=range(1,max(df['start_number']+1)),
690
+ y=df_item[df_item['pitch_type']==i]['tj_stuff_plus'].rolling(window).sum()/window,
691
+ color=pitch_colours[df[df['pitch_type']==i]['pitch_description'].values[0]],
692
+ ax=ax,linewidth=3)
693
+
694
+ # for d in df_item[df_item['game_id'].isnull()]['start_number']:
695
+ # df_item['tj_stuff_plus'] = df_item['tj_stuff_plus'].bfill()
696
+ # df_item['pitch_type'] = df_item['pitch_type'].bfill()
697
+ # df_item['pitch_description'] = df_item['pitch_description'].bfill()
698
+ # print(d)
699
+ # sns.scatterplot(
700
+ # x=[d+1],
701
+ # y=(df_item[df_item['pitch_type']==i]['tj_stuff_plus'].rolling(window).sum()/window)[d],
702
+ # edgecolor=pitch_colours[df[df['pitch_type']==i]['pitch_description'].values[0]],color='white',
703
+ # s=50,zorder=30,
704
+ # ax=ax)
705
 
706
  # Adjust x-axis limits to start from 1
707
+ ax.set_xlim(window,max(df['start_number']))
708
+ ax.set_ylim(70,130)
709
  #ax.get_legend().remove()
710
+ ax.set_xlabel('Games', fontdict=font_properties_axes)
711
+ ax.set_ylabel('tjStuff+', fontdict=font_properties_axes)
712
+ ax.set_title(f"{window} Game Rolling tjStuff+",fontdict=font_properties_titles)
713
  # ax.axis('square')
714
  # ax.set_xlim(left=1)
715
  ax.xaxis.set_major_locator(MaxNLocator(integer=True))
 
1118
  if len(pitch_location_group['px'])>0:
1119
  ax.get_legend().remove()
1120
  ax.grid(False)
1121
+ ax.set_title(f"Pitch Locations vs {hand}HB\n{pitch_location_group['pitches'].sum()} Pitches",fontdict=font_properties_titles)