{"spec_id":"histogram-capability","library":"makie","language":"julia","code":"# anyplot.ai\n# histogram-capability: Process Capability Plot with Specification Limits\n# Library: makie 0.22.10 | Julia 1.11.9\n# Quality: 87/100 | Created: 2026-06-20\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 ELEVATED_BG = THEME == \"light\" ? colorant\"#FFFDF6\" : colorant\"#242420\"\nconst INK         = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT    = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\nconst INK_MUTED   = THEME == \"light\" ? colorant\"#6B6A63\" : colorant\"#A8A79F\"\n\nconst IMPRINT_PALETTE = [\n    colorant\"#009E73\",\n    colorant\"#C475FD\",\n    colorant\"#4467A3\",\n    colorant\"#BD8233\",\n    colorant\"#AE3030\",\n    colorant\"#2ABCCD\",\n    colorant\"#954477\",\n    colorant\"#99B314\",\n]\n\n# Data — shaft diameter measurements (mm), slightly off-center process\nconst N      = 200\nconst LSL    = 9.95\nconst USL    = 10.05\nconst TARGET = 10.00\n\nmeasurements = randn(N) .* 0.015 .+ 10.012\n\nmu    = mean(measurements)\nsigma = std(measurements)\ncp    = (USL - LSL) / (6 * sigma)\ncpk   = min((USL - mu) / (3 * sigma), (mu - LSL) / (3 * sigma))\n\n# Normal distribution curve scaled to histogram counts\nn_bins           = 20\napprox_bin_width = (maximum(measurements) - minimum(measurements)) / n_bins\nx_curve          = range(LSL - 0.012, USL + 0.012, length = 400)\npdf_vals         = @. exp(-0.5 * ((x_curve - mu) / sigma)^2) / (sigma * sqrt(2π))\npdf_scaled       = pdf_vals .* N .* approx_bin_width\n\ny_ceil = maximum(pdf_scaled) * 1.4\n\n# Plot\nfig = Figure(\n    resolution      = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title             = \"histogram-capability · julia · makie · anyplot.ai\",\n    titlesize         = 20,\n    titlecolor        = INK,\n    xlabel            = \"Shaft Diameter (mm)\",\n    ylabel            = \"Count\",\n    xlabelsize        = 14,\n    ylabelsize        = 14,\n    xticklabelsize    = 12,\n    yticklabelsize    = 12,\n    xlabelcolor       = INK,\n    ylabelcolor       = INK,\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.12f0),\n    yminorgridvisible = false,\n    xminorgridvisible = false,\n)\n\n# Shaded reject zones outside spec limits (vspan! — Makie-native region primitive)\nvspan!(ax, [LSL - 0.015, USL], [LSL, USL + 0.015]; color = (IMPRINT_PALETTE[5], 0.08f0))\n\n# Histogram bars — first Imprint series (brand green)\nhist!(ax, measurements;\n    bins        = n_bins,\n    color       = IMPRINT_PALETTE[1],\n    strokecolor = PAGE_BG,\n    strokewidth = 1.0,\n)\n\n# Normal distribution curve — Imprint blue\nlines!(ax, collect(x_curve), pdf_scaled;\n    color     = IMPRINT_PALETTE[3],\n    linewidth = 2.5,\n)\n\n# Specification limit lines — matte red dashed (semantic: spec boundary / error)\nvlines!(ax, [LSL, USL];\n    color     = IMPRINT_PALETTE[5],\n    linewidth = 2.0,\n    linestyle = :dash,\n)\n\n# Target (nominal) line — ochre dashed\nvlines!(ax, [TARGET];\n    color     = IMPRINT_PALETTE[4],\n    linewidth = 2.0,\n    linestyle = :dash,\n)\n\nylims!(ax, 0, y_ceil)\nxlims!(ax, LSL - 0.015, USL + 0.015)\n\n# Limit and target labels\nlabel_y = y_ceil * 0.94\ntext!(ax, LSL + 0.001, label_y;\n    text     = \"LSL\",\n    align    = (:left, :top),\n    fontsize = 12,\n    color    = IMPRINT_PALETTE[5],\n)\ntext!(ax, TARGET + 0.001, label_y;\n    text     = \"Target\",\n    align    = (:left, :top),\n    fontsize = 12,\n    color    = IMPRINT_PALETTE[4],\n)\ntext!(ax, USL - 0.001, label_y;\n    text     = \"USL\",\n    align    = (:right, :top),\n    fontsize = 12,\n    color    = IMPRINT_PALETTE[5],\n)\n\n# Capability annotation — upper right, below limit labels\ncp_rounded  = round(cp,  digits = 2)\ncpk_rounded = round(cpk, digits = 2)\ntext!(ax, USL - 0.003, y_ceil * 0.72;\n    text     = \"Cp  = $(cp_rounded)\\nCpk = $(cpk_rounded)\",\n    align    = (:right, :top),\n    fontsize = 14,\n    color    = INK,\n)\n\n# Save\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}