Plotting Pairwise Relationships
The PairGrid is a subplot grid for plotting pairwise relationships in a dataset.
It creates a matrix of axes where each variable in the dataset is shared across a row and a column.
- Diagonal: shows the univariate distribution of a single variable (since x=y);
- Off-Diagonal: shows the bivariate relationship between two different variables.
Controlling the Grid
Unlike pairplot (which is fully automatic), PairGrid requires you to map plots to specific sections explicitly.
g.map_diag(func): plots on the diagonal (e.g.,sns.histplot);g.map_offdiag(func): plots on all non-diagonal cells (e.g.,sns.scatterplot);g.map_upper(func)/g.map_lower(func): plots specifically in the upper or lower triangle of the grid.
Example
Here we create a grid where the diagonal shows histograms and the lower triangle shows density contours.
123456789101112131415import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('penguins') # 1. Initialize the grid g = sns.PairGrid(df, hue='species') # 2. Map plots to specific regions g.map_diag(sns.histplot) # Diagonal: Histograms g.map_offdiag(sns.scatterplot) # Off-diagonal: Scatterplots g.add_legend() plt.show()
Swipe to start coding
Create a customized grid to analyze the relationships between penguin measurements.
- Set the style to
'ticks'. Change the figure background color to'lightpink'('figure.facecolor'). - Initialize the
PairGrid(g):- Use the
dfdataset. - Color the data points by
'species'(hue). - Use the
'rocket_r'palette. - Set
diag_sharey=False(this allows diagonal plots to have their own Y-axis scale).
- Use the
- Diagonal plots: map
sns.histplotto the diagonal using.map_diag(). Add a KDE curve (kde=True). - Off-diagonal plots: map
sns.scatterplotto the rest of the grid using.map_offdiag(). Set the point border width (linewidth) to0.9and the border color (edgecolor) to'purple'. - Add the legend and display the plot.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.55
Plotting Pairwise Relationships
Swipe to show menu
The PairGrid is a subplot grid for plotting pairwise relationships in a dataset.
It creates a matrix of axes where each variable in the dataset is shared across a row and a column.
- Diagonal: shows the univariate distribution of a single variable (since x=y);
- Off-Diagonal: shows the bivariate relationship between two different variables.
Controlling the Grid
Unlike pairplot (which is fully automatic), PairGrid requires you to map plots to specific sections explicitly.
g.map_diag(func): plots on the diagonal (e.g.,sns.histplot);g.map_offdiag(func): plots on all non-diagonal cells (e.g.,sns.scatterplot);g.map_upper(func)/g.map_lower(func): plots specifically in the upper or lower triangle of the grid.
Example
Here we create a grid where the diagonal shows histograms and the lower triangle shows density contours.
123456789101112131415import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('penguins') # 1. Initialize the grid g = sns.PairGrid(df, hue='species') # 2. Map plots to specific regions g.map_diag(sns.histplot) # Diagonal: Histograms g.map_offdiag(sns.scatterplot) # Off-diagonal: Scatterplots g.add_legend() plt.show()
Swipe to start coding
Create a customized grid to analyze the relationships between penguin measurements.
- Set the style to
'ticks'. Change the figure background color to'lightpink'('figure.facecolor'). - Initialize the
PairGrid(g):- Use the
dfdataset. - Color the data points by
'species'(hue). - Use the
'rocket_r'palette. - Set
diag_sharey=False(this allows diagonal plots to have their own Y-axis scale).
- Use the
- Diagonal plots: map
sns.histplotto the diagonal using.map_diag(). Add a KDE curve (kde=True). - Off-diagonal plots: map
sns.scatterplotto the rest of the grid using.map_offdiag(). Set the point border width (linewidth) to0.9and the border color (edgecolor) to'purple'. - Add the legend and display the plot.
Solution
Thanks for your feedback!
single