I would like to create a simple linear regression chart just like in excel. With the shortest way possible.

Which is the easiest way to to plot a stock returns chart with a regression line using the pandas .plot ?

有帮助吗?

解决方案

It would be pretty simple with statsmodels

import statsmodels.api as sm
mod = sm.OLS.from_formula('y ~ x', data=df)  # y and x are column names in the DataFrame
res = mod.fit()
fig, ax = plt.subplots()
sm.graphics.abline_plot(model_results=res, ax=ax)
df.plot(kind='scatter', x='x', y='y', ax=ax)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top