如何通过指数曲线拟合数据

时间:2016-04-25 07:42:47

标签: python matplotlib plot curve-fitting exponential

我的项目有点问题,因为我有一组数据,我绘制它以获得2条曲线,我想用指数曲线拟合这些图。

我看过这篇文章:fitting exponential decay with no initial guessing。 但我的例子有点不同。

这就是我对数据的看法:

enter image description here

我的脚本如下:

mask_G = np.bitwise_and( tbdata['G'] < 99.99, tbdata['GERR'] < 0.2)
mask_R = np.bitwise_and( tbdata['R'] < 99.99, tbdata['RERR'] < 0.2)

G_corrected = tbdata[mask_G]
R_corrected = tbdata[mask_R]


fig13 = plt.gcf()
fig13.set_size_inches(16, 9)


fig13, (ax1,ax2) = plt.subplots(1,2)

fig_error_g = ax1.plot(G_corrected['G'], G_corrected['GERR'], '.')
ax1.set_xlabel('G')
ax1.set_ylabel('GERR')
ax1.set_title('Evolution de GERR en fonction de G')

fig_error_r = ax2.plot(R_corrected['R'], R_corrected['RERR'], '.')
ax2.set_xlabel('R')
ax2.set_ylabel('RERR')
ax2.set_title('Evolution de RERR en fonction de R')

fig13.tight_layout() 

plt.savefig('graphique.png')

plt.show()

我试着写这个,基于scipy doc:

def exponential(x,a,b,c) :
    return a * np.exp(-b * x) + c

xdata = G_corrected['G']
y = G_corrected['GERR']
ydata = y + 0.2 * np.random.normal(size=len(xdata))

popt, pcov = curve_fit(exponential, xdata, ydata)

但我明白了:

  

/home/user/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.py:601:OptimizeWarning:无法估计参数的协方差
  类别= OptimizeWarning)

您对我如何处理有任何想法吗?

非常感谢你;)

编辑:

我试着像我那样适合我的情节:

mask_G = np.bitwise_and( tbdata['G'] < 99.99, tbdata['GERR'] < 0.2)
mask_R = np.bitwise_and( tbdata['R'] < 99.99, tbdata['RERR'] < 0.2)

G_corrected = tbdata[mask_G]
R_corrected = tbdata[mask_R]


params = np.polyfit(G_corrected['G'], np.log(G_corrected['GERR']),1)
a = params[0]
A = np.exp(params[1])


fig13 = plt.gcf()
fig13.set_size_inches(16, 9)


fig13, (ax1,ax2) = plt.subplots(1,2)

fig_error_g = ax1.plot(G_corrected['G'], (G_corrected['GERR']), '.')
fig_error_g = ax1.plot(G_corrected['G'], (A*np.exp(a*G_corrected['G'])),'.')

ax1.set_xlabel('G')
ax1.set_ylabel('GERR')
ax1.set_title('Evolution de GERR en fonction de G')

fig_error_r = ax2.plot(R_corrected['R'], np.log(R_corrected['RERR']), '.')
ax2.set_xlabel('R')
ax2.set_ylabel('RERR')
ax2.set_title('Evolution de RERR en fonction de R')

fig13.tight_layout() 

plt.savefig('graphique.png')

plt.show()

我得到了:

enter image description here

您如何看待结果?

1 个答案:

答案 0 :(得分:3)

最简单的方法是将对数缩放应用于绘图。你当然知道log(exp(x))= x,即如果你将log()应用于你的y值并绘制你应该得到一个线性图。完成后,您可以使用线性工具箱(Gaussian Least Square method)。得到的斜率是exp(ax)中的前因,你试图获得它。

如果你对x轴有另一种依赖关系,那么制作一个数据的对数 - 日志图来确定所有的依赖关系可能是有益的。