【问题标题】:Simple fit regression line in pythonpython中的简单拟合回归线
【发布时间】:2018-06-16 11:49:42
【问题描述】:

我从 dataFrame 绘制的数据看起来像这样 Image 现在我需要添加一条适合现有图的回归线。

添加回归线最简单的方法是什么?

使用的代码是这样的:

df2 = myReport.loc[:, ['Quantity']]
print(df2)

plt.plot(df2, linestyle="", marker="." )
plt.axis([0,45,5023203,5938119])
plt.grid(True)
plt.legend(df2.columns)
plt.ylabel ('Population')
plt.xlabel ('Timeline')

plt.show()

使用 python 2.7 版。

【问题讨论】:

    标签: python plot regression


    【解决方案1】:

    试试

    from scipy.stats import linregress
    X = np.arange(length(df2))
    y = df2
    
    slope, intercept, r_value, p_value, std_err = linregress(X,y)
    
    x = np.linspace(x.min(), x.max(),1001)
    plt.plot(x, slope*x+intercept)
    

    【讨论】:

    • @dunkerboy 如果这是你喜欢的,你能接受答案吗?
    猜你喜欢
    • 2016-04-14
    • 1970-01-01
    • 2021-08-02
    • 2021-01-15
    • 2014-05-04
    • 2020-10-22
    • 1970-01-01
    • 2019-05-15
    • 2017-08-20
    相关资源
    最近更新 更多