To complete this assignment, create a code cell that:

  • Creates a number of subplots using the pyplot subplots or matplotlib gridspec functionality.
  • Creates an animation, pulling between 100 and 1000 samples from each of the random variables (x1, x2, x3, x4) for each plot and plotting this as we did in the lecture on animation.
  • Bonus: Go above and beyond and "wow" your classmates (and me!) by looking into matplotlib widgets and adding a widget which allows for parameterization of the distributions behind the sampling animations.

Tips:

  • Before you start, think about the different ways you can create this visualization to be as interesting and effective as possible.
  • Take a look at the histograms below to get an idea of what the random variables look like, as well as their positioning with respect to one another. This is just a guide, so be creative in how you lay things out!
  • Try to keep the length of your animation reasonable (roughly between 10 and 30 seconds).
In [13]:
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')
Traceback (most recent call last):
  File "/home/sabodhapati/anaconda3/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 389, in process
    proxy(*args, **kwargs)
  File "/home/sabodhapati/anaconda3/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 227, in __call__
    return mtd(*args, **kwargs)
  File "/home/sabodhapati/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py", line 1560, in _stop
    self.event_source.remove_callback(self._loop_delay)
AttributeError: 'NoneType' object has no attribute 'remove_callback'
Out[13]:
Text(15.4818,0.5,'x4\nUniform')
In [61]:
#%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')
Out[61]:
Text(15.981,0.38,'x4\nUniform')
<matplotlib.figure.Figure at 0x7f9564a32d68>
In [43]:
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()
In [26]:
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)
In [16]:
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)
In [32]:
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)
In [23]:
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)