A box plot summarises a sample: median, quartiles, whiskers, and outliers. Several boxes side by side compare groups.
Goal
Draw one box, then one box per city.
One sample
rng = np.random.default_rng(0)
temps = 26 + rng.normal(scale=1.2, size=80)
fig, ax = plt.subplots()
ax.boxplot(temps, labels=["Nairobi"])
ax.set_ylabel("°C")
ax.set_title("Daily max")
plt.show()If labels warns on a newer matplotlib, switch to tick_labels. This workbench accepts labels.
Several groups
rng = np.random.default_rng(0)
nairobi = 26 + rng.normal(scale=1.2, size=80)
mombasa = 31 + rng.normal(scale=0.8, size=80)
kisumu = 28 + rng.normal(scale=1.0, size=80)
fig, ax = plt.subplots()
ax.boxplot([nairobi, mombasa, kisumu], labels=["Nairobi", "Mombasa", "Kisumu"])
ax.set_ylabel("°C")
ax.set_title("Daily max by city")
plt.show()Show the mean
rng = np.random.default_rng(0)
data = [26 + rng.normal(scale=1.2, size=80), 31 + rng.normal(scale=0.8, size=80)]
fig, ax = plt.subplots()
ax.boxplot(data, labels=["Nairobi", "Mombasa"], showmeans=True)
ax.set_title("Means as extra markers")
plt.show()Pitfall
Passing a 2-D array to boxplot treats columns as groups. A list of 1-D arrays is clearer when the groups have different lengths.