【问题标题】:Gaussian fit to histogram is flat直方图的高斯拟合是平坦的
【发布时间】:2018-02-17 20:04:18
【问题描述】:

我创建了一个 python 脚本,它从文件中绘制一行数据,然后用高斯曲线拟合它。红色阶梯直方图是一组数据,我想将其平均值与真实数据值(蓝色虚线)进行比较。高斯拟合在图表底部几乎看不到,它是一条绿色虚线。我不知道为什么拟合是平坦的而不是曲线,因为计算的平均值和西格玛是正确的;它们是图表的标题。

我的源代码

    from scipy.stats import norm
    import scipy, pylab
    import numpy as np
    import matplotlib.pyplot as plt


    df = numpy.loadtxt('CR_count_TAL=0.10472.dat',dtype='str')

    for num in range(1):
        nu=df[num].astype('float')
        data = nu[1]
        mc=df[2:numpy.size(nu)]
        #plot the MC distribution
        #hist(nu[2:size(nu)],bins=100,color='r',range=(100,500),histtype='step')

        #plot the dataline
        axvline(data,color='b',linewidth=2, linestyle='--')
        #fit a gaussian
        #(mu, sigma) = norm.fit(nu)
        plt.hist(nu[2:size(nu)],bins=100,color='r',range=(100,500),histtype='step')
        y = mlab.normpdf(bins, mu, sigma)
        l = plt.plot(bins, y, 'g', linewidth = 2, linestyle='--')

        plt.title(r'$\ \mu=%.3f,\ \sigma=%.3f$' %(mu, sigma))
        plt.show()

【问题讨论】:

    标签: python curve-fitting gaussian


    【解决方案1】:

    请在此处查看完整的工作示例,改编自您的代码

    import numpy as np                                                                 
    import matplotlib.pyplot as plt                                                    
    import matplotlib.mlab as mlab                                                     
    
    mu = 100                                                                           
    sigma = 20                                                                         
    n_sample = 3000                                                                    
    
    data = np.random.normal(mu, sigma, n_sample)                                       
    
    # plot the data                                                               
    _, bins, _ = plt.hist(data, bins=100, color='r', range=(50, 150),
                          histtype='step', normed=True)                                                                                       
    # plot the gaussian PDF                                                              
    y = mlab.normpdf(bins, mu, sigma)                                                  
    plt.plot(bins, y, 'g', linewidth=2, linestyle='--')                                
    plt.axvline(mu, color='b', linewidth=2, linestyle='--')                                                                                        
    plt.title(r'$\ \mu=%.3f,\ \sigma=%.3f$' % (mu, sigma))                             
    plt.show() 
    

    它给出了这张图片

    问题是您的 y 是一个归一化为 1 的概率分布,但您的直方图不是。因此有两种处理方式:

    • 缩放 PDF
    • 标准化直方图

    归一化基本上是直方图曲线下的面积,可以通过数值积分来计算。受

    影响
    • 样本数
    • bin 大小

    请注意,plt.hist 可以通过传入 normed=True 选项来为您执行此操作。

    【讨论】:

    • 我做了这个更正,但现在我完全没有高斯拟合
    • @nos normed 已弃用 - density=True 会做同样的工作吗?
    猜你喜欢
    • 2016-07-05
    • 2014-11-12
    • 2015-08-11
    • 2022-01-06
    • 2013-11-19
    • 2017-10-10
    • 2016-07-26
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多