使用Matplotlib进行多曲线拟合

huangapple go评论110阅读模式
英文:

Multiple curve fitting using Matplotlib

问题

  1. 我正在生成如下所示的三个图表我还想对每个图表进行曲线拟合使用一个误差函数并打印出函数中定义的相应参数但是我遇到了一个错误我还展示了每个图表的外观任何帮助将不胜感激
  2. ```python
  3. import matplotlib.pyplot as plt
  4. import scipy.optimize as optimize
  5. import numpy as np
  6. import pandas as pd
  7. import scipy
  8. Folder1='1012 nodes_seed1711_var1_1000stepsize_0.4initial_K_1e3'
  9. Folder2='1012 nodes_seed6541_var5_1000stepsize_0.4initial_K_1e3'
  10. Folder3='1012 nodes_seed9637_var10_1000stepsize_0.4initial_K_1e3'
  11. 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)
  12. y1 = y1.to_numpy()
  13. y1 = 1-np.ravel(1.015*y1/1012)
  14. 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)
  15. y2 = y2.to_numpy()
  16. y2 = 1-np.ravel(y2/1012)
  17. 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)
  18. y3 = y3.to_numpy()
  19. y3 = 1-np.ravel(1.015*y3/1012)
  20. 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)
  21. x = x.to_numpy()
  22. x = np.ravel(x)
  23. plt.plot(x, y1, label='var 1')
  24. plt.plot(x, y2, label='var 5')
  25. plt.plot(x, y3, label='var 10')
  26. def func(x, a, b, mu, sigma):
  27. y = a* scipy.special.erf((x - mu)/(sigma*np.sqrt(2))) + b
  28. return y
  29. p0 = [1, 0, 1, 10]
  30. popt, pcov = optimize.curve_fit(func, x, y1, p0=p0, maxfev=8000)
  31. a_opt = popt[0]
  32. b_opt = popt[1]
  33. mu_opt = popt[2]
  34. sigma_opt = popt[3]
  35. print("优化后的数值:")
  36. print("a =", a_opt)
  37. print("b =", b_opt)
  38. print("mu =", mu_opt)
  39. print("sigma =", sigma_opt)
  40. y_fit = func(x, *popt)
  41. residuals = y1 - y_fit
  42. ss_residuals = np.sum(residuals**2)
  43. ss_total = np.sum((y1 - np.mean(y1))**2)
  44. r_squared = 1 - (ss_residuals / ss_total)
  45. print("R-平方:", r_squared)
  46. plt.plot(x, func(x, *popt), label="拟合曲线")
  47. plt.xlabel("经过的时间(秒)",fontsize=15.0)
  48. plt.ylabel("侵入分数",fontsize=15.0)
  49. plt.legend(loc='lower right')
  50. plt.xlim(0, 1e6)
  51. 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.

  1. import matplotlib.pyplot as plt
  2. import scipy.optimize as optimize
  3. import numpy as np
  4. import pandas as pd
  5. import scipy
  6. Folder1='1012 nodes_seed1711_var1_1000stepsize_0.4initial_K_1e3'
  7. Folder2='1012 nodes_seed6541_var5_1000stepsize_0.4initial_K_1e3'
  8. Folder3='1012 nodes_seed9637_var10_1000stepsize_0.4initial_K_1e3'
  9. 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)
  10. y1 = y1. to_numpy()
  11. #print("F1 =",F1)
  12. y1 = 1-np.ravel(1.015*y1/1012)
  13. #print("F1 =",[F1])
  14. 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)
  15. y2 = y2. to_numpy()
  16. #print("F1 =",F1)
  17. y2 = 1-np.ravel(y2/1012)
  18. 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)
  19. y3 = y3. to_numpy()
  20. #print("F1 =",F1)
  21. y3 = 1-np.ravel(1.015*y3/1012)
  22. 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)
  23. x = x. to_numpy()
  24. #print("t1 =",t1)
  25. x = np.ravel(x)
  26. #print("t1 =",[t1])
  27. #plt.plot(x,y1,x,y2,x,y3, label="var1,var2,var3")
  28. plt.plot(x, y1, label='var 1')
  29. plt.plot(x, y2, label='var 5')
  30. plt.plot(x, y3, label='var 10')
  31. #popt, pcov = optimize.curve_fit(func, t1, F1, maxfev=1000)
  32. def func(x, a, b, mu, sigma):
  33. y = a* scipy.special.erf((x - mu)/(sigma*np.sqrt(2))) + b
  34. return y
  35. # Initial guess for the parameters
  36. p0 = [1, 0, 1, 10]
  37. # Fit the curve
  38. popt, pcov = optimize.curve_fit(func, x, y1,y2,y3, p0=p0, maxfev=8000)
  39. a_opt = popt[0] # Optimized value of parameter 'a'
  40. b_opt = popt[1] # Optimized value of parameter 'b'
  41. mu_opt = popt[2] # Optimized value of parameter 'z'
  42. sigma_opt = popt[3] # Optimized value of parameter 'f'
  43. print("Optimized values:")
  44. print("a =", a_opt)
  45. print("b =", b_opt)
  46. print("mu =", mu_opt)
  47. print("sigma =", sigma_opt)
  48. # Calculate R-squared
  49. y_fit = func(x, *popt)
  50. residuals = y1 - y_fit
  51. ss_residuals = np.sum(residuals**2)
  52. ss_total = np.sum((y1 - np.mean(y1))**2)
  53. r_squared = 1 - (ss_residuals / ss_total)
  54. print("R-squared:", r_squared)
  55. # #popt, pcov = optimize.curve_fit(func, x, y, maxfev=8000)
  56. plt.plot(x, func(x, *popt), label="Fitted Curve")
  57. plt.xlabel("Elapsed time (sec)",fontsize=15.0)
  58. plt.ylabel("Invaded fraction",fontsize=15.0)
  59. plt.legend(loc='lower right')
  60. plt.xlim(0, 1e6)
  61. plt.show()

The plots are

使用Matplotlib进行多曲线拟合

The error is

  1. in compat_exec
  2. exec(code, globals, locals)
  3. File d:\users\debanikb\onedrive - technion\research_technion\python_pnm\surfactant a-d\plot_fit\plotting.py:61
  4. popt, pcov = optimize.curve_fit(func, x, y1,y2,y3, p0=p0, maxfev=8000)
  5. TypeError: curve_fit() got multiple values for argument 'p0'

答案1

得分: 2

在这行代码中:

  1. 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:

  1. 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

huangapple
  • 本文由 发表于 2023年7月3日 13:58:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定