Python 3.x statsmodels.api和scipy.stats无法生成正确的拟合

Python 3.x statsmodels.api和scipy.stats无法生成正确的拟合,python-3.x,scipy,statsmodels,best-fit,Python 3.x,Scipy,Statsmodels,Best Fit,我试图用scipy.stats和statsmodels.api通过两组数据绘制一条最佳拟合线 import matplotlib.pyplot as plt import numpy as np import statsmodels.api as sm from scipy import stats # toy data y1 = np.array([1,2,3,4,5]) x1 = np.array([2,4,6,8,10]) y2 = np.array([1,3.0,5.0,7.0,9

我试图用
scipy.stats
statsmodels.api
通过两组数据绘制一条最佳拟合线

import matplotlib.pyplot as plt
import numpy as np
import statsmodels.api as sm
from scipy import stats

# toy data
y1 =  np.array([1,2,3,4,5])
x1 =  np.array([2,4,6,8,10])
y2 = np.array([1,3.0,5.0,7.0,9.0])
x2 = np.array([1,2.9,5.3,7.4,8.9])

#  should produce straight lines through each data set
plt.scatter(x1, y1, label = 'LRIS')
plt.scatter(x2, y2, label = 'PFCam')
for x, y in zip([x1, x2], [y1, y2]):
    model = sm.OLS(y, sm.add_constant(x))
    results = model.fit()
    params = stats.linregress(x, y)
    plt.plot(params[0]*x + params[1])

plt.xlabel('log Integration time, t [s]')
plt.ylabel('V [mag]')
plt.legend()
plt.show()
产生


我不明白是什么产生了这样的“最佳”拟合线。

您想绘制X vs Y:

    plt.plot(x, x * params.slope + params.intercept)
LGTM


您想要绘制X与Y的对比图:

    plt.plot(x, x * params.slope + params.intercept)
LGTM