jldbc / pybaseball

Pull current and historical baseball statistics using Python (Statcast, Baseball Reference, FanGraphs)
MIT License
1.18k stars 322 forks source link

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

Closed JackMentch closed 1 year ago

JackMentch commented 1 year ago

Description

I added the functionplot_teams( ) to pybaseball/plotting.py so that you can easily plot all teams against any two variables in a dataframe

Rationale

The two functions team_pitching( ) and team_batting( ) both return stats for all 30 teams.

For example,

Running team_batting(2023) would return a dataframe similar to this...

teamIDfg Season Team ... HR ... BB ...
12 2023 TBR ... 44 ... 102 ...
17 2023 LAD ... 30 ... 83 ...
30 2023 CHC ... 32 ... 77 ...
34 2023 TEX ... 23 ... 89 ...
... ... ... ... ... ... ... ...

Using my function plot_teams( ) you could plot each team's total HRs and BBs for 2023

from pybaseball import plot_teams, team_batting

data = team_batting(2023)

plot_teams(data, "HR", "BB")

This would generate the following plot:

plot_teams_example

Limitations:

JackMentch commented 1 year ago

Hey Brayan, I'm cool with whatever you think is best! Thank you for taking a look as well!

schorrm commented 1 year ago

This is really nice! Sorry for the delay

schorrm commented 1 year ago

I would suggest that we either throw an error when missing that is more informative, or can we fall back to text for missing team codes?

JackMentch commented 1 year ago

Hi Moshe,

I appreciate your response! Just made a new commit that plots text (team abbreviation) instead of the logo if it cannot find a corresponding image. Let me know if this looks fine:

Screenshot 2023-05-04 at 11 01 57 AM
schorrm commented 1 year ago

Yeah, this is a great fallback!