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)
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)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With