Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

scatter plot with regression line python

Adding regression line to a scatterplot between two numerical variables is great way to see the linear trend. In this post, we will see two ways of making scatter plot with regression line using Seaborn in Python. And we will also see an example of customizing the scatter plot with regression line.

Let us load the packages we need to make scatter plot with regression line.

1
2
3
import seaborn as sns
import matplotlib.pyplot as plt
from vega_datasets import data
We use Seattle weather dataset available from vega_datasets.

1
2
seattle_weather = data.seattle_weather()
print(seattle_weather.head(n=3))
1
2
3
4
        date  precipitation  temp_max  temp_min  wind  weather
0 2012-01-01            0.0      12.8       5.0   4.7  drizzle
1 2012-01-02           10.9      10.6       2.8   4.5     rain
2 2012-01-03            0.8      11.7       7.2   2.3     rain
Instead of using the full dataset, we will subsample and randomly select 100 observations using Pandas sample() function.

1
df = seattle_weather.sample(100)
Now we are all set to make scatter plot with regression line. We will see two ways to add regression line to scatter plot.

Scatter plot with regression line: Seaborn regplot()
First, we can use Seaborn’s regplot() function to make scatter plot. And regplot() by default adds regression line with confidence interval.

In this example, we make scatter plot between minimum and maximum temperatures.

1
2
3
sns.regplot(x="temp_max",
            y="temp_min", 
            data=df);
We can customize the scatterplot by removing confidence interval band. With the additional argument ci=None, we get a scatter plot with regression line, but without confidence interval band.
Scatter plot with regression line: Remove CI band Seaborn regplot()
1
2
3
4
sns.regplot(x="temp_max",
            y="temp_min", 
            ci=None,
            data=df);
            
Scatter plot with regression line: Seaborn lmplot()
We can also use Seaborn’s lmplot() function and make a scatter plot with regression line. In this example below, we show the basic scatterplot with regression line using lmplot().

1
2
3
sns.lmplot(x="temp_max",
           y="temp_min", 
           data=df);
Scatter plot with regression line: Remove CI band Seaborn lmplot()
1
2
3
4
sns.lmplot(x="temp_max",
           y="temp_min", 
           ci=None,
           data=df);            
Comment

scatterplot with regression line

import seaborn as sns

#create scatterplot with regression line and confidence interval lines
sns.regplot(x, y)
Comment

python scatter matrix with regression line

from scipy.stats import pearsonr
def reg_coef(x,y,label=None,color=None,**kwargs):
    ax = plt.gca()
    r,p = pearsonr(x,y)
    ax.annotate('r = {:.2f}'.format(r), xy=(0.5,0.5), xycoords='axes fraction', ha='center')
    ax.set_axis_off()

iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map_diag(sns.distplot)
g.map_lower(sns.regplot)
g.map_upper(reg_coef)
Comment

scatterplot with regression line

import seaborn as sns

#create scatterplot with regression line
sns.regplot(x, y, ci=None)
Comment

PREVIOUS NEXT
Code Example
Python :: load data python 
Python :: oops concept in python 
Python :: python class arbitrary arguments 
Python :: pil format multiline text 
Python :: python for loop increment 
Python :: Python how to use __lt__ 
Python :: dictionary in python 
Python :: python loop index and value 
Python :: proper function pandas 
Python :: soustraire deux listes python 
Python :: hide tkinter window 
Python :: how to write a python comment 
Python :: check if item exists in list python 
Python :: pandas resample friday 
Python :: identity matrix python 
Python :: trim string to max length python 
Python :: keras loss plot 
Python :: request session python 
Python :: access to specific column array numpy 
Python :: pandas series 
Python :: what are args and kwargs in python 
Python :: shrink colorbar matplotlib 
Python :: Identify Null and NAN python 
Python :: pandas read csv python 
Python :: python install progressbar 
Python :: pandas if value present in df index 
Python :: python printing hello world 
Python :: compare lists element wise python 
Python :: open a python script on click flask 
Python :: python logger format 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =