scipy 使用Matplotlib进行多曲线拟合

3mpgtkmj  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(154)

我正在生成三个图,如下所示。我还想使用误差函数对这些图中的每一个进行曲线拟合,并打印函数中定义的相应参数。但我得到一个错误。我还展示了每个情节的样子。如果你能帮忙的话,我将不胜感激。

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()

字符串
情节是


的数据
错误是

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'

vlurs2pr

vlurs2pr1#

在这一行中:

popt, pcov = optimize.curve_fit(func, x, y1,y2,y3, p0=p0, maxfev=8000)

字符串
curve_fit()只接受一组Y值。如果要拟合多条曲线,则需要多次调用curve_fit()。
请参阅文档了解更多信息:https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

相关问题