Reputation: 59
I want to plot a linear regression model fit for my dataset using Seaborn.
As this data set has different depths in water column (Bottom, Middle and Top), I wanted my plot to have 3 different colors, but the linear regression would be to the overall dataset. I divided this dataset to plot them separately just like the following:
fig, axarr = plt.subplots(3, sharex = True)
axarr[0].scatter(averageOBSB_3min,PumpBotSSC,c='r', label='Bottom')
axarr[0].scatter(averageOBSM_3min,PumpMidSSC,c='g', label='Middle')
axarr[0].scatter(averageOBST_3min,PumpTopSSC,c='b', label='Top')
But that doesn't work for Seaborn, obviously.
My question is: how can I have different colors on the plot, but do the regression for the hole dataset?
Upvotes: 2
Views: 7425
Reputation: 2508
This is an extension to @O.Suleiman answer and your comment. Seaborn lmplot
nor regplot
have a way how to directly annotate your fit with its parameters. For that you should check this post. To sum up both the solutions and since you do not provide any data I used seaborn dataset, what you would use is:
import seaborn as sns
from scipy import stats
# get coeffs of linear fit
slope, intercept, r_value, p_value, std_err = stats.linregress(tips['total_bill'],tips['tip'])
# Use lmplot to plot scatter points
sns.lmplot(x='total_bill', y='tip', hue='smoker', data=tips, fit_reg=False, legend=False)
# Use regplot to plot the regression line and use line_kws to set line label for legend
ax = sns.regplot(x="total_bill", y="tip", data=tips, scatter_kws={"zorder":-1},
line_kws={'label':"y={0:.1f}x+{1:.1f}".format(slope,intercept)})
# plot legend
ax.legend()
Upvotes: 2
Reputation: 918
Use mix of Seaborn's lmplot
and regplot
:
import seaborn as sns
#Use lmplot to plot scatter points
graph = sns.lmplot(x='x_value', y='y_value', hue='water_value', data=df, fit_reg=False)
#Use regplot to plot the regression line for the whole points
sns.regplot(x='x_value', y='y_value', data=df, scatter=False, ax=graph.axes[0, 0]))
Upvotes: 5