Search code examples
pythonmatplotlibnormal-distribution

Drawing multiple univariate normal distribution


Does anyone know how to draw multiple Gaussian distributions on a single plot using Python? Ive got some normal distributed data with different means and standard deviations that i need to plot. Thnx a lot I could draw only one. Please be simple with me, ive literally just started using Python


Solution

  • Let's suppose you have 3 different combinations of mean mu and standard deviaton sigma. You can choose as many as you like, but for example purposes I used 3.

    from matplotlib import pyplot as mp
    import numpy as np
    
    def gaussian(x, mu, sig):
        return 1./(np.sqrt(2*np.pi)*sigma)*np.exp(-0.5 * (1./sigma*(x - mu))**2)
    
    for mu, sig in [(0.5, 0.1), (1.0, 0.2), (1.5, 0.3)]: #(mu,sigma)
        mp.plot(gaussian(np.linspace(-8, 8, 100), mu, sig))
    
    mp.show()
    

    Define your mu and sigma in this line, you can add as many combinations as you like:

    for mu, sig in [(0.5, 0.1), (1.0, 0.2), (1.5, 0.3)]: #(mu,sigma)
    

    in my case it is

    • mu = 0.5, sigma = 0.1
    • mu = 1.0, sigma = 0.2
    • mu = 1.5, sigma = 0.3

    Result:

    enter image description here

    *EDIT

    %matplotlib inline
    from matplotlib import pyplot as mp
    import numpy as np
    
    def gaussian(x, mu, sig):
        return 1./(np.sqrt(2*np.pi)*sigma)*np.exp(-0.5 * (1./sigma*(x - mu))**2)
    
    for mu, sigma in [(1, 2), (0.5, 1), (0, 0.5)]: #(mu,sigma)
        mp.plot(gaussian(np.linspace(-4, 6, 100, ), mu, sigma))
        mp.xlim(0,110)  #set x-axes limits
        mp.ylim(0,1)  #set y-axes limits
    
    mp.show()
    

    Result:

    enter image description here