Fit a gaussian function

后端 未结 4 477
天命终不由人
天命终不由人 2020-11-30 02:20

I have a histogram (see below) and I am trying to find the mean and standard deviation along with code which fits a curve to my histogram. I think there is something in SciP

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 02:39

    I am not sure what your input is, but possibly your y-axis scale is too large (20000), try reducing this number. The following code works for me:

    import matplotlib.pyplot as plt
    import numpy as np
    
    #created my variable
    v = np.random.normal(0,1,1000)
    
    
    fig, ax = plt.subplots()
    
    
    plt.hist(v, bins=500, normed=1, color='#7F38EC', histtype='step')
    
    #plot
    plt.title("Gaussian")
    plt.axis([-1, 2, 0, 1]) #changed 20000 to 1
    
    plt.show()
    

    Edit:

    If you want the actual count of values on y-axis, you can set normed=0. And would just get rid of the plt.axis([-1, 2, 0, 1]).

    import matplotlib.pyplot as plt
    import numpy as np
    
    #function
    v = np.random.normal(0,1,500000)
    
    
    fig, ax = plt.subplots()
    
    # changed normed=1 to normed=0
    plt.hist(v, bins=500, normed=0, color='#7F38EC', histtype='step')
    
    #plot
    plt.title("Gaussian")
    #plt.axis([-1, 2, 0, 20000]) 
    
    plt.show()
    

提交回复
热议问题