<jupyter_start><jupyter_text>"Univariate Normal(Interactive plots - MatPlotLib and Streamlit )">  The previous post introduced univariate normal distribution. This second part intends to explain how to  visualize the same using MatPlotLib animation and Streamlit librabry.- toc: true - badges: true- comments: true- categories: ['jupyter','univariate','normal','animation','streamlit']- author : <NAME>- image: images/unianim.jpg  Streamlit and MatPlotLib Animation* [Streamlit](https://streamlit.io/) is a library built on Python which helps create deployable code outputs. It is a visualisation TOOL that proves to be highly effective for technical presentations.* [MatPlotLib Animation](https://matplotlib.org/stable/api/animation_api.html) is a class which  can be ussed to create inline interactive plots in jupyter notebooks.  Univariate animation with MatPlotLibThese are the dependencies needed which include [Seaborn](https://seaborn.pydata.org/) for embedding a dark background in the plots.<jupyter_code>#collapse-show
# Imports
%matplotlib inline

import sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import cm # Colormaps
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable
import seaborn as sns

sns.set_style('darkgrid')
np.random.seed(42)<jupyter_output><empty_output><jupyter_text>customized univariate function Instead of importing this fucntion from Numpy or Scipy, we have created out own function which simply translates the equation and returns the single value.  Create a LIST of VarianceTo animate the plot, we need an animate function, which generates a random set of numbers and updates the widths of the Normal pdf functions. Here, the mean value is purposefully kept constant at zero.  As the animation frames begin to unroll, a clear increment in the variance LIST is obserrved and are also displayed in the title. Calling `update()` will define animate function working with supplied `univariate_normal()` function, all this is used to setup FuncAnimation.<jupyter_code>#collapse-show
x = np.linspace(-np.pi, np.pi,200)   # range of x define between -pi to  +pi
mean  = 0 
variance  = [0.05, 0.75,2.0]     # defined a LIST<jupyter_output><empty_output><jupyter_text>Custom defined Normal pdf  function<jupyter_code>#collapse-show
def univariate_normal(x, mean, variance):
    """pdf of the univariate normal distribution."""
    return ((1. / np.sqrt(2 * np.pi * variance)) * 
            np.exp(-(x - mean)**2 / (2 * variance)))<jupyter_output><empty_output><jupyter_text>Using simple FOR loops to iterate`for` loops are an easier way out  to visualise multiple plots with varying VARIANCE, but have a limitation in that the plots can become too small if the list has many items. Else `subplot()` are to be used to display a grid. In any case all these are STATIC graphs.`FuncAnimation()` in turn helps one visualize the actual change in the variance dynaically<jupyter_code>#collapse-show
plt.figure(figsize=(5,3))
for i, var in enumerate(variance):
    plt.subplot(3,1,i+1)  
    plt.plot(x,univariate_normal(x, mean, var))
    plt.show()<jupyter_output><empty_output><jupyter_text>matplot lib animation below here<jupyter_code>#collapse-show
x = np.linspace(0.,1.,500) # 500 points evenly spaced ovr [0,1]
mu = np.zeros((500))
#collapse-show
fig,ax = plt.subplots()
from matplotlib.animation import FuncAnimation
from matplotlib import rc
import seaborn as sns

sns.set_style('darkgrid')

var = [0.05, 0.1, 0.25, 0.5, 1., 2., 4.]

x = np.linspace(-2.,2.,500) # 500 points evenly spaced ovr [0,1]
mu = np.zeros((500))

def update(iterations):
    ax.cla()
    k = var[iterations]
    for i in range(20):
        ax.plot(x,univariate_normal(x, mean, k),color='b', alpha=0.2)
        ax.set_title("$Univariate-Normal$\n variance = %s " %var[iterations]);
        ax.set_ylim((0,2))

num_iterations = len(var)
anim = FuncAnimation(fig, update, frames = np.arange(0,num_iterations-1,1), interval = 250)
plt.close()

rc('animation',html='jshtml')
anim<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09938949938949938, 'fraction_numerical': 0.018315018315018316, 'mean_word_length': 5.03240058910162, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '306908', 'license': 'Apache-2.0', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1252, 'n_words': 447, 'path': '_notebooks/2021-05-03-UnivariateInteractive.ipynb', 'repo_name': 'AnandKhandekar/blog', 'size': '4104'}