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 个值的列表。
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.
添加回答
举报