Reputation: 63
I am trying to make a histogram using animation, but it is not showing.
#using animation library
%matplotlib notebook
import pandas as pd
import numpy as np
import matplotlib.animation as animation
import matplotlib.pyplot as plt
n=100
x=np.random.randn(n)
def update(curr):
if curr==n:
a.event_source.stop()
plt.cla()
bins=np.arange(-4,4,0.5)
plt.hist(x[:curr],bins=bins)
plt.axis([-4,4,0,30])
plt.gca().set_title('sampling the normal distribution')
plt.gca.set_ylabel('frequency')
plt.gca().set_xlabel('value')
plt.annoate('n={}'.format(curr),[3,27])
fig=plt.figure()
a=animation.FuncAnimation(fig,update,interval=100)
Upvotes: 6
Views: 6725
Reputation: 12496
You have some typo inside the update
function:
plt.gca.set_ylabel('frequency')
should be replaced by plt.gca().set_ylabel('frequency')
plt.annoate('n={}'.format(curr),[3,27])
should be replaced by plt.gca().annotate('n={}'.format(curr),[3,27])
Check this code:
%matplotlib notebook
import pandas as pd
import numpy as np
import matplotlib.animation as animation
import matplotlib.pyplot as plt
n = 100
x = np.random.randn(n)
def update(curr):
if curr == n:
a.event_source.stop()
plt.cla()
bins = np.arange(-4, 4, 0.5)
plt.hist(x[:curr], bins = bins)
plt.axis([-4, 4, 0, 30])
plt.gca().set_title('sampling the normal distribution')
plt.gca().set_ylabel('frequency')
plt.gca().set_xlabel('value')
plt.gca().annotate('n={}'.format(curr), [3, 27])
fig = plt.figure()
a = animation.FuncAnimation(fig, update, interval = 100)
plt.show()
which gives this animation:
Upvotes: 6