I am having some issues following the Matplotlib documentation on animating histograms. The document is here: https://matplotlib.org/stable/gallery/animation/animated_histogram.html
I wish to group my data by each month of the year and plot my values for that month in a histogram. At the moment, I am looping over my df, taking the values for each month, and plotting them in a histogram. As shown here:
for i in months:
temp = df.loc[df['month_number'] == i]
plt.hist(temp['values'], density=False, bins=30)
plt.show()
Trying to follow along, my code is as such:
# Fixing bin edges to be between -5 and 5
HIST_BINS = np.linspace(-5, 5, 30)
# histogram our data with numpy
data = df['values']
n, _ = np.histogram(data, HIST_BINS)
The above seems fine to me. The trouble is understanding and altering the next portion. Here is where I am at, which may be far from correct. I am not entirely sure how to use these python closures and am studying them at the moment due to this task. Any guidance welcomed.
def prepare_animation(bar_container):
def animate(HIST_BINS,data,n,_,frame_number):
for count, rect in zip(n, bar_container.patches):
rect.set_height(count)
return bar_container.patches
return animate()
fig, ax = plt.subplots()
a, b, bar_container = ax.hist(data, HIST_BINS, lw=1,ec="red", fc="blue", alpha=0.5)
ax.set_ylim(top=140)
ani = animation.FuncAnimation(fig, prepare_animation(bar_container), 50,
repeat=False, blit=True)
HTML(ani.to_html5_video())
TypeError: animate() missing 5 required positional arguments: 'HIST_BINS', 'data', 'n', '_', and 'frame_number'
Any help altering this code would be greatly appreciated.
