Lesson 5 of 8
Module 1 · Python basics
matplotlib draws any figure; seaborn sits on top and makes statistical plots in one line. Together they cover every figure in this course, from violins to clustered heatmaps.
Before you start. This lesson carries on in the notebook from NumPy, pandas and files. If you open a new notebook, run this setup cell first.
import numpy as np import pandas as pd
A figure and its axes
You create a figure (the whole image) with one or more axes (plotting areas), then draw on each axes. Here, simulated expression values for 200 genes in two groups.
Note that in matplotlib, "axes" means a whole plotting area, with its own x axis, y axis, labels and title, and the same word is used for one or several. Separating the image from its plotting areas lets you put several related plots side by side in one file. Simulated data are numbers drawn at random by the computer from a known distribution, so you know in advance what the plot should show. That makes them a good way to learn what a plot can and cannot reveal.
In Cleaning and combining data you ended with a table you could trust. A figure is the next step, because a table of hundreds of values is hard to read, while a picture of the same values shows their shape, their spread and how groups compare in a glance. This lesson covers the mechanics of making figures; Module 2 is about making them honest and clear.
import matplotlib.pyplot as plt
import seaborn as sns
rng = np.random.default_rng(1) # repeatable random numbers
expr = pd.DataFrame({
"condition": ["normal"] * 200 + ["tumour"] * 200,
"log_expr": np.concatenate([rng.normal(6, 2, 200), rng.normal(6.5, 2, 200)]),
})
palette = {"normal": "#0F1B33", "tumour": "#D9481F"}
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
sns.violinplot(data=expr, x="condition", y="log_expr", hue="condition",
palette=palette, inner="box", ax=axes[0])
sns.histplot(data=expr, x="log_expr", hue="condition", palette=palette,
bins=30, ax=axes[1])
axes[0].set_xlabel("")
axes[0].set_ylabel("log2 expression")
plt.tight_layout()
fig.savefig("violin_hist.png", dpi=300, bbox_inches="tight")
plt.show()What this does, block by block
- Imports.
matplotlib.pyplotis loaded aspltand seaborn assns, the usual nicknames. The code also usesnpandpd, imported in NumPy, pandas and files. - Random numbers.
np.random.default_rng(1)creates a random number generator. The1is its seed: the starting point for the sequence of random numbers, so the same seed gives the same numbers on every run and on every computer. - The data.
["normal"] * 200 + ["tumour"] * 200builds a list of 400 labels: 200 of each.rng.normal(6, 2, 200)draws 200 values from a normal (bell-shaped) distribution with mean 6 and standard deviation 2; the tumour group has mean 6.5.np.concatenatejoins the two sets into one array of 400 values. The table has one row per value, with its group in a separate column. This "long" layout is what seaborn expects. - Long and wide. In a wide table, each group would have its own column, one column of 200 normal values and one of 200 tumour values. In the long table built here, all values share one column and a second column says which group each belongs to. Long tables let seaborn colour, split and label by any column you name. If your data arrive wide,
pd.melt()reshapes them into the long layout. - The palette. A dictionary that gives each group its colour, written as hex codes: navy for normal, vermilion for tumour.
- The layout.
plt.subplots(1, 2, figsize=(10, 4))makes one row of two axes, in a figure 10 inches wide and 4 inches tall. It returns the figure,fig, and the two axes together,axes, in whichaxes[0]is the left plot andaxes[1]the right. - The violins.
sns.violinplottakes the table (data=expr), putsconditionalong x andlog_exprup y, colours bycondition, draws a small box inside each violin (inner="box") and draws on the left axes (ax=axes[0]). - The histograms.
sns.histplotsorts the values into 30 equal-width bins and draws one set of bars per group, overlapping, on the right axes. - Labels.
set_xlabel("")removes the x label from the violins, since the group names already say it;set_ylabelgives the y axis a clear name. - Saving.
plt.tight_layout()adjusts the spacing so labels do not overlap.fig.savefigwrites a PNG image at 300 dots per inch;bbox_inches="tight"trims empty margins.plt.show()then displays the figure.
axes. Every drawing call is told which axes to use with ax=. Schematic, not to scale.The violin shows the full distribution, with a box for the median and middle half inside; the histogram shows how the two groups overlap. Use sns.boxplot() or sns.scatterplot() in the same way.
Reading the plots
A violin is a smoothed outline of where the values fall, mirrored on both sides: wide where many values sit, narrow where few do. The median is the middle value when all are sorted, and the "middle half" is the range holding the central 50% of values. In the histogram, each bar counts how many values fall within one bin. Here the two groups overlap heavily: the means differ by only 0.5, while each group has a standard deviation of 2. Seeing that overlap is the point. A bar chart of the two means would show two bars of slightly different height and hide the fact that most individual values in the two groups are indistinguishable.
"In the same way" means the same arguments: data, x, y, hue, palette and ax work across seaborn's plotting functions, so once you know one, you know the pattern for the others.
For a single plot, ask for one axes: fig, ax = plt.subplots(figsize=(4, 4)), then pass ax=ax to the seaborn call. The result is the same plot on its own, which is often what you want for a slide. Each axes also has a set_title() method, so axes[1].set_title("Distribution by group") would label the right-hand plot.
A common pitfall when drawing several plots is to forget ax=. Without it, seaborn draws on whichever axes matplotlib considers current, usually the last one created, so two plots end up on top of each other in one panel and the other panel stays empty. Telling every call which axes to use, as the code above does, removes the guesswork. matplotlib also has a shorter style, with calls such as plt.xlabel() that act on the current axes; it works for one plot, but with several it is easy to label the wrong one, which is why this course uses the ax style throughout.
matplotlib: Hunter (2007), "Matplotlib: a 2D graphics environment", Computing in Science & Engineering 9(3), 90–95. seaborn: Waskom (2021), "seaborn: statistical data visualization", Journal of Open Source Software 6(60), 3021 (doi:10.21105/joss.03021). These are the citations the two projects ask for. seaborn's interface changed noticeably in versions 0.12 and 0.13 (2022–2023); for example, newer versions expect hue whenever you pass a palette, which is why the code above sets both. Code written for an older version may give warnings or look different, so report the versions you used: sns.__version__, and matplotlib.__version__ after import matplotlib.
Clustered heatmaps
sns.clustermap() reorders rows and columns so similar ones sit together. z_score=0 shows each gene relative to its own average.
A heatmap shows a table of numbers as a grid of coloured squares. Clustering groups rows (and columns) whose patterns of values are alike, and draws the grouping as a branching tree, called a dendrogram, along the edges. A z-score re-expresses each value as the number of standard deviations it lies above or below its row's mean. Without that step, a gene that is highly expressed everywhere would dominate the colours, and the differences between samples, which are usually what you want to see, would be hard to read.
To decide which rows are alike, clustermap measures the distance between every pair of rows, by default the ordinary straight-line (Euclidean) distance, and joins the closest ones first. You can change both choices, the distance with metric= and the joining rule with method=, and different choices can give different trees from the same data. That is one reason to say in the legend how a heatmap was clustered.
mat = pd.DataFrame(rng.normal(size=(10, 6)),
index=[f"Gene{i}" for i in range(1, 11)],
columns=[f"S{i}" for i in range(1, 7)])
g = sns.clustermap(mat, z_score=0, cmap="RdBu_r", figsize=(5, 6))
g.savefig("heatmap.png", dpi=300)What this does
rng.normal(size=(10, 6))draws a grid of random values with 10 rows and 6 columns, from a normal distribution with mean 0 and standard deviation 1, the default when no mean and spread are given.[f"Gene{i}" for i in range(1, 11)]builds the row labels Gene1 to Gene10.range(1, 11)counts from 1 up to, but not including, 11, the same rule as slicing. Thefbefore the quotes makes an f-string, in which{i}is replaced by the current number. The columns are named S1 to S6 in the same way.sns.clustermap(mat, …)clusters and draws the table.z_score=0standardises each row (0 means rows; 1 would mean columns).cmap="RdBu_r"chooses a red–blue colour scale, reversed by_rso that high values are red and low values blue.figsize=(5, 6)sets the size in inches.clustermapbuilds its own figure and returns an object, hereg, that has its ownsavefig.
Reading the heatmap
Each square is one gene in one sample. After z-scoring, white or pale squares are near that gene's own average, red squares above it and blue squares below. Genes joined low down in the dendrogram have similar patterns across the samples. Because these values are random, any clusters you see here are chance groupings, which is a useful warning: clustering always produces a tree, even when there is no real structure in the data.
On real expression data, a heatmap of all 20,000 genes is unreadable, so analysts usually show a selection: the genes that vary most across samples, or the genes found to differ between groups. Say which selection you used. A heatmap of genes chosen because they differ between groups will, of course, show the groups separating; that is a picture of the selection, not independent evidence for it.
Two practical points. First, a gene with the same value in every sample has a standard deviation of zero, so its z-scores cannot be calculated and the row appears blank; remove such genes before plotting. Second, it helps to show which group each sample belongs to. clustermap accepts col_colors=, a list of one colour per column, and draws it as a coloured strip above the heatmap, so you can see at once whether samples from the same condition cluster together.
Go deeper: what clustermap is measuring
To build its trees, clustermap needs two choices: how to measure the distance between two rows, and how to measure the distance between two groups of rows once they start to join, called the linkage. By default it uses Euclidean distance, the straight-line distance between two rows' values, and average linkage, the mean distance between all members of the two groups. The SciPy library does the clustering underneath.
You can change both, as in sns.clustermap(mat, z_score=0, metric="correlation", method="average"), which groups genes whose values rise and fall together. After z-scoring, Euclidean and correlation distances give closely related trees, because every row then has the same mean and spread. Without z-scoring, Euclidean distance mostly groups genes by how high they are. Different choices can give visibly different trees from the same data, so name the distance and linkage in the figure legend.
Key point
Save with fig.savefig() before plt.show(); in some setups the figure is cleared once shown. Use 300 dpi for anything you might publish.
Dots per inch sets how many pixels the saved image has for each inch of figure size. The violin figure is 10 inches wide, so at 300 dpi it is about 3,000 pixels across before the margins are trimmed; at 72 dpi it would be only 720, and it would look blurred once printed or enlarged on a slide. Choose figsize close to the size the figure will actually appear, so the text stays readable when it is shrunk to fit a page.
Further reading · Module 1The Python Tutorial: the official tutorial, for anything you want explained a second way. · All reading for this course
Check your understanding
1. In fig, axes = plt.subplots(1, 2) , what is axes[1] ?
2. What does z_score=0 do in a clustermap?
3. Why use a violin rather than a bar of means?
Up next
Lesson 6
Sequences with Biopython