{"spec_id":"bar-stacked","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nbar-stacked: Stacked Bar Chart\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-09\n\"\"\"\n\nimport os\nimport pathlib\nimport sys\n\nimport pandas as pd\n\n\n# Remove local module from path to avoid shadowing the plotnine library\nsys.path = [p for p in sys.path if \"bar-stacked/implementations/python\" not in p]\n\nfrom plotnine import (\n    aes,\n    element_line,\n    element_rect,\n    element_text,\n    geom_bar,\n    geom_text,\n    ggplot,\n    labs,\n    position_stack,\n    scale_fill_manual,\n    theme,\n    theme_minimal,\n)\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 - Quarterly sales by product category\ndata = {\n    \"Quarter\": [\"Q1\", \"Q2\", \"Q3\", \"Q4\"] * 4,\n    \"Category\": [\"Electronics\"] * 4 + [\"Clothing\"] * 4 + [\"Home\"] * 4 + [\"Sports\"] * 4,\n    \"Sales\": [45, 52, 48, 68, 32, 38, 55, 42, 28, 31, 35, 38, 18, 22, 28, 25],\n}\n\ndf = pd.DataFrame(data)\n\n# Order categories for consistent stacking (largest at bottom)\ncategory_order = [\"Sports\", \"Home\", \"Clothing\", \"Electronics\"]\ndf[\"Category\"] = pd.Categorical(df[\"Category\"], categories=category_order, ordered=True)\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"Quarter\", y=\"Sales\", fill=\"Category\"))\n    + geom_bar(stat=\"identity\", position=\"stack\", width=0.7)\n    + geom_text(aes(label=\"Sales\"), position=position_stack(vjust=0.5), size=12, color=\"white\", fontweight=\"bold\")\n    + scale_fill_manual(values=IMPRINT)\n    + labs(title=\"bar-stacked · plotnine · anyplot.ai\", x=\"Quarter\", y=\"Sales (thousands USD)\", fill=\"Category\")\n    + theme_minimal()\n    + theme(\n        figure_size=(16, 9),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_line(color=INK, size=0.3, alpha=0.10),\n        panel_grid_minor=element_line(color=INK, size=0.2, alpha=0.05),\n        panel_border=element_rect(color=INK_SOFT, fill=None),\n        plot_title=element_text(size=24, weight=\"bold\", color=INK),\n        axis_title=element_text(size=20, color=INK),\n        axis_text=element_text(size=16, color=INK_SOFT),\n        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n        legend_title=element_text(size=18, color=INK),\n        legend_text=element_text(size=16, color=INK_SOFT),\n        legend_position=\"right\",\n    )\n)\n\n# Save\noutput_dir = pathlib.Path(__file__).parent\nplot.save(output_dir / f\"plot-{THEME}.png\", dpi=300, verbose=False)\n"}