{"spec_id":"dumbbell-basic","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\ndumbbell-basic: Basic Dumbbell Chart\nLibrary: plotly 6.8.0 | Python 3.13.14\nQuality: 90/100 | Updated: 2026-06-30\n\"\"\"\n\nimport os\n\nimport plotly.graph_objects as go\n\n\n# Theme-adaptive chrome tokens (Imprint style guide)\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.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Imprint palette data colors (theme-independent)\nBEFORE_COLOR = \"#009E73\"  # position 1 — first categorical series\nAFTER_COLOR = \"#C475FD\"  # position 2 — second series\nAMBER = \"#DDCC77\"  # semantic anchor — warning / regression flag\n\n# Employee satisfaction scores before and after policy changes (deterministic)\ncategories = [\n    \"Engineering\",\n    \"Sales\",\n    \"Marketing\",\n    \"Customer Support\",\n    \"Finance\",\n    \"Human Resources\",\n    \"Operations\",\n    \"Product\",\n    \"Legal\",\n]\nbefore = [62, 71, 58, 45, 68, 52, 64, 73, 70]\nafter = [78, 82, 75, 69, 74, 71, 79, 85, 67]\n\n# Sort ascending by delta (regression at top, largest gain at bottom)\ndata = sorted(zip(categories, before, after, strict=True), key=lambda x: x[2] - x[1])\ncategories = [d[0] for d in data]\nbefore = [d[1] for d in data]\nafter = [d[2] for d in data]\ndeltas = [a - b for a, b in zip(after, before, strict=True)]\n\nfig = go.Figure()\n\n# Connecting lines — amber for regression, subtle ink-soft for improvement\nfor i in range(len(categories)):\n    is_regression = deltas[i] < 0\n    fig.add_trace(\n        go.Scatter(\n            x=[before[i], after[i]],\n            y=[categories[i], categories[i]],\n            mode=\"lines\",\n            line={\"color\": AMBER if is_regression else INK_SOFT, \"width\": 2.5 if is_regression else 1.5},\n            showlegend=False,\n            hoverinfo=\"skip\",\n        )\n    )\n\n# \"Before\" markers — Imprint green (position 1)\nfig.add_trace(\n    go.Scatter(\n        x=before,\n        y=categories,\n        mode=\"markers\",\n        marker={\"size\": 14, \"color\": BEFORE_COLOR, \"line\": {\"color\": PAGE_BG, \"width\": 2}},\n        name=\"Before\",\n        hovertemplate=\"<b>%{y}</b><br>Before: %{x}/100<extra></extra>\",\n    )\n)\n\n# \"After\" markers — Imprint lavender (position 2)\nfig.add_trace(\n    go.Scatter(\n        x=after,\n        y=categories,\n        mode=\"markers\",\n        marker={\"size\": 14, \"color\": AFTER_COLOR, \"line\": {\"color\": PAGE_BG, \"width\": 2}},\n        name=\"After\",\n        hovertemplate=\"<b>%{y}</b><br>After: %{x}/100<extra></extra>\",\n    )\n)\n\n# Delta labels to the right of each dumbbell — amber for regression\nannotations = []\nfor cat, b, a, delta in zip(categories, before, after, deltas, strict=True):\n    sign = \"+\" if delta >= 0 else \"\"\n    label_color = AMBER if delta < 0 else INK_SOFT\n    annotations.append(\n        {\n            \"x\": max(b, a) + 1.5,\n            \"y\": cat,\n            \"text\": f\"{sign}{delta} pts\",\n            \"showarrow\": False,\n            \"font\": {\"size\": 11, \"color\": label_color},\n            \"xanchor\": \"left\",\n            \"yanchor\": \"middle\",\n        }\n    )\n\nfig.update_layout(\n    autosize=False,\n    title={\n        \"text\": \"Employee Satisfaction · dumbbell-basic · python · plotly · anyplot.ai\",\n        \"font\": {\"size\": 16, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    xaxis={\n        \"title\": {\"text\": \"Satisfaction Score (out of 100)\", \"font\": {\"size\": 12, \"color\": INK}},\n        \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n        \"range\": [35, 100],\n        \"gridcolor\": GRID,\n        \"gridwidth\": 1,\n        \"linecolor\": INK_SOFT,\n        \"zerolinecolor\": INK_SOFT,\n        \"showgrid\": True,\n    },\n    yaxis={\n        \"title\": {\"text\": \"Department\", \"font\": {\"size\": 12, \"color\": INK}},\n        \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n        \"linecolor\": INK_SOFT,\n        \"zerolinecolor\": INK_SOFT,\n        \"showgrid\": False,\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font={\"color\": INK},\n    legend={\n        \"orientation\": \"h\",\n        \"yanchor\": \"bottom\",\n        \"y\": 1.02,\n        \"xanchor\": \"center\",\n        \"x\": 0.5,\n        \"font\": {\"size\": 10, \"color\": INK_SOFT},\n        \"bgcolor\": ELEVATED_BG,\n        \"bordercolor\": INK_SOFT,\n        \"borderwidth\": 1,\n    },\n    annotations=annotations,\n    margin={\"l\": 150, \"r\": 90, \"t\": 80, \"b\": 60},\n)\n\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}