Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added plot_teams function to plot all team's stats for a season(s) #351

Merged
merged 3 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/images/logos/ARI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/ATL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/BAL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/BOS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/CHC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/CHW.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/CIN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/CLE.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/COL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/DET.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/HOU.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/KCR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/LAA.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/LAD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/MIA.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/MIL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/MIN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/NYM.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/NYY.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/OAK.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/PHI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/PIT.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/SDP.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/SEA.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/SFG.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logos/STL.png
Binary file added docs/images/logos/TBR.png
Binary file added docs/images/logos/TEX.png
Binary file added docs/images/logos/TOR.png
Binary file added docs/images/logos/WSN.png
Binary file added docs/images/plot_teams_example.png
25 changes: 25 additions & 0 deletions docs/plotting.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,28 @@ plt.show()

![](images/plot_bb_profile_example.png)

---
`plot_team(data, x_axis, y_axis, title=None)`

## Arguments

`data`: Fangraphs team data from either team_batting or team_pitching

`x_axis`: name of stat to be plotted as the x_axis of the chart

`y_axis`: name of stat to be plotted as the y_axis of the chart

`title`: title for your chart

# Example

```python
from pybaseball import plot_teams, team_batting

data = team_batting(2023)

plot_teams(data, "HR", "BB")
```
![Plot Teams_2023_BB/HR](images/plot_teams_example.png)

`
1 change: 1 addition & 0 deletions pybaseball/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
from .team_game_logs import team_game_logs
from .plotting import plot_stadium
from .plotting import spraychart
from .plotting import plot_teams
from .datasources.fangraphs import (fg_batting_data, fg_pitching_data, fg_team_batting_data, fg_team_fielding_data,
fg_team_pitching_data)
from .split_stats import get_splits
Expand Down
61 changes: 61 additions & 0 deletions pybaseball/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from matplotlib import axes
from matplotlib import patches
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import matplotlib.path
import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -187,3 +188,63 @@ def plot_bb_profile(df: pd.DataFrame, parameter: Optional[str] = "launch_angle")
bins = np.arange(df_skimmed[parameter].min(), df_skimmed[parameter].max(), 2)
plt.hist(df_skimmed[parameter], bins=bins, alpha=0.5, label=bb_type.replace("_", " ").capitalize())
plt.tick_params(labelsize=12)


def plot_teams(data: pd.DataFrame, x_axis: str, y_axis: str, title: Optional[str] = None) -> None:
"""Plots a scatter plot with each MLB team

Args:
data: (pandas.DataFrame)
pandas.DataFrame of Fangraphs team data (retrieved through team_batting or team_pitching)
x_axis: (str)
Stat name to be plotted as the x_axis of the chart
y_axis: (str)
Stat name to be plotted as the y_axis of the chart
title: (str), default = None
Optional: Title of the plot
"""

data = data[['Team', x_axis, y_axis]]

fig, ax = plt.subplots()

for index, row in data.iterrows():

# Get path of team logo
path = Path(CUR_PATH, '../docs/images/logos', f"{row['Team']}.png")

# Read in the image from the folder location
img = OffsetImage(plt.imread(path, format="png"), zoom=.3)

# Convert the image into a plottable object
ab = AnnotationBbox(img, (float(row[x_axis]), float(row[y_axis])), frameon=False)

# Plot the MLB logo
ax.add_artist(ab)

# add some spacing to the x axis and y axis so the logos stay within the bounds of the chart
x_axis_spacing = (data[x_axis].max() - data[x_axis].min()) * .1
y_axis_spacing = (data[y_axis].max() - data[y_axis].min()) * .1

# set the axis with the spacing from above
plt.xlim([data[x_axis].min() - x_axis_spacing, data[x_axis].max() + x_axis_spacing])
plt.ylim([data[y_axis].min() - y_axis_spacing, data[y_axis].max() + y_axis_spacing])

# Plot league average for y axis variable
plt.axhline(y=data[y_axis].mean(), color='k', linestyle='-')
plt.text(data[x_axis].min() - x_axis_spacing * .9, data[y_axis].mean() + y_axis_spacing * .1, 'league average')

# Plot league average for x axis variable
plt.axvline(x=data[x_axis].mean(), color='k', linestyle='-')
plt.text(data[x_axis].mean() + x_axis_spacing * .05, data[y_axis].min() - y_axis_spacing * .8, 'league average',
rotation=90)

# Add title and labels
if not title:
title = f'Plot of Team {x_axis}/Team {y_axis}'

plt.title(title)
plt.xlabel(x_axis)
plt.ylabel(y_axis)

plt.show()