python - 如何用指数曲线拟合数据

标签 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 文档编写该内容:

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: Covariance of the parameters could not be estimated
category=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

您对这个结果有何看法?

最佳答案

最简单的方法是将对数缩放应用于绘图。您当然知道 log(exp(x)) = x,即如果您将 log() 应用于您的 y 值并绘制,您应该得到一个线性图。一旦你有了它,你就可以将它与你的线性工具箱( Gaussian Least Square method )相匹配。所得斜率是您尝试获取的 exp(ax) 中的前置因子。

如果您对 x 轴有其他依赖关系,则制作数据的对数图以找出所有依赖关系可能会有所帮助。

关于python - 如何用指数曲线拟合数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36834637/

相关文章:

python - Pandas 散点图 : stop automatic scaling of axis

python - 试图从谷歌抓取图像

python - 在没有 fillna 或 Interpolate 的情况下从数据框中删除 NaN 值

python - Matplotlib 输出图像质量

python - Matplotlib : what is wrong with my code? 子图中循环内的图例

graphics - 打印机/绘图仪上的 Direct3D 输出

numpy 数组的 Python 类列表字符串表示

python - 如何从 header 中的 Content-Disposition 获取文件名

python - Pyplot - 当一个轴的列表长度不一致时,如何在同一个图表上绘制多条线?

python - x y z 轴的范围相同