Compute density heights for unequal intervals, document edge inclusion and reconcile plotted area to the measured population.
Unequal-bin histograms: make rectangle area carry frequency
Define the measured population before the bins
A histogram partitions a numerical measure into adjacent intervals. Each observation belongs to one interval, and the shape depends on the chosen boundaries. Specify the observation unit, measurement unit, eligibility window and treatment of missing values before drawing. A delivery-latency chart over requests differs from one over customers even if both use milliseconds on the horizontal axis. Fixed bin edges let readers compare weeks without interpreting a shifted boundary as a changed distribution. Do not silently trim values outside the visible domain; either expand the domain or label the excluded count and range. Grain and denominator choices come first, and outlier displays can preserve extreme values that a compact histogram hides.
For unequal widths, height is a density
When widths vary, raw counts cannot be the rectangle heights if the picture is supposed to preserve frequency. Divide each bin's count by its width for count density, or divide again by the total number of included observations for probability density. The rectangle area then equals its count or share. A 40-unit-wide bin containing two requests and an 80-unit-wide bin containing four requests have equal probability-density height, although the second contains twice as many requests. Print counts or shares on demand; height alone is easy to misread. State whether intervals are left-closed and right-open, and make the final upper boundary inclusive if that is the published rule. Axis integrity requires a labeled density unit, such as share per millisecond, rather than a mislabeled 'number of requests' vertical axis.
Choose edges without chasing the answer
A histogram with very wide bins buries local peaks. Very narrow bins turn sampling noise into apparent structure. Pick boundaries using measurement resolution and a declared analytical purpose, then inspect sensitivity using another reasonable fixed bin plan. Do not search many boundaries and publish only the one that makes a preferred group look unusual. For side-by-side cohorts, use shared edges and a shared vertical density scale; sample-size differences belong in annotations and raw-count tables. If an interval is open-ended, it has no finite width and cannot receive an ordinary density rectangle. Split it at a finite bound, or display its count separately. Shared-scale small multiples make the bin plan visible across groups.
Reconcile the chart before interpretation
The bin ledger should sum to the included record count. For a probability-density histogram, the sum of height times width should equal one apart from floating-point rounding. Those checks catch dropped boundary observations, double assignment and a denominator that includes records outside the plotted range. The code below rejects out-of-range measurements rather than making an attractive but incomplete chart. A separate report can show excluded or invalid records under a declared policy. Give readers an accessible interval table with bounds, counts, shares and density; a smooth overlay is a different estimate with its own bandwidth and should not erase the measured bins. Density bandwidth and text alternatives cover those follow-on decisions.
Worked contract
from bisect import bisect_right
from math import isfinite
def histogram_ledger(latencies_ms: list[float], edges_ms: list[float]):
if (not latencies_ms or len(edges_ms) < 2 or
any(not isfinite(edge) for edge in edges_ms) or
any(left >= right for left, right in zip(edges_ms, edges_ms[1:]))):
raise ValueError("supply observations and strictly increasing finite edges")
counts = [0] * (len(edges_ms) - 1)
for latency in latencies_ms:
if not isfinite(latency) or latency < edges_ms[0] or latency > edges_ms[-1]:
raise ValueError("every observation must be finite and inside the domain")
index = min(bisect_right(edges_ms, latency) - 1, len(counts) - 1)
counts[index] += 1
total = len(latencies_ms)
ledger = [(left, right, count, count / total,
count / (total * (right - left)))
for left, right, count in zip(edges_ms, edges_ms[1:], counts)]
assert sum(row[2] for row in ledger) == total
assert abs(sum((row[1] - row[0]) * row[4] for row in ledger) - 1) < 1e-12
return ledger
request_latencies = [47, 59, 83, 97, 131, 149, 173, 211, 227, 251]
bins = histogram_ledger(request_latencies, [40, 80, 160, 320])
assert [row[2] for row in bins] == [2, 4, 4]
assert bins[0][4] == bins[1][4] == 0.005
assert histogram_ledger([40, 80, 160, 320], [40, 80, 160, 320])[-1][2] == 2Performance and operating cost
For N measurements and B bins, binary search assigns each observation in O(log B) time, so the sweep costs O(N log B); constructing the ledger costs O(B). The returned counts and rows need O(B) extra space. The ten-request check gives bin shares 0.2, 0.4 and 0.4, while the first two densities both equal 0.005 share per millisecond. The boundary assertion places 80 in the second interval and includes 320 in the final interval. Preserve this boundary policy when computing comparison charts.
Common Mistakes
- Using raw counts as rectangle heights when widths differ.
- Labeling a probability-density vertical axis as a count axis.
- Dropping the last-edge observation or assigning an internal edge twice.
- Changing bin boundaries between cohorts and reading the changed geometry as changed behavior.
Validation drill
Measure 470 request latencies, declare three unequal finite intervals and audit every boundary value. Publish an interval ledger, an excluded-record count, a fixed-edge comparison for a second week and a density-area check that equals one.
