Python 利用 curve_fit 进行 e 指数函数拟合

可能对大家有参考价值的信息是:

  1. 如何用 curve_fit 进行 e 指数函数拟合;
  2. 如何将拟合后的结果输出到画布上;
  3. 坐标轴、标签、图例样式的设计;
  4. 文本框内容和格式。

话不多说,直接上代码。文末附上对应的图。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#导入必要的包
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter

#生成e指数函数
def func(x,a,b):
    return a*np.exp(b*x) - a

#生成x,y数据
x = np.linspace(0.6,1.7,23)
y = func(x,0.5,2.0)
y = y + 0.1 * np.random.randn(len(x))

#拟合
popt, pcov = curve_fit(func,x,y)

#获得拟合后的参数
a = popt[0]
b = popt[1]

#计算拟合所得数据
y2 = func(x,a,b)

#获取当前子图
ax = plt.gca()

# set x,y limits
ax.set_xlim(0.5,1.8)
ax.set_ylim(0,16)

# set x,y major_tick_locator
ax.xaxis.set_major_locator(MultipleLocator(0.2))
ax.yaxis.set_major_locator(MultipleLocator(4))

# set x,y minor_tick_locator
ax.xaxis.set_minor_locator(AutoMinorLocator(2))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))

# set tick format
ax.tick_params(which = "major", length = 8, width = 2, colors = "black", labelsize = 18, pad = 10)
ax.tick_params(which = "minor", length = 4, width = 1.2, colors = "black", labelsize = 18)

# set width of axis 
ax.spines['bottom'].set_linewidth(2)
ax.spines['left'].set_linewidth(2) 
ax.spines['right'].set_linewidth(2)
ax.spines['top'].set_linewidth(2)   

plt.text(0.05,0.8, r"$y$ = $a*e^{b*x} - a$", fontsize = 20, transform=ax.transAxes)
plt.text(0.05,0.7, r"$a$ = %1.3f" %a, fontsize = 20, transform=ax.transAxes)
plt.text(0.05,0.6, r"$b$ = %1.3f" %b, fontsize = 20, transform=ax.transAxes)

plot1 = plt.scatter(x,y, color="green", label='original values', zorder = 2, marker = "o", linewidth = 5)

plot2 = plt.plot(x,y2, color="red", ls = "-", label='curve_fit values', zorder = 1, linewidth = 2)

plt.xlabel(r'$x$', fontsize = 20)
plt.ylabel(r'$y$', fontsize = 20)
ax.legend(loc=(0.48,0.05), ncol=1, frameon = False, scatterpoints = 1, numpoints = 1, prop = {'size': 20} )

plt.savefig('Exp-Fit.png',bbox_inches='tight',dpi=100)

plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值