'Sliders in distributions animations

I'm enrolled in a Michigan University MOOC in coursera, and in this optional assignment they asked us to plot (with four subplots) the animation of this 4 kinds of distributions (normal, gamma, exponential and uniform). I figured out how to do it, but then I tried to plot and animate this four graphs with sliders to parametrize each of these distributions. I've built this code using some research in the forum and tried to adapt it to fit my 4 graphs. I couldn't understand what's wrong with the code (I'm very unfamiliar with matplotlib animations):

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from matplotlib.widgets import Slider, Button
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(10,10))
gspec = gridspec.GridSpec(3, 2)

_1 = plt.subplot(gspec[0, 0])
_2 = plt.subplot(gspec[0, 1])
_3 = plt.subplot(gspec[1, 0])
_4 = plt.subplot(gspec[1, 1])

ax2=plt.subplot(gspec[2, 0:])             
ax2.axis('off')
axis_color = 'lightgoldenrodyellow'
E1_slider_ax = fig.add_axes([0.13, .22, 0.3, 0.02], facecolor = axis_color)
E2_slider_ax = fig.add_axes([0.13, .17, 0.3, .02], facecolor = axis_color)
E1_slider = Slider(E1_slider_ax, r'Normal $\mu$', valmin = -5, valmax = 5, valinit = -2.5)
E1_slider.label.set_size(15)
E2_slider = Slider(E2_slider_ax, r'Normal $\sigma$', 0, 5, valinit = 1)
E2_slider.label.set_size(15)

E3_slider_ax = fig.add_axes([0.13, .12, 0.3, 0.02], facecolor = axis_color)
E4_slider_ax = fig.add_axes([0.13, .07, 0.3, .02], facecolor = axis_color)
E3_slider = Slider(E3_slider_ax, r'Gamma $\mu$', valmin = -5, valmax = 5, valinit = -2.5)
E3_slider.label.set_size(15)
E4_slider = Slider(E4_slider_ax, r'Gamma $\sigma$', 0, 5, valinit = 1)
E4_slider.label.set_size(15)

E5_slider_ax = fig.add_axes([.6, .22, 0.3, 0.02], facecolor = axis_color)
E5_slider = Slider(E5_slider_ax, 'Scale', valmin = -5, valmax = 5, valinit = -2.5)
E5_slider.label.set_size(15)

E6_slider_ax = fig.add_axes([0.6, .17, 0.3, 0.02], facecolor = axis_color)
E7_slider_ax = fig.add_axes([0.6, .12, 0.3, .02], facecolor = axis_color)
E6_slider = Slider(E6_slider_ax, 'Low', valmin = -5, valmax = 5, valinit = -2.5)
E6_slider.label.set_size(15)
E7_slider = Slider(E7_slider_ax, 'High', 0, 5, valinit = 1)
E7_slider.label.set_size(15)

def update(curr, sample):
    # check if animation is at the last frame, and if so, stop the animation a
    
    if curr == n: 
        a.event_source.stop()
    
    plt.subplot(gspec[0, 0])
    plt.cla()
    plt.hist(sample[0][:curr], density=True, stacked=True)
    
    plt.subplot(gspec[0, 1])
    plt.cla()
    plt.hist(sample[1][:curr], density=True, stacked=True)
    
    plt.subplot(gspec[1, 0])
    plt.cla()
    plt.hist(sample[2][:curr], density=True, stacked=True)
    
    plt.subplot(gspec[1, 1])
    plt.cla()
    plt.hist(sample[3][:curr], density=True, stacked=True)
    
def animate_button(self):
    sample = [np.random.normal(E1_slider.val, E2_slider.val, 10000), 
              np.random.gamma(E3_slider.val, E4_slider.val, 10000), 
              np.random.exponential(E5_slider.val, 10000)+7,
             np.random.uniform(E6_slider.val, E7_slider.val, 10000)]
    fig = plt.figure(figsize=(10,10))
    a = animation.FuncAnimation(fig, update, fargs=(sample), interval=50)
    
axnext = fig.add_axes([0.785, 0.02,0.1, 0.075], facecolor = axis_color)
bnext = Button(axnext, 'Run \nSimulations!')
bnext.on_clicked(animate_button)
    
plt.show()

With this code I managed to get the grids, sliders and buttons plotted, but that's all. The animation doesn't start when I hit the button.

Thanks for your attention!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source