{"spec_id":"bar-error","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nbar-error: Bar Chart with Error Bars\nLibrary: plotly 7.1.0 | Python 3.13.15\nQuality: 85/100 | Updated: 2026-09-27\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Theme tokens (Imprint palette)\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)\"\nBRAND = \"#009E73\"  # Imprint palette position 1\n\n# Data - lab experiment comparing treatment effectiveness against a control\nnp.random.seed(42)\ncategories = [\"Control\", \"Treatment A\", \"Treatment B\", \"Treatment C\", \"Treatment D\"]\nvalues = np.array([45.2, 62.8, 58.3, 71.5, 55.9])\n# Asymmetric errors (±1 SD), more dramatic variation across groups\nerrors_lower = np.array([3.2, 7.8, 2.9, 8.1, 3.5])\nerrors_upper = np.array([4.1, 9.2, 3.6, 11.3, 4.8])\ncontrol_value = values[0]\n\nfig = go.Figure()\n\nfig.add_trace(\n    go.Bar(\n        x=categories,\n        y=values,\n        marker=dict(color=BRAND, line=dict(color=INK_SOFT, width=1.5)),\n        error_y=dict(\n            type=\"data\",\n            symmetric=False,\n            array=errors_upper,\n            arrayminus=errors_lower,\n            color=INK_SOFT,\n            thickness=2.5,\n            width=8,\n        ),\n        name=\"Measurement\",\n        hovertemplate=\"<b>%{x}</b><br>Value: %{y:.1f}%<br>+%{error_y.array:.1f}% / -%{error_y.arrayminus:.1f}%<extra></extra>\",\n    )\n)\n\n# Dashed control baseline so the reader can immediately see which treatments\n# beat the control, without needing to eyeball the axis (design storytelling).\n# Label sits in the empty top-left corner of the plot area, not on the line\n# itself, so it never overlaps the Control bar's own error cap.\nfig.add_hline(y=control_value, line=dict(color=INK_SOFT, width=1.5, dash=\"dash\"))\n\nfig.update_layout(\n    autosize=False,\n    title=dict(text=\"bar-error · python · plotly · anyplot.ai\", font=dict(size=16, color=INK), x=0.5, xanchor=\"center\"),\n    xaxis=dict(\n        title=dict(text=\"Treatment Group\", font=dict(size=12, color=INK)),\n        tickfont=dict(size=10, color=INK_SOFT),\n        showgrid=False,\n        showline=True,\n        linecolor=INK_SOFT,\n        linewidth=1,\n        mirror=False,\n        ticks=\"outside\",\n        tickcolor=INK_SOFT,\n    ),\n    yaxis=dict(\n        title=dict(text=\"Response Value (%)\", font=dict(size=12, color=INK)),\n        tickfont=dict(size=10, color=INK_SOFT),\n        gridcolor=GRID,\n        gridwidth=1,\n        zeroline=False,\n        showline=True,\n        linecolor=INK_SOFT,\n        linewidth=1,\n        mirror=False,\n        range=[0, 90],\n    ),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font=dict(color=INK),\n    bargap=0.3,\n    showlegend=True,\n    legend=dict(\n        x=0.98, y=0.98, bgcolor=ELEVATED_BG, bordercolor=INK_SOFT, borderwidth=1, font=dict(size=10, color=INK_SOFT)\n    ),\n    margin=dict(l=80, r=40, t=80, b=60),\n)\n\n# NOTE: added via add_annotation (not update_layout(annotations=[...])) so\n# repeated calls append instead of clobbering each other.\nfig.add_annotation(\n    text=\"Error bars: ±1 SD (asymmetric)\",\n    xref=\"paper\",\n    yref=\"paper\",\n    x=0.02,\n    y=0.02,\n    xanchor=\"left\",\n    yanchor=\"bottom\",\n    font=dict(size=10, color=INK_SOFT),\n    showarrow=False,\n)\nfig.add_annotation(\n    text=f\"┄ Control baseline ({control_value:.1f}%)\",\n    xref=\"paper\",\n    yref=\"paper\",\n    x=0.02,\n    y=0.95,\n    xanchor=\"left\",\n    yanchor=\"top\",\n    font=dict(size=10, color=INK_SOFT),\n    showarrow=False,\n)\n\n# Theme-aware hover tooltip so the interactive HTML stays legible in dark mode too.\nfig.update_traces(hoverlabel=dict(bgcolor=ELEVATED_BG, bordercolor=INK_SOFT, font=dict(color=INK, size=10)))\n\n# Hard target: 3200 x 1800 (landscape). See prompts/library/plotly.md \"Canvas\".\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}