【发布时间】:2014-11-24 22:41:56
【问题描述】:
提前感谢您提供任何帮助或提示。
我正在尝试将拟合法线可视化到我的数据框列之一。到目前为止,我已经能够通过以下方式绘制直方图:
df.radon_adj.hist(bins=30)
我有这个“template”,但我遇到了错误。
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")
【问题讨论】:
-
一些示例数据在这里会很有用——没有它我们实际上无法运行您的示例代码。尝试模拟一个我们可以复制粘贴的小型示例数据集(
np.random.randn()对于生成随机数据非常有用)。 -
你试过从 Matplotlib 运行这个例子吗? matplotlib.org/examples/statistics/histogram_demo_features.html
-
stanford.edu/~mwaskom/software/seaborn/tutorial/… 我会从它的情节中解脱出来