Skip to content

Instantly share code, notes, and snippets.

@lmeulen
Created August 22, 2022 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmeulen/b5ffffd2e2e0f12de5e855fbee4a103b to your computer and use it in GitHub Desktop.
Save lmeulen/b5ffffd2e2e0f12de5e855fbee4a103b to your computer and use it in GitHub Desktop.
ergast_compare2prevyear_teams
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