Skip to content

Instantly share code, notes, and snippets.

Created December 21, 2020 12:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save amankharwal/1fadea63a9fce274d60186d816b67c36 to your computer and use it in GitHub Desktop.
from matplotlib.animation import FuncAnimation, FFMpegWriter
selected = racing_bar_filled.iloc[-1,:].sort_values(ascending=False)[:20].index
data = racing_bar_filled[selected].round()
fig,ax = plt.subplots(figsize=(9.3,7))
no_of_frames = data.shape[0] #Number of frames
#initiate the barplot with the first rows of the dataframe
bars = sns.barplot(y=data.columns,x=data.iloc[0,:],orient="h",ax=ax)
txts = [ax.text(0,i,0,va="center") for i in range(data.shape[1])]
title_txt = ax.text(650,-1,"Date: ",fontsize=12)
ax.set_xlabel("Pay (Millions USD)")
def animate(i):
# print(f"i={i}/{no_of_frames}")
#get i'th row of data
y = data.iloc[i,:]
#update title of the barplot axis
title_txt.set_text(f"Date: {str(data.index[i].date())}")
#update elements in both plots
for j, b, in enumerate(bars.patches):
#update each bar's height
#update text for each bar (optional)
anim=FuncAnimation(fig,animate,repeat=False,frames=no_of_frames,interval=1,blit=False)'athletes.gif', writer='imagemagick', fps=120)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment