Skip to content

Histogram

A histogram answers “how is this number distributed?” Plot expresses it as a rectY mark wrapped in a binX transform: the transform groups rows into bins, the mark draws a bar for each bin’s count.

```plot view=order_amounts
Plot.rectY(data, Plot.binX({ y: "count" }, { x: "amount" }))
```
```plot view=order_amounts
{
marks: [
Plot.rectY(data, Plot.binX({ y: "count" }, { x: "amount" })),
Plot.ruleX([100], { stroke: "tomato", strokeDasharray: "4 2" }),
]
}
```

Pass a fill channel; bins stack by group at each x.

```plot view=order_amounts_by_segment
Plot.rectY(data, Plot.binX({ y: "count" }, { x: "amount", fill: "segment" }))
```
OptionMeaning
xcolumn name (the numeric variable being binned)
thresholdsbin count, e.g. 24; or an array of cut points; or a function
fillcolumn name → grouping (rows in the same bin stack by this)
Plot.binX({ y: "count" }, ...)count rows per bin (most common)
Plot.binX({ y: "sum" }, ...)sum a column per bin (set y2: "amount")
Plot.binX({ y: "mean" }, ...)mean a column per bin
intervalfor date bins: "day", "month", "year", etc.
cumulativetrue for a cumulative histogram

See Observable Plot — bin and rect.