Check user bias with a population pyramid

Visualize

Check user bias with a population pyramid

Created: Last updated: Read time: 1 min

To communicate gender splits or attribute differences in A/B tests, a population pyramid style chart works well. Bias by age band becomes obvious.

import numpy as np
import matplotlib.pyplot as plt

age_groups = ["18-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54"]
male = np.array([4200, 3800, 3300, 2700, 2200, 1600, 1200])
female = np.array([4000, 3600, 3500, 2900, 2400, 1700, 1300])

fig, ax = plt.subplots(figsize=(6, 4.8))
ax.barh(age_groups, male, color="#38bdf8", label="Male")
ax.barh(age_groups, -female, color="#f472b6", label="Female")

ax.set_xlabel("Users")
ax.set_title("Members by age group (population pyramid)")
ax.set_xlim(-4500, 4500)
ax.set_xticks([-4000, -2000, 0, 2000, 4000], labels=["4000", "2000", "0", "2000", "4000"])
ax.legend(loc="upper right")

for idx, (m_val, f_val) in enumerate(zip(male, female)):
    ax.text(m_val + 80, idx, f"{m_val}", va="center")
    ax.text(-f_val - 280, idx, f"{f_val}", va="center")

ax.axvline(0, color="#0f172a", linewidth=1)
ax.spines[["right", "top"]].set_visible(False)
ax.grid(axis="x", alpha=0.2)

fig.tight_layout()

plt.show()

Age band bias becomes visible at a glance.

Reading tips #

  • Compare left vs right to see group differences; larger gaps mean stronger bias.
  • Keep the same scale on both sides for accurate visual comparison.
  • Add labels so counts are readable without estimating bar length.