Build variable-width stacked columns from a counted ledger and explain the distinct denominators behind column width, segment height and cell area.
Marimekko charts: make width, height and area reconcile
Start with one counted cross-tab
A Marimekko chart divides a rectangle into columns of unequal width, then stacks categories within each column. One counted ledger must drive every dimension: the column total determines width, a cell divided by its column total determines height, and the same cell divided by the grand total determines rectangle area. These are three related but different questions. For support channels by outcome, a wide channel is common in the entire population; a tall Escalated segment means a high escalation share within that channel. Neither alone proves which channel contributes the most escalations overall. Count records once, define the period and clarify whether the cell count is tickets, interactions or customers. A customer who opened three tickets contributes three tickets if tickets are the declared grain. Grain and denominator and mosaic plots cover the adjoining accounting ideas.
Derive geometry from counts
The sample ledger totals 116 tickets across Web, Phone and Partner channels. Web holds 38, Phone 50 and Partner 28, so their horizontal shares are 38/116, 50/116 and 28/116. Within Phone, 25 of 50 tickets are Escalated; that cell is half the Phone column's height but only 25/116 of the whole chart area. The code returns both conditional and joint shares for every cell, then checks that all cell areas sum to one. Keep the channel order fixed between periods. A category missing from one channel gets a zero-height cell in the data table, not a fabricated sliver. Categories with no counts anywhere can leave the comparison, but state the universe used for the display. Stacked bars are easier when the column volumes are similar or only within-channel rates matter.
Area is hard to read precisely
A narrow column can contain a tall segment that still has little area. If the viewer needs to compare exact escalation rates, label the percentages or pair the chart with a sorted bar chart. If the viewer needs exact contribution to all escalations, show counts or joint shares beside the chart. Do not place a single legend percentage against every segment because each conditional percentage has a different column denominator. Avoid text inside cells too small to hold it: give those records an external label, a keyboard-accessible table and a search path. Color should identify outcomes consistently across columns, and cell borders should remain visible without relying solely on color. Text alternatives and diverging stacks offer related approaches.
Use the right comparison for change
Two Marimekko panels can be compared only if their column and category definitions are the same and both panels disclose their population sizes. If the population grows, the second panel still fills the same rectangle; its areas communicate shares, not absolute growth. Show the grand totals in the titles, or pair the panels with a count chart. Keep the same outcome colors and category ordering, and do not reorder columns by a changed rank if the task is to track a channel over time. Very small columns may vanish on a mobile viewport. Collapse the display to a table or offer a focused single-channel view rather than making touch targets thinner than a finger. The export should include the original counts, column totals, conditional shares and joint shares, not just the rendered coordinates. Export manifests and small multiples make repeat views auditable.
Worked contract
from collections import Counter
def prepare_channel_mekko(ticket_counts, channel_order, outcome_order):
if not ticket_counts or not channel_order or not outcome_order:
raise ValueError("counts and category orders are required")
if (len(set(channel_order)) != len(channel_order) or
len(set(outcome_order)) != len(outcome_order)):
raise ValueError("category order contains duplicates")
known_channels, known_outcomes = set(channel_order), set(outcome_order)
cells = Counter()
for channel, outcome, count in ticket_counts:
if (channel not in known_channels or outcome not in known_outcomes or
isinstance(count, bool) or not isinstance(count, int) or count < 0):
raise ValueError("invalid counted cell")
cells[(channel, outcome)] += count
totals = {channel: sum(cells[(channel, outcome)] for outcome in outcome_order)
for channel in channel_order}
if any(total == 0 for total in totals.values()):
raise ValueError("each displayed column needs a positive total")
grand_total = sum(totals.values())
columns, left = [], 0.0
for channel in channel_order:
width = totals[channel] / grand_total
bottom, segments = 0.0, []
for outcome in outcome_order:
count = cells[(channel, outcome)]
height = count / totals[channel]
segments.append({"outcome": outcome, "count": count,
"height": height, "area": count / grand_total,
"bottom": bottom})
bottom += height
assert abs(bottom - 1) < 1e-12
columns.append({"channel": channel, "left": left,
"width": width, "total": totals[channel],
"segments": segments})
left += width
assert abs(left - 1) < 1e-12
return grand_total, columns
ticket_counts = [
("Web", "Closed", 21), ("Web", "Escalated", 8), ("Web", "Open", 9),
("Phone", "Closed", 17), ("Phone", "Escalated", 25), ("Phone", "Open", 8),
("Partner", "Closed", 12), ("Partner", "Escalated", 6),
("Partner", "Open", 10),
]
grand_total, columns = prepare_channel_mekko(
ticket_counts, ["Web", "Phone", "Partner"],
["Closed", "Escalated", "Open"])
assert grand_total == 116
assert [column["total"] for column in columns] == [38, 50, 28]
assert columns[1]["segments"][1]["height"] == 0.5
assert columns[1]["segments"][1]["area"] == 25 / 116
assert abs(sum(segment["area"] for column in columns
for segment in column["segments"]) - 1) < 1e-12Performance and operating cost
With R counted rows, C displayed columns and O outcomes, aggregation takes O(R) expected time and O(CO) cell storage. Constructing the full rectangular grid takes O(CO) time and space even when some observed cells are absent; those cells retain explicit zero counts. A fixed category order lets a renderer place rectangles in one pass. Floating-point coordinates may not sum to exactly one, so the tests use a small tolerance while the underlying integer counts remain authoritative. Do not round each cell percentage before computing positions: independent rounding creates gaps, overlaps and totals that no longer reconcile.
Common Mistakes
- Giving every column equal width while claiming to show its share of the total.
- Reading conditional segment height as the segment's contribution to all records.
- Rounding percentages before laying out the rectangles and losing reconciliation.
- Comparing equal-sized panels as absolute totals without stating their different population sizes.
- Putting text only inside narrow cells that cannot be read or focused.
Validation drill
Build a channel-by-outcome ledger for 47 reporting units. Reconcile row, column and grand totals; label one cell with both its within-channel share and its joint share; then test the chart at a mobile width without discarding the small cells.
