{"spec_id":"bar-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nbar-basic: Basic Bar Chart\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-28\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove this file's directory from sys.path to prevent self-import\n# (this file is named plotnine.py, same as the library being imported)\n_dir = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p) != _dir]\n\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    annotate,\n    coord_cartesian,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_bar,\n    geom_text,\n    ggplot,\n    labs,\n    scale_fill_manual,\n    scale_x_discrete,\n    scale_y_continuous,\n    theme,\n    theme_minimal,\n)\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nBRAND = \"#009E73\"  # Imprint palette position 1 — always first series\n\n# Data — non-monotonic: Clothing and Home & Garden are close rivals\ndata = pd.DataFrame(\n    {\n        \"category\": [\"Electronics\", \"Clothing\", \"Home & Garden\", \"Sports\", \"Books\", \"Toys\"],\n        \"value\": [45200, 31400, 30800, 19700, 15300, 12400],\n    }\n)\ndata[\"category\"] = pd.Categorical(\n    data[\"category\"], categories=data.sort_values(\"value\", ascending=False)[\"category\"], ordered=True\n)\n\n# Highlight the leading category\ndata[\"highlight\"] = data[\"value\"] == data[\"value\"].max()\n\n# Annotation: how far ahead is the leader\ntop_val = data[\"value\"].max()\nsecond_val = data[\"value\"].nlargest(2).iloc[1]\nlead_pct = (top_val - second_val) / second_val * 100\n\n# Value label formatting\ndata[\"label\"] = data[\"value\"].apply(lambda v: f\"${v:,.0f}\")\n\ntitle = \"bar-basic · python · plotnine · anyplot.ai\"\n\n# Plot\nplot = (\n    ggplot(data, aes(x=\"category\", y=\"value\", fill=\"highlight\"))\n    + geom_bar(stat=\"identity\", width=0.7, show_legend=False)\n    + scale_fill_manual(values={True: BRAND, False: INK_MUTED})\n    + geom_text(aes(label=\"label\"), va=\"bottom\", size=3.5, color=INK, nudge_y=600)\n    + annotate(\n        \"text\",\n        x=1,\n        y=top_val * 0.82,\n        label=f\"▲ {lead_pct:.0f}% ahead\\nof 2nd place\",\n        size=3.0,\n        color=INK,\n        fontstyle=\"italic\",\n        ha=\"center\",\n        va=\"center\",\n    )\n    + scale_y_continuous(\n        labels=lambda vals: [f\"${v / 1000:.0f}K\" for v in vals], breaks=range(0, 55000, 10000), expand=(0, 0, 0.08, 0)\n    )\n    + scale_x_discrete(expand=(0.05, 0.4))\n    + coord_cartesian(ylim=(0, None))\n    + labs(x=\"Product Category\", y=\"Sales (USD)\", title=title)\n    + theme_minimal(base_size=8, base_family=\"sans-serif\")\n    + theme(\n        figure_size=(8, 4.5),\n        plot_title=element_text(size=12, weight=\"bold\", color=INK, margin={\"b\": 10}),\n        axis_title_x=element_text(size=10, color=INK, margin={\"t\": 8}),\n        axis_title_y=element_text(size=10, color=INK, margin={\"r\": 8}),\n        axis_text=element_text(size=8, color=INK_SOFT),\n        axis_text_x=element_text(rotation=0, ha=\"center\"),\n        panel_grid_major_x=element_blank(),\n        panel_grid_minor=element_blank(),\n        panel_grid_major_y=element_line(alpha=0.15, size=0.4, color=INK),\n        axis_ticks=element_blank(),\n        axis_line=element_blank(),\n        panel_border=element_blank(),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        plot_margin=0.02,\n    )\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\")\n"}