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

Conversation

JackMentch
Copy link
Contributor

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:

  • Only works with dataframes generated by team_batting and team_pitching
  • Adding the 30 .png files for each logo adds about 0.45 MB of storage (not sure if this is too much)
  • Only added logos for the current 30 MLB teams (if you pass a dataframe containing the Montreal Expos to plot_teams( ) it will crash since the logo does not exist)
  • The function only accepts 1 dataframe. If you wanted to plot HRs from a team_batting dataframe and ERA from a team_pitching dataframe, you would have to join the two separate dataframes before passing it to plot_teams( )

Copy link
Contributor

@BrayanMnz BrayanMnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice feature!

The only thing is - why not adding a check for the year being passed?

If the year is < 2004 let's just maybe drop it (Expos) from the dataframe and print this behavior to the user or let's just add their logo so we can avoid this kind of crashing.

I'm happy to do it as an enhancement of your PR after the maintainers review and merge this.

@JackMentch
Copy link
Contributor Author

JackMentch commented Apr 30, 2023

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

@schorrm
Copy link
Collaborator

schorrm commented May 3, 2023

This is really nice! Sorry for the delay

@schorrm
Copy link
Collaborator

schorrm commented May 3, 2023

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
Copy link
Contributor Author

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
Copy link
Collaborator

schorrm commented May 7, 2023

Yeah, this is a great fallback!

@schorrm schorrm merged commit fd6c4e4 into jldbc:master May 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants