{"spec_id":"area-stacked-percent","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\narea-stacked-percent: 100% Stacked Area Chart\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 76/100 | Updated: 2026-05-12\n\"\"\"\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Data - Energy source market share over time\nnp.random.seed(42)\nyears = list(range(2015, 2025))\n\n# Simulate energy source percentages that evolve over time\n# Solar grows, Coal declines, Natural Gas stable, Wind grows\ncoal_base = np.linspace(40, 22, len(years)) + np.random.randn(len(years)) * 2\ngas_base = np.linspace(30, 32, len(years)) + np.random.randn(len(years)) * 1\nwind_base = np.linspace(12, 20, len(years)) + np.random.randn(len(years)) * 1\nsolar_base = np.linspace(8, 18, len(years)) + np.random.randn(len(years)) * 1\nhydro_base = np.linspace(10, 8, len(years)) + np.random.randn(len(years)) * 0.5\n\n# Ensure no negative values\ncoal_base = np.clip(coal_base, 5, 50)\ngas_base = np.clip(gas_base, 20, 40)\nwind_base = np.clip(wind_base, 5, 25)\nsolar_base = np.clip(solar_base, 3, 25)\nhydro_base = np.clip(hydro_base, 5, 15)\n\n# Normalize to 100%\ntotals = coal_base + gas_base + wind_base + solar_base + hydro_base\ncoal = (coal_base / totals) * 100\ngas = (gas_base / totals) * 100\nwind = (wind_base / totals) * 100\nsolar = (solar_base / totals) * 100\nhydro = (hydro_base / totals) * 100\n\n# Colors - Python Blue, Yellow, and additional colorblind-safe colors\ncolors = [\"#306998\", \"#FFD43B\", \"#4DAF4A\", \"#E7298A\", \"#66A61E\"]\n\n# Create figure\nfig = go.Figure()\n\n# Add stacked areas (using stackgroup for 100% stacking)\ncategories = [\"Coal\", \"Natural Gas\", \"Wind\", \"Solar\", \"Hydro\"]\nvalues = [coal, gas, wind, solar, hydro]\n\nfor i, (cat, val) in enumerate(zip(categories, values)):\n    fig.add_trace(\n        go.Scatter(\n            x=years,\n            y=val,\n            name=cat,\n            mode=\"lines\",\n            line=dict(width=0.5, color=colors[i]),\n            stackgroup=\"one\",\n            groupnorm=\"percent\",\n            fillcolor=colors[i],\n            hovertemplate=\"%{y:.1f}%<extra>\" + cat + \"</extra>\",\n        )\n    )\n\n# Update layout\nfig.update_layout(\n    title=dict(text=\"area-stacked-percent · plotly · pyplots.ai\", font=dict(size=28), x=0.5, xanchor=\"center\"),\n    xaxis=dict(\n        title=dict(text=\"Year\", font=dict(size=22)),\n        tickfont=dict(size=18),\n        dtick=1,\n        showgrid=True,\n        gridwidth=1,\n        gridcolor=\"rgba(0,0,0,0.1)\",\n    ),\n    yaxis=dict(\n        title=dict(text=\"Market Share (%)\", font=dict(size=22)),\n        tickfont=dict(size=18),\n        ticksuffix=\"%\",\n        range=[0, 100],\n        showgrid=True,\n        gridwidth=1,\n        gridcolor=\"rgba(0,0,0,0.1)\",\n    ),\n    template=\"plotly_white\",\n    legend=dict(font=dict(size=18), orientation=\"h\", yanchor=\"bottom\", y=1.02, xanchor=\"center\", x=0.5),\n    margin=dict(l=80, r=40, t=120, b=60),\n    hovermode=\"x unified\",\n)\n\n# Save as PNG and HTML\nfig.write_image(\"plot.png\", width=1600, height=900, scale=3)\nfig.write_html(\"plot.html\", include_plotlyjs=\"cdn\")\n"}