Pair Plot
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.
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()
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.
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()
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.
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()
'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.
Explore more in the pairplot()
documentation.
Swipe to start coding
- Use the correct function to create a pair plot.
- Set the data for the plot to be
penguins_df
via the first argument. - Set
'sex'
as the column which will map the plot aspects to different colors via specifying the second argument. - Set the non-diagonal plots to have a regression line (
'reg'
) via specifying the third argument. - Set
height
to2
. - Set
aspect
to0.8
.
Solution
Thanks for your feedback!