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 [1]:
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')
#plt.text?
Out[1]:
In [1]:
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
n = 100
# 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)
# create the function that will do the plotting, where curr is the current frame
def update(curr):
if curr == n:
a.event_source.stop()
print (curr)
# subplot with 1 row, 4 columns, and current axis is 1st subplot axes
#plt.axis([-7, 2, 0, .6])
plt.clf()
ax1 = plt.subplot(2, 2, 1)
plt.hist(x1[:curr], normed=True, bins=20, alpha=0.5)
plt.text(x1[:curr].mean()-1.5, 0.428, 'x1 Normal')
# subplot with 1 row, 4 columns, and current axis is 2nd subplot axes
#plt.clf()
plt.subplot(2, 2, 2, sharey=ax1)
plt.hist(x2[:curr], normed=True, bins=20, alpha=0.5)
plt.text(x2[:curr].mean()-1.5, 0.428, 'x2 Gamma')
# subplot with 1 row, 4 columns, and current axis is 3rd subplot axes
#plt.clf()
plt.subplot(2, 2, 3, sharey=ax1)
plt.hist(x3[:curr], normed=True, bins=20, alpha=0.5)
plt.text(x3[:curr].mean()-1.5, 0.428, 'x3 Exponential')
# subplot with 1 row, 4 columns, and current axis is 4th subplot axes
#plt.clf()
plt.subplot(2, 2, 4, sharey=ax1)
plt.hist(x4[:curr], normed=True, bins=20, alpha=0.5);
plt.text(x4[:curr].mean()-1.5, 0.428, 'x4 Uniform')
#plt.subplots_adjust(left=None, bottom=None, right=None, top=1, wspace=None, hspace=None)
#plt.axis([-7,21,0,0.6])
In [2]:
# plot the histograms
fig = plt.figure(figsize=(9,5))
#plt.subplots_adjust(left=0.25, bottom=0.25)
#fig = plt.figure()
a = animation.FuncAnimation(fig, update, interval=10)
In [5]:
In [ ]: