{"spec_id":"histogram-kde","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nhistogram-kde: Histogram with KDE Overlay\nLibrary: seaborn 0.13.2 | Python 3.13.14\nQuality: 91/100 | Updated: 2026-08-05\n\"\"\"\n\nimport os\n\nimport matplotlib.patches as mpatches\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nfrom matplotlib.lines import Line2D\n\n\n# Theme tokens (see prompts/default-style-guide.md)\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# Imprint palette — first series always #009E73\nHISTOGRAM_COLOR = \"#009E73\"  # brand green\nKDE_COLOR = \"#C475FD\"  # lavender (Imprint position 2)\n\n# Configure seaborn theme with theme-adaptive colors\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.18,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data: Test score distribution (quality control scenario)\nnp.random.seed(42)\n# Mix of different student performance patterns\ntest_scores = np.concatenate(\n    [\n        np.random.normal(72, 8, 300),  # Most students in 60-85 range\n        np.random.normal(92, 5, 80),  # High-performing students\n        np.random.normal(45, 10, 40),  # Struggling students\n    ]\n)\ntest_scores = np.clip(test_scores, 0, 100)  # Bound to valid range\nnp.random.shuffle(test_scores)\n\n# Plot — canvas locked to figsize x dpi = 3200x1800, see prompts/library/seaborn.md\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Histogram with semi-transparent bars\nsns.histplot(\n    test_scores,\n    bins=26,\n    kde=False,\n    stat=\"density\",\n    alpha=0.5,\n    color=HISTOGRAM_COLOR,\n    edgecolor=PAGE_BG,\n    linewidth=0.8,\n    ax=ax,\n    label=\"Histogram\",\n)\n\n# Rug plot surfaces the individual observations beneath the histogram\nsns.rugplot(test_scores, color=HISTOGRAM_COLOR, alpha=0.3, height=0.03, ax=ax)\n\n# KDE overlay for smooth density curve\nsns.kdeplot(test_scores, color=KDE_COLOR, linewidth=3, ax=ax, label=\"KDE\")\n\n# Style\nax.set_xlabel(\"Test Score (%)\", fontsize=10, color=INK)\nax.set_ylabel(\"Density\", fontsize=10, color=INK)\nax.set_title(\"histogram-kde · python · seaborn · anyplot.ai\", fontsize=11, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\n\n# Trimmed, offset spines — idiomatic seaborn polish beyond the plain L-frame\nsns.despine(ax=ax, offset=6, trim=True)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_color(INK_SOFT)\n\n# Subtle grid\nax.yaxis.grid(True, alpha=0.18, linewidth=0.8, linestyle=\"-\", color=INK)\n\n# Legend — explicit handles so Histogram (primary, first-drawn series) lists above KDE\nlegend_handles = [\n    mpatches.Patch(facecolor=HISTOGRAM_COLOR, alpha=0.5, edgecolor=PAGE_BG, label=\"Histogram\"),\n    Line2D([0], [0], color=KDE_COLOR, linewidth=3, label=\"KDE\"),\n]\nax.legend(\n    handles=legend_handles,\n    frameon=True,\n    fancybox=False,\n    fontsize=8,\n    framealpha=0.95,\n    edgecolor=INK_SOFT,\n    facecolor=ELEVATED_BG,\n)\n\nplt.tight_layout()\noutput_dir = os.path.dirname(os.path.abspath(__file__))\nplt.savefig(os.path.join(output_dir, f\"plot-{THEME}.png\"), dpi=400, facecolor=PAGE_BG)\n"}