英文:
Multiple curve fitting using Matplotlib
问题
我正在生成如下所示的三个图表。我还想对每个图表进行曲线拟合,使用一个误差函数,并打印出函数中定义的相应参数。但是我遇到了一个错误。我还展示了每个图表的外观。任何帮助将不胜感激。
```python
import matplotlib.pyplot as plt
import scipy.optimize as optimize
import numpy as np
import pandas as pd
import scipy
Folder1='1012 nodes_seed1711_var1_1000stepsize_0.4initial_K_1e3'
Folder2='1012 nodes_seed6541_var5_1000stepsize_0.4initial_K_1e3'
Folder3='1012 nodes_seed9637_var10_1000stepsize_0.4initial_K_1e3'
y1 = pd.read_csv(rf'D:\Users\debanikb\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder1}\Averaged_0.5_501files.txt',header=None)
y1 = y1.to_numpy()
y1 = 1-np.ravel(1.015*y1/1012)
y2 = pd.read_csv(rf'D:\Users\debanikb\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder2}\Averaged_0.5_501files.txt',header=None)
y2 = y2.to_numpy()
y2 = 1-np.ravel(y2/1012)
y3 = pd.read_csv(rf'D:\Users\debanikb\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder3}\Averaged_0.5_501files.txt',header=None)
y3 = y3.to_numpy()
y3 = 1-np.ravel(1.015*y3/1012)
x = pd.read_csv(rf'D:\Users\debanikb\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Plot_Fit\All_values_0_1000001_1000.txt',header=None)
x = x.to_numpy()
x = np.ravel(x)
plt.plot(x, y1, label='var 1')
plt.plot(x, y2, label='var 5')
plt.plot(x, y3, label='var 10')
def func(x, a, b, mu, sigma):
y = a* scipy.special.erf((x - mu)/(sigma*np.sqrt(2))) + b
return y
p0 = [1, 0, 1, 10]
popt, pcov = optimize.curve_fit(func, x, y1, p0=p0, maxfev=8000)
a_opt = popt[0]
b_opt = popt[1]
mu_opt = popt[2]
sigma_opt = popt[3]
print("优化后的数值:")
print("a =", a_opt)
print("b =", b_opt)
print("mu =", mu_opt)
print("sigma =", sigma_opt)
y_fit = func(x, *popt)
residuals = y1 - y_fit
ss_residuals = np.sum(residuals**2)
ss_total = np.sum((y1 - np.mean(y1))**2)
r_squared = 1 - (ss_residuals / ss_total)
print("R-平方:", r_squared)
plt.plot(x, func(x, *popt), label="拟合曲线")
plt.xlabel("经过的时间(秒)",fontsize=15.0)
plt.ylabel("侵入分数",fontsize=15.0)
plt.legend(loc='lower right')
plt.xlim(0, 1e6)
plt.show()
英文:
I am generating three plots as shown below. I also want to do curve fitting on each of these plots using an error function and print the corresponding parameters defined in the function. But I am getting an error. I also show how each of the plots look like. Any help would be much appreciated.
import matplotlib.pyplot as plt
import scipy.optimize as optimize
import numpy as np
import pandas as pd
import scipy
Folder1='1012 nodes_seed1711_var1_1000stepsize_0.4initial_K_1e3'
Folder2='1012 nodes_seed6541_var5_1000stepsize_0.4initial_K_1e3'
Folder3='1012 nodes_seed9637_var10_1000stepsize_0.4initial_K_1e3'
y1 = pd.read_csv(rf'D:\Users\debanikb\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder1}\Averaged_0.5_501files.txt',header=None)
y1 = y1. to_numpy()
#print("F1 =",F1)
y1 = 1-np.ravel(1.015*y1/1012)
#print("F1 =",[F1])
y2 = pd.read_csv(rf'D:\Users\debanikb\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder2}\Averaged_0.5_501files.txt',header=None)
y2 = y2. to_numpy()
#print("F1 =",F1)
y2 = 1-np.ravel(y2/1012)
y3 = pd.read_csv(rf'D:\Users\debanikb\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder3}\Averaged_0.5_501files.txt',header=None)
y3 = y3. to_numpy()
#print("F1 =",F1)
y3 = 1-np.ravel(1.015*y3/1012)
x = pd.read_csv(rf'D:\Users\debanikb\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\Plot_Fit\All_values_0_1000001_1000.txt',header=None)
x = x. to_numpy()
#print("t1 =",t1)
x = np.ravel(x)
#print("t1 =",[t1])
#plt.plot(x,y1,x,y2,x,y3, label="var1,var2,var3")
plt.plot(x, y1, label='var 1')
plt.plot(x, y2, label='var 5')
plt.plot(x, y3, label='var 10')
#popt, pcov = optimize.curve_fit(func, t1, F1, maxfev=1000)
def func(x, a, b, mu, sigma):
y = a* scipy.special.erf((x - mu)/(sigma*np.sqrt(2))) + b
return y
# Initial guess for the parameters
p0 = [1, 0, 1, 10]
# Fit the curve
popt, pcov = optimize.curve_fit(func, x, y1,y2,y3, p0=p0, maxfev=8000)
a_opt = popt[0] # Optimized value of parameter 'a'
b_opt = popt[1] # Optimized value of parameter 'b'
mu_opt = popt[2] # Optimized value of parameter 'z'
sigma_opt = popt[3] # Optimized value of parameter 'f'
print("Optimized values:")
print("a =", a_opt)
print("b =", b_opt)
print("mu =", mu_opt)
print("sigma =", sigma_opt)
# Calculate R-squared
y_fit = func(x, *popt)
residuals = y1 - y_fit
ss_residuals = np.sum(residuals**2)
ss_total = np.sum((y1 - np.mean(y1))**2)
r_squared = 1 - (ss_residuals / ss_total)
print("R-squared:", r_squared)
# #popt, pcov = optimize.curve_fit(func, x, y, maxfev=8000)
plt.plot(x, func(x, *popt), label="Fitted Curve")
plt.xlabel("Elapsed time (sec)",fontsize=15.0)
plt.ylabel("Invaded fraction",fontsize=15.0)
plt.legend(loc='lower right')
plt.xlim(0, 1e6)
plt.show()
The plots are
The error is
in compat_exec
exec(code, globals, locals)
File d:\users\debanikb\onedrive - technion\research_technion\python_pnm\surfactant a-d\plot_fit\plotting.py:61
popt, pcov = optimize.curve_fit(func, x, y1,y2,y3, p0=p0, maxfev=8000)
TypeError: curve_fit() got multiple values for argument 'p0'
答案1
得分: 2
在这行代码中:
popt, pcov = optimize.curve_fit(func, x, y1, p0=p0, maxfev=8000)
curve_fit() 只接受一个 Y 值集合。如果你想要拟合多个曲线,你需要多次调用 curve_fit()。
查看文档获取更多信息:https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
英文:
In this line:
popt, pcov = optimize.curve_fit(func, x, y1,y2,y3, p0=p0, maxfev=8000)
curve_fit() only accepts one set of Y values. If you want to fit multiple curves, you need to call curve_fit() multiple times.
See the documentation for more: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论