Norm Distribution Viz
Plot a Gaussian N(μ, σ²) as a smooth bell curve with shaded ±1σ and ±2σ regions. Two sliders drive μ and σ; the curve translates and stretches in place inside a stable x-axis. Useful for teaching the geometry of the normal distribution, the 68-95-99.7 rule, or the effect of parameter changes on a probability density.
Normal distribution visualizationNormal distribution N(0.00, 1.00). Mean 0.00, standard deviation 1.00. Plotted over x in [-15.00, 15.00].
μ = 0.00σ = 1.00σ² = 1.00
0.00
1.00
Customize
Parameters
0
1
Display
Installation
npx shadcn@latest add https://craftbits.dev/r/norm-distribution-viz.jsonUsage
import { NormDistributionViz } from "@craft-bits/core";
<NormDistributionViz defaultMean={0} defaultStddev={1} />Control μ and σ from outside the component:
const [mu, setMu] = useState(0);
const [sigma, setSigma] = useState(1);
<NormDistributionViz
mean={mu}
onMeanChange={setMu}
stddev={sigma}
onStddevChange={setSigma}
/>Pin the x-axis and hide the shaded regions:
<NormDistributionViz
defaultMean={0}
defaultStddev={1}
xRange={[-5, 5]}
showSigmaRegions={false}
/>Understanding the component
- Bell curve from analytic parameters. The component samples the Gaussian PDF
f(x | μ, σ) = exp(−(x − μ)² / 2σ²) / (σ · √(2π))at 120 evenly-spaced x-values and rasterizes the result as one SVGpath. No samples, no smoothing, no histogramming — the curve is the analytic density. - Shaded ±σ regions. When
showSigmaRegionsis on, two filled regions sit under the curve: the inner ±1σ band at 30% accent opacity (~68% of probability mass), and the outer ±2σ band at 15% accent opacity (~95% of mass). The ±2σ band is drawn first so the ±1σ band visually deepens on top of it. - Stable x-axis. Unless
xRangeis supplied, the plot uses a fixed window of[meanMin − 4·sigmaMax, meanMax + 4·sigmaMax]. It stays put as the sliders move, so the user sees the curve translate and stretch inside the same frame instead of the axis re-fitting under them. - Auto-normalized y-axis. The y-scale is normalized to the current peak density
1 / (σ√(2π)). Sharpening σ makes the curve taller; flattening σ makes it shorter. Visual height encodes density, not raw mass. - Controlled and uncontrolled, twice. Both
meanandstddevpair a controlledvalue+onChangewith an uncontrolleddefault*. Out-of-range values clamp on read. - Spring transitions. The bell
d, the two shaded region paths, and the mean guideline all animate withSPRINGS.smooth.prefers-reduced-motion: reducecollapses every spring toduration: 0.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
mean | number | — | Controlled mean μ. Pair with onMeanChange. |
defaultMean | number | 0 | Uncontrolled initial mean. |
onMeanChange | (mean: number) => void | — | Fires on every μ commit. |
stddev | number | — | Controlled standard deviation σ. Pair with onStddevChange. |
defaultStddev | number | 1 | Uncontrolled initial standard deviation. |
onStddevChange | (stddev: number) => void | — | Fires on every σ commit. |
xRange | [number, number] | auto | Override the x-axis range. |
showSigmaRegions | boolean | true | Shade the ±1σ and ±2σ regions under the curve. |
sigmaMin | number | 0.2 | Minimum σ the slider can reach (must be > 0). |
sigmaMax | number | 3 | Maximum σ the slider can reach. |
meanMin | number | -3 | Minimum μ the slider can reach. |
meanMax | number | 3 | Maximum μ the slider can reach. |
showControls | boolean | true | Render the μ / σ sliders. |
showAxis | boolean | true | Render the x-axis baseline and tick labels. |
transition | Transition | SPRINGS.smooth | Spring for the curve / region / guideline updates. |
className | string | — | Merged onto the root <div> via cn(). |
Accessibility
- The root
<div>isrole="figure"with a visually-hidden title plus anaria-live="polite"summary that announcesN(μ, σ²), μ, σ, and the x-range whenever a slider commits. - The SVG is
role="img"with the same accessible name; inner paths and the guideline are decorative. - Each slider is the library's
LabeledSlider, a native<input type="range">wrapper with full keyboard support (arrows,Home,End) andaria-valuenow/aria-valuetext. - Motion respects
prefers-reduced-motion: reduce— all springs collapse to an instant swap.
Credits
- Extracted from:
craftingattention(app/src/lessons/primitives/viz/NormDistributionViz.tsx). The original was a four-panel LayerNorm pipeline widget — raw / centered / scaled /γ·x + βhistograms wired to a training-step scrubber, predict-mode quizzes, and per-narration plumbing. The library extract is the underlying statistics primitive: a singleN(μ, σ²)bell curve with shaded ±σ bands and standard μ / σ controls.