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.
sns.pairplotmakes 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: withhue=, a KDE curve per species.vars=picks the columns (otherwise it takes every numeric one, includingyear).- The grid is symmetric: the panel in row 2, column 1 is the one in row 1, column 2 with the axes swapped.
sns.jointplotzooms 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:
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.
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.heatmapdraws it as a grid of coloured squares, andannot=Truewrites 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=1andcenter=0make 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.
Three things are wrong, and each takes a word to fix:
- No key. With
cbar=Falseand 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
vminandvmax, 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
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.
- seaborn: seaborn.pairplot: vars, hue, kind and diag_kind
- seaborn: seaborn.heatmap: annot, vmin, vmax, center and cbar
- seaborn: seaborn.clustermap: a heat map with rows and columns grouped by similarity
- Becker and Cleveland, Brushing Scatterplots, Technometrics 29(2), 1987: selecting points in one panel and seeing them in all the others