In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import ipywidgets
from IPython.display import display
from collections import OrderedDict
%matplotlib inline
pd.options.mode.chained_assignment = None # default='warn'
In [2]:
dfMaster = pd.read_csv('../data/dayByDayStandings2015.csv')
dfMaster.fillna(0, inplace=True)
dfMaster['date'] = pd.to_datetime(dfMaster['date'])
dfMaster['date'] = dfMaster['date'].apply(pd.datetools.normalize_date)
dfMaster.set_index(['division', 'date'], inplace=True)
dfMaster = dfMaster[['Tm', 'W', 'L', 'GB', 'RS', 'RA', 'W-L%', 'pythW-L%']]
In [3]:
dfMaster.info()
In [4]:
dfMaster.tail()
Out[4]:
In [5]:
dfALCentral = dfMaster.ix['AL Central', ['Tm', 'GB']]
In [6]:
dfALCentral.replace({'--': 0}, inplace=True)
dfALCentral['GB'] = dfALCentral['GB'].astype(float)
In [7]:
cle = dfALCentral[dfALCentral.Tm == 'CLE']
kcr = dfALCentral[dfALCentral.Tm == 'KCR']
minn = dfALCentral[dfALCentral.Tm == 'MIN']
chw = dfALCentral[dfALCentral.Tm == 'CHW']
det = dfALCentral[dfALCentral.Tm == 'DET']
plt.figure()
plt.plot(cle.index.values, cle['GB'], label='CLE')
plt.plot(kcr.index.values, kcr['GB'], label='KCR')
plt.plot(minn.index.values, minn['GB'], label='MIN')
plt.plot(chw.index.values, chw['GB'], label='CHW')
plt.plot(det.index.values, det['GB'], label='DET')
plt.legend(loc='best')
plt.show()
In [8]:
dropList = OrderedDict(zip(dfMaster.index.levels[0].values, dfMaster.index.levels[0].values))
mlb_colors = {'HOU': '#072854', 'CIN': '#C6011F', 'NYM': '#FB4F14', 'PHI': '#BA0C2F',
'LAD': '#083C6B', 'LAA': '#B71234', 'COL': '#333366', 'TOR': '#003DA5',
'WSN': '#BA122B', 'BAL': '#ED4C09', 'STL': '#C41E3A', 'SDP': '#002147',
'ARI': '#A71930', 'MIL': '#92754C', 'MIN': '#C6011F', 'MIA': '#F9423A',
'BOS': '#C60C30', 'OAK': '#003831', 'PIT': '#FDB829', 'CHC': '#003279',
'CHW': '#000000', 'SFG': '#F2552C', 'DET': '#001742', 'TBR': '#00285D',
'KCR': '#15317E', 'ATL': '#B71234', 'TEX': '#BD1021', 'SEA': '#005C5C',
'CLE': '#003366', 'NYY': '#1C2841'}
In [11]:
def plot_division(division):
tmpMaster = dfMaster.ix[division, ['Tm', 'GB']]
plt.figure(figsize=(15,6))
ax = plt.subplot(111)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
for team in enumerate(tmpMaster.Tm.value_counts().index.values):
tmp = tmpMaster[tmpMaster.Tm == team[1]]
tmp.replace({'--': 0}, inplace=True)
tmp['GB'] = tmp['GB'].astype(float)
tmp.loc[:,'GB'] = tmp.loc[:,'GB'] * -1
plt.plot(tmp.index.values, tmp['GB'], label=team[1], color=mlb_colors[team[1]])
plt.legend(loc='best')
plt.show()
divW = ipywidgets.Dropdown(options=dropList)
init = divW.value
j = ipywidgets.interactive(plot_division, division=divW)
display(j)
In [ ]: