Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Pair Plot | Plotting with Seaborn
Ultimate Visualization with Python

Swipe to show menu

book
Pair Plot

Note
Definition

Pair plot is used to plot a pairwise relationship between the numeric variables in a dataset. It is quite similar to a joint plot, however, it is not limited to only two variables. In fact, a pair plot creates an NxN grid of the Axes objects (multiple subplots) where N is the number of numeric variables (numeric columns in a DataFrame).

Pair Plot Description

In a pair plot, each column shares the same x-axis variable, and each row shares the same y-axis variable. The diagonal shows histograms of individual variables, while the other plots display scatter plots.

Creating a Pair Plot

Creating a pair plot with seaborn comes down to calling its pairplot() function. Its most important and the only required parameter is data which should be a DataFrame object.

12345678910
import seaborn as sns import matplotlib.pyplot as plt # Loading the dataset with data about three different iris species iris_df = sns.load_dataset('iris') # Creating a pair plot sns.pairplot(iris_df, height=2, aspect=0.8) plt.show()
copy

The DataFrame iris_df is passed to the pairplot() function. The parameters height and aspect define the height and width (calculated as height multiplied by aspect) of each facet in inches.

Hue

Another parameter which is worth mentioning is hue which specifies the variable (column name) in data to map plot aspects to different colors or even create separate plots (on one Axes) for each of its values.

1234567891011121314
import seaborn as sns import matplotlib.pyplot as plt # Ignoring warnings import warnings warnings.filterwarnings('ignore') # Loading the dataset with data about three different iris species iris_df = sns.load_dataset('iris') # Setting the hue parameter to 'species' sns.pairplot(iris_df, hue='species', height=2, aspect=0.8) plt.show()
copy

You can clearly see the difference here. The data points in each scatter plot are colored based on the species they belong to, using the values from the species column. The diagonal plots are now KDE plots for each species instead of histograms.

In classification problems, it often makes sense to create a pair plot with the hue parameter set to the target variable, which is the categorical variable we want to predict.


The difference is clear. Data points in each scatter plot are colored according to their species, based on the values in the species column. The diagonal plots have been replaced with KDE plots for each species instead of histograms.

In classification tasks, creating a pair plot with the hue parameter set to the target variable β€” the categorical variable to be predicted β€” is often useful.

Changing Plot Kinds

You can change the type of plots used instead of the default scatter plots, as well as the plots shown on the diagonal. The kind parameter controls the main plots and defaults to scatter plots, while the diag_kind parameter controls the diagonal plots and is automatically chosen based on whether the hue parameter is set.

12345678910
import seaborn as sns import matplotlib.pyplot as plt # Loading the dataset with data about three different iris species iris_df = sns.load_dataset('iris') # Setting the kind parameter and diag_kind parameters sns.pairplot(iris_df, hue='species', kind='reg', diag_kind=None, height=2, aspect=0.8) plt.show()
copy

'scatter', 'kde', 'hist', 'reg' are possible values for the kind parameter.

diag_kind can be set to one of the following values:

  • 'auto';

  • 'hist';

  • 'kde';

  • None.

Everything is similar to the jointplot() function in this regard.

Note
Study More

Explore more in the pairplot() documentation.

Task

Swipe to start coding

  1. Use the correct function to create a pair plot.
  2. Set the data for the plot to be penguins_df via the first argument.
  3. Set 'sex' as the column which will map the plot aspects to different colors via specifying the second argument.
  4. Set the non-diagonal plots to have a regression line ('reg') via specifying the third argument.
  5. Set height to 2.
  6. Set aspect to 0.8.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 6
We're sorry to hear that something went wrong. What happened?

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Pair Plot

Note
Definition

Pair plot is used to plot a pairwise relationship between the numeric variables in a dataset. It is quite similar to a joint plot, however, it is not limited to only two variables. In fact, a pair plot creates an NxN grid of the Axes objects (multiple subplots) where N is the number of numeric variables (numeric columns in a DataFrame).

Pair Plot Description

In a pair plot, each column shares the same x-axis variable, and each row shares the same y-axis variable. The diagonal shows histograms of individual variables, while the other plots display scatter plots.

Creating a Pair Plot

Creating a pair plot with seaborn comes down to calling its pairplot() function. Its most important and the only required parameter is data which should be a DataFrame object.

12345678910
import seaborn as sns import matplotlib.pyplot as plt # Loading the dataset with data about three different iris species iris_df = sns.load_dataset('iris') # Creating a pair plot sns.pairplot(iris_df, height=2, aspect=0.8) plt.show()
copy

The DataFrame iris_df is passed to the pairplot() function. The parameters height and aspect define the height and width (calculated as height multiplied by aspect) of each facet in inches.

Hue

Another parameter which is worth mentioning is hue which specifies the variable (column name) in data to map plot aspects to different colors or even create separate plots (on one Axes) for each of its values.

1234567891011121314
import seaborn as sns import matplotlib.pyplot as plt # Ignoring warnings import warnings warnings.filterwarnings('ignore') # Loading the dataset with data about three different iris species iris_df = sns.load_dataset('iris') # Setting the hue parameter to 'species' sns.pairplot(iris_df, hue='species', height=2, aspect=0.8) plt.show()
copy

You can clearly see the difference here. The data points in each scatter plot are colored based on the species they belong to, using the values from the species column. The diagonal plots are now KDE plots for each species instead of histograms.

In classification problems, it often makes sense to create a pair plot with the hue parameter set to the target variable, which is the categorical variable we want to predict.


The difference is clear. Data points in each scatter plot are colored according to their species, based on the values in the species column. The diagonal plots have been replaced with KDE plots for each species instead of histograms.

In classification tasks, creating a pair plot with the hue parameter set to the target variable β€” the categorical variable to be predicted β€” is often useful.

Changing Plot Kinds

You can change the type of plots used instead of the default scatter plots, as well as the plots shown on the diagonal. The kind parameter controls the main plots and defaults to scatter plots, while the diag_kind parameter controls the diagonal plots and is automatically chosen based on whether the hue parameter is set.

12345678910
import seaborn as sns import matplotlib.pyplot as plt # Loading the dataset with data about three different iris species iris_df = sns.load_dataset('iris') # Setting the kind parameter and diag_kind parameters sns.pairplot(iris_df, hue='species', kind='reg', diag_kind=None, height=2, aspect=0.8) plt.show()
copy

'scatter', 'kde', 'hist', 'reg' are possible values for the kind parameter.

diag_kind can be set to one of the following values:

  • 'auto';

  • 'hist';

  • 'kde';

  • None.

Everything is similar to the jointplot() function in this regard.

Note
Study More

Explore more in the pairplot() documentation.

Task

Swipe to start coding

  1. Use the correct function to create a pair plot.
  2. Set the data for the plot to be penguins_df via the first argument.
  3. Set 'sex' as the column which will map the plot aspects to different colors via specifying the second argument.
  4. Set the non-diagonal plots to have a regression line ('reg') via specifying the third argument.
  5. Set height to 2.
  6. Set aspect to 0.8.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 6
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt