{"spec_id":"frequency-polygon-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nfrequency-polygon-basic: Frequency Polygon for Distribution Comparison\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\n\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\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Okabe-Ito palette (positions 1-3)\nCOLORS = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\n\n# Data - Heights by age group\nnp.random.seed(42)\nn_per_group = 200\n\n# Generate three distinct distributions representing height measurements (cm)\nyoung_adults = np.random.normal(loc=172, scale=8, size=n_per_group)\nmiddle_aged = np.random.normal(loc=170, scale=9, size=n_per_group)\nseniors = np.random.normal(loc=166, scale=10, size=n_per_group)\n\ngroups = [young_adults, middle_aged, seniors]\ngroup_names = [\"Young Adults (18-25)\", \"Middle-Aged (40-50)\", \"Seniors (65+)\"]\n\n# Create common bin edges for all groups\nall_data = np.concatenate(groups)\nbin_edges = np.linspace(all_data.min() - 5, all_data.max() + 5, 20)\nbin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nfor data, name, color in zip(groups, group_names, COLORS, strict=True):\n    # Calculate histogram frequencies\n    counts, _ = np.histogram(data, bins=bin_edges)\n\n    # Extend to zero at both ends to close the polygon\n    x_extended = np.concatenate([[bin_edges[0]], bin_centers, [bin_edges[-1]]])\n    y_extended = np.concatenate([[0], counts, [0]])\n\n    # Plot frequency polygon line\n    ax.plot(\n        x_extended,\n        y_extended,\n        linewidth=3,\n        color=color,\n        label=name,\n        marker=\"o\",\n        markersize=6,\n        markerfacecolor=color,\n        markeredgecolor=PAGE_BG,\n        markeredgewidth=1,\n    )\n\n    # Add semi-transparent fill\n    ax.fill(x_extended, y_extended, color=color, alpha=0.15)\n\n# Labels and styling\nax.set_xlabel(\"Height (cm)\", fontsize=20, color=INK)\nax.set_ylabel(\"Frequency\", fontsize=20, color=INK)\nax.set_title(\"frequency-polygon-basic · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Spine styling\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\n# Grid styling\nax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK)\n\n# Legend with theme-adaptive background\nleg = ax.legend(fontsize=16, loc=\"upper left\", framealpha=0.95)\nif leg:\n    leg.get_frame().set_facecolor(ELEVATED_BG)\n    leg.get_frame().set_edgecolor(INK_SOFT)\n    leg.get_frame().set_linewidth(0.8)\n    for text in leg.get_texts():\n        text.set_color(INK_SOFT)\n\n# Ensure y-axis starts at 0\nax.set_ylim(bottom=0)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}