{"spec_id":"bar-horizontal","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nbar-horizontal: Horizontal Bar Chart\nLibrary: seaborn 0.13.2 | Python 3.13.14\nQuality: 91/100 | Updated: 2026-08-05\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport seaborn as sns\n\n\n# Theme tokens (see prompts/default-style-guide.md \"Background\" + \"Theme-adaptive Chrome\")\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nBRAND = \"#009E73\"  # Imprint palette position 1 — ALWAYS first series\n\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.15,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data — most populous countries (UN 2024 estimates)\ndata = {\n    \"Country\": [\n        \"India\",\n        \"China\",\n        \"United States\",\n        \"Indonesia\",\n        \"Pakistan\",\n        \"Brazil\",\n        \"Nigeria\",\n        \"Bangladesh\",\n        \"Russia\",\n        \"Mexico\",\n    ],\n    \"Population\": [1417, 1412, 338, 275, 235, 215, 223, 170, 144, 128],\n}\ndf = pd.DataFrame(data)\n\n# Rank ascending so the largest bar lands at the top of the horizontal axis\norder = df.sort_values(\"Population\", ascending=True)[\"Country\"].tolist()\ntop_country = df.loc[df[\"Population\"].idxmax(), \"Country\"]\n\n# Focal-point emphasis, graduated by rank: sns.light_palette generates a brand-anchored\n# tint ramp (reverse=True keeps index 0 pixel-identical to BRAND), so the top-3 countries\n# step from full brand green down to a soft tint instead of a flat two-tone split — the\n# rest stay muted ink. hue=Country + palette=dict is the seaborn-idiomatic per-bar recolor.\ntop_n = 3\nranked = df.sort_values(\"Population\", ascending=False)[\"Country\"].tolist()\nrank_shades = sns.light_palette(BRAND, n_colors=top_n + 1, reverse=True)[:top_n]\ncolor_map = {\n    country: (rank_shades[ranked.index(country)] if country in ranked[:top_n] else INK_MUTED)\n    for country in df[\"Country\"]\n}\n\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400)\n\nsns.barplot(\n    data=df,\n    y=\"Country\",\n    x=\"Population\",\n    order=order,\n    hue=\"Country\",\n    palette=color_map,\n    legend=False,\n    dodge=False,\n    edgecolor=PAGE_BG,\n    linewidth=1,\n    ax=ax,\n)\n\n# Value labels at the end of each bar\nfor container in ax.containers:\n    ax.bar_label(container, fmt=lambda v: f\"{v:.0f}M\", padding=6, fontsize=9, color=INK)\n\n# Mandated title (scales fontsize when longer than the 67-char baseline)\ntitle = \"Most Populous Countries (2024) · bar-horizontal · python · seaborn · anyplot.ai\"\nn = len(title)\nratio = 67 / n if n > 67 else 1.0\ntitle_fontsize = round(12 * ratio)\nax.set_title(title, fontsize=title_fontsize, fontweight=\"bold\", color=INK, pad=16)\n\nax.set_xlabel(\"Population (millions)\", fontsize=10, color=INK)\nax.set_ylabel(\"Country\", fontsize=10, color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\n\n# Bold the highlighted tick label to reinforce the visual emphasis\nfor tick_label in ax.get_yticklabels():\n    if tick_label.get_text() == top_country:\n        tick_label.set_fontweight(\"bold\")\n        tick_label.set_color(INK)\n\nax.xaxis.grid(True, alpha=0.15, linewidth=0.8)\nax.set_axisbelow(True)\nax.set_xlim(0, df[\"Population\"].max() * 1.12)\n\nsns.despine(ax=ax)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}