Turn overlapping memberships into exclusive intersection bars with a stated universe and reproducible set order.
UpSet plots: count exact intersections without double counting
Define one universe of unique entities
An intersection plot is useful when a Venn drawing becomes too crowded. Consider support tickets tagged Billing, Delivery or Returns. Each ticket needs a stable unique ID and a documented observation period; multiple log rows for the same ticket should not become multiple members. Decide whether the universe is all tickets, all tagged tickets or only tickets passing an eligibility filter. That choice changes the meaning of the empty-membership group. Aggregation grain and denominator apply before any bars are drawn. A set-size bar counts every ticket in a set, including tickets in other sets; summing set-size bars therefore double-counts overlap by design.
Distinguish exact from inclusive intersections
An exact intersection for Billing and Returns contains tickets with those two tags and no other displayed tag. An inclusive intersection contains those tickets even when they also carry Delivery. UpSet-style bars often use exact membership masks, but analysts sometimes compute inclusive intersections in SQL and label them as exact. State which contract each bar uses. The worked function returns an exact mask for every record and excludes the all-false mask from plotted bars while retaining its count in the audit. The sum of all exact masks must equal the size of the universe, including items outside all displayed sets. Mosaic plots offer a different view of joint categories, but they also require a clear rule for empty cells and denominators.
Keep column and set order stable
Sort intersections by count for exploration, then freeze a documented column order when comparing periods. Otherwise a bar that moves horizontally can be mistaken for a membership change. Order the set rows consistently, and show the connected dots or a textual equivalent beneath each bar. A bar for no sets can be meaningful if every ticket belongs to the universe regardless of tagging; do not silently discard it from reconciliation. Stable facet order helps the same reader compare versions. A companion table should give exact mask, count, period, filters, and the underlying entity IDs or an authorized export path.
Stop before combinations swamp the chart
With K sets there are up to 2^K exact masks, including the empty mask, even though many may have zero observations. An unreadable wall of tiny bars is not comprehensive communication. Select a justified subset of sets, group lower-value combinations with a visible membership rule or offer a searchable intersection table. Do not hide suppressed small groups inside a falsely labeled zero. Keyboard and table access matters because a dot matrix alone cannot expose exact membership to every reader. If the set definitions drift across months, a stable-looking bar chart can still compare different concepts; publish the rule version beside the counts.
Worked contract
def exact_intersections(records: dict[str, set[str]],
sets_in_order: list[str]):
if not sets_in_order or len(sets_in_order) != len(set(sets_in_order)):
raise ValueError("set names must be unique and nonempty")
if any(not name for name in sets_in_order):
raise ValueError("set names must be nonempty")
known = set(sets_in_order)
if any(not ticket_id or not membership <= known
for ticket_id, membership in records.items()):
raise ValueError("ticket IDs and memberships must be valid")
counts = {}
for membership in records.values():
mask = tuple(name in membership for name in sets_in_order)
counts[mask] = counts.get(mask, 0) + 1
assert sum(counts.values()) == len(records)
return counts
ticket_tags = {"ticket-47": {"Billing", "Returns"},
"ticket-59": {"Billing", "Returns", "Delivery"},
"ticket-83": {"Delivery"}, "ticket-97": set()}
set_order = ["Billing", "Delivery", "Returns"]
intersections = exact_intersections(ticket_tags, set_order)
assert intersections[(True, False, True)] == 1
assert intersections[(True, True, True)] == 1
assert intersections[(False, False, False)] == 1
assert sum(intersections.values()) == 4Performance and operating cost
For N records and K named sets, computing membership masks takes O(NK) time and O(MK) space for M observed masks, with M at most min(N, 2^K). The worst-case chart itself becomes difficult well before that bound. The function assumes each record key is already a unique entity and its tag collection is a set. It counts exact intersections, not inclusive overlaps, and deliberately retains the empty mask for reconciliation.
Common Mistakes
- Summing set-size bars as if their members were disjoint.
- Labeling an inclusive SQL overlap as an exact intersection.
- Dropping the no-set group without explaining the changed universe.
- Reordering sets or redefining tags across comparison panels without a version note.
Validation drill
Build an exact-mask ledger for 470 tickets and four tags. Reconcile every mask to the eligible universe, then compute one inclusive overlap separately and explain why the two counts differ.
