nesticot commited on
Commit
cd18a04
·
verified ·
1 Parent(s): 5028997

Update functions/PitchPlotFunctions.py

Browse files
Files changed (1) hide show
  1. functions/PitchPlotFunctions.py +14 -13
functions/PitchPlotFunctions.py CHANGED
@@ -365,22 +365,22 @@ class PitchPlotFunctions:
365
  dict_colour, dict_pitch = self.pitch_colours()
366
  custom_theme, colour_palette = self.sns_custom_theme()
367
 
368
- # Draw comparison ellipses first (if comparison data provided) - dashed lines, no fill
369
  if compare_df is not None and len(compare_df) > 0:
370
  compare_labels = compare_df['pitch_type'].unique()
371
  for label in compare_labels:
372
  subset = compare_df.filter(pl.col('pitch_type') == label)
373
  if len(subset) > 4:
374
  try:
375
- # Draw comparison ellipses with dashed lines and lighter color
376
  self.confidence_ellipse(
377
  subset['hb'] * 1,
378
  subset['ivb'],
379
  ax=ax,
380
  edgecolor=dict_colour.get(label, '#888888'),
381
  n_std=2,
382
- facecolor='none', # No fill for comparison
383
- alpha=0.8,
384
  linestyle='--',
385
  linewidth=2.5
386
  )
@@ -585,7 +585,7 @@ class PitchPlotFunctions:
585
  )
586
  return df
587
 
588
- def final_plot(self, df: pl.DataFrame, pitcher_id: str, plot_picker: str, sport_id: int, game_type: list = ['R'], compare_df: pl.DataFrame = None):
589
  """
590
  Creates a final plot with player headshot, bio, logo, and pitch movement plots.
591
 
@@ -595,6 +595,7 @@ class PitchPlotFunctions:
595
  plot_picker (str): The type of plot to create ('short_form_movement', 'long_form_movement', 'release_point').
596
  sport_id (int): The sport ID to determine the plot title.
597
  compare_df (pl.DataFrame): Optional DataFrame for comparison data (draws only ellipses).
 
598
  """
599
  # Set the theme for seaborn plots
600
  sns.set_theme(style="whitegrid", rc=self.sns_custom_theme()[0])
@@ -648,18 +649,18 @@ class PitchPlotFunctions:
648
  # Create custom legend handles with circles
649
  legend_handles = [mlines.Line2D([], [], color=color, marker='o', linestyle='None', markersize=5, label=label) for color, label in zip(ordered_colors, items_in_order)]
650
 
651
- # Add comparison indicator to legend if comparison data is present
652
- if compare_df is not None and len(compare_df) > 0:
653
- # Add a dashed line legend entry to indicate comparison ellipses
654
- compare_handle = mlines.Line2D([], [], color='gray', linestyle='--', linewidth=2, label='Comparison (dashed ellipse)')
655
- legend_handles.append(compare_handle)
656
-
657
  # Add legend to ax_legend
658
  ncols = min(len(legend_handles), 6)
659
  if len(items_in_order) <= 5:
660
- ax_legend.legend(handles=legend_handles, bbox_to_anchor=(0.1, 0, 0.8, 0.7), ncol=ncols, fancybox=True, loc='center', fontsize=10, framealpha=1.0, markerscale=2, prop={'size': 10})
661
  else:
662
- ax_legend.legend(handles=legend_handles, bbox_to_anchor=(0.1, 0, 0.8, 0.7), ncol=ncols, fancybox=True, loc='center', fontsize=10, framealpha=1.0, markerscale=2, prop={'size': 10})
 
 
 
 
 
 
663
 
664
  # Add footer text
665
  ax_footer.text(x=0.075, y=0, s='By: Thomas Nestico\n @TJStats', fontname='Calibri', ha='left', fontsize=12, va='bottom')
 
365
  dict_colour, dict_pitch = self.pitch_colours()
366
  custom_theme, colour_palette = self.sns_custom_theme()
367
 
368
+ # Draw comparison ellipses first (if comparison data provided) - dashed lines with fill
369
  if compare_df is not None and len(compare_df) > 0:
370
  compare_labels = compare_df['pitch_type'].unique()
371
  for label in compare_labels:
372
  subset = compare_df.filter(pl.col('pitch_type') == label)
373
  if len(subset) > 4:
374
  try:
375
+ # Draw comparison ellipses with dashed lines and filled color
376
  self.confidence_ellipse(
377
  subset['hb'] * 1,
378
  subset['ivb'],
379
  ax=ax,
380
  edgecolor=dict_colour.get(label, '#888888'),
381
  n_std=2,
382
+ facecolor=dict_colour.get(label, '#888888'), # Fill with alpha 0.2
383
+ alpha=0.2,
384
  linestyle='--',
385
  linewidth=2.5
386
  )
 
585
  )
586
  return df
587
 
588
+ def final_plot(self, df: pl.DataFrame, pitcher_id: str, plot_picker: str, sport_id: int, game_type: list = ['R'], compare_df: pl.DataFrame = None, compare_year: int = None):
589
  """
590
  Creates a final plot with player headshot, bio, logo, and pitch movement plots.
591
 
 
595
  plot_picker (str): The type of plot to create ('short_form_movement', 'long_form_movement', 'release_point').
596
  sport_id (int): The sport ID to determine the plot title.
597
  compare_df (pl.DataFrame): Optional DataFrame for comparison data (draws only ellipses).
598
+ compare_year (int): The year being compared to (for display in the note).
599
  """
600
  # Set the theme for seaborn plots
601
  sns.set_theme(style="whitegrid", rc=self.sns_custom_theme()[0])
 
649
  # Create custom legend handles with circles
650
  legend_handles = [mlines.Line2D([], [], color=color, marker='o', linestyle='None', markersize=5, label=label) for color, label in zip(ordered_colors, items_in_order)]
651
 
 
 
 
 
 
 
652
  # Add legend to ax_legend
653
  ncols = min(len(legend_handles), 6)
654
  if len(items_in_order) <= 5:
655
+ ax_legend.legend(handles=legend_handles, bbox_to_anchor=(0.1, 0.3, 0.8, 0.7), ncol=ncols, fancybox=True, loc='center', fontsize=10, framealpha=1.0, markerscale=2, prop={'size': 10})
656
  else:
657
+ ax_legend.legend(handles=legend_handles, bbox_to_anchor=(0.1, 0.3, 0.8, 0.7), ncol=ncols, fancybox=True, loc='center', fontsize=10, framealpha=1.0, markerscale=2, prop={'size': 10})
658
+
659
+ # Add comparison note below the legend if comparison data is present
660
+ if compare_df is not None and len(compare_df) > 0 and compare_year is not None:
661
+ ax_legend.text(0.5, -0.1, f'Dashed ellipses show comparison to {compare_year}',
662
+ ha='center', va='top', fontsize=11, fontstyle='italic',
663
+ transform=ax_legend.transAxes)
664
 
665
  # Add footer text
666
  ax_footer.text(x=0.075, y=0, s='By: Thomas Nestico\n @TJStats', fontname='Calibri', ha='left', fontsize=12, va='bottom')