{"spec_id":"area-stacked-confidence","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\narea-stacked-confidence: Stacked Area Chart with Confidence Bands\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\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\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n\n# Data: quarterly energy consumption by source with measurement uncertainty\nnp.random.seed(42)\n\n# Time axis: 8 years of quarterly data (32 quarters)\nquarters = pd.date_range(start=\"2016-01-01\", periods=32, freq=\"QS\")\n\n# Base consumption patterns for each energy source (in TWh)\n# Solar: growing trend with seasonal variation\nsolar_base = 20 + np.linspace(0, 40, 32) + 8 * np.sin(np.linspace(0, 8 * np.pi, 32))\nsolar_uncertainty = 3 + np.linspace(0, 5, 32)\n\n# Wind: moderate growth with higher seasonal variation\nwind_base = 35 + np.linspace(0, 30, 32) + 12 * np.sin(np.linspace(0, 8 * np.pi, 32) + np.pi / 2)\nwind_uncertainty = 5 + np.linspace(0, 6, 32)\n\n# Hydro: stable with seasonal peaks\nhydro_base = 50 + 15 * np.sin(np.linspace(0, 8 * np.pi, 32) - np.pi / 4)\nhydro_uncertainty = 4 + 2 * np.abs(np.sin(np.linspace(0, 8 * np.pi, 32)))\n\n# Natural Gas: declining trend\ngas_base = 80 - np.linspace(0, 25, 32) + 5 * np.random.randn(32)\ngas_uncertainty = 6 + np.linspace(0, 4, 32)\n\n# Create stacked values (cumulative)\nsolar_stack = solar_base\nwind_stack = solar_base + wind_base\nhydro_stack = wind_stack + hydro_base\ngas_stack = hydro_stack + gas_base\n\n# Confidence bands (stacked appropriately)\n# Solar bands\nsolar_lower = solar_base - solar_uncertainty\nsolar_upper = solar_base + solar_uncertainty\n\n# Wind bands (stacked on solar)\nwind_lower = solar_stack + (wind_base - wind_uncertainty)\nwind_upper = solar_stack + (wind_base + wind_uncertainty)\n\n# Hydro bands (stacked on wind)\nhydro_lower = wind_stack + (hydro_base - hydro_uncertainty)\nhydro_upper = wind_stack + (hydro_base + hydro_uncertainty)\n\n# Gas bands (stacked on hydro)\ngas_lower = hydro_stack + (gas_base - gas_uncertainty)\ngas_upper = hydro_stack + (gas_base + gas_uncertainty)\n\n# Create figure\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Plot stacked areas from bottom to top with enhanced styling\n# Solar (bottom layer)\nax.fill_between(quarters, 0, solar_stack, color=IMPRINT[0], alpha=0.85, label=\"Solar\", zorder=3)\nax.fill_between(quarters, solar_lower, solar_upper, color=IMPRINT[0], alpha=0.25, linewidth=0, zorder=2)\nax.plot(quarters, solar_lower, color=IMPRINT[0], linewidth=0.8, alpha=0.4, linestyle=\":\", zorder=1)\nax.plot(quarters, solar_upper, color=IMPRINT[0], linewidth=0.8, alpha=0.4, linestyle=\":\", zorder=1)\n\n# Wind (second layer)\nax.fill_between(quarters, solar_stack, wind_stack, color=IMPRINT[1], alpha=0.85, label=\"Wind\", zorder=3)\nax.fill_between(quarters, wind_lower, wind_upper, color=IMPRINT[1], alpha=0.25, linewidth=0, zorder=2)\nax.plot(quarters, wind_lower, color=IMPRINT[1], linewidth=0.8, alpha=0.4, linestyle=\":\", zorder=1)\nax.plot(quarters, wind_upper, color=IMPRINT[1], linewidth=0.8, alpha=0.4, linestyle=\":\", zorder=1)\n\n# Hydro (third layer)\nax.fill_between(quarters, wind_stack, hydro_stack, color=IMPRINT[2], alpha=0.85, label=\"Hydro\", zorder=3)\nax.fill_between(quarters, hydro_lower, hydro_upper, color=IMPRINT[2], alpha=0.25, linewidth=0, zorder=2)\nax.plot(quarters, hydro_lower, color=IMPRINT[2], linewidth=0.8, alpha=0.4, linestyle=\":\", zorder=1)\nax.plot(quarters, hydro_upper, color=IMPRINT[2], linewidth=0.8, alpha=0.4, linestyle=\":\", zorder=1)\n\n# Natural Gas (top layer)\nax.fill_between(quarters, hydro_stack, gas_stack, color=IMPRINT[3], alpha=0.85, label=\"Natural Gas\", zorder=3)\nax.fill_between(quarters, gas_lower, gas_upper, color=IMPRINT[3], alpha=0.25, linewidth=0, zorder=2)\nax.plot(quarters, gas_lower, color=IMPRINT[3], linewidth=0.8, alpha=0.4, linestyle=\":\", zorder=1)\nax.plot(quarters, gas_upper, color=IMPRINT[3], linewidth=0.8, alpha=0.4, linestyle=\":\", zorder=1)\n\n# Add prominent center lines for clarity and visual hierarchy\nax.plot(quarters, solar_stack, color=IMPRINT[0], linewidth=3, alpha=1.0, zorder=4, solid_capstyle=\"round\")\nax.plot(quarters, wind_stack, color=IMPRINT[1], linewidth=3, alpha=1.0, zorder=4, solid_capstyle=\"round\")\nax.plot(quarters, hydro_stack, color=IMPRINT[2], linewidth=3, alpha=1.0, zorder=4, solid_capstyle=\"round\")\nax.plot(quarters, gas_stack, color=IMPRINT[3], linewidth=3, alpha=1.0, zorder=4, solid_capstyle=\"round\")\n\n# Styling\nax.set_xlabel(\"Quarter\", fontsize=20, color=INK)\nax.set_ylabel(\"Energy Consumption (TWh)\", fontsize=20, color=INK)\nax.set_title(\"area-stacked-confidence · Python · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Remove top and right 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# Grid on both axes (subtle)\nax.grid(True, alpha=0.1, linewidth=0.8, color=INK)\nax.set_axisbelow(True)\n\n# Format x-axis dates\nfig.autofmt_xdate(rotation=45)\n\n# Legend with confidence band note\nlegend = ax.legend(\n    loc=\"upper left\", fontsize=16, framealpha=0.95, title=\"Energy Source (shaded: 90% CI bands)\", title_fontsize=16\n)\nlegend.get_frame().set_facecolor(ELEVATED_BG)\nlegend.get_frame().set_edgecolor(INK_SOFT)\nlegend.get_frame().set_linewidth(1)\nfor text in legend.get_texts():\n    text.set_color(INK_SOFT)\nlegend.get_title().set_color(INK)\n\n# Set y-axis to start at 0\nax.set_ylim(bottom=0)\n\n# Add subtle interpretive annotations highlighting key trends\nsolar_trend_idx = -1\nsolar_trend_value = solar_stack[solar_trend_idx]\ngas_trend_idx = -1\ngas_trend_value = gas_stack[gas_trend_idx] - hydro_stack[gas_trend_idx]\n\nmid_idx = 15\nax.annotate(\n    \"Renewable expansion\",\n    xy=(quarters[mid_idx], solar_stack[mid_idx] * 0.35),\n    xytext=(quarters[mid_idx + 4], solar_stack[mid_idx + 4] * 0.25),\n    fontsize=12,\n    color=INK_SOFT,\n    weight=\"500\",\n    bbox={\n        \"boxstyle\": \"round,pad=0.4\",\n        \"facecolor\": ELEVATED_BG,\n        \"edgecolor\": INK_SOFT,\n        \"alpha\": 0.75,\n        \"linewidth\": 0.8,\n    },\n    arrowprops={\"arrowstyle\": \"->\", \"connectionstyle\": \"arc3,rad=0.2\", \"color\": INK_SOFT, \"lw\": 1.2, \"alpha\": 0.7},\n)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}