如何根据测量数据拟合图? [英] How can I fit my plots from measured data?
问题描述
如何拟合图?
到目前为止,我有以下代码,该代码可以绘制数组中的各种图形(实验中的数据)放入循环中
Up to now, I've got the following code, which plots a variety of graphs from an array (data from an experiment) as it is placed in a loop:
import matplotlib as plt
plt.figure(6)
plt.semilogx(Tau_Array, Correlation_Array, '+-')
plt.ylabel('Correlation')
plt.xlabel('Tau')
plt.title("APD" + str(detector) + "_Correlations_log_graph")
plt.savefig(DataFolder + "/APD" + str(detector) + "_Correlations_log_graph.png")
到目前为止,这种方法可以使用对数图,但是我想知道拟合过程在这里如何工作。最后,我希望有一个最能描述我所测量数据的公式或/和图表。
This works so far with a logarithmic plot, but I am wondering how the fitting process could work right here. In the end I would like to have some kind of a formula or/and a graph which best describes the data I measured.
如果有人可以帮助我,我将非常高兴!
I would be pleased if someone could help me!
推荐答案
您可以在 scipy.optimize <中使用
curve_fit
/ code>。这是一个示例
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()
在您的情况下,可以相应地定义 func
。 popt
是一个包含拟合参数值的数组。
In yourcase, you can define the func
accordingly. popt
is an array containing the value of your fitting parameters.
这篇关于如何根据测量数据拟合图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!