Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 38 ยท Multi-plot Grids

Pair Plots and PairGrid

Compare every numeric column against every other with pairplot, then take full control with PairGrid: diagonal plots, triangles, hue, corner and custom functions.

When you first meet a new dataset with five or six numeric columns, the question is "what is related to what?". A pair plot answers it in one picture: a grid where each row and column is a variable, every off-diagonal panel is a scatter plot of two variables, and the diagonal shows each variable's own distribution.

pairplot() makes the standard version in one line. PairGrid is the building block underneath, and it lets you choose a different plot for the diagonal, the upper triangle and the lower triangle. This lesson covers both.

pairplot in one line

Python A first pair plot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With no arguments, pairplot() uses every numeric column, draws scatter plots off the diagonal and histograms on it. With hue, the diagonal becomes a KDE per group. The species separate clearly in some pairs (flipper against bill depth) and overlap in others, which tells you which measurements are most useful for telling them apart.

Key arguments:

Argument Effect
vars=[...] Choose which columns to use (otherwise all numeric ones).
x_vars, y_vars A non-square grid: some variables as rows, others as columns.
kind Off-diagonal plot: "scatter" (default), "kde", "hist", "reg".
diag_kind "auto", "hist", "kde" or None.
corner=True Draw only the lower triangle (no duplicate panels).
plot_kws, diag_kws Keyword dictionaries for the off-diagonal and diagonal plots.
palette, markers Group colours and marker shapes.

Corner and regression variants

A full grid shows each pair twice. corner=True removes the mirror image, and kind="reg" adds a fitted line to each panel:

Python corner=True with regression lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the plot_kws structure for kind="reg": it is passed to regplot, so it carries its own nested scatter_kws and line_kws. The shape of plot_kws always follows the function behind kind.

PairGrid for full control

PairGrid creates the empty grid. You then fill the regions with map_diag, map_upper, map_lower or map_offdiag. A common design: scatter below, density contours above, histograms on the diagonal.

Python Different plots above, below and on the diagonal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three rules to remember:

  1. Upper and lower are mirror regions. Plot something different in each to give the reader two views of every pair.
  2. hue is set on the grid, not on the mapped function, so every region is coloured consistently.
  3. Call add_legend() when you use hue.

Custom functions: put the correlation in the corner

Any function with the signature f(x, y, **kwargs) can fill map_upper. The inputs are the two Series, and color and label arrive through kwargs.

Python Correlation coefficients in the upper triangle

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The font size scales with the strength of the correlation, so strong pairs jump out. This layout is a favourite for reports because it carries the same information as a correlation heatmap plus the raw scatter.

Practical advice

  • Cost grows with the square of the variables. Ten variables mean 100 panels; choose vars carefully.
  • Large data: sample rows (df.sample(2000)), use small markers (s=8) and alpha, or draw hexbins/KDE in the triangles.
  • Categorical columns are ignored unless you list them in vars; use hue for them instead.
  • Ordering matters. Put related variables next to each other.

Pair plots show every pair; joint plots zoom in on one pair and add its marginal distributions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What appears on the diagonal of pairplot() by default?

    Plotting a variable against itself is pointless, so the diagonal shows its distribution as a histogram or KDE.
  2. 2. What does corner=True do?

    It removes the mirrored upper triangle, saving space.
  3. 3. Which method fills only the panels above the diagonal?

    map_upper, map_lower, map_diag and map_offdiag target the regions of a PairGrid.
  4. 4. Where do you set hue for a PairGrid?

    hue belongs to the grid so all regions stay consistent.

Summary

  • pairplot() plots every numeric pair; hue, vars, kind, diag_kind and corner tune it.
  • PairGrid lets you map different functions to the diagonal, the upper and the lower triangles.
  • Custom functions of the form f(x, y, **kwargs) work in any region.
  • Limit the variables and sample large data.

Next: Joint Plots and Marginal Distributions