SoFunction
Updated on 2024-11-19

Python visualization learning seaborn plotting line regression curve

Quick Facts

1. Mapping data preparation

Still using the Iris iris dataset, see previous posts for details.

# Import the libraries to be used in this post, declaring the following:
import  as plt
import numpy as np
import pandas as pd 
import palettable
from pandas import Series,DataFrame
from sklearn import datasets
import seaborn as sns
import palettable
#Importing the Iris iris dataset (method 1)
# The method is more helpful in understanding the dataset
iris=datasets.load_iris()
x, y =,
y_1 = (['setosa' if i==0 else 'versicolor' if i==1 else 'virginica' for i in y])
pd_iris = (((x, y_1.reshape(150,1))),columns=['sepal length(cm)','sepal width(cm)','petal length(cm)','petal width(cm)','class'])
 
#astype modify data type object in pd_iris to float64
pd_iris['sepal length(cm)']=pd_iris['sepal length(cm)'].astype('float64')
pd_iris['sepal width(cm)']=pd_iris['sepal width(cm)'].astype('float64')
pd_iris['petal length(cm)']=pd_iris['petal length(cm)'].astype('float64')
pd_iris['petal width(cm)']=pd_iris['petal width(cm)'].astype('float64')
 
 
#Importing the Iris iris dataset (method II)
#This method can sometimes be Kaspersky, so it is discarded
#import seaborn as sns
#iris_sns = sns.load_dataset("iris")

Simple view of the dataset

2、

(x, y, data=None, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, seed=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=True, dropna=True, x_jitter=None, y_jitter=None, label=None, color=None, marker='o', scatter_kws=None, line_kws=None, ax=None)

regplot default parameter line regression plot

(dpi=100)
(style="whitegrid",font_scale=1.2)#Set theme, text size
g=(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             color='#000000',# set marker and line colors
             marker='*',#Set the marker shape
             )

Set point and fit line properties separately

(dpi=100)
(style="whitegrid",font_scale=1.2)
g=(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
              color='#000000',
              marker='*',
              scatter_kws={'s': 60,'color':'g',},# Set the scatter attribute, refer to
              line_kws={'linestyle':'--','color':'r'}# Set the line attributes, refer to

Confidence interval setting

Note the change in shaded area around the fitted line

(dpi=100)
(style="whitegrid",font_scale=1.2)
g=(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             color='#000000',
             marker='*',
             ci=60,# Confidence interval settings, the default is 95% confidence interval, the larger the line around the shaded area of the larger
             )

Extension of the fitting line intersects the coordinate axes

# extend the regression line to the axis limits
(dpi=100)
(style="whitegrid",font_scale=1.2)
g=(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             color='#000000',
             marker='*',
             truncate=False,# Let the fitted line intersect the axis
             )

Fitting Discrete Variable Curves

(dpi=100)
(style="whitegrid",font_scale=1.2)
x_discrete=[0 if i=='setosa' else 1 if i=='versicolor' else 2 for i in pd_iris['class']]#
g=(x=x_discrete, y='sepal width(cm)', data=pd_iris,#x is now a discrete variable
             color='#000000',
             marker='*',
             )

Polynomial regression fitting curve

(dpi=110)
(style="whitegrid",font_scale=1.2)
g=(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             marker='*',
             order=4,# Defaults to 1, the larger the more curved
             scatter_kws={'s': 60,'color':'#016392',},# Set the scatter attribute, cf.
             line_kws={'linestyle':'--','color':'#c72e29'}# Set the line attribute, cf.
             
             )

3、

(x, y, data, hue=None, col=None, row=None, palette=None, col_wrap=None, height=5, aspect=1, markers='o', sharex=True, sharey=True, hue_order=None, col_order=None, row_order=None, legend=True, legend_out=True, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, seed=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=True, x_jitter=None, y_jitter=None, scatter_kws=None, line_kws=None, size=None)

Combine () and FacetGrid for more flexibility than () to draw more customized graphics.

Fitting regression lines by variable

(dpi=100)
(style="whitegrid",font_scale=1.2)
g=(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             hue='class',
             )
.set_size_inches(10,8)

Scattermarker settings

(dpi=100)
(style="whitegrid",font_scale=1.2)
g=(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             hue='class',
             markers=['+','^','o'],   #Set the scattermarker
             )
.set_size_inches(10,8)

scattered palettes

(dpi=100)
(style="whitegrid",font_scale=1.2)
g=(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             hue='class',
             markers=['+','^','*'],
             scatter_kws={'s':180},
             palette=["#01a2d9", "#31A354", "#c72e29"], #color palette
             )
.set_size_inches(10,8)

Fitting line property settings

(dpi=100)
(style="whitegrid",font_scale=1.2)
g=(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             hue='class',
             markers=['+','^','*'],
             scatter_kws={'s':180},
             line_kws={'linestyle':'--'},#Fit line property settings
             palette=["#01a2d9", "#31A354", "#c72e29"],
             )
.set_size_inches(10,8)

Drawing of facets

(dpi=100)
(style="whitegrid",font_scale=1.2)
g=(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris,
             col='class',#Surfacing by class
             markers='*',
             scatter_kws={'s':150,'color':'#01a2d9'},
             line_kws={'linestyle':'--','color':'#c72e29'}, #Linear property setting
             )
.set_size_inches(10,8)

Above is Python visualization learning of seaborn plotting line regression curve details, more information about Python seaborn line regression curve please pay attention to my other related articles!