使用MatPlotLib和Numpy将高斯拟合到直方图-错误的Y缩放? [英] Fitting a Gaussian to a histogram with MatPlotLib and Numpy - wrong Y-scaling?

查看:192
本文介绍了使用MatPlotLib和Numpy将高斯拟合到直方图-错误的Y缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码,以将高斯曲线拟合为直方图.尽管Y缩放比例不同,但似乎可行.我在做什么错了?

I have written the below code to fit a Gaussian curve to a histogram. It seems to work, although the Y scaling is different. What am I doing wrong?

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab

list = [0,1,1,2,2,2,3,3,4]

plt.figure(1)
plt.hist(list)
plt.xlim((min(list), max(list)))

mean = np.mean(list)
variance = np.var(list)
sigma = np.sqrt(variance)
x = np.linspace(min(list), max(list),100)
plt.plot(x,mlab.normpdf(x,mean,sigma))

plt.show()

谢谢!

推荐答案

您需要对直方图进行归一化,因为您绘制的分布也已归一化:

You need to normalize the histogram, since the distribution you plot is also normalized:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab

arr = np.random.randn(100)

plt.figure(1)
plt.hist(arr, normed=True)
plt.xlim((min(arr), max(arr)))

mean = np.mean(arr)
variance = np.var(arr)
sigma = np.sqrt(variance)
x = np.linspace(min(arr), max(arr), 100)
plt.plot(x, mlab.normpdf(x, mean, sigma))

plt.show()

请注意对plt.hist的调用中的normed=True.还请注意,我更改了示例数据,因为直方图看起来很奇怪,数据点太少了.

Note the normed=True in the call to plt.hist. Note also that I changed your sample data, because the histogram looks weird with too few data points.

如果您想保留原始直方图并调整分布,则必须缩放分布,以使分布上的积分等于直方图的积分,即列表中的项目数乘以宽度酒吧.可以像

If you instead want to keep the original histogram and rather adjust the distribution, you have to scale the distribution such that the integral over the distribution equals the integral of the histogram, i.e. the number of items in the list multiplied by the width of the bars. This can be achieved like

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab

arr = np.random.randn(1000)

plt.figure(1)
result = plt.hist(arr)
plt.xlim((min(arr), max(arr)))

mean = np.mean(arr)
variance = np.var(arr)
sigma = np.sqrt(variance)
x = np.linspace(min(arr), max(arr), 100)
dx = result[1][1] - result[1][0]
scale = len(arr)*dx
plt.plot(x, mlab.normpdf(x, mean, sigma)*scale)

plt.show()

请注意scale系数,该系数是根据项目数乘以单个条形的宽度计算得出的.

Note the scale factor calculated from the number of items times the width of a single bar.

这篇关于使用MatPlotLib和Numpy将高斯拟合到直方图-错误的Y缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆