The same plot in 14 other libraries — JavaScript: Chart.js, D3.js, Apache ECharts, Highcharts; Python: Altair, Bokeh, lets-plot, Matplotlib, Plotly, plotnine, Pygal, Seaborn; R: ggplot2; Julia: Makie.jl. Compare all 15 side by side: Density Histogram in Python, R, Julia and JavaScript.
A density histogram displays the distribution of a continuous variable normalized so that the total area under the histogram equals 1, representing probability density instead of raw counts. This normalization allows direct comparison between distributions with different sample sizes and enables overlaying theoretical probability density functions (PDFs) for statistical analysis.

// anyplot.ai
// histogram-density: Density Histogram
// Library: muix 9.16.0 | JavaScript 22.23.3
// Quality: 86/100 | Updated: 2026-10-24
import { ChartsDataProvider } from "@mui/x-charts/ChartsDataProvider";
import { ChartsWrapper } from "@mui/x-charts/ChartsWrapper";
import { ChartsSurface } from "@mui/x-charts/ChartsSurface";
import { ChartsLegend } from "@mui/x-charts/ChartsLegend";
import { BarPlot } from "@mui/x-charts/BarChart";
import { LinePlot } from "@mui/x-charts/LineChart";
import { ChartsXAxis } from "@mui/x-charts/ChartsXAxis";
import { ChartsYAxis } from "@mui/x-charts/ChartsYAxis";
import { ChartsGrid } from "@mui/x-charts/ChartsGrid";
import { axisClasses } from "@mui/x-charts/ChartsAxis";
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
const t = window.ANYPLOT_TOKENS;
// Constants
const FONT = "system-ui, -apple-system, sans-serif";
const TITLE = "histogram-density · javascript · muix · anyplot.ai";
const BIN_COUNT = 18;
const Y_HEADROOM_RATIO = 0.15;
const BRAND = t.palette[0];
const CURVE = t.palette[1];
// Columns: set these to your data's column names
const WEIGHT = "Package weight (g)"; // role: values
// Data: replace this block with your own records
type DensityRow = {
[WEIGHT]: number;
};
const data: DensityRow[] = [];
let seed = 7 >>> 0;
const sampleSize = 400;
for (let i = 0; i < sampleSize; i++) {
seed = (Math.imul(1664525, seed) + 1013904223) >>> 0;
const u1 = Math.max(seed / 0x100000000, 1e-10);
seed = (Math.imul(1664525, seed) + 1013904223) >>> 0;
const u2 = seed / 0x100000000;
const z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
data.push({
[WEIGHT]: Number((500 + z * 6).toFixed(3)),
});
}
// Plot: reads only data and the constants above
const weights = data.map((r) => r[WEIGHT]);
const n = weights.length;
const dataMin = Math.min(...weights);
const dataMax = Math.max(...weights);
const binWidth = (dataMax - dataMin) / BIN_COUNT;
const binCounts = new Array(BIN_COUNT).fill(0);
for (const w of weights) {
const idx = Math.min(BIN_COUNT - 1, Math.floor((w - dataMin) / binWidth));
binCounts[idx] += 1;
}
const density = binCounts.map((c) => c / (n * binWidth));
const binCenters = binCounts.map((_, i) => dataMin + (i + 0.5) * binWidth);
const sampleMean = weights.reduce((s, w) => s + w, 0) / n;
const sampleSd = Math.sqrt(
weights.reduce((s, w) => s + (w - sampleMean) ** 2, 0) / (n - 1),
);
const normalPdf = binCenters.map((x) => {
const z = (x - sampleMean) / sampleSd;
return Math.exp(-0.5 * z * z) / (sampleSd * Math.sqrt(2 * Math.PI));
});
const yMax = Math.max(...density, ...normalPdf) * (1 + Y_HEADROOM_RATIO);
const unitMatch = WEIGHT.match(/\(([^)]+)\)/);
const unitStr = unitMatch ? unitMatch[1] : "";
const unitSuffix = unitStr ? ` ${unitStr}` : "";
const SUBTITLE = `${WEIGHT} (n = ${n}, bin width = ${binWidth.toFixed(2)}${unitSuffix}) · Mean ${sampleMean.toFixed(1)}${unitSuffix}, SD ${sampleSd.toFixed(1)}${unitSuffix}`;
export default function Chart() {
const { width, height } = window.ANYPLOT_SIZE;
const HEADER_H = 76;
const LEGEND_H = 40;
return (
<Box
sx={{
width,
height,
bgcolor: t.pageBg,
display: "flex",
flexDirection: "column",
overflow: "hidden",
}}
>
<Typography
sx={{
fontSize: 22,
fontWeight: 600,
color: t.ink,
pt: "14px",
px: "40px",
pb: 0,
lineHeight: 1.2,
fontFamily: FONT,
}}
>
{TITLE}
</Typography>
<Typography
sx={{
fontSize: 15,
fontWeight: 400,
color: t.inkSoft,
px: "40px",
pt: "4px",
pb: 0,
lineHeight: 1.2,
fontFamily: FONT,
}}
>
{SUBTITLE}
</Typography>
<ChartsDataProvider
width={width}
height={height - HEADER_H - LEGEND_H}
series={[
{
type: "bar",
id: "observed",
data: density,
label: `Observed density (${BIN_COUNT} bins)`,
labelMarkType: "square",
color: BRAND,
},
{
type: "line",
id: "fitted",
data: normalPdf,
label: "Fitted Normal PDF",
labelMarkType: "line",
color: CURVE,
showMark: false,
curve: "monotoneX",
},
]}
xAxis={[
{
id: "weight-axis",
scaleType: "band",
data: binCenters,
categoryGapRatio: 0.02,
height: 60,
label: WEIGHT,
valueFormatter: (v: number) => v.toFixed(1),
tickLabelInterval: (_v: number, i: number) => i % 2 === 0,
labelStyle: { fontSize: 15, fill: t.ink, fontFamily: FONT },
tickLabelStyle: { fontSize: 14, fill: t.inkSoft, fontFamily: FONT },
},
]}
yAxis={[
{
id: "density-axis",
min: 0,
max: yMax,
width: 96,
label: unitStr ? `Probability Density (1/${unitStr})` : "Probability Density",
valueFormatter: (v: number) => v.toFixed(3),
labelStyle: { fontSize: 15, fill: t.ink, fontFamily: FONT },
tickLabelStyle: { fontSize: 14, fill: t.inkSoft, fontFamily: FONT },
},
]}
margin={{ top: 20, right: 40, bottom: 20, left: 20 }}
skipAnimation
>
<ChartsWrapper
legendPosition={{ horizontal: "start", vertical: "top" }}
legendDirection="horizontal"
>
<ChartsLegend
sx={{
fontSize: 14,
fontWeight: 600,
color: t.ink,
fontFamily: FONT,
px: "40px",
pt: "8px",
gap: "24px",
}}
/>
<ChartsSurface>
<ChartsGrid
horizontal
sx={{ "& line": { stroke: t.grid, strokeWidth: 0.8 } }}
/>
<BarPlot skipAnimation />
<LinePlot
skipAnimation
slotProps={{ line: { sx: { strokeWidth: 3.5 } } }}
/>
<ChartsXAxis
axisId="weight-axis"
sx={{
[`& .${axisClasses.line}`]: { stroke: t.grid },
[`& .${axisClasses.tick}`]: { stroke: t.grid },
}}
/>
<ChartsYAxis
axisId="density-axis"
sx={{
[`& .${axisClasses.line}`]: { stroke: t.grid },
[`& .${axisClasses.tick}`]: { stroke: t.grid },
}}
/>
</ChartsSurface>
</ChartsWrapper>
</ChartsDataProvider>
</Box>
);
}
Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/histogram-density/muix/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.
{
"spec_id": "histogram-density",
"language": "javascript",
"library": "muix",
"page": "https://anyplot.ai/histogram-density/javascript/muix",
"hub": "https://anyplot.ai/histogram-density",
"code_json": "https://api.anyplot.ai/specs/histogram-density/muix/code",
"spec_json": "https://api.anyplot.ai/specs/histogram-density",
"render_light_png": "https://storage.googleapis.com/anyplot-images/plots/histogram-density/javascript/muix/plot-light.png",
"render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/histogram-density/javascript/muix/plot-dark.png",
"interactive_light_html": "https://storage.googleapis.com/anyplot-images/plots/histogram-density/javascript/muix/plot-light.html",
"interactive_dark_html": "https://storage.googleapis.com/anyplot-images/plots/histogram-density/javascript/muix/plot-dark.html",
"quality_score": 86.0,
"license": "MIT",
"guide": "https://anyplot.ai/llms.txt"
}Part of Density Histogram on anyplot.ai.