Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the line color in seaborn linear regression jointplot

As described in the seaborn API the following code will produce a linear regression plot.

import numpy as np, pandas as pd; np.random.seed(0)
import seaborn as sns; sns.set(style="white", color_codes=True)
tips = sns.load_dataset("tips")
g = sns.jointplot(x="total_bill", y="tip", data=tips, kind='reg')
sns.plt.show()

However, with a lot of data points the regression line is not really visible anymore. How can I change its color? I could not find a builtin seaborn command.

In case the line is in the background (i.e. behind the dots), I would also like to ask how to bring it to the front.

like image 685
n1000 Avatar asked Jul 22 '15 16:07

n1000


People also ask

How do you change the color of a regression line?

By default, the regression line is blue. To change the color we have to use the keyword color inside the geom_smooth( ) function.

What does SNS Jointplot do?

Draw a plot of two variables with bivariate and univariate graphs. This function provides a convenient interface to the JointGrid class, with several canned plot kinds.

What is the use of Seaborn Regplot () method?

regplot() : This method is used to plot data and a linear regression model fit. There are a number of mutually exclusive options for estimating the regression model.

What does the function SNS Lmplot () perform in Seaborn Library?

lmplot() method is used to draw a scatter plot onto a FacetGrid.


1 Answers

There are a couple approaches, as mwaskom tactfully pointed out. You can pass arguments to the joint plot, but setting color there affects the whole scatterplot:

import numpy as np, pandas as pd; np.random.seed(0)
import seaborn as sns#; sns.set(style="white", color_codes=True)
tips = sns.load_dataset("tips")
g = sns.jointplot(x="total_bill", y="tip", data=tips, kind='reg',
                  joint_kws={'color':'green'}) # Scatter and regression all green

enter image description here

Or pass a dictionary of line-plotting keywords through that dictionary of scatterplot keywords. I read seaborn/linearmodels.py to figure out where to do this, which was entertaining and informative in itself. Dict in dict:

g = sns.jointplot(x="total_bill", y="tip", data=tips, kind='reg',
                  joint_kws={'line_kws':{'color':'cyan'}}) # Only regression cyan

enter image description here

Or you can access the line after it's been plotted and change it directly. This depends on the regression line being the first line plotted, so could break with seaborn updates. It's also aesthetically/pedagogically different, as you don't recolor the uncertainty spread. It is a good way to get familiar with what the JointGrid object is and how else you might interact with it. (And maybe there are properties you can't set with the function call arguments, although I can't think of any.)

g = sns.jointplot(x="total_bill", y="tip", data=tips, kind='reg')
regline = g.ax_joint.get_lines()[0]
regline.set_color('red')
regline.set_zorder(5)

enter image description here

like image 143
cphlewis Avatar answered Oct 03 '22 15:10

cphlewis