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:
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')
#%matplotlib inline
plt.figure()
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2, sharey = True);
axs = [ax1, ax2, ax3, ax4];
axs[0].hist(x1, normed = True, bins = 200, alpha = 0.5)
axs[1].hist(x2, normed=True, bins=200, alpha=0.5)
axs[2].hist(x3, normed=True, bins=200, alpha=0.5)
axs[3].hist(x4, normed=True, bins=200, alpha=0.5)
axs[0].text(x1.mean()-1.5, 0.4, 'x1\nNormal')
axs[1].text(x2.mean()-1.5, 0.4, 'x2\nGamma')
axs[2].text(x3.mean()+1.5, 0.38, 'x3\nExponential')
axs[3].text(x4.mean()-1, 0.38, 'x4\nUniform')
import matplotlib.gridspec as gridspec
plt.figure()
gspec = gridspec.GridSpec(3, 4)
top_histogram = plt.subplot(gspec[0, 1:3])
left_side_histogram = plt.subplot(gspec[1:, 0])
right_side_historgram = plt.subplot(gspec[1:, 3])
lower_histogram = plt.subplot(gspec[1:, 1:3])
top_histogram.hist(x4, normed=True, bins=200, alpha=0.5)
lower_histogram.hist(x3, normed=True, bins=200, alpha=0.5)
left_side_histogram.hist(x1, orientation = 'horizontal', normed=True, bins=200, alpha=0.5)
right_side_historgram.hist(x2, orientation = 'horizontal',normed=True, bins=200, alpha=0.5)
left_side_histogram.invert_xaxis()
import matplotlib.animation as animation
n = 1000
def update(curr):
if curr == n:
a.event_source.stop()
plt.cla()
bins = np.arange(-7,1, 0.05)
plt.hist(x1[:curr], bins = bins)
plt.axis([-7,1,0,30])
plt.gca().set_title("Sampling the Normal Distribution")
plt.gca().set_ylabel("Frequence")
plt.gca().set_xlabel("Value")
plt.annotate('n = {}'.format(curr), [0,25])
fig = plt.figure()
a = animation.FuncAnimation(fig, update, interval = 10)
n = 1000
x1 = np.random.normal(-2.5, 1, n)
def update(curr):
if curr == n:
a.event_source.stop()
plt.cla()
bins = np.arange(0,15, 0.5)
plt.hist(x2[:curr], bins = bins)
plt.axis([0,15,0,40])
plt.gca().set_title("Sampling the Gamma Distribution")
plt.gca().set_ylabel("Frequence")
plt.gca().set_xlabel("Value")
plt.annotate('n = {}'.format(curr), [12,25])
fig = plt.figure()
a = animation.FuncAnimation(fig, update, interval = 10)
n = 1000
def update(curr):
if curr == n:
a.event_source.stop()
plt.cla()
bins = np.arange(5,20, 0.05)
plt.hist(x3[:curr], bins = bins)
plt.axis([5,20,0,30])
plt.gca().set_title("Sampling the Exponential Distribution")
plt.gca().set_ylabel("Frequence")
plt.gca().set_xlabel("Value")
plt.annotate('n = {}'.format(curr), [18,28])
fig = plt.figure()
a = animation.FuncAnimation(fig, update, interval = 10)
n = 1000
x1 = np.random.normal(-2.5, 1, n)
def update(curr):
if curr == n:
a.event_source.stop()
plt.cla()
bins = np.arange(14,20, 0.01)
plt.hist(x4[:curr], bins = bins)
plt.axis([14,20,0,10])
plt.gca().set_title("Sampling the Uniform Distribution")
plt.gca().set_ylabel("Frequence")
plt.gca().set_xlabel("Value")
plt.annotate('n = {}'.format(curr), [19,8])
fig = plt.figure()
a = animation.FuncAnimation(fig, update, interval = 10)