Heatmaps in Python
imshow draws a matrix; the colorbar and the ticks make it legible.
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
langs = ["python", "rust", "sql"]
days = ["mon", "tue", "wed"]
grid = np.array([[88, 97, 104], [80, 85, 91], [70, 76, 79]])
figure, axes = plt.subplots()
image = axes.imshow(grid, cmap="magma")
axes.set_xticks(range(len(days)), labels=days)
axes.set_yticks(range(len(langs)), labels=langs)
for row in range(grid.shape[0]):
for col in range(grid.shape[1]):
axes.text(col, row, grid[row, col], ha="center", va="center",
color="white", fontsize=8)
figure.colorbar(image, ax=axes, label="tpm")
figure.savefig("heatmap.png")
How it works
imshowmaps values to colors over a grid.- Set both tick sets, or the axes lie about the labels.
- Writing the number in each cell beats guessing from the color.
Keywords and builtins used here
asforlenrange
The run, in numbers
- Lines
- 20
- Characters to type
- 613
- Tokens
- 224
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 128 seconds.
Step 5 of 5 in Plot types, step 9 of 14 in Plotting with matplotlib.