为了账号安全,请及时绑定邮箱和手机立即绑定

Python:曲线拟合看起来很混乱

Python:曲线拟合看起来很混乱

隔江千里 2023-09-26 14:53:59
我试图将曲线拟合到某些数据,但生成的曲线看起来像一团乱麻。不知道系数是否准确。使用这个示例数据集,它会打印出类似三角形的东西,而使用我的原始数据集,它看起来更糟。大部分都是教程。我尝试从备用教程中删除 sympy 代码,但这样做没有任何效果。import matplotlib.pyplot as pltfrom scipy.optimize import curve_fitimport numpy as npimport sympy as symx = [0.0009425070688029959,0.0009398496240601303,0.0018779342723004293,0.004694835680751241,0.0009425070688029959,0.004734848484848552,0.0018993352326685255,0.0009460737937558928]y = [0.0028301886792453904,0.003762935089369628,0.001881467544684814,0.0009433962264150743,0.0028301886792453904,0.0019029495718363059,0.0038058991436727804,0.0018939393939393534]"""Plot your data"""plt.plot(x, y, 'ro',label="Original Data")"""brutal force to avoid errors"""    x = np.array(x, dtype=float) #transform your data in a numpy array of floats y = np.array(y, dtype=float) #so the curve_fit can work"""create a function to fit with your data. a, b, c and d are the coefficientsthat curve_fit will calculate for you. In this part you need to guess and/or use mathematical knowledge to finda function that resembles your data"""def func(x, b, c, d):  return b * x * x + c * x + d"""make the curve_fit"""popt, pcov = curve_fit(func, x, y)"""The result is:popt[0] = a , popt[1] = b, popt[2] = c and popt[3] = d of the function,so f(x) = popt[0]*x**3 + popt[1]*x**2 + popt[2]*x + popt[3]."""print("b = " + str(popt[0]) + "  c = " + str(popt[1]) + "  d = " + str(popt[2]))"""tUse sympy to generate the latex sintax of the function"""xs = sym.Symbol('\lambda')    tex = sym.latex(func(xs,*popt)).replace('$', '')plt.title(r'$f(\lambda)= %s$' %(tex),fontsize=16)"""Print the coefficients and plot the funcion."""plt.plot(x, func(x, *popt), label="Fitted Curve") #same as line above \/#plt.plot(x, popt[0]*x**3 + popt[1]*x**2 + popt[2]*x + popt[3], label="Fitted Curve") plt.legend(loc='upper left')plt.show()
查看完整描述

2 回答

?
白衣染霜花

TA贡献1796条经验 获得超10个赞

这是因为 Matplotlib 只会在原始数据(x 和 y 数组中)的几个点之间并按照它们定义的顺序绘制线条。只有 3 个唯一的 x 值(加上一些噪声),这就是为什么您看到的看起来像三角形的原因。

解决方法是创建一个新数组,在您感兴趣的范围内均匀分布且有序的 x 值。您可以使用numpy 中的 linspace 函数来完成此操作。

例如,尝试执行第二个绘图命令:

x_eval = np.linspace(min(x), max(x), 100)

plt.plot(x_eval, func(x_eval, *popt), label="Fitted Curve")

x_eval上面是原始数据中最小和最大 x 值之间均匀分布的 100 个值的列表。


查看完整回答
反对 回复 2023-09-26
?
守候你守候我

TA贡献1802条经验 获得超10个赞

看起来你需要排序xdata。


尝试插入这个:


x,y = zip(*sorted(zip(x, y)))


这样


import matplotlib.pyplot as plt

from scipy.optimize import curve_fit

import numpy as np

import sympy as sym


x = [0.0009425070688029959,

0.0009398496240601303,

0.0018779342723004293,

0.004694835680751241,

0.0009425070688029959,

0.004734848484848552,

0.0018993352326685255,

0.0009460737937558928]

y = [0.0028301886792453904,

0.003762935089369628,

0.001881467544684814,

0.0009433962264150743,

0.0028301886792453904,

0.0019029495718363059,

0.0038058991436727804,

0.0018939393939393534]

"""

Plot your data

"""

plt.plot(x, y, 'ro',label="Original Data")


"""

brutal force to avoid errors

"""    


x,y = zip(*sorted(zip(x, y)))


x = np.array(x, dtype=float) #transform your data in a numpy array of floats 

y = np.array(y, dtype=float) #so the curve_fit can work


"""

create a function to fit with your data. a, b, c and d are the coefficients

that curve_fit will calculate for you. 

In this part you need to guess and/or use mathematical knowledge to find

a function that resembles your data

"""

def func(x, b, c, d):

  return b * x * x + c * x + d


"""

make the curve_fit

"""

popt, pcov = curve_fit(func, x, y)


"""

The result is:

popt[0] = a , popt[1] = b, popt[2] = c and popt[3] = d of the function,

so f(x) = popt[0]*x**3 + popt[1]*x**2 + popt[2]*x + popt[3].

"""

print("b = " + str(popt[0]) + "  c = " + str(popt[1]) + "  d = " + str(popt[2]))


"""t

Use sympy to generate the latex sintax of the function

"""

xs = sym.Symbol('\lambda')    

tex = sym.latex(func(xs,*popt)).replace('$', '')

plt.title(r'$f(\lambda)= %s$' %(tex),fontsize=16)


"""

Print the coefficients and plot the funcion.

"""


plt.plot(x, func(x, *popt), label="Fitted Curve") #same as line above \/

#plt.plot(x, popt[0]*x**3 + popt[1]*x**2 + popt[2]*x + popt[3], label="Fitted Curve") 


plt.legend(loc='upper left')

plt.show()


The plotted curve from the data above.



查看完整回答
反对 回复 2023-09-26
  • 2 回答
  • 0 关注
  • 147 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信