{"spec_id":"bar-error","library":"makie","language":"julia","code":"# anyplot.ai\n# bar-error: Bar Chart with Error Bars\n# Library: makie 0.21.9 | Julia 1.11.9\n# Quality: 81/100 | Updated: 2026-09-27\n\nusing CairoMakie\nusing Colors\nusing Random\nusing Statistics\n\nRandom.seed!(42)\n\n# Theme tokens (see prompts/default-style-guide.md \"Theme-adaptive Chrome\")\nTHEME    = get(ENV, \"ANYPLOT_THEME\", \"light\")\nPAGE_BG  = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nINK      = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nINK_SOFT = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\nBRAND    = colorant\"#009E73\"  # Imprint palette position 1 — ALWAYS first series\n\n# Data — 30 simulated runs per catalyst, drawn from each catalyst's known\n# reaction-yield distribution. Yield is bounded by a 100% ceiling, so the\n# upside spread compresses relative to the downside the closer the mean\n# sits to that ceiling — modeled here as an asymmetric half-normal mixture\n# rather than a symmetric SD, giving genuinely asymmetric error bars that\n# are derived from the runs themselves (not hardcoded).\ncatalysts     = [\"Pd/C\", \"Pt/C\", \"Ru/C\", \"Ni\", \"Cu\", \"Fe\"]\ntrue_mean     = [87.4, 82.1, 74.9, 71.6, 63.8, 54.2]\ntrue_std_low  = [3.6, 5.2, 5.8, 6.3, 7.3, 8.4]   # downside spread\ntrue_std_high = [2.1, 3.4, 4.2, 5.0, 6.2, 7.6]   # upside spread, ceiling-compressed\nn_runs = 30\n\nruns = [\n    tm .+ ifelse.(rand(n_runs) .< 0.5, -tsl .* abs.(randn(n_runs)), tsh .* abs.(randn(n_runs)))\n    for (tm, tsl, tsh) in zip(true_mean, true_std_low, true_std_high)\n]\nmean_yield = mean.(runs)\nlower_err  = mean_yield .- quantile.(runs, 0.16)\nupper_err  = quantile.(runs, 0.84) .- mean_yield\nx = 1:length(catalysts)\nbest = argmax(mean_yield)\nrunner_up = partialsortperm(mean_yield, 2, rev = true)\n\n# Bar stroke: the top performer gets a bolder ink-colored outline so the\n# eye lands on it before reading the bracket callout below.\nbar_strokecolors = [i == best ? INK : PAGE_BG for i in 1:length(catalysts)]\nbar_strokewidths = [i == best ? 3.0 : 1.5 for i in 1:length(catalysts)]\n\n# Plot — see default-style-guide.md \"Visual Sizing Defaults\" and prompts/library/makie.md\nfig = Figure(\n    size            = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title             = \"bar-error · julia · makie · anyplot.ai\",\n    titlesize         = 20,\n    titlecolor        = INK,\n    subtitle          = \"Error bars: 16th–84th percentile range (n = 30 runs per catalyst)\",\n    subtitlesize      = 14,\n    subtitlecolor     = INK_SOFT,\n    xlabel            = \"Catalyst\",\n    ylabel            = \"Reaction Yield (%)\",\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    xticks            = (x, catalysts),\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    xminorgridvisible = false,\n    yminorgridvisible = false,\n)\n\nbarplot!(ax, x, mean_yield;\n    color       = BRAND,\n    strokecolor = bar_strokecolors,\n    strokewidth = bar_strokewidths,\n    width       = 0.6,\n)\n\n# Raincloud-style overlay: jittered individual runs, hinting at the\n# per-catalyst distribution the bar+error-bar summary is drawn from.\njitter_x = vcat([fill(xi, n_runs) .+ (rand(n_runs) .- 0.5) .* 0.32 for xi in x]...)\njitter_y = vcat(runs...)\nscatter!(ax, jitter_x, jitter_y;\n    color       = (INK, 0.22),\n    markersize  = 5,\n    strokewidth = 0,\n)\n\nerrorbars!(ax, x, mean_yield, lower_err, upper_err;\n    color        = INK,\n    linewidth    = 2,\n    whiskerwidth = 18,\n)\n\n# Makie-specific `bracket!` recipe: a curly brace spanning the top two\n# catalysts' error-bar caps, labeled with the actual yield gap between\n# them — a distinctive Makie primitive (no direct matplotlib/plotly\n# equivalent) that doubles as the chart's data-storytelling focal point.\ngap = round(mean_yield[best] - mean_yield[runner_up]; digits = 1)\nbracket!(ax,\n    x[best], mean_yield[best] + upper_err[best],\n    x[runner_up], mean_yield[runner_up] + upper_err[runner_up];\n    text        = \"+$(gap) pts vs runner-up\",\n    style       = :curly,\n    orientation = :up,\n    offset      = 14,\n    width       = 20,\n    rotation    = 0,\n    align       = (:center, :bottom),\n    color       = INK_SOFT,\n    textcolor   = INK,\n    fontsize    = 13,\n    linewidth   = 1.5,\n)\n\nylims!(ax, 0, maximum(mean_yield .+ upper_err) * 1.22)\n\n# Save\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}