{"spec_id":"histogram-density","library":"makie","language":"julia","code":"# anyplot.ai\n# histogram-density: Density Histogram\n# Library: makie 0.21.9 | Julia 1.11.9\n# Quality: 70/100 | Created: 2026-09-05\n\nusing CairoMakie\nusing Colors\nusing Random\nusing Statistics\n\nRandom.seed!(42)\n\n# --- Theme tokens -----------------------------------------------------------\nconst THEME    = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG  = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst INK      = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\nconst IMPRINT_PALETTE = [\n    colorant\"#009E73\", colorant\"#C475FD\", colorant\"#4467A3\", colorant\"#BD8233\",\n    colorant\"#AE3030\", colorant\"#2ABCCD\", colorant\"#954477\", colorant\"#99B314\",\n]\n\n# --- Data ---------------------------------------------------------------\n# Net weight (grams) of cereal boxes off a filling line, nominal fill 500 g.\nbox_weights = 500 .+ 15 .* randn(450)\nnominal_fill = 500.0\nn_bins = 20\n\nmu = mean(box_weights)\nsigma = std(box_weights)\nx_fit = range(minimum(box_weights), maximum(box_weights); length = 200)\npdf_fit = @. 1 / (sigma * sqrt(2π)) * exp(-0.5 * ((x_fit - mu) / sigma)^2)\n\n# Locate the isolated out-of-spec box (heaviest bin) for a callout annotation.\nbin_edges = range(minimum(box_weights), maximum(box_weights); length = n_bins + 1)\nbin_width = step(bin_edges)\noutlier_lo = bin_edges[end-1]\noutlier_mid = (bin_edges[end-1] + bin_edges[end]) / 2\noutlier_density = count(>=(outlier_lo), box_weights) / (length(box_weights) * bin_width)\noutlier_label_y = outlier_density + 0.0045\n\n# --- Plot -----------------------------------------------------------------\ntitle_text = \"Cereal Box Net Weight · histogram-density · julia · makie · anyplot.ai\"\n\nfig = Figure(\n    resolution      = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title             = title_text,\n    titlesize         = 19,\n    titlecolor        = INK,\n    xlabel            = \"Net Weight (g)\",\n    ylabel            = \"Density\",\n    xlabelsize        = 14,\n    ylabelsize        = 14,\n    xlabelcolor       = INK,\n    ylabelcolor       = INK,\n    xticklabelsize    = 12,\n    yticklabelsize    = 12,\n    xticklabelcolor   = INK_SOFT,\n    yticklabelcolor   = INK_SOFT,\n    xtickcolor        = INK_SOFT,\n    ytickcolor        = INK_SOFT,\n    backgroundcolor   = PAGE_BG,\n    topspinevisible   = false,\n    rightspinevisible = false,\n    leftspinecolor    = INK_SOFT,\n    bottomspinecolor  = INK_SOFT,\n    xgridvisible      = false,\n    ygridcolor        = RGBAf(INK.r, INK.g, INK.b, 0.15),\n)\n\nhist!(\n    ax, box_weights;\n    normalization = :pdf,\n    bins          = n_bins,\n    color         = IMPRINT_PALETTE[1],\n    strokewidth   = 1,\n    strokecolor   = PAGE_BG,\n    label         = \"Observed boxes\",\n)\n\nband!(ax, x_fit, zeros(length(x_fit)), pdf_fit; color = (IMPRINT_PALETTE[2], 0.18))\nlines!(ax, x_fit, pdf_fit; color = IMPRINT_PALETTE[2], linewidth = 3, label = \"Normal fit\")\n\ndensity!(\n    ax, box_weights;\n    npoints     = 200,\n    color       = :transparent,\n    strokecolor = IMPRINT_PALETTE[3],\n    strokewidth = 4,\n    linestyle   = :dash,\n    label       = \"Smoothed density (KDE)\",\n)\n\nvlines!(ax, [nominal_fill]; color = INK, linestyle = :dash, linewidth = 2.5, label = \"Nominal fill (500 g)\")\n\nlines!(\n    ax, [outlier_mid, outlier_mid], [outlier_density, outlier_label_y];\n    color = INK, linewidth = 1.5, linestyle = :dot,\n)\ntext!(\n    ax, outlier_mid, outlier_label_y;\n    text = \"Out-of-spec box\", align = (:center, :bottom), color = INK, fontsize = 13, font = :bold,\n)\n\naxislegend(\n    ax;\n    position      = :rt,\n    framevisible  = false,\n    labelcolor    = INK_SOFT,\n    labelsize     = 12,\n    backgroundcolor = :transparent,\n)\n\n# --- Save -------------------------------------------------------------------\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}