想要拟合一个指数型曲线,如下
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(x, a, b, c):
return a * np.exp(-b * x) + c
#Define the data to be fit with some noise:
P_example = np.array([750,950,1150,1350])
Q_example = np.array([80190,43100,20010,8030])
#Fit for the parameters a, b, c of the function func:
popt, pcov = curve_fit(func,P_example, Q_example)
a = popt[0]
b = popt[1]
c = popt[2]
Pest = np.array([750,950,1150,1350])
Q_est = func(Pest, a, b,c)
plt.scatter(Pest, Q_est)
plt.plot(Pest, Q_est, color="#72CD28", label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
为什么会是一条直线呢?