{"spec_id":"bar-stacked-percent","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nbar-stacked-percent: 100% Stacked Bar Chart\nLibrary: letsplot 4.11.0 | Python 3.13.15\nQuality: 90/100 | Updated: 2026-08-18\n\"\"\"\n\nimport os\n\nimport pandas as pd\nfrom lets_plot import (\n    LetsPlot,\n    aes,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_bar,\n    geom_text,\n    ggplot,\n    ggsave,\n    ggsize,\n    labs,\n    position_fill,\n    scale_color_identity,\n    scale_fill_manual,\n    scale_y_continuous,\n    theme,\n    theme_minimal,\n)\n\n\nLetsPlot.setup_html()\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\"\nGRID_COLOR = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Imprint palette (first series always #009E73)\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\"]\n\n# Data: Energy source mix by country, ranked by renewable share (descending)\ndata = {\n    \"country\": [\"Spain\"] * 5 + [\"Germany\"] * 5 + [\"Italy\"] * 5 + [\"UK\"] * 5 + [\"France\"] * 5 + [\"Poland\"] * 5,\n    \"source\": [\"Coal\", \"Natural Gas\", \"Nuclear\", \"Renewables\", \"Other\"] * 6,\n    \"value\": [\n        # Spain\n        3,\n        22,\n        21,\n        50,\n        4,\n        # Germany\n        26,\n        15,\n        6,\n        46,\n        7,\n        # Italy\n        6,\n        42,\n        0,\n        45,\n        7,\n        # UK\n        5,\n        38,\n        15,\n        39,\n        3,\n        # France\n        2,\n        7,\n        68,\n        21,\n        2,\n        # Poland\n        68,\n        10,\n        0,\n        17,\n        5,\n    ],\n}\n\ndf = pd.DataFrame(data)\n\n# Set category order for proper stacking (ranked by renewable share, descending)\ndf[\"country\"] = pd.Categorical(\n    df[\"country\"], categories=[\"Spain\", \"Germany\", \"Italy\", \"UK\", \"France\", \"Poland\"], ordered=True\n)\ndf[\"source\"] = pd.Categorical(\n    df[\"source\"], categories=[\"Coal\", \"Natural Gas\", \"Nuclear\", \"Renewables\", \"Other\"], ordered=True\n)\n\n# In-segment percentage labels (values already sum to 100 per country); suppress\n# labels on slivers too narrow to hold text cleanly\ndf[\"label\"] = df[\"value\"].apply(lambda v: f\"{v}%\" if v >= 5 else \"\")\n\n# Per-segment label ink chosen for WCAG AA contrast against each fill color\nLABEL_INK = {\n    \"Coal\": \"#1A1A17\",\n    \"Natural Gas\": \"#1A1A17\",\n    \"Nuclear\": \"#FFFFFF\",\n    \"Renewables\": \"#1A1A17\",\n    \"Other\": \"#FFFFFF\",\n}\ndf[\"label_color\"] = df[\"source\"].map(LABEL_INK)\n\n# Create 100% stacked bar chart with position=\"fill\"\nplot = (\n    ggplot(df, aes(x=\"country\", y=\"value\", fill=\"source\"))\n    + geom_bar(stat=\"identity\", position=\"fill\", width=0.75, alpha=0.9)\n    + geom_text(aes(label=\"label\", color=\"label_color\"), position=position_fill(vjust=0.5), size=3.5, fontface=\"bold\")\n    + scale_color_identity()\n    + scale_fill_manual(values=IMPRINT)\n    + scale_y_continuous(format=\".0%\")\n    + labs(\n        title=\"bar-stacked-percent · python · letsplot · anyplot.ai\",\n        x=\"Country\",\n        y=\"Share of Energy Mix\",\n        fill=\"Energy Source\",\n    )\n    + theme_minimal()\n    + theme(\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_border=element_blank(),\n        panel_grid_major=element_line(color=GRID_COLOR, size=0.2),\n        panel_grid_minor=element_blank(),\n        axis_title=element_text(size=12, color=INK),\n        axis_text=element_text(size=10, color=INK_SOFT),\n        plot_title=element_text(size=16, face=\"bold\", color=INK),\n        legend_background=element_rect(fill=ELEVATED_BG, color=ELEVATED_BG),\n        legend_title=element_text(size=12, color=INK),\n        legend_text=element_text(size=10, color=INK_SOFT),\n        legend_position=\"right\",\n        panel_grid_major_x=element_blank(),\n    )\n    + ggsize(800, 450)\n)\n\n# Save as PNG (scale 4x for 3200 x 1800 px) and HTML\nggsave(plot, f\"plot-{THEME}.png\", path=\".\", scale=4)\nggsave(plot, f\"plot-{THEME}.html\", path=\".\")\n"}