{"spec_id":"histogram-density","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nhistogram-density: Density Histogram\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-11\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\nBRAND = \"#009E73\"  # First categorical series\nACCENT = \"#C475FD\"  # Second color for overlay\n\n# Data: Generate realistic test score data with a normal distribution\nnp.random.seed(42)\ntest_scores = np.random.normal(loc=75, scale=12, size=500)\ntest_scores = np.clip(test_scores, 0, 100)\n\n# Create theoretical normal PDF for overlay\nmu, sigma = 75, 12\nx_pdf = np.linspace(30, 110, 200)\npdf = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x_pdf - mu) / sigma) ** 2)\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Density histogram with Okabe-Ito brand color\nax.hist(\n    test_scores,\n    bins=25,\n    density=True,\n    alpha=0.7,\n    color=BRAND,\n    edgecolor=PAGE_BG,\n    linewidth=1.5,\n    label=\"Observed Distribution\",\n)\n\n# Overlay theoretical normal PDF with fill_between for visual distinction\nax.plot(x_pdf, pdf, color=ACCENT, linewidth=3, label=\"Normal PDF (μ=75, σ=12)\")\nax.fill_between(x_pdf, pdf, alpha=0.15, color=ACCENT)\n\n# Add mean line for reference\nax.axvline(mu, color=INK_SOFT, linestyle=\"--\", linewidth=2, alpha=0.6, label=f\"Mean = {mu}\")\n\n# Labels and styling\nax.set_xlabel(\"Test Score (points)\", fontsize=20, color=INK)\nax.set_ylabel(\"Probability Density\", fontsize=20, color=INK)\nax.set_title(\"histogram-density · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Theme-adaptive spine and grid styling\nfor spine in (\"left\", \"bottom\"):\n    ax.spines[spine].set_color(INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\n\n# Legend with theme-adaptive styling\nleg = ax.legend(fontsize=16, loc=\"upper right\")\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.5)\n    plt.setp(leg.get_texts(), color=INK_SOFT)\n\nax.set_xlim(30, 110)\nax.set_ylim(0, None)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}