{"spec_id":"bar-basic","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nbar-basic: Basic Bar Chart\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 90/100 | Created: 2026-05-28\n\"\"\"\n\nimport os\nimport sys\n\n\n# Prevent self-import: remove this script's own directory from sys.path so that\n# \"import plotly\" resolves to the installed package, not this file.\n_here = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p) != _here]\n\nimport plotly.graph_objects as go\n\n\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\n\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nGRID = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Imprint palette: brand green for top performer, blue for rest\nBRAND = \"#009E73\"\nBASE = \"#4467A3\"\n\n# Data — product sales by department, sorted descending\ncategories = [\"Electronics\", \"Clothing\", \"Home & Garden\", \"Sports\", \"Books\", \"Toys\", \"Automotive\", \"Health\"]\nvalues = [45200, 38700, 31500, 27800, 24300, 21600, 18900, 15400]\n\nbar_colors = [BRAND] + [BASE] * (len(categories) - 1)\n\nfig = go.Figure()\n\nfig.add_trace(\n    go.Bar(\n        x=categories,\n        y=values,\n        marker={\"color\": bar_colors, \"line\": {\"color\": \"rgba(0,0,0,0.06)\", \"width\": 1}},\n        text=values,\n        textposition=\"outside\",\n        texttemplate=\"$%{text:,.0f}\",\n        textfont={\"size\": 10, \"color\": INK_SOFT},\n        hovertemplate=\"<b>%{x}</b><br>Sales: $%{y:,.0f}<extra></extra>\",\n    )\n)\n\n# Annotation: arrow from Electronics bar top sweeps right to a callout in clear whitespace;\n# the shallow diagonal passes above all shorter bars, avoiding the Clothing value label\nfig.add_annotation(\n    x=\"Electronics\",\n    y=45200,\n    text=\"<b>Top seller</b><br>17% ahead of #2\",\n    showarrow=True,\n    arrowhead=2,\n    arrowsize=1.2,\n    arrowwidth=2,\n    arrowcolor=BRAND,\n    ax=280,\n    ay=30,\n    font={\"size\": 10, \"color\": BRAND},\n    align=\"left\",\n    bordercolor=BRAND,\n    borderwidth=1.5,\n    borderpad=6,\n    bgcolor=ELEVATED_BG,\n)\n\navg_value = sum(values) / len(values)\nfig.add_hline(\n    y=avg_value,\n    line={\"color\": INK_MUTED, \"width\": 1.5, \"dash\": \"dot\"},\n    annotation_text=f\"Avg ${avg_value:,.0f}\",\n    annotation_font_size=10,\n    annotation_font_color=INK_MUTED,\n    annotation_position=\"top left\",\n)\n\nfig.update_layout(\n    autosize=False,\n    title={\n        \"text\": \"bar-basic · python · plotly · anyplot.ai\",\n        \"font\": {\"size\": 16, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    xaxis={\n        \"title\": {\"text\": \"Product Category\", \"font\": {\"size\": 12, \"color\": INK}},\n        \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n        \"linecolor\": INK_SOFT,\n        \"showgrid\": False,\n    },\n    yaxis={\n        \"title\": {\"text\": \"Sales ($)\", \"font\": {\"size\": 12, \"color\": INK}},\n        \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n        \"tickprefix\": \"$\",\n        \"tickformat\": \",.0f\",\n        \"gridcolor\": GRID,\n        \"linecolor\": INK_SOFT,\n        \"zeroline\": False,\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font={\"color\": INK},\n    template=\"plotly_white\",\n    bargap=0.3,\n    margin={\"t\": 80, \"b\": 60, \"l\": 100, \"r\": 40},\n    showlegend=False,\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"}