Python 如何用指数曲线拟合数据

Python 如何用指数曲线拟合数据,python,matplotlib,plot,curve-fitting,exponential,Python,Matplotlib,Plot,Curve Fitting,Exponential,我的项目有点小问题,因为我有一组数据,我绘制它以得到两条曲线,我想用指数曲线拟合这个曲线 我看了这篇帖子:。 但我的例子有点不同 这是我从数据中得到的: 我的脚本如下: 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 = t

我的项目有点小问题,因为我有一组数据,我绘制它以得到两条曲线,我想用指数曲线拟合这个曲线

我看了这篇帖子:。 但我的例子有点不同

这是我从数据中得到的:

我的脚本如下:

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()
但我得到:

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

你知道我怎么处理吗

非常感谢;)

编辑:

我试着把我的情节安排成这样:

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()
mask\u G=np.位和(tbdata['G']<99.99,tbdata['GERR']<0.2)
掩码R=np.位和(tbdata['R']<99.99,tbdata['RERR']<0.2)
G_corrected=tbdata[mask_G]
R_corrected=tbdata[mask_R]
参数=np.polyfit(G_校正['G'],np.log(G_校正['GERR']),1)
a=参数[0]
A=np.exp(参数[1])
图13=plt.gcf()
图13.设置尺寸英寸(16,9)
图13,(ax1,ax2)=plt.子批次(1,2)
图(g)误差(g(g)校正(g)(g)校正(g)(g(g)校正(g)
fig_error_g=ax1.绘图(g_校正['g'],(A*np.exp(A*g_校正['g']),'。)
ax1.set_xlabel('G')
ax1.set_ylabel('GERR'))
ax1.设置标题(‘G条款的演变’)
fig_error_r=ax2.plot(r_corrected['r'],np.log(r_corrected['RERR']),'
ax2.set_xlabel('R')
ax2.set_ylabel('RERR')
ax2.设置标题(‘基础功能的演变’)
图13.紧_布局()
plt.savefig('graphique.png'))
plt.show()
我得到:


您认为结果如何?

最简单的方法是对绘图应用对数缩放。正如您肯定知道的,log(exp(x))=x,也就是说,如果您将log()应用于y值并进行绘图,则应该得到一个线性绘图。一旦有了它,就可以用线性工具箱()来拟合它。得到的斜率是exp(ax)中的预因子,您将尝试获取它


如果您在x轴上有另一个依赖项,那么对您的数据进行日志绘制以找出所有依赖项可能是有益的。

谢谢您的回答。正如你所说的,我在我的y值的绘图中应用了对数缩放(即问题编辑)。现在,我需要使用scipy提供的
曲线拟合
?我会在日志数据上使用线性拟合:-你能在对数尺度上绘制数据,看看它在该尺度上是否是线性的(你的编辑)?可能是日志缩放,即两个轴都应该这样做。
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()