可能对大家有参考价值的信息是:
- 如何用 curve_fit 进行 e 指数函数拟合;
- 如何将拟合后的结果输出到画布上;
- 坐标轴、标签、图例样式的设计;
- 文本框内容和格式。
话不多说,直接上代码。文末附上对应的图。
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter
def func(x,a,b):
return a*np.exp(b*x) - a
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()
ax.set_xlim(0.5,1.8)
ax.set_ylim(0,16)
ax.xaxis.set_major_locator(MultipleLocator(0.2))
ax.yaxis.set_major_locator(MultipleLocator(4))
ax.xaxis.set_minor_locator(AutoMinorLocator(2))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))
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)
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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)