5.4.16 · D2Scientific Computing (Python)

Visual walkthrough — 2D plots — line, scatter, bar, histogram, contour, imshow

1,836 words8 min readBack to topic

Step 1 — What data even is (dots on a number line)

WHAT. We have a list of measured numbers. Call them . Here the tiny subscript is just an address: is the first number, the second, and is how many numbers we have in total.

WHY. Before we can bucket anything, we must see the raw values. A histogram's whole job is to summarise this cloud of dots — so we look at the cloud first, undecorated.

PICTURE. Below, eight sample heights (in cm) are drawn as dots on a single horizontal line. No bars yet — just where each number lives.

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow

Step 2 — Fencing off the territory: min, max, and the range

WHAT. We find the smallest value and the largest , then look at the stretch between them: the range .

WHY. A histogram needs a fixed playing field. Every bucket must live inside , otherwise some numbers would have nowhere to go. The range is the total width we must divide up.

PICTURE. Two vertical fences drop at and . The green double-arrow is — the distance the fences enclose.

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow


Step 3 — Choosing buckets and slicing the range

WHAT. We pick a number of buckets, (the bins). We cut the range into equal slices. Each slice has width

WHY divide? We chose equal-width buckets so that every bar is comparable — a tall bar means "more numbers here," never "wider bucket here." Division is the tool that answers "if I share this length fairly among buckets, how much does each get?" — exactly like splitting a chocolate bar into equal pieces.

PICTURE. Same fences as Step 2, now chopped by interior tick marks into equal slices, each labelled with its width .

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow


Step 4 — Which bucket does a number fall in?

WHAT. Bucket (counting ) covers the interval The square bracket means "including this end"; the round bracket means "up to but not including this end." A number lands in bucket when

WHY the half-open interval? Every number must belong to exactly one bucket — no number may sit on a fence and get counted twice or zero times. Making the left edge inclusive () and the right edge exclusive () glues the buckets together with no overlap and no gaps.

PICTURE. One highlighted bucket . Its left edge is a solid dot (included), its right edge a hollow dot (excluded). A test point is shown obeying .

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow


Step 5 — Counting: the height

WHAT. For each bucket we simply count how many data points landed inside it. That count is the bucket's height: The symbol means "the number of items in this set" — literally "count them."

WHY. This is the payoff. A histogram deliberately forgets the exact value of each point and remembers only how crowded each bucket is. Crowding is what reveals the shape of the data.

PICTURE. The eight dots drop straight down into their buckets and stack up. The stack height in bucket is . This is the first time you see actual bars.

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow

Step 6 — From counts to a density (making the area equal 1)

WHAT. Raw counts depend on how many samples you took — 5000 draws give taller bars than 50. To compare shapes fairly (and to overlay a theoretical curve), we rescale each bar's height to

WHY this exact divisor ? We want the total area of all bars to equal (the meaning of a probability density). The area of bar is height width: Sum over all buckets: Dividing by cancels the width, and dividing by cancels the sample size — so the shape survives but the total area is pinned to 1.

PICTURE. Left panel: the count histogram (tall, height depends on ). Right panel: the same shape rescaled so its total area is 1, with a theoretical bell curve laid on top — now they live on the same vertical scale.

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow

Step 7 — Edge & degenerate cases (never leave the reader stranded)

WHAT / WHY / PICTURE — three things that break the naive formula:

  1. All values identical (). Then and — division gives a zero-width bucket, undefined. What tools do: Matplotlib nudges the fences apart (e.g. to ) so one lonely bar of height appears.
  2. A value exactly on the far-right fence (). The half-open rule (Step 4) would drop it — so the last bucket is made a special closed interval to catch it.
  3. A value outside because you forced a range with range=(a,b). Points beyond the fences are simply not counted — the areas no longer sum to 1. Watch for silently missing data.
Figure — 2D plots — line, scatter, bar, histogram, contour, imshow

The one-picture summary

Everything above compressed into one flow: dots → fences → equal slices → count → rescale to area 1.

Figure — 2D plots — line, scatter, bar, histogram, contour, imshow
Recall Feynman: retell the whole walkthrough in plain words

I start with a handful of numbers and just poke them onto a number line as dots (Step 1). I find the smallest and biggest and put fences there — the gap between fences is my "range" (Step 2). I decide how many buckets I want, and chop the range into that many equal-width slices; each slice's width is range-divided-by-number-of-buckets (Step 3). I glue the buckets edge-to-edge with a rule — a number belongs to a bucket if it's at-or-past the left edge but strictly before the right edge — so nobody is counted twice (Step 4). Then I let every dot fall into its bucket and I count the pile; the pile height is the bar (Step 5). Because tall piles just mean I took more samples, I shrink each bar by dividing by (number of samples × bucket width) so the total area becomes exactly 1 — now it's a probability density I can compare to a theory curve (Step 6). Finally I check the weird cases: all-equal numbers, a value sitting right on the last fence, and values pushed outside a forced range (Step 7). That's the whole histogram, no magic.

Recall

What does mean and how is it computed? ::: The width of one bin, . Why divide the height by for a density? ::: So that bar area = and all areas sum to 1. Why is the bin interval half-open ? ::: So every point lands in exactly one bin — no overlap, no gaps. What does count? ::: How many data points fall inside bin .

Related: NumPy meshgrid and broadcasting · Colormaps and perceptual uniformity (viridis) · Matplotlib Figure vs Axes object model · Seaborn — statistical plotting wrappers