Scatter plots, histograms, and box plots

3.11.2

Choose a chart for paired values, distributions, or comparisons of spread.

On this page

Show paired values

Use scatter for one point per paired observation. If many points overlap, smaller markers or partial transparency help reveal dense areas. An association in a scatter plot does not establish causation.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter([55, 85, 120], [115, 190, 270], alpha=0.75)
ax.set(xlabel="Floor area (m²)", ylabel="Price (thousands)")
plt.show()
Three houses are plotted by floor area and price. The larger houses have higher prices in this sample. Download image

Show a distribution with bins

A histogram counts observations within numerical intervals by default. Bin width changes the visible shape. Use the same bin edges when comparing two distributions.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
counts, _, _ = ax.hist([1, 2, 3, 6, 8, 12], bins=[0, 5, 10, 15])
print(counts.astype(int).tolist())
plt.show()
Histogram with three bins containing three, two, and one observation respectively. Download image
Example output
[3, 2, 1]

Compare centers and spreads

A box plot shows the median and the first and third quartiles. By default, whiskers reach the most extreme data points within 1.5 times the interquartile range from the quartiles. Points outside are plotted as fliers.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.boxplot([[2, 4, 6], [4, 8, 10]], tick_labels=["Store A", "Store B"])
ax.set(ylabel="Units sold")
plt.show()
Box plots for two stores, with median units of four and eight respectively. Download image

Official references