Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Plotting Pairwise Relationships | Section
Statistical Visualization with Seaborn

bookPlotting 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.

123456789101112131415
import 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()
copy
Task

Swipe to start coding

Create a customized grid to analyze the relationships between penguin measurements.

  1. Set the style to 'ticks'. Change the figure background color to 'lightpink' ('figure.facecolor').
  2. Initialize the PairGrid (g):
    • Use the df dataset.
    • 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).
  3. Diagonal plots: map sns.histplot to the diagonal using .map_diag(). Add a KDE curve (kde=True).
  4. Off-diagonal plots: map sns.scatterplot to the rest of the grid using .map_offdiag(). Set the point border width (linewidth) to 0.9 and the border color (edgecolor) to 'purple'.
  5. Add the legend and display the plot.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 19
single

single

Ask AI

expand

Ask AI

ChatGPT

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

close

bookPlotting 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.

123456789101112131415
import 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()
copy
Task

Swipe to start coding

Create a customized grid to analyze the relationships between penguin measurements.

  1. Set the style to 'ticks'. Change the figure background color to 'lightpink' ('figure.facecolor').
  2. Initialize the PairGrid (g):
    • Use the df dataset.
    • 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).
  3. Diagonal plots: map sns.histplot to the diagonal using .map_diag(). Add a KDE curve (kde=True).
  4. Off-diagonal plots: map sns.scatterplot to the rest of the grid using .map_offdiag(). Set the point border width (linewidth) to 0.9 and the border color (edgecolor) to 'purple'.
  5. Add the legend and display the plot.

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Β 1. ChapterΒ 19
single

single

some-alt