{"spec_id":"histogram-overlapping","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nhistogram-overlapping: Overlapping Histograms\nLibrary: matplotlib 3.11.1 | Python 3.13.15\nQuality: 91/100 | Updated: 2026-08-18\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# Imprint palette (first series is always #009E73)\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\n\n# Data - Comparing salary distributions across three departments\nnp.random.seed(42)\n\n# Engineering: higher salaries, tighter distribution\nengineering = np.random.normal(95000, 12000, 200)\n\n# Marketing: moderate salaries, wider spread\nmarketing = np.random.normal(75000, 18000, 180)\n\n# Sales: lower base pay plus a smaller high-commission cohort, giving the\n# distribution a light second mode - unlike the two unimodal groups above.\nsales = np.concatenate([np.random.normal(60000, 14000, 180), np.random.normal(105000, 11000, 40)])\n\ngroups = [(\"Engineering\", engineering, IMPRINT[0]), (\"Marketing\", marketing, IMPRINT[1]), (\"Sales\", sales, IMPRINT[2])]\n\n# Create plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Define consistent bins for all groups\nbins = np.linspace(20000, 150000, 35)\n\n# Overlapping histograms as filled steps - a single continuous outline per\n# group reads more clearly than per-bar edges once fills stack on top of\n# each other, and keeps the silhouette of each distribution legible.\nfor name, data, color in groups:\n    ax.hist(data, bins=bins, alpha=0.4, label=name, color=color, histtype=\"stepfilled\", edgecolor=color, linewidth=1.8)\n    ax.axvline(data.mean(), color=color, linestyle=\":\", linewidth=1.3, alpha=0.9)\n\n# Labels and styling\ntitle = \"histogram-overlapping · python · matplotlib · anyplot.ai\"\nax.set_xlabel(\"Annual Salary ($)\", fontsize=10, color=INK)\nax.set_ylabel(\"Number of Employees\", fontsize=10, color=INK)\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\n\n# Grid - y-axis only, subtle\nax.yaxis.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\n# Format x-axis with thousands separator\nax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: f\"{x / 1000:.0f}k\"))\n\n# Callout: highlight the largest mean gap (Engineering vs. Sales)\neng_mean, sales_mean = engineering.mean(), sales.mean()\nheadroom = ax.get_ylim()[1] * 1.15\nax.set_ylim(top=headroom)\ncallout_y = headroom * 0.94\nax.annotate(\n    \"\",\n    xy=(eng_mean, callout_y),\n    xytext=(sales_mean, callout_y),\n    arrowprops={\"arrowstyle\": \"<->\", \"color\": INK_SOFT, \"linewidth\": 1.2},\n)\nax.annotate(\n    f\"${(eng_mean - sales_mean) / 1000:.0f}k mean gap\",\n    xy=((eng_mean + sales_mean) / 2, callout_y),\n    xytext=(0, 6),\n    textcoords=\"offset points\",\n    ha=\"center\",\n    fontsize=8,\n    color=INK,\n    bbox={\"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"linewidth\": 0.8, \"alpha\": 0.9, \"boxstyle\": \"round,pad=0.3\"},\n)\n\n# Legend styling\nleg = ax.legend(fontsize=8, 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.8)\n    plt.setp(leg.get_texts(), color=INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}