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

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

PREVIOUS NEXT
Code Example
Python :: function for permutation sampling and test statistic from a specified operation 
Python :: python synta error 
Python :: assemblyai 
Python :: Python RegEx Split – re.split() Syntax 
Python :: tutorial on how to search the database in your django project 
Python :: set layer start and end time arcpy 
Python :: methods accesory python 
Python :: return a table of selected features pandas 
Python :: threading lock example 
Python :: python4 
Python :: bagging algorithm 
Python :: python kivy black screen 
Python :: how to break out of while loop when the user hit ctrl + d python 
Python :: calculate sin cos tan python 
Python :: matplotlib boxplot fill box with pattern 
Python :: How to convert Gender to numeric variable 
Python :: 1042 uri solution 
Python :: Custom x, y-ticks using ax 
Python :: how to read file from terminal in python inside code 
Python :: py variable space padding 
Python :: write yaml file without deleting content 
Python :: how to know the number of CPu using python 
Python :: why we need open ( ) and close ( ) in os 
Python :: is python3 enough for react native 
Python :: can you use the astro a50 with a phone 
Python :: using -h on python file 
Python :: pyqt5 update display 
Python :: django BruteBuster error failed attempts 
Python :: assign multiple vabies in one line 
Python :: how to read a data file in python and build a list of files 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =