Seaborn Pairplot in Detail| Python Seaborn Tutorial

Seaborn Pairplot uses to get the relation between each and every variable present in Pandas DataFrame. It works like a seaborn scatter plot but it plot only two variables plot and sns paiplot plot the pairwise plot of multiple features/variable in a grid format.

How to plot Seaborn Pairplot?

To plot seaborn pairplot we use sns.pairplot() function.

Syntax: sns.pairplot(
                                        data,
                                        hue=None,
                                        hue_order=None,
                                        palette=None,
                                        vars=None,
                                        x_vars=None,
                                        y_vars=None,
                                        kind=’scatter’,
                                        diag_kind=’auto’,
                                        markers=None,
                                        height=2.5,
                                        aspect=1,
                                        dropna=True,
                                        plot_kws=None,
                                        diag_kws=None,
                                        grid_kws=None,
                                        size=None,
                                        )

For the best understanding, I suggest you follow the seaborn scatter plot and matplotlib scatter plot tutorial.

Note: Practical perform on Jupyter NoteBook and at the end of this seaborn pairplot tutorial, you will get ‘.ipynb‘ file for download.

Import Libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

Load Dataset

from sklearn.datasets import load_breast_cancer
cancer_dataset = load_breast_cancer()
# create datafrmae
cancer_df = pd.DataFrame(np.c_[cancer_dataset['data'],cancer_dataset['target']],
             columns = np.append(cancer_dataset['feature_names'], ['target']))
cancer_df.head(6)

Plotting Seaborn Pairplot

data: data parameter is the required parameter to pass data to plot pairplot

#plot seaborn pairplot
sns.pairplot(cancer_df)

Output>>>

seaborn pairplot with all features

vars: It hep to plot pairplot accounting to required features/variable. Pass list of variable names, optional

sns.pairplot(cancer_df, vars = ['mean radius', 'mean texture', 'mean perimeter', 'mean area',
        'mean smoothness'])

Output>>>

seaborn pairplot vars

hue: Map the third feature to get more insights. Pass string (variable name), optional

sns.pairplot(cancer_df, vars = ['mean radius', 'mean texture', 'mean perimeter', 'mean area',
        'mean smoothness'], hue ='target')

Output>>>

seaborn pairplot hue

hue_order: To change the order of hue. Pass list of strings

sns.pairplot(cancer_df, vars = ['mean radius', 'mean texture', 'mean perimeter', 'mean area',
        'mean smoothness'], hue ='target', hue_order = [1.0, 0.0])

Output>>>

sns pairplot hue_order

palette: To change the color of the seaborn pairplot, it works like cmap. Pass dict or seaborn color palette

sns.pairplot(cancer_df, vars = ['mean radius', 'mean texture', 'mean perimeter', 'mean area',
        'mean smoothness'], hue ='target', palette='Dark2')

Output>>>

seaborn pairplot palette

x_vars, y_vars: If you want required features on the x-axis and the y-axis the use it. Pass lists of variable names, optional

sns.pairplot(cancer_df, hue ='target', x_vars = ['mean radius', 'mean texture'], y_vars =['mean radius'])

Output>>>

seaborn pairplot x_vars and y_vars

kind: To find the linearity. Pass {‘scatter’, ‘reg’}, optional

sns.pairplot(cancer_df, vars = ['mean radius', 'mean texture', 'mean perimeter', 'mean area',
        'mean smoothness'], hue ='target', kind = 'reg')

Output>>>

seaborn pairplot kind

diag_kind: To change the diagonal distribution. Pass {‘auto’, ‘hist’, ‘kde’}, optional

sns.pairplot(cancer_df, vars = ['mean radius', 'mean texture', 'mean perimeter', 'mean area',
        'mean smoothness'], hue ='target', diag_kind = 'hist')

Output>>>

seaborn pairplot diag_kind

markers: To change the markers of scatter dots. Pass single matplotlib marker code or list, optional

sns.pairplot(cancer_df, vars = ['mean radius', 'mean texture', 'mean perimeter', 'mean area',
        'mean smoothness'], hue ='target', markers = ['*', "<"])

Output>>>

seaborn pairplot markers

height: To change the height of the seaborn scatterplot. Pass scalar, optional

sns.pairplot(cancer_df, vars = ['mean radius', 'mean texture', 'mean perimeter', 'mean area',
        'mean smoothness'], hue ='target', height = 20)

Output>>>

sns pairplot height

Conclusion

In the seaborn pairplot tutorial, we learn how to create a seaborn pairplot with a real-time example using sns.pairplot() function. Along with that used different functions, and parameters. We suggest you make your hand dirty with each and every parameter of the above function because This is the best coding practice. Still, you didn’t complete matplotlib tutorial then I recommend to you, catch it.

Download the above seaborn pairplot source code in Jupyter NoteBook file formate.

Official Document click here

Leave a Reply

Top