{"spec_id":"bar-stacked","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nbar-stacked: Stacked Bar Chart\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-09\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\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\"\n\n# Okabe-Ito palette (positions 1-4)\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n\n# Data: Quarterly revenue by product category (in millions USD)\ncategories = [\"Q1\", \"Q2\", \"Q3\", \"Q4\"]\nproducts = [\"Software\", \"Hardware\", \"Services\", \"Support\"]\n\nsoftware = np.array([45, 52, 48, 68])\nhardware = np.array([32, 28, 35, 42])\nservices = np.array([28, 31, 38, 35])\nsupport = np.array([15, 18, 20, 22])\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nx = np.arange(len(categories))\nbar_width = 0.6\n\n# Create stacked bars\nbottom = np.zeros(len(categories))\nfor i, (product, values) in enumerate(zip(products, [software, hardware, services, support], strict=True)):\n    ax.bar(x, values, bar_width, label=product, bottom=bottom, color=IMPRINT[i], edgecolor=PAGE_BG, linewidth=1.5)\n    bottom += values\n\n# Add total labels above each stacked bar\ntotals = software + hardware + services + support\nfor i, total in enumerate(totals):\n    ax.text(i, total + 3, f\"${total}M\", ha=\"center\", va=\"bottom\", fontsize=18, fontweight=\"bold\", color=INK)\n\n# Style\nax.set_xlabel(\"Quarter\", fontsize=20, color=INK)\nax.set_ylabel(\"Revenue (Millions USD)\", fontsize=20, color=INK)\nax.set_title(\"bar-stacked · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.set_xticks(x)\nax.set_xticklabels(categories)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT, labelcolor=INK_SOFT)\n\n# Legend\nleg = ax.legend(fontsize=16, loc=\"upper left\", bbox_to_anchor=(1.02, 1))\nif leg:\n    leg.get_frame().set_facecolor(ELEVATED_BG)\n    leg.get_frame().set_edgecolor(INK_SOFT)\n    plt.setp(leg.get_texts(), color=INK_SOFT)\n\n# Grid (subtle, y-axis only)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\nax.set_axisbelow(True)\n\n# Spines\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)\n\n# Y-axis limits with headroom for labels\nax.set_ylim(0, max(totals) + 20)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}