Green Bay Packers Career Receiving Yards Leaders by Year

#import libraries
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
import matplotlib.colors as mc
import colorsys
from random import randint
import re
import ipywidgets as wg
from ipywidgets import Text, HBox, VBox, Box, Layout
%matplotlib inline
#import the file into Python 
rec_yds=pd.read_excel('Packers_Top_ReceivingYds.xlsx')
rec_yds
Year Player Receiving Yards Years Played
0 2019 Donald Driver 10137 1999-2012
1 2019 James Lofton 9656 1978-1986
2 2019 Sterling Sharpe 8134 1988-1994
3 2019 Don Hutson 7991 1935-1945
4 2019 Jordy Nelson 7848 2008-2017
... ... ... ... ...
872 1932 Lavvie Dilweg 83 1927-1934
873 1932 Milt Gantenbein 71 1931-1940
874 1932 Tom Nash 50 1928-1932
875 1932 Wuert Engelmann 33 1930-1933
876 1932 Al Rose 20 1932-1936

877 rows × 4 columns

#sort values in desc order by year and yards
rec_yds=rec_yds.sort_values(by=['Year','Receiving Yards'], ascending=False)
#variable to set the desired year to show
global year
year=2019
#create new dataframe containing single year as indicated by 'year' variable
rec_yds_year=rec_yds[rec_yds['Year'].eq(year)]
rec_yds_year
Year Player Receiving Yards Years Played
0 2019 Donald Driver 10137 1999-2012
1 2019 James Lofton 9656 1978-1986
2 2019 Sterling Sharpe 8134 1988-1994
3 2019 Don Hutson 7991 1935-1945
4 2019 Jordy Nelson 7848 2008-2017
5 2019 Boyd Dowler 6918 1959-1969
6 2019 Antonio Freeman 6651 1995-2001, 2003
7 2019 Greg Jennings 6537 2006-2012
8 2019 Max McGee 6346 1954-1967
9 2019 Billy Howton 5581 1952-1958
#Code to assign each player a different color and keep them consistent if the years are changed
def transform_color(color, amount = 0.5):
    try:
        c = mc.cnames[color]
    except:
        c = color
        c = colorsys.rgb_to_hls(*mc.to_rgb(c))
    return colorsys.hls_to_rgb(c[0], 1 - amount * (1 - c[1]), c[2])

all_names = rec_yds['Player'].unique().tolist()
random_hex_colors = []
for i in range(len(all_names)):
    random_hex_colors.append('#' + '%06X' % randint(0, 0xFFFFFF))
    
    

rgb_colors = [transform_color(i, 1) for i in random_hex_colors]

#Create figure and call the draw_barchart function
#fig, ax = plt.subplots(figsize=(35, 20))
def draw_barchart(year):
    rec_yds_year=rec_yds[rec_yds['Year'].eq(year)]
    rec_yds_year = rec_yds_year[::-1]
    ax.clear()
    
    bar_colors = dict(zip(rec_yds['Player'].unique(), rgb_colors))
    
    ax.barh(rec_yds_year['Player'], rec_yds_year['Receiving Yards'], color = [bar_colors[i] for i in rec_yds_year['Player']])
    #loop to put text of yards after bar and player/years played inside bar
    for i, (total,Player, YearsPlayed) in enumerate(zip(rec_yds_year['Receiving Yards'], rec_yds_year['Player'], rec_yds_year['Years Played'])):
         ax.text(total, i,   total, ha='right', color = 'black', weight=600, size = 38)
         ax.text(total, i,   Player, ha='left', color = 'black', size = 31, va='bottom')
         ax.text(total, i,   YearsPlayed, ha='left', color = 'black', size = 26, va='top')
    #Putting a bigger year label on the right of graph to make it more obvious what year the data is showing
    ax.text(1, 0.4, year, transform=ax.transAxes, size=70, ha='right')
    
    ax.xaxis.set_ticks_position('top')
    
    plt.tick_params(labelsize = 24)
    
    ax.set_title('Career Packers Receiving Yards Leaders in' + ' ' + str(year), size = 40, weight=600)
    ax.set_yticklabels([])
    plt.box(False)
    ax.grid(which='major', axis='x', linestyle='-')
    
def return_anim():
    global animator
    animator = animation.FuncAnimation(fig, draw_barchart, frames=range(2000, 2002), interval = 1000, repeat = False)
    return animator


def showanim():
    
    global fig
    global ax
    fig, ax = plt.subplots(figsize=(40, 25)) 
    return_anim()
    display(HTML(animator.to_jshtml(default_mode='reflect')))
    plt.close()
    
global showresult
showresult=False
    
def run_all(e):
    global showresult
    showresult=e
    if(showresult==True):
        showanim()
    
resultcheck=wg.Checkbox(
    value=False,
    layout=Layout(height='30px',width='500px'),
    description='Show Result',
    disabled=False,
    indent=False
)



#wg.interact(run_all,e=resultcheck)


    
run_all(True)