{"spec_id":"errorbar-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nerrorbar-basic: Basic Error Bar Plot\nLibrary: matplotlib 3.11.0 | Python 3.13.14\nQuality: 91/100 | Updated: 2026-06-30\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.lines import Line2D\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nBRAND = \"#009E73\"  # Imprint palette position 1 — in-spec machines\nIMPRINT_RED = \"#AE3030\"  # Imprint palette position 5 — out-of-spec machines\n\n# Data: fill weight calibration across 6 production machines (target: 500 g, USL: 505 g)\n# Machines 1-3 are well-calibrated (symmetric errors); machines 4-6 show process drift (asymmetric)\nmachines = [\"Machine 1\", \"Machine 2\", \"Machine 3\", \"Machine 4\", \"Machine 5\", \"Machine 6\"]\nx = np.arange(len(machines))\nweights = np.array([498.2, 499.8, 502.3, 507.5, 511.8, 518.4])\nTARGET = 500.0\nUPPER_SPEC = 505.0\n\n# Symmetric errors for well-calibrated machines; asymmetric for drifting machines\nerrors_lower = np.array([1.5, 1.2, 2.1, 2.8, 3.2, 4.1])\nerrors_upper = np.array([1.5, 1.2, 2.1, 4.5, 6.8, 8.3])\n\n# Color by compliance: in-spec = brand green, out-of-spec = Imprint red\npoint_colors = [BRAND if w <= UPPER_SPEC else IMPRINT_RED for w in weights]\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Tolerance band and reference lines drawn first (below data)\nax.axhspan(TARGET - 5, UPPER_SPEC, color=BRAND, alpha=0.07, zorder=0)\nax.axhline(TARGET, color=INK_SOFT, linewidth=1.2, linestyle=\"--\", alpha=0.6, zorder=1)\nax.axhline(UPPER_SPEC, color=IMPRINT_RED, linewidth=1.2, linestyle=\":\", alpha=0.7, zorder=1)\n\n# One errorbar call per point to support per-machine color coding\nfor xi, yi, el, eu, col in zip(x, weights, errors_lower, errors_upper, point_colors, strict=True):\n    ax.errorbar(\n        xi,\n        yi,\n        yerr=[[el], [eu]],\n        fmt=\"o\",\n        markersize=8,\n        color=col,\n        ecolor=col,\n        elinewidth=2.5,\n        capsize=7,\n        capthick=2.5,\n        markeredgecolor=PAGE_BG,\n        markeredgewidth=0.8,\n        alpha=0.95,\n    )\n\n# Style\ntitle = \"errorbar-basic · python · matplotlib · anyplot.ai\"\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK)\nax.set_xlabel(\"Production Machine\", fontsize=10, color=INK)\nax.set_ylabel(\"Fill Weight (g)\", fontsize=10, color=INK)\nax.set_xticks(x)\nax.set_xticklabels(machines)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\nax.yaxis.grid(True, alpha=0.12, linewidth=0.8, color=INK)\nax.set_axisbelow(True)\nax.set_ylim(490, 532)\n\n# Legend — in-spec/out-of-spec groups plus reference lines\nlegend_elements = [\n    Line2D(\n        [0],\n        [0],\n        marker=\"o\",\n        color=\"none\",\n        markerfacecolor=BRAND,\n        markeredgecolor=PAGE_BG,\n        markersize=8,\n        label=\"In-spec\",\n    ),\n    Line2D(\n        [0],\n        [0],\n        marker=\"o\",\n        color=\"none\",\n        markerfacecolor=IMPRINT_RED,\n        markeredgecolor=PAGE_BG,\n        markersize=8,\n        label=\"Out-of-spec\",\n    ),\n    Line2D([0], [0], color=INK_SOFT, linewidth=1.2, linestyle=\"--\", label=\"Target (500 g)\"),\n    Line2D([0], [0], color=IMPRINT_RED, linewidth=1.2, linestyle=\":\", label=\"Upper spec (505 g)\"),\n]\nleg = ax.legend(handles=legend_elements, fontsize=8, loc=\"upper left\")\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nplt.setp(leg.get_texts(), color=INK_SOFT)\n\n# Save\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}