This assignment is optional, and I encourage you to share your solutions with me and your peers in the discussion forums!
To complete this assignment, create a code cell that:
pyplot subplots
or matplotlib gridspec
functionality.x1
, x2
, x3
, x4
) for each plot and plotting this as we did in the lecture on animation.Tips:
In [2]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
# generate 4 random variables from the random, gamma, exponential, and uniform distributions
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)
# plot the histograms
plt.figure(figsize=(9,3))
plt.hist(x1, normed=True, bins=20, alpha=0.5)
plt.hist(x2, normed=True, bins=20, alpha=0.5)
plt.hist(x3, normed=True, bins=20, alpha=0.5)
plt.hist(x4, normed=True, bins=20, alpha=0.5);
plt.axis([-7,21,0,0.6])
plt.text(x1.mean()-1.5, 0.5, 'x1\nNormal')
plt.text(x2.mean()-1.5, 0.5, 'x2\nGamma')
plt.text(x3.mean()-1.5, 0.5, 'x3\nExponential')
plt.text(x4.mean()-1.5, 0.5, 'x4\nUniform')
Out[2]:
In [5]:
import matplotlib.animation as animation
n = 100 # nb samples
x = np.random.randn(n)
# create the function that will do the plotting, where frameValue is the current frame
def update(frameValue):
# check if animation is at the last frame, and if so, stop the animation a
if frameValue == n:
a.event_source.stop()
fig.subplots_adjust(hspace=0.3)
ax1.clear()
ax1.hist(x1[:frameValue], normed=True, bins='auto', alpha=0.5)
ax1.set_title('random')
ax2.clear()
ax2.hist(x2[:frameValue], normed=True, bins='auto', alpha=0.5)
ax2.set_title('gamma')
ax3.clear()
ax3.hist(x3[:frameValue], normed=True, bins='auto', alpha=0.5)
ax3.set_title('exponential')
ax4.clear()
ax4.hist(x4[:frameValue], normed=True, bins='auto', alpha=0.5)
ax4.set_title('uniform')
xmin = min(x1)
xmax = max(x4)
plt.axis([xmin, xmax , 0, 1])
plt.sca(ax2)
plt.annotate('n = {}'.format(frameValue), [xmax-7, 0.8])
In [6]:
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True)
a = animation.FuncAnimation(fig, update, interval=100)
In [ ]: