Build ribbons from stable record identities, preserve the stage total, and distinguish an unobserved state from a measured exit.
Alluvial diagrams: follow one cohort through categorical stages
Start with the same population on every axis
An alluvial diagram follows records through a sequence of categorical observations. Each vertical axis is a stage or attribute; a ribbon groups records that share adjacent categories. The picture answers a different question from a general Sankey flow: which members of a fixed cohort changed category, and where did their recorded paths split? Establish the cohort before counting anything. If the first axis includes all accounts opened in April but the third includes every active account in June, the ribbon widths no longer describe transitions within one population. New entrants need a separate cohort or an explicit entrant category. Each record needs a stable identifier, a stage definition, and at most one category at a stage. A row count without identity cannot reveal whether the same seven accounts moved through the diagram or seven different accounts were sampled each time. Grain and denominators are the first validation gate.
Keep unobserved records in the ledger
A missing stage value is not automatically churn, failure, or inactivity. It may reflect a late data feed, an ineligible record, or a lost join. The worked contract keeps an explicit Not observed category so every axis and adjacent ribbon set still sums to the original cohort size. Analysts can then separate recorded churn from incomplete measurement. If the business meaning of absence is genuinely an exit, encode that as a distinct recorded event with a timestamp and an eligibility rule. Do not silently turn null into churn. Count each adjacent pair from the same record ledger; independently aggregated stage totals cannot reconstruct who crossed between categories. For a large diagram, group rare categories by a declared rule and keep an exact transition table. Cohort retention, missingness and linked filters show how the population can drift.
Order categories without inventing movement
Ribbon geometry depends on vertical category order. If each axis is reordered by its latest count, unchanged paths can cross even when the underlying records have not moved. Choose a domain order for each stage, version it, and hold it across comparable panels and exports. Keep a stable color meaning: color by the initial category when the question is origin, or by the final outcome when the question is destination. A gradient that changes meaning halfway through the diagram makes the path hard to decode. The ribbon width should represent count on a shared scale, while the hover text and adjacent table expose the exact count and denominator. Stage-specific percentages can be useful, but label their denominator; a ribbon of three out of four active trials is not the same quantity as three out of seven original accounts. Stacked bars are easier when only stage composition matters.
Design for inspection, not just spectacle
Crossing ribbons are expensive to trace by eye. Highlight a selected origin or destination, dim unrelated paths, and include a keyboard-accessible transition table with stage pair, source, destination and count. Do not make users identify a one-record path from a hairline ribbon. If there are many stages, provide a small-multiple view of adjacent pairs, then let a reader inspect one segment at a time. A stage with a newly introduced category should disclose the schema change; otherwise its ribbon can masquerade as behavioral movement. Preserve the filter state and cohort key in exported data. When identity cannot be linked across stages, the correct output is a sequence of separate category summaries, not an invented alluvial path. Keyboard access, export manifests and chord diagrams provide nearby design choices.
Worked contract
from collections import Counter
def build_account_alluvium(cohort_ids, stages, observations):
if (not cohort_ids or len(set(cohort_ids)) != len(cohort_ids) or
any(not isinstance(account_id, str) or not account_id.strip()
for account_id in cohort_ids)):
raise ValueError("cohort IDs must be unique nonempty strings")
if (len(stages) < 2 or len(set(stages)) != len(stages) or
any(not isinstance(stage, str) or not stage.strip() for stage in stages)):
raise ValueError("at least two unique stages are required")
cohort, stage_set = set(cohort_ids), set(stages)
state_by_account_stage = {}
for account_id, stage, state in observations:
key = (account_id, stage)
if (account_id not in cohort or stage not in stage_set or
not isinstance(state, str) or not state.strip() or
state == "Not observed" or key in state_by_account_stage):
raise ValueError(f"invalid or duplicate observation: {key}")
state_by_account_stage[key] = state
def state_at(account_id, stage):
return state_by_account_stage.get((account_id, stage), "Not observed")
stage_counts = [Counter(state_at(account_id, stage) for account_id in cohort_ids)
for stage in stages]
ribbons = [Counter((state_at(account_id, left), state_at(account_id, right))
for account_id in cohort_ids)
for left, right in zip(stages, stages[1:])]
assert all(sum(counts.values()) == len(cohort_ids) for counts in stage_counts)
assert all(sum(counts.values()) == len(cohort_ids) for counts in ribbons)
return stage_counts, ribbons
accounts = [f"AC-{number}" for number in range(41, 48)]
stages = ["intake", "trial", "renewal"]
observations = [
("AC-41", "intake", "direct"), ("AC-42", "intake", "direct"),
("AC-43", "intake", "referral"), ("AC-44", "intake", "referral"),
("AC-45", "intake", "partner"), ("AC-46", "intake", "partner"),
("AC-47", "intake", "direct"),
("AC-41", "trial", "active"), ("AC-42", "trial", "active"),
("AC-43", "trial", "inactive"), ("AC-44", "trial", "active"),
("AC-45", "trial", "active"), ("AC-46", "trial", "inactive"),
("AC-41", "renewal", "renewed"), ("AC-42", "renewal", "churned"),
("AC-43", "renewal", "churned"), ("AC-44", "renewal", "renewed"),
("AC-45", "renewal", "renewed"),
]
stage_counts, ribbons = build_account_alluvium(accounts, stages, observations)
assert stage_counts[2]["Not observed"] == 2
assert ribbons[1][("active", "renewed")] == 3
assert ribbons[1][("inactive", "Not observed")] == 1Performance and operating cost
Let C be cohort size, S the number of stages, and R the number of supplied observations. Validating the observations and building the stage and adjacent-pair ledgers takes O(R + CS) time; storing the input lookup and aggregate counts takes O(R + CS) space in the worst case. In the worked seven-account cohort, three active trials become recorded renewals, one inactive trial has no renewal observation, and two accounts lack a renewal observation in total. Both adjacent ribbon sets still sum to seven. The code prepares counts; a renderer must apply a documented category order and a fixed width scale. Avoid drawing every record as its own ribbon when C is large: aggregate equal paths and retain the exact ledger for inspection.
Common Mistakes
- Building each stage from a different population and labeling the ribbons as cohort transitions.
- Inferring a person's path from stage-level totals without stable record identity.
- Treating a missing observation as a recorded exit or churn event.
- Changing category order or color meaning between comparison panels.
- Displaying percentages without naming whether the denominator is the original cohort or a stage subset.
Validation drill
Take a fixed cohort of 47 accounts observed at onboarding, first use and renewal. Verify one category per account per stage, preserve missing observations explicitly, reconcile every stage and ribbon set to 47, and publish a tabular list of the transitions that remain after rare categories are grouped.
