Matplotlib and seaborn 3 cheatsheet

Matplotlib and seaborn 3: Chart types

8 common Matplotlib and seaborn 3 tasks.

Scatter plot

Plot two variables as points, colored by a third.

rng = np.random.default_rng(0)
x, y, size = rng.random(50), rng.random(50), rng.random(50)
plt.scatter(x, y, c=size, cmap="viridis")
plt.colorbar(label="size")

Bar chart

Compare values across categories.

plt.bar(["A", "B", "C"], [5, 9, 3], color="steelblue")

Horizontal bar chart

A better choice when category names are long.

plt.barh(["Alpha", "Beta", "Gamma"], [5, 9, 3])

Histogram

Show the distribution of a numeric variable in 30 bins.

data = np.random.default_rng(1).normal(size=1000)
plt.hist(data, bins=30, edgecolor="white")

Box plot

Compare distributions with medians, quartiles and outliers.

plt.boxplot([np.random.default_rng(i).normal(i, 1, 100) for i in range(3)], tick_labels=["a", "b", "c"])

Pie chart

Show parts of a whole. Use sparingly: bars are easier to compare.

plt.pie([40, 35, 25], labels=["A", "B", "C"], autopct="%1.0f%%")

Heatmap with imshow

Show a matrix as colors, with a color scale.

grid = np.random.default_rng(2).random((6, 6))
plt.imshow(grid, cmap="magma")
plt.colorbar()

Error bars

Plot values with uncertainty.

plt.errorbar([1, 2, 3], [2.0, 3.1, 4.2], yerr=[0.2, 0.3, 0.25], fmt="o-", capsize=3)

Was this page helpful?