pandas Python:可视化数据直方图上的正态曲线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27115531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Visualize a normal curve on data's histogram
提问by HolaGonzalo
Thanks in advance for any assistance or tips.
在此先感谢您提供任何帮助或提示。
I'm trying to visualize the fitted normal to one of my dataframe's column. So far, I've been able to plot the histogram by:
我正在尝试将拟合法线可视化到我的数据框列之一。到目前为止,我已经能够通过以下方式绘制直方图:
df.radon_adj.hist(bins=30)


I've this 'template', but I encounter errors.
我有这个“模板”,但我遇到了错误。
import pylab as py
import numpy as np
from scipy import optimize
# Generate a
y = df.radon_adj
data = py.hist(y, bins = 25)
# Equation for Gaussian
def f(x, a, b, c):
return a * py.exp(-(x - b)**2.0 / (2 * c**2))
# Generate data from bins as a set of points
x = [0.5 * (data[1][i] + data[1][i+1]) for i in xrange(len(data[1])-1)]
y = data[0]
popt, pcov = optimize.curve_fit(f, x, y)
x_fit = py.linspace(x[0], x[-1], 100)
y_fit = f(x_fit, *popt)
plot(x_fit, y_fit, lw=4, color="r")
回答by JD Long
I wouldn't reinvent the wheel by defining the equation for Gaussian. Stand on the shoulders of the scipy package:
我不会通过定义高斯方程来重新发明轮子。站在 scipy 包的肩膀上:
from scipy.stats import norm
df = pd.DataFrame({'A': np.random.normal(size=100)})
df.A.plot(kind='hist', normed=True)
range = np.arange(-4, 4, 0.001)
plt.plot(range, norm.pdf(range,0,1))


Note that the only 'magic' here is making sure the histogram is normed.
请注意,这里唯一的“魔法”是确保直方图是规范的。

![pandas 日期时间索引 KeyError: '标签 [2000-01-03 00:00:00] 不在 [索引]'](/res/img/loading.gif)