{"spec_id":"bar-horizontal","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nbar-horizontal: Horizontal Bar Chart\nLibrary: matplotlib 3.11.1 | Python 3.13.14\nQuality: 91/100 | Updated: 2026-08-05\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.ticker import PercentFormatter\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\"\nMUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nBRAND = \"#009E73\"\n\n# Data: Top 10 programming languages by popularity\nnp.random.seed(42)\ncategories = [\"Python\", \"JavaScript\", \"Java\", \"C++\", \"TypeScript\", \"C#\", \"Go\", \"Rust\", \"PHP\", \"Swift\"]\nvalues = [68.2, 62.5, 45.8, 42.1, 38.7, 33.5, 28.4, 25.1, 22.8, 18.3]\n\n# Sort ascending so the top-ranked language sits at the top of the chart\nsorted_indices = np.argsort(values)\ncategories = [categories[i] for i in sorted_indices]\nvalues = [values[i] for i in sorted_indices]\naverage = np.mean(values)\n\n# Highlight the leader in brand green, mute the rest to guide the eye\ncolors = [BRAND if i == len(values) - 1 else MUTED for i in range(len(values))]\n\n# Plot — see default-style-guide.md \"Visual Sizing Defaults\" for the canvas + sizing values\ntitle = \"bar-horizontal · python · matplotlib · anyplot.ai\"\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\ny_positions = np.arange(len(categories))\nbars = ax.barh(y_positions, values, height=0.65, color=colors, edgecolor=\"none\")\nax.bar_label(bars, fmt=\"%.1f%%\", padding=4, fontsize=8, color=INK)\n\n# Average reference line for at-a-glance comparison\nax.axvline(average, color=INK_SOFT, linewidth=1, linestyle=\"--\", alpha=0.6)\nax.annotate(\n    f\"avg {average:.1f}%\",\n    xy=(average, len(values) - 1),\n    xytext=(average, len(values) - 0.35),\n    fontsize=8,\n    color=INK_SOFT,\n    ha=\"center\",\n)\n\n# Labels and styling\nax.set_xlabel(\"Popularity (%)\", fontsize=10, color=INK)\nax.set_ylabel(\"Programming Language\", fontsize=10, color=INK)\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK)\nax.set_yticks(y_positions)\nax.set_yticklabels(categories, color=INK_SOFT)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\nax.xaxis.set_major_formatter(PercentFormatter(xmax=100, decimals=0))\nax.set_xlim(0, max(values) * 1.22)\n\n# Grid on x-axis only\nax.xaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\nax.set_axisbelow(True)\n\n# Remove top and right spines\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}