Skip to content

Instantly share code, notes, and snippets.

@caged
Created February 9, 2014 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caged/8896387 to your computer and use it in GitHub Desktop.
Save caged/8896387 to your computer and use it in GitHub Desktop.
Generate graphs from NBA data
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import glob
import os.path
font = {'family' : 'sans-serif',
'size' : 8}
matplotlib.rc('font', **font)
files = glob.glob('./out/point-diff-*.csv')
for name in files:
sname = os.path.splitext(os.path.basename(name))[0]
data = pd.read_csv(name)
# Point spread barchart
ps_data = data[['Abbr', 'Above 500', 'Below 500', 'Point spread']].sort(columns='Point spread', ascending=False)
ps_data = ps_data[['Abbr', 'Point spread']]
ps_data.plot(kind='bar', x='Abbr', color=['green'], edgecolor='darkgreen', linewidth=0.5)
plt.xlabel('Team')
plt.ylabel('Points better vs below .500 than above .500')
plt.text(17.6, -1.8,'Authored 02/08/2014 by Justin Palmer (dealloc.me). Source: stats.nba.com', fontsize=6, color='gray')
plt.tight_layout()
plt.savefig(sname + '-spread.png', dpi=150)
plt.close()
# Above/Below 500 grouped bar
ab_data = data[['Abbr', 'Above 500', 'Below 500']].sort(columns='Above 500', ascending=False)
ab_data.plot(kind='bar', x='Abbr', color=['green', 'lightgrey'], edgecolor='black', linewidth=0.5)
plt.xlabel('Team')
plt.ylabel('Point differential')
plt.text(17.5, -23.8,'Authored 02/08/2014 by Justin Palmer (dealloc.me). Source: stats.nba.com', fontsize=6, color='gray')
plt.tight_layout()
plt.savefig(sname + '-ab.png', dpi=150)
plt.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment