Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I draw a regression line and show parameters using scatterplot with a pandas dataframe?

I would like to produce a Scatterplot from a Pandas dataframe using the following code:

df.plot.scatter(x='one', y='two, title='Scatterplot') 

Is there a Parameter I can send with the Statement, so it plots a Regression line and shows the Parameters of the fit?

something like:

df.plot.scatter(x='one', y='two', title='Scatterplot', Regression_line)
like image 303
Markus W Avatar asked Apr 05 '16 08:04

Markus W


2 Answers

I don't think that there's such a paramter for DataFrame.plot(). However, you can easily achieve this using Seaborn. Just pass the pandas dataframe to lmplot (assuming you have seaborn installed):

import seaborn as sns
sns.lmplot(x='one',y='two',data=df,fit_reg=True) 
like image 93
Pascal dB Avatar answered Oct 04 '22 00:10

Pascal dB


You can use sk-learn to get the regression line combined with scatter plot.

from sklearn.linear_model import LinearRegression
X = df.iloc[:, 1].values.reshape(-1, 1)  # iloc[:, 1] is the column of X
Y = df.iloc[:, 4].values.reshape(-1, 1)  # df.iloc[:, 4] is the column of Y
linear_regressor = LinearRegression()
linear_regressor.fit(X, Y)
Y_pred = linear_regressor.predict(X)

plt.scatter(X, Y)
plt.plot(X, Y_pred, color='red')
plt.show()

enter image description here

like image 37
ospider Avatar answered Oct 03 '22 22:10

ospider