2013-07-29 161 views
0

嗨,我是编程新手,对我来说,我似乎无法弄清楚如何将高斯拟合到我的数据中。这是我目前拥有的。任何帮助将不胜感激!在python中拟合高斯数据

from numpy import * 
import matplotlib.pyplot as plt 

data = loadtxt("/home/*****") 

t,q = data[:,2], data[:,3]       
t,q = loadtxt("/home/*****", usecols = (2,3), unpack=True) 

plt.scatter(t,q, marker='.', s=20) 
plt.show() 
+0

你想绘制一个直方图,而不是一个散点图吗? – doctorlove

+0

此问题已被再次询问并在这里回答:http://stackoverflow.com/q/23156522/2062965 – strpeter

回答

0

您当前绘制散点图。 docs有一个绘制直方图的演示,该直方图可能是高斯,取决于您的数据分布。 你需要像

plt.hist(x, 50, normed=1, histtype='stepfilled') 

有进一步演示here

+0

但是,它不需要是一个2D直方图来说明数组的两列吗? – boson

+0

@boson我不清楚你想从问题中想做什么 – doctorlove

0

你可以看看astropy.modeling。高斯拟合的例子从他们的网站:

import numpy as np 
from astropy.modeling import models, fitting 

# Generate fake data 
np.random.seed(0) 
x = np.linspace(-5., 5., 200) 
y = 3 * np.exp(-0.5 * (x - 1.3)**2/0.8**2) 
y += np.random.normal(0., 0.2, x.shape) 

# Fit the data using a box model 
t_init = models.Trapezoid1D(amplitude=1., x_0=0., width=1., slope=0.5) 
fit_t = fitting.LevMarLSQFitter() 
t = fit_t(t_init, x, y) 

# Fit the data using a Gaussian 
g_init = models.Gaussian1D(amplitude=1., mean=0, stddev=1.) 
fit_g = fitting.LevMarLSQFitter() 
g = fit_g(g_init, x, y) 

# Plot the data with the best-fit model 
plt.figure(figsize=(8,5)) 
plt.plot(x, y, 'ko') 
plt.plot(x, t(x), 'b-', lw=2, label='Trapezoid') 
plt.plot(x, g(x), 'r-', lw=2, label='Gaussian') 
plt.xlabel('Position') 
plt.ylabel('Flux') 
plt.legend(loc=2)