density
A Gaussian kernel density estimate of a sample, drawn as one curve.
densityxyplane

density(x: #sample.height) draws a Gaussian kernel density estimate of the readings: one curve, spread along the axis the readings are written on and rising up the other. y: turns the picture a quarter turn. Two coordinates are refused (canvas/density-form), and the mark is on the xy plane only.
The curve's heights are densities, in reciprocal units of the readings. The whole estimate has area 1, but it is drawn only from the smallest reading to the largest and cut square at each end, so the drawn area is a little under 1. A histogram's bars are counts, so the two do not share a y scale.
bandwidth is the standard deviation of the kernel, in the readings' units. Left out, it is Silverman's rule, 0.9 × min(s, IQR ÷ 1.34) × n^(−1/5), with s the sample standard deviation and the IQR from Tukey's hinges. A written bandwidth must be positive (canvas/density-bandwidth); a parameter driven to zero draws nothing. The curve publishes mode, the reading it peaks at, peak, its height there, and current-bandwidth, the width actually used — the only way to state what Silverman's rule chose.
scene{
Smoothed by {{round(#kde.current-bandwidth, 2)}}, the incomes peak at {{round(#kde.mode, 1)}}.
}[
graph{
slider:h(name: bandwidth; range: [0.5, 8]; default: 2)
density#kde(x: #income.k; bandwidth: h)
}(
x axis label: income (£k)
)
]#Attributes
xdensity:valueyx:.bandwidthcanvas/density-bandwidth); a parameter driven to zero draws nothing.colourcolour-alongcolorpurplewidthcurve's is.width-along2stroke-stylesolid, dashed or dotted. A function of the point it is at, so it may change along the curve.stroke-alongsolidfill-stylesolid, hatched, dotted, or outlined, the curve alone. It may change along the curve, so a wash can cover only the tail past a critical value.fill-alongoutlinedopacity1label$maths$ and inline formatting render. Placed at the curve's peak, which is where a family of densities is told apart.hiddenhidden: true !cue.to{false}(at: 2) reveals it — and unlike a cue, a hidden element still frames the plot, so nothing moves when it appears.falselockeditable: graph. Nothing at all on a graph that is not editable.lockedfalsetext-size20layergroupgroup: Treated, Projected. It is drawn in the colour of the first group that has one, over its own colour:, and the footer's key names every group. A stroke the graph's key: gives a group does not change it.#Derived attributes
Computed while the element renders — read one with #id.name in attribute position or {{#id.name}} in prose, never set.
countmeansdmodepeakcurrent-bandwidthbandwidth attribute, which says what was asked for rather than what was used.#Allowed in
#Examples


graph{
density(x: #sample.height)
}

graph{
slider:h(name: bandwidth; range: [0.2, 4]; default: 1)
density(x: #sample.height; bandwidth: h; colour: purple)
}

graph{
density(x: #avp.income; bandwidth: 0.5; label: narrow)
density(x: #avp.income; bandwidth: 2; label: wide; colour: teal)
}