Issue
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.
Solution
I followed the example linked in your question and modified your code. I have no knowledge of closures myself, but the error was in the argument. The error does not occur with the arguments as shown in the sample. There is no data example in your question, so I created a data frame with appropriate numbers for a 1 year time series, and then created a column of month values from the time series and used that to create the data by specifying the month in an animated loop process.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
np.random.seed(20211025)
df = pd.DataFrame({'date': pd.date_range('2020-01-01','2021-01-01',freq='1d'), 'values':np.random.randn(367)})
df['date'] = pd.to_datetime(df['date'])
df['month'] = df['date'].dt.month
months = ['January','February','March','April','May','June','July','August','September','October','November','December']
# Fixing bin edges to be between -5 and 5
HIST_BINS = np.linspace(-5, 5, 30)
# histogram our data with numpy
data = np.random.randn(1000)
n, _ = np.histogram(data, HIST_BINS)
def prepare_animation(bar_container):
def animate(frame_number): # HIST_BINS,data,n,_,
#data = np.random.randn(1000)
#print(frame_number)
m = frame_number + 1
ax.set_title("{}".format(months[frame_number]))
data = df.query('month == @m')
n, _ = np.histogram(data['values'], HIST_BINS)
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=20)
# plt.show()
ani = animation.FuncAnimation(fig, prepare_animation(bar_container), 12, repeat=False, blit=True, interval=500)
HTML(ani.to_html5_video())
Answered By - r-beginners
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.