Skip to content

Instantly share code, notes, and snippets.

@jmduke
Created January 12, 2014 08:51
Show Gist options
  • Save jmduke/8382405 to your computer and use it in GitHub Desktop.
Save jmduke/8382405 to your computer and use it in GitHub Desktop.
Fun with NFL scores.
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import vincent
# source: http://www.pro-football-reference.com/boxscores/game_scores.cgi
SOURCE_FILE = "./nflscores.csv"
data = pd.read_csv(SOURCE_FILE, header=0)
header_rows = data.apply(lambda row : row['Rk'] == 'Rk', axis=1)
data = data[~header_rows]
data[['PtDif', 'Count']] = data[['PtDif', 'Count']].astype('int')
score_differentials = data.groupby('PtDif').sum()['Count']
populate_histogram = lambda diff: score_differentials[diff] if diff in score_differentials else 0
histogram = [populate_histogram(i) for i in range(74)]
line = vincent.Bar(histogram)
line.axis_titles(x='Point differential', y='Games')
line.height = 300
line.width = 900
ax = vincent.AxisProperties(labels = vincent.PropertySet(angle=vincent.ValueRef(value=90)))
line.axes[0].properties = ax
line.to_json('test.json')
data[['PtsW', 'PtsL']] = data[['PtsW', 'PtsL']].astype('int')
pivoted_data = data.pivot(index='PtsW', columns='PtsL', values='Count')
# This is devestatingly ugly code.
populate_heatmap = lambda x, y: pivoted_data[x][y] if x in pivoted_data and y in pivoted_data[x] else 0
heatmap_data = pd.DataFrame([[populate_heatmap(x, y) for y in range(73)] for x in range(73)])
fig, ax = plt.subplots()
plt.pcolor(heatmap_data, cmap=plt.cm.Blues, alpha=0.8, vmin=0, vmax=50)
ax.set_xlim([0, 73])
ax.set_xlabel("Winning team's points")
ax.set_ylim([0, 73])
ax.set_ylabel("Losing team's points")
ax = plt.gca()
ax = plt.gca()
for t in ax.xaxis.get_major_ticks():
t.tick1On = False
t.tick2On = False
for t in ax.yaxis.get_major_ticks():
t.tick1On = False
t.tick2On = False
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment