I'm trying to add a title or a note (the year actualy plot) who change at each itteration of this annimated plot. The code follow show my last attempt with a note "plt.text". The variable "yrs" is a list like this [1950, 1951, 1952...2020] I want these value to be show at each frame itteration.
# Fixing bin edges
HIST_BINS = np.linspace(data[3].min(), data[var].max(), round(data[var].max())+1)
# histogram our data with numpy
dt = data[var]
dt2 = data[var] # ref
dt2 = dt2[dt2 > 1]
n, _ = np.histogram(dt2, HIST_BINS)
def prepare_animation(bar_container):
def animate(frame_number):
# simulate new data coming in
dt = data.iloc[data.index.year.isin([frame_number]), var]
dt = dt[dt > 1]
n, _ = np.histogram(dt, HIST_BINS)
for count, rect in zip(n, bar_container.patches):
rect.set_height(count)
return bar_container.patches
plt.text(0, 2, str(frame_number),
horizontalalignment='left',
verticalalignment='center')
return animate
fig, ax = plt.subplots()
ax.hist(dt2, HIST_BINS, lw=1, weights=np.ones(len(dt2)) / len(dt2)*100,
color='steelblue', alpha=0.5, label='histo')
_, _, bar_container = ax.hist(dt, HIST_BINS, lw=1, weights=np.ones(len(dt)) / len(dt),
color='orange', alpha=0.5, label='window')
ax.set_ylim(top=40) # set safe limit to ensure that all data is visible.
# ax.set_yscale('log')
plt.legend()
plt.xlabel('mm')
plt.ylabel("freq. %")
ani = animation.FuncAnimation(fig, prepare_animation(bar_container), frames=yrs,
repeat=False, blit=True)
plt.show()
It finally work... when plot to the screen it doesn't work but the saved .gif work fine....