{
let x = d3.scaleLinear().domain([-w, w])
let d = gaussian(mean, std**2)
let points = x => [x, d.pdf(x)]
let data = x.ticks(100).map(points)
let maxvalue = d.pdf(mean)
let val = clamp(-w, w)(d.ppf(score/100))
let top = Math.max(0.5, maxvalue+0.1)
let sigmas = [-3, -2, -1, 0, 1, 2, 3]
.map(n => {
const x = mean + n * std
return { x, y: d.pdf(x), label: n==0 ? `μ`: `${n}σ` }
})
let qx = d3
.range(0,100,100/quantiles)
.map(p => ({p, x:d.ppf(p/100)}))
.map(p => ({...p, y: d.pdf(p.x)}))
return Plot.plot({
width,
x: { domain: [-w, w], grid:false, line:true, ticks: w*2},
y: { ticks: 4, grid: false, domain: [0, top], },
marks: [
Plot.ruleX([val], {y:d.pdf(val), strokeWidth: 1.2, stroke: 'red', strokeDasharray: [4, 4]}),
Plot.ruleX(qx, {x:'x', y:'y', stroke: 'darkblue', strokeOpacity: 0.4, strokeWidth: 1}),
Plot.line(data,{curve: 'natural', strokeWidth: 1.5, stroke: 'darkblue'}),
Plot.text([[val , 0], [w, maxvalue]], {text: [val, maxvalue].map(n=>n.toFixed(3)), textAnchor: 'end', dy: -5, dx: -5}),
Plot.text(sigmas, {
x: 'x',
y: 'y',
text:'label',
textAnchor: 'center',
dy: -12,
fill: 'darkblue',
title: s => `${s.label}: ${s.x.toFixed(2)}`,
})
]
})
}