{"spec_id":"bar-stacked","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nbar-stacked: Stacked Bar Chart\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 84/100 | Updated: 2026-05-09\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\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 (first series always #009E73)\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n\n# Data - Monthly sales by product category\nnp.random.seed(42)\ncategories = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\"]\nproducts = [\"Electronics\", \"Clothing\", \"Home & Garden\", \"Sports\"]\n\ndata = {\n    \"Month\": categories * len(products),\n    \"Product\": [p for p in products for _ in categories],\n    \"Sales\": [\n        # Electronics - highest, growing trend\n        120,\n        135,\n        145,\n        160,\n        175,\n        190,\n        # Clothing - seasonal variation\n        85,\n        70,\n        95,\n        110,\n        90,\n        75,\n        # Home & Garden - spring/summer peak\n        45,\n        55,\n        80,\n        95,\n        85,\n        60,\n        # Sports - summer peak\n        35,\n        40,\n        55,\n        70,\n        85,\n        65,\n    ],\n}\n\ndf = pd.DataFrame(data)\n\n# Preserve category order\ndf[\"Month\"] = pd.Categorical(df[\"Month\"], categories=categories, ordered=True)\n# Order products by total sales (largest at bottom of stack)\nproduct_totals = df.groupby(\"Product\")[\"Sales\"].sum().sort_values(ascending=False)\nordered_products = product_totals.index.tolist()\ndf[\"Product\"] = pd.Categorical(df[\"Product\"], categories=ordered_products, ordered=True)\n\n# Set theme and styling\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\nsns.set_context(\"talk\", font_scale=1.2)\n\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\n\n# Create color map for products\nproduct_colors = {p: IMPRINT[i] for i, p in enumerate(ordered_products)}\ncolors = [product_colors[p] for p in ordered_products]\n\n# Plot stacked bar chart using histplot\nsns.histplot(\n    data=df,\n    x=\"Month\",\n    weights=\"Sales\",\n    hue=\"Product\",\n    multiple=\"stack\",\n    palette=colors,\n    shrink=0.7,\n    edgecolor=PAGE_BG,\n    linewidth=1.5,\n    ax=ax,\n)\n\n# Calculate totals for labels on top of stacks\ntotals = df.groupby(\"Month\", observed=True)[\"Sales\"].sum()\nfor i, (_month, total) in enumerate(totals.items()):\n    ax.text(i, total + 8, f\"${int(total)}K\", ha=\"center\", va=\"bottom\", fontsize=16, fontweight=\"bold\", color=INK)\n\n# Styling\nax.set_xlabel(\"Month\", fontsize=20, color=INK)\nax.set_ylabel(\"Sales (Thousands $)\", fontsize=20, color=INK)\nax.set_title(\"bar-stacked · seaborn · anyplot.ai\", fontsize=24, fontweight=\"bold\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Legend\nlegend = ax.get_legend()\nlegend.set_title(\"Product Category\")\nlegend.get_title().set_fontsize(18)\nlegend.get_title().set_color(INK)\nfor text in legend.get_texts():\n    text.set_fontsize(16)\n    text.set_color(INK)\nlegend.set_bbox_to_anchor((1.02, 1))\nlegend.set_loc(\"upper left\")\nlegend.get_frame().set_facecolor(ELEVATED_BG)\nlegend.get_frame().set_edgecolor(INK_SOFT)\n\n# Grid styling\nax.yaxis.grid(True, alpha=0.10, linewidth=0.8)\nax.xaxis.grid(False)\nax.set_axisbelow(True)\n\n# Spine styling\nfor spine in [\"top\", \"right\"]:\n    ax.spines[spine].set_visible(False)\nfor spine in [\"left\", \"bottom\"]:\n    ax.spines[spine].set_color(INK_SOFT)\n\n# Adjust y-axis to accommodate total labels\nax.set_ylim(0, totals.max() * 1.15)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}