Initiate Environment


In [2]:
%matplotlib inline

In [3]:
import pandas as pd
import matplotlib as mp
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

Basic Visualization for QB Population Count and Avg. QB Rating


In [11]:
player_years = pd.read_csv('qb_yearly.csv')
player_per_year = player_years.groupby(['CareerYear']).count() 
yearly_rates = player_years[['CareerYear', 'Rate']] 
avg_yearly_rate = yearly_rates.groupby(['CareerYear']).mean()
#red_patch = mpatches.Patch(color='r', label='# Players per Year')
#blue_patch = mpatches.Patch(color='b', label='Avg QB Rating by Year')

plt.bar(player_per_year, np.max(player_per_year), color='r') 
plt.plot(avg_yearly_rate, color='b') 

#plt.legend(handles=[red_patch, blue_patch], loc='lower left') 
plt.show()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-7cd4d412afd8> in <module>()
      6 #blue_patch = mpatches.Patch(color='b', label='Avg QB Rating by Year')
      7 
----> 8 plt.bar(player_per_year, np.max(player_per_year), color='r')
      9 plt.plot(avg_yearly_rate, color='b')
     10 

/Users/jph/anaconda/lib/python3.5/site-packages/matplotlib/pyplot.py in bar(left, height, width, bottom, hold, data, **kwargs)
   2641     try:
   2642         ret = ax.bar(left, height, width=width, bottom=bottom, data=data,
-> 2643                      **kwargs)
   2644     finally:
   2645         ax.hold(washold)

/Users/jph/anaconda/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1810                     warnings.warn(msg % (label_namer, func.__name__),
   1811                                   RuntimeWarning, stacklevel=2)
-> 1812             return func(ax, *args, **kwargs)
   1813         pre_doc = inner.__doc__
   1814         if pre_doc is None:

/Users/jph/anaconda/lib/python3.5/site-packages/matplotlib/axes/_axes.py in bar(self, left, height, width, bottom, **kwargs)
   2078         if len(height) != nbars:
   2079             raise ValueError("incompatible sizes: argument 'height' "
-> 2080                               "must be length %d or scalar" % nbars)
   2081         if len(width) != nbars:
   2082             raise ValueError("incompatible sizes: argument 'width' "

ValueError: incompatible sizes: argument 'height' must be length 18 or scalar

In [44]:
rb = pd.read_csv('rb_yearly.csv')
rb['YPG'] = rb['YPG'].apply(pd.to_numeric, errors='coerce')

In [64]:
player_years = pd.read_csv('rb_yearly.csv')
player_years['YPG'] = player_years['YPG'].apply(pd.to_numeric, errors='coerce')
player_per_year = player_years.groupby(['CareerYear']).count()
print(len(player_per_year))
rates_per_min100_carry = player_years[player_years['Att'] >= 100]
yearly100_rates = rates_per_min100_carry[['CareerYear', 'YPG']]
avg_yearly100_rate = yearly100_rates.groupby(['CareerYear']).mean()

rates_per_min200_carry = player_years[player_years['Att'] >= 200]
yearly200_rates = rates_per_min200_carry[['CareerYear', 'YPG']]
avg_yearly200_rate = yearly200_rates.groupby(['CareerYear']).mean()

rates_per_min300_carry = player_years[player_years['Att'] >= 300]
yearly300_rates = rates_per_min300_carry[['CareerYear', 'YPG']]
avg_yearly300_rate = yearly300_rates.groupby(['CareerYear']).mean()

red_patch = mpatches.Patch(color='r', label='# Players per Year')
blue_patch = mpatches.Patch(color='b', label='YPG by Year for 100+')
green_patch = mpatches.Patch(color='g', label='YPG by Year for 200+')
cyan_patch = mpatches.Patch(color='c', label='YPG by Year for 300+')



plt.plot(player_per_year, color='r') 
plt.plot(avg_yearly100_rate, color='b') 
plt.plot(avg_yearly200_rate, color='g') 
plt.plot(avg_yearly300_rate, color='c') 



plt.legend(handles=[red_patch, blue_patch, green_patch, cyan_patch], loc='upper right') 
plt.show()


13