[英]Plot a curve to a set of points in python
我有
x_data = [3, 6, 9, 12, 16, 21, 28, 50]
和
y_data = [0, 333.33333333, 333.33333333, 333.33333333, 250, 200, 142.85714286, 45.45454545]
我附上了这张图:
我需要用 function 将其拟合到曲线上:f(x) = a * exp(-x / b) + c。
但是我无法让 scipy curve_fit
工作。 我怎么能 go 关于这个?
您必须定义要适合的 function ,然后将其传递给curve_fit
。 您的 function 必须先取自变量,然后再取所有剩余参数。
def func(x, a, b, c):
return a * np.exp(-x / b) + c
# Get parameters and covariance
params, cov = scipy.optimize.curve_fit(f=f, xdata=x_data, ydata=y_data)
# Declare dedicated letters for each parameter
a, b, c = params
# Plot
plt.plot(x_data, y_data)
plt.plot(x_data, [func(x, a=a, b=b, c=c) for x in x_data])
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.