Skip to content

Instantly share code, notes, and snippets.

@kellycampbell
Last active August 29, 2015 13:56
Show Gist options
  • Save kellycampbell/9289884 to your computer and use it in GitHub Desktop.
Save kellycampbell/9289884 to your computer and use it in GitHub Desktop.
Shows the distribution of batting averages for MLB players by the handedness they bat with. (Right, Left, Both)
import pandas
from ggplot import *
# Shows the distribution of batting averages for MLB players by the handedness they bat with. (Right, Left, Both)
def compare_averages(filename):
"""
Data from: https://www.dropbox.com/s/xcn0u2uxm8c4n6l/baseball_data.csv
Example output: http://imgur.com/21Rs287
"""
data = pandas.read_csv(filename)
plotdata = data[data['avg'] != 0] # filters out players with no batting average
p = ggplot(aes(x='avg', color='handedness'), data=plotdata) \
+ geom_density() \
+ labs(x="Batting Average", y="Density")
print p
if __name__ == "__main__":
compare_averages('baseball_data.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment