Declare coordinates, bin resolution and whether color means count or rate.
Hexbin density: count overlapping points without hiding exposure
Overplotting is a counting problem
When thousands of marks overlap, a dark patch can reflect drawing order or opacity rather than a trustworthy count. Binning makes the count explicit. A hexagonal grid offers a compact neighborhood shape, but it still imposes a resolution and origin. Record both. Overplotting and sampling bias explain why a random sample may help inspection but cannot silently replace total-density reporting. Show how many points survived filtering and coordinate validation before any bins are painted.
Coordinate units determine the grid
A grid in longitude and latitude degrees does not represent equal ground area across latitudes. For a geographic density question, project coordinates into an appropriate metric system before choosing cell width. If this is only a relative screen-space view, say so and avoid claims about area-normalized risk. Keep projection, extent and grid origin fixed across time panels. Otherwise unchanged points may fall into different cells. Geographic rate maps have a related denominator problem.
Count and exposure answer different questions
A cell with 81 incidents may cover 810,000 journeys; a cell with 47 may cover 4,700. Count highlights workload, while incidents per journey describe a different quantity. Preserve zero-exposure cells separately from zero-incident cells. Dividing by drawn polygon area does not produce a population risk. The denominator contract must match the decision. If sparse cells are suppressed for privacy, label that suppression and retain a public aggregate total rather than pretending the hidden cells are zero.
Test resolution and edge effects
A coarse grid can merge two nearby clusters; a fine grid can fragment one. Inspect multiple declared widths while keeping the color domain comparable. Points exactly on the extent edge need a documented inclusion rule. Export each bin's identifier, center or polygon, count, exposure and suppression state so the chart can be checked without its pixels. The export manifest should retain projection, extent, width and origin. An accessible summary should identify the highest-count cells and explain the unit encoded by color.
Worked contract
from collections import Counter
from math import floor, isfinite
def audit_spatial_counts(points: list[tuple[float, float]],
cell_width: float) -> Counter[tuple[int, int]]:
if not isfinite(cell_width) or cell_width <= 0:
raise ValueError("cell width must be positive")
if any(not isfinite(x) or not isfinite(y) for x, y in points):
raise ValueError("coordinates must be finite")
return Counter((floor(x / cell_width), floor(y / cell_width))
for x, y in points)
depot_coordinates = [(47.2, 81.6), (47.7, 81.1), (59.4, 81.5)]
audit = audit_spatial_counts(depot_coordinates, 4)
assert audit[(11, 20)] == 2 and sum(audit.values()) == 3Performance and operating cost
This rectangular audit grid takes O(N) expected time and O(B) space for N points and B occupied bins. It checks count conservation before passing projected coordinates to a hexbin renderer; rectangular cells and hexagons do not share identifiers. The renderer decides hexagon placement and edge rules. A real map also needs projection and exposure joins versioned with its bin table. Preserve input and retained counts to detect losses.
Common Mistakes
- Calling raw incident counts a risk rate.
- Using degree-based cells as if they had equal ground area.
- Changing grid extent between periods and interpreting bin movement as behavior change.
- Hiding suppressed or zero-exposure cells as ordinary zero counts.
Validation drill
Bin 810 projected service calls at two widths. Reconcile counts to input, attach journey exposure by cell and compare the busiest cell with the highest incident-rate cell.
Read next
- Dense scatterplots: reveal overlap without hiding rare observations
- Choropleth maps: normalize counts and audit boundary coverage
- Aggregation grain and denominator: make every plotted mark auditable
- Chart exports: carry the data, filters and render contract together
