Problem Statement:
I have some nice data in a pandas dataframe. I'd like to run simple linear regression on it:
Using statsmodels, I perform my regression. Now, how do I get my plot? I've tried statsmodels' plot_fit
method, but the plot is a little funky:
I was hoping to get a horizontal line which represents the actual result of the regression.
Statsmodels has a variety of methods for plotting regression (a few more details about them here) but none of them seem to be the super simple "just plot the regression line on top of your data" -- plot_fit
seems to be the closest thing.
Questions:
- The first picture above is from pandas' plot function, which returns a
matplotlib.axes._subplots.AxesSubplot
. Can I overlay a regression line easily onto that plot? - Is there a function in statsmodels I've overlooked?
- Is there a better way to put together this figure?
Two related questions:
- Plotting Pandas OLS linear regression results
- Getting the regression line to plot from a Pandas regression
Neither seems to have a good answer.
Sample data
motifScore expression
6870 1.401123 0.55
10456 1.188554 -1.58
12455 1.476361 -1.75
18052 1.805736 0.13
19725 1.110953 2.30
30401 1.744645 -0.49
30716 1.098253 -1.59
30771 1.098253 -2.04
abline_plot
I had tried this, but it doesn't seem to work... not sure why:
seaborn.regplot
which will accomplish what you need, if you are okay with having that dependency. – Gaitskell