#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='-')