seaborn and interactive charts · lesson 5 of 10
Parts in this lesson
Asns.pairplot(df, hue="g", vars=[...])every pair of columns, in a grid
Bsns.jointplot(data=df, x="a", y="b")one pair, with each variable's distribution
Csns.heatmap(corr, annot=True, vmin=-1, vmax=1, cmap="RdBu_r")a table of numbers as colours
Ddf.corr()the correlation of every pair of columns

Many variables at once

Last updated

A first look at a new dataset usually asks: how does everything relate to everything else? seaborn can answer in one figure. In this lesson you'll draw a pair plot of every measurement against every other, zoom in on one pair with a joint plot, sum the whole table up as a correlation heat map, and fix a heat map that has no key and a rainbow of colours.

By the end of this lesson you can

  • Draw a pair plot and a joint plot to explore several variables
  • Draw a correlation matrix as a heat map with a diverging map and a key
  • Say what a clustermap adds, and what it needs

Every pair at once

Run the cells. The first draws all four penguin measurements against each other.

explore.ipynb
  • sns.pairplot makes a grid with one row and one column per variable. Each panel off the diagonal is a scatter plot of its row's variable against its column's. The panels on the diagonal show each variable on its own: with hue=, a KDE curve per species. vars= picks the columns (otherwise it takes every numeric one, including year).
  • The grid is symmetric: the panel in row 2, column 1 is the one in row 1, column 2 with the axes swapped.
  • sns.jointplot zooms in on one pair: a scatter plot in the middle, and each variable's distribution along the top and right edges.

Looking along the grid, species separate in almost every panel, flipper length and body mass rise together tightly, and bill depth is the odd one out: Gentoos have the shallowest bills but the biggest bodies.

A pair plot is a picture, so you can't point at a group and ask where it is in the other panels. Interactive tools can, by brushing: select points in one panel and they light up everywhere. Try it on three of the measurements:

Linked pair plotdrag a box in any scatter panel
bill lengthbill depthflipper lengthbill lengthbill depthflipper length

Drag a box around a cluster of points in any scatter panel, or pick a group above.

Brush the long, shallow bills in the bill panel, and in the flipper panels they sit at the long end: almost all of them are Gentoos. Lesson 7.9 builds linked charts like this yourself, with Altair.

A correlation heat map

A pair plot of 10 variables has 100 panels. To see every pair at once in less space, sum each pair up as one number, its correlation (from −1 to 1), and draw the table of them as colours. Run the cells.

correlations.ipynb
  • penguins[[...]].corr() gives a 4 × 4 table: each measurement's correlation with each other one, with 1 down the diagonal (everything correlates perfectly with itself).
  • sns.heatmap draws it as a grid of coloured squares, and annot=True writes each number in its square, so nobody has to judge a colour to read a value.
  • The colours: cmap="RdBu_r" is a diverging map (6.7), red for positive and blue for negative; vmin=-1, vmax=1 and center=0 make zero exactly the pale middle, and the same shade mean the same strength above and below. The colour bar beside it is the key.

The strongest pair is flipper length and body mass, at 0.87. Bill depth is negatively correlated with everything else, the same mix-of-species effect as in 7.4.

sns.clustermap goes a step further: it reorders the rows and columns so that similar variables sit next to each other, and draws a tree of which are most alike. It uses SciPy to group them, which this page's Python doesn't have until Stage 8; on your computer, sns.clustermap(corr, annot=True, vmin=-1, vmax=1, cmap="RdBu_r") works the same way as heatmap.

Wrong way: no key, and a rainbow

Run the cells: the same correlations, drawn the way heatmap will happily draw them if you're careless.

rainbow.ipynb

Three things are wrong, and each takes a word to fix:

  • No key. With cbar=False and no numbers, a reader can't tell what any colour means. Is dark red 1, or 0.5?
  • A rainbow. cmap="jet" (6.7) has no pale middle, so a correlation near 0 gets a bright, eye-catching cyan or yellow-green, and its brightness rises and falls, inventing borders.
  • No fixed range. Without vmin and vmax, the colours stretch from the smallest number in the table to the largest. Here the smallest is bill depth's −0.58 with flipper length, so −0.58 gets the darkest blue that should mean −1. Another table would colour the same number differently.

The second cell prints bill depth's row, the numbers behind one row of squares. Step 2's cell is the fix: annot=True, a diverging map, vmin=-1, vmax=1, center=0, and the colour bar left on.

Check what you learned

You run sns.pairplot on a table with 6 numeric columns. How many scatter plots are in the grid?
Which settings make a correlation heat map honest?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis