{"spec_id":"area-stacked","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\narea-stacked: Stacked Area Chart\nLibrary: pygal 3.1.3 | Python 3.13.15\nQuality: 91/100 | Updated: 2026-08-17\n\"\"\"\n\nimport os\n\nimport pygal\nfrom pygal.style import Style\n\n\n# Theme tokens (see prompts/default-style-guide.md)\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Imprint palette (first series is always #009E73)\nIMPRINT = (\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\")\n\n# Data - National electricity consumption by sector, 2010-2024 (TWh)\n# Stack order is fixed by each sector's average magnitude across the full\n# 15-year range (largest at bottom), not by any single year's snapshot -\n# a fixed order keeps the stack visually stable instead of reshuffling\n# bands as ranks cross over (e.g. Transportation overtakes Commercial only\n# in the final years, 2023-2024).\nyears = [str(y) for y in range(2010, 2025)]\n\nindustrial = [420, 425, 415, 430, 445, 450, 440, 460, 475, 470, 430, 465, 485, 495, 505]\ntransportation = [95, 100, 105, 115, 125, 135, 145, 155, 165, 170, 130, 175, 200, 225, 250]\nresidential = [260, 265, 270, 268, 275, 280, 285, 290, 295, 300, 310, 305, 315, 320, 325]\ncommercial = [180, 182, 185, 188, 190, 195, 198, 200, 205, 208, 195, 210, 215, 220, 225]\n\n# Custom style for the 3200x1800 canonical canvas\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_MUTED,\n    opacity=\".85\",\n    opacity_hover=\".95\",\n    colors=IMPRINT,\n    title_font_size=72,\n    label_font_size=56,\n    major_label_font_size=44,\n    legend_font_size=44,\n    value_font_size=36,\n    stroke_width=2.5,\n    guide_stroke_dasharray=\"none\",\n    guide_stroke_color=INK_MUTED,\n    major_guide_stroke_dasharray=\"none\",\n    major_guide_stroke_color=INK_MUTED,\n)\n\n# Create stacked area chart\nchart = pygal.StackedLine(\n    width=3200,\n    height=1800,\n    style=custom_style,\n    title=\"area-stacked · python · pygal · anyplot.ai\",\n    x_title=\"Year\",\n    y_title=\"Electricity Consumption (TWh)\",\n    fill=True,\n    range=(0, 1400),\n    show_y_guides=True,\n    show_x_guides=False,\n    x_label_rotation=0,\n    x_labels_major_count=8,\n    show_minor_x_labels=False,\n    legend_at_bottom=True,\n    legend_at_bottom_columns=4,\n    legend_box_size=34,\n    truncate_legend=-1,\n    show_dots=False,\n    value_formatter=lambda v: f\"{v:,.0f} TWh\",\n    margin=60,\n    margin_bottom=120,\n    spacing=50,\n)\n\n# Add x-axis labels\nchart.x_labels = years\n\n# Add series (largest at bottom for easier reading)\nchart.add(\"Industrial\", industrial)\nchart.add(\"Residential\", residential)\nchart.add(\"Commercial\", commercial)\nchart.add(\"Transportation\", transportation)\n\n# Save as PNG and HTML with theme-suffixed names\nchart.render_to_png(f\"plot-{THEME}.png\")\nwith open(f\"plot-{THEME}.html\", \"wb\") as f:\n    f.write(chart.render())\n"}