{"spec_id":"count-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\ncount-basic: Basic Count Plot\nLibrary: matplotlib 3.11.2 | Python 3.13.15\nQuality: 86/100 | Updated: 2026-09-30\n\"\"\"\n\nimport os\nimport sys\n\n\nsys.path.pop(0)\nimport matplotlib.pyplot as plt\nimport numpy as np\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\"\nBRAND = \"#009E73\"  # Imprint palette position 1 — ALWAYS first series\n\n# Data - Survey responses with varying frequencies\nnp.random.seed(42)\ncategories = [\"Strongly Agree\", \"Agree\", \"Neutral\", \"Disagree\", \"Strongly Disagree\"]\nweights = [0.15, 0.35, 0.25, 0.18, 0.07]\nresponses = np.random.choice(categories, size=200, p=weights)\n\n# Count occurrences\nunique, counts = np.unique(responses, return_counts=True)\n\n# Sort by frequency (descending)\nsort_idx = np.argsort(counts)[::-1]\nunique = unique[sort_idx]\ncounts = counts[sort_idx]\ntotal = counts.sum()\npercentages = counts / total * 100\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Basic variant: one color for every bar, no per-bar emphasis.\nbars = ax.bar(unique, counts, color=BRAND, edgecolor=PAGE_BG, linewidth=1.5, width=0.62)\n\n# Direct value + share labels replace the y-axis (matplotlib's bar_label API);\n# an explicit axis title still tells a reader skimming the axis that these\n# are response counts before they reach the bar labels.\nax.bar_label(\n    bars,\n    labels=[f\"{c}\\n{p:.0f}%\" for c, p in zip(counts, percentages, strict=True)],\n    padding=10,\n    fontsize=10,\n    color=INK,\n    linespacing=1.3,\n)\n\n# Style — minimalist: no numeric y-axis ticks, values are direct-labeled on the\n# bars instead; an explicit axis title still tells a reader skimming the axis\n# that these are response counts.\nax.set_xlabel(\"Survey Response\", fontsize=10, color=INK)\nax.set_ylabel(\"Number of Responses\", fontsize=10, color=INK)\nax.set_title(\"count-basic · python · matplotlib · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"x\", labelsize=8, colors=INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_visible(False)\nax.spines[\"bottom\"].set_color(INK_SOFT)\n\n# Headroom for the two-line bar labels above the tallest bar\nax.set_ylim(0, counts.max() * 1.3)\n\n# Faint reference gridlines (no numeric labels) give returning readers a scale\n# anchor without reintroducing a full y-axis — the direct bar labels stay the\n# primary reading mode.\nax.set_yticks(np.linspace(0, counts.max() * 1.3, 5))\nax.set_yticklabels([])\nax.tick_params(axis=\"y\", length=0)\nax.yaxis.grid(True, color=INK, alpha=0.08, linewidth=0.8, zorder=0)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}