{"spec_id":"bar-stacked-labeled","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nbar-stacked-labeled: Stacked Bar Chart with Total Labels\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 94/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\nimport sys\n\n\n# Work around naming conflict: remove current directory from import path\nsys.path.pop(0)\nimport plotly.graph_objects as go\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\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\n\n# Okabe-Ito palette (first series is ALWAYS #009E73)\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n\n# Data: Quarterly revenue by product category\nquarters = [\"Q1 2024\", \"Q2 2024\", \"Q3 2024\", \"Q4 2024\", \"Q1 2025\"]\nproducts = [\"Software\", \"Hardware\", \"Services\", \"Maintenance\"]\n\n# Revenue data in thousands (realistic business scenario)\nrevenue_data = {\n    \"Software\": [180, 210, 195, 240, 260],\n    \"Hardware\": [120, 95, 110, 130, 105],\n    \"Services\": [85, 90, 105, 115, 125],\n    \"Maintenance\": [45, 50, 52, 55, 60],\n}\n\n# Calculate totals for labels\ntotals = [sum(revenue_data[p][i] for p in products) for i in range(len(quarters))]\n\n# Create figure\nfig = go.Figure()\n\n# Add stacked bars for each product\nfor product, color in zip(products, IMPRINT, strict=True):\n    fig.add_trace(\n        go.Bar(\n            name=product,\n            x=quarters,\n            y=revenue_data[product],\n            marker_color=color,\n            text=[f\"${v}K\" for v in revenue_data[product]],\n            textposition=\"inside\",\n            textfont={\"size\": 14, \"color\": \"white\"},\n            insidetextanchor=\"middle\",\n        )\n    )\n\n# Add total labels as annotations above each bar stack\nfor quarter, total in zip(quarters, totals, strict=True):\n    fig.add_annotation(\n        x=quarter,\n        y=total + 15,\n        text=f\"<b>${total}K</b>\",\n        showarrow=False,\n        font={\"size\": 18, \"color\": INK},\n        yanchor=\"bottom\",\n    )\n\n# Layout for 4800x2700 px output\nfig.update_layout(\n    title={\n        \"text\": \"bar-stacked-labeled · Python · plotly · anyplot.ai\",\n        \"font\": {\"size\": 28, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    xaxis={\n        \"title\": {\"text\": \"Quarter\", \"font\": {\"size\": 22, \"color\": INK}},\n        \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n        \"gridcolor\": GRID,\n        \"linecolor\": INK_SOFT,\n        \"zerolinecolor\": INK_SOFT,\n    },\n    yaxis={\n        \"title\": {\"text\": \"Revenue ($ thousands)\", \"font\": {\"size\": 22, \"color\": INK}},\n        \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n        \"range\": [0, max(totals) + 60],\n        \"gridcolor\": GRID,\n        \"linecolor\": INK_SOFT,\n        \"zerolinecolor\": INK_SOFT,\n    },\n    barmode=\"stack\",\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    legend={\n        \"orientation\": \"h\",\n        \"yanchor\": \"bottom\",\n        \"y\": 1.02,\n        \"xanchor\": \"center\",\n        \"x\": 0.5,\n        \"font\": {\"size\": 16, \"color\": INK_SOFT},\n        \"bgcolor\": ELEVATED_BG,\n        \"bordercolor\": INK_SOFT,\n        \"borderwidth\": 1,\n    },\n    margin={\"t\": 120, \"b\": 80, \"l\": 80, \"r\": 40},\n)\n\n# Save as PNG and HTML (4800x2700)\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}