How can I fit my plots from measured data?

人盡茶涼 提交于 2019-12-04 19:02:49

You can use curve_fit from scipy.optimize for this. Here is an example

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

def func(x,a):
   return np.exp(a*x)

x,y,z = np.loadtxt("fit3.dat",unpack=True)

popt,pcov = curve_fit(func,x,y)
y_fit = np.exp(popt[0]*x)


plt.plot(x,y,'o')
plt.errorbar(x,y,yerr=z)
plt.plot(x,y_fit)
plt.savefig("fit3_plot.png")
plt.show()

In yourcase, you can define the func accordingly. popt is an array containing the value of your fitting parameters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!