Created
August 22, 2022 16:06
-
-
Save lmeulen/b5ffffd2e2e0f12de5e855fbee4a103b to your computer and use it in GitHub Desktop.
ergast_compare2prevyear_teams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
year=2022 | |
nw = stats.get_wcc_standing(year) | |
rnd = nw['round'].max() | |
nw = nw[nw['round'] == rnd][['name', 'points']].sort_values('points', ascending=False) | |
pv = stats.get_wcc_standing(year-1) | |
pv = pv[pv['round'] == rnd][['name', 'points']].sort_values('points', ascending=False) | |
pv = pv.rename(columns={"points": "prev_year"}) | |
comp = pd.merge(nw, pv, on='name') | |
comp['diff'] = comp['points'] - comp['prev_year'] | |
comp['color'] = 'green' | |
comp.loc[comp['diff'] < 0, 'color'] = 'red' | |
fig, ax = plt.subplots(figsize=(15,8)) | |
# Plotting the horizontal lines | |
rects = ax.hlines(y=comp.name, xmin=0, xmax=comp['diff'], color=comp['color'], | |
alpha=0.4, linewidth=20) | |
ax.invert_yaxis() | |
for nm, vl in zip(comp.name, comp['diff']): | |
label = ax.annotate(str(vl), xy=(vl, nm), xytext=(5 if vl > 0 else -30, 0), | |
textcoords="offset points", | |
ha="left", va='center') | |
ax.axvline(0, color='black') | |
ax.set_title('Difference in points after {} rounds between {} and {}'.format(rnd, year, year-1)) | |
_ = ax.set_xlabel('Points difference') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment