Build a mirrored age distribution with shared age bins, one total-population denominator and an explicit unrecorded-category ledger.
Population pyramids: compare age bands without losing the denominator
A mirror is a layout, not a negative population
A population pyramid places age bands vertically and two recorded categories on opposite sides of a central zero line. One side is plotted with negative coordinates solely to mirror the bars; its underlying counts remain nonnegative. Name the categories as they were collected rather than guessing what a binary field means. The familiar pyramid shape is not a requirement: a region with older residents or migration concentrated in working ages may have a wide middle. Keep youngest ages at the bottom or label any reversed order prominently. Axis integrity matters because the left and right extents must have equal magnitude when their bar lengths are compared.
Choose count or share deliberately
Counts show service volume. Shares show age structure for differently sized places or years. If each bar uses a percent, choose a denominator and print it: this lesson divides every age-and-category count by the total recorded population, including people whose category is unrecorded. Dividing the left side by its own category total and the right side by another total answers a different conditional question and makes widths deceptively comparable. For two places, use identical bin widths, axis limits and denominator definitions; show the underlying total of each place nearby. Shared-scale panels prevent a small district from visually matching a large city by accident.
Reconcile coverage before drawing
Age bands must cover the intended population once each without overlaps or gaps, and the reporting date must be the same across both sides. A top band such as 85 and older is open-ended; preserve that label rather than pretending it spans the same number of years as a five-year band. Unknown ages need an exclusion count and explanation outside the plotted bands. An unrecorded category is also not a third side; print its total and age-band breakdown in a companion table so the two mirrored sides do not silently claim to represent everyone. Composition charts offer another view when more than two categories are central to the question.
Treat small counts as data, not shape
A one-person change can visibly reshape a small-area band. Consider disclosure rules and uncertainty when publishing small geographies; combining bins may protect privacy and reduce noise, but the regrouping must apply to every compared area and year. Suppressed cells are not zero. Printed counts and an accessible table let readers distinguish a narrow bar from a missing value. Accessible alternatives should give the age band, each count, total and percentage without relying on color or left-right position alone. A smooth outline drawn through sparse age groups adds no information and can imply detail the input never had.
Worked contract
def pyramid_ledger(age_bands: list[tuple[int, int, int, int, int]]):
if not age_bands:
raise ValueError("at least one age band is required")
ordered = sorted(age_bands)
for index, (start_age, end_age, male, female, unrecorded) in enumerate(ordered):
if (type(start_age) is not int or type(end_age) is not int
or start_age < 0 or end_age <= start_age or
any(type(count) is not int or count < 0
for count in (male, female, unrecorded))):
raise ValueError("invalid age boundary or count")
if index and start_age != ordered[index - 1][1]:
raise ValueError("age bands must be contiguous and nonoverlapping")
population = sum(male + female + unrecorded
for _, _, male, female, unrecorded in ordered)
if population == 0:
raise ValueError("percentage denominator cannot be zero")
bars = [(start_age, end_age, -male / population,
female / population, unrecorded / population)
for start_age, end_age, male, female, unrecorded in ordered]
assert abs(sum(right - left + missing
for _, _, left, right, missing in bars) - 1) < 1e-12
return population, bars
catchment_counts = [(0, 18, 47, 45, 3), (18, 65, 141, 138, 5),
(65, 85, 29, 35, 2)]
population, bars = pyramid_ledger(catchment_counts)
assert population == 445
assert bars[0] == (0, 18, -47 / 445, 45 / 445, 3 / 445)
assert sum(band[4] for band in bars) == 10 / 445Performance and operating cost
For B age bands, sorting and validating the intervals takes O(B log B) time; the ledger needs O(B) output space. If input is already sorted and validated upstream, the count sweep is O(B). The 445-person check proves that both mirrored sides and the ten unrecorded-category records share one denominator. The code accepts only closed, finite upper age boundaries; an open-ended oldest band needs a separately labeled representation rather than a fabricated endpoint.
Common Mistakes
- Reading negative drawing coordinates as negative people.
- Using each side's own total while labeling bars as shares of the whole population.
- Changing age-band widths between places or treating suppressed cells as zero.
- Dropping unrecorded categories while claiming the visible bars sum to the entire population.
Validation drill
Compare two synthetic clinic catchments with 445 and 1,410 registered residents. Reconcile every age-band count to its population total, draw count and percentage versions on shared axes, and print unrecorded-category counts beside both charts.
