{"spec_id":"density-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\ndensity-basic: Basic Density Plot\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-30\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nfrom scipy import stats\n\n\n# Theme tokens — Imprint palette + theme-adaptive chrome\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\"\nBRAND = \"#009E73\"  # Imprint palette position 1 — always first series\n\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.15,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data — bimodal API response latencies: cache hits (fast) vs. cache misses (slow)\nnp.random.seed(42)\nlatencies = np.concatenate(\n    [\n        np.random.normal(45, 12, 350),  # Cache hit: fast path ~45ms\n        np.random.normal(185, 40, 150),  # Cache miss: slow path ~185ms\n    ]\n)\nlatencies = np.clip(latencies, 5, 320)\n\n# Pre-compute KDE peaks for annotation placement\nkde_func = stats.gaussian_kde(latencies)\nx_fine = np.linspace(5, 310, 1000)\ny_fine = kde_func(x_fine)\n\npeak1_mask = x_fine < 120\npeak1_x = x_fine[peak1_mask][np.argmax(y_fine[peak1_mask])]\npeak1_y = np.max(y_fine[peak1_mask])\n\npeak2_mask = x_fine >= 120\npeak2_x = x_fine[peak2_mask][np.argmax(y_fine[peak2_mask])]\npeak2_y = np.max(y_fine[peak2_mask])\n\n# Canvas: 3200 × 1800 px (16:9 landscape)\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# KDE with native seaborn fill (idiomatic fill=True)\nsns.kdeplot(data=latencies, ax=ax, fill=True, alpha=0.35, color=BRAND, linewidth=2.5, bw_adjust=0.9)\n\n# Rug plot showing individual observations\nsns.rugplot(data=latencies, ax=ax, color=BRAND, alpha=0.3, height=0.04)\n\n# Peak annotations\nax.annotate(\n    \"Cache hit\",\n    xy=(peak1_x, peak1_y),\n    xytext=(peak1_x - 18, peak1_y * 0.65),\n    fontsize=8,\n    fontweight=\"medium\",\n    color=INK,\n    arrowprops={\"arrowstyle\": \"->\", \"color\": INK_SOFT, \"lw\": 1.2},\n    ha=\"center\",\n    va=\"top\",\n)\nax.annotate(\n    \"Cache miss\",\n    xy=(peak2_x, peak2_y),\n    xytext=(peak2_x + 32, peak2_y + peak1_y * 0.18),\n    fontsize=8,\n    fontweight=\"medium\",\n    color=INK,\n    arrowprops={\"arrowstyle\": \"->\", \"color\": INK_SOFT, \"lw\": 1.2},\n    ha=\"center\",\n    va=\"bottom\",\n)\n\n# Style\ntitle = \"density-basic · python · seaborn · anyplot.ai\"\nn = len(title)\nratio = 67 / n if n > 67 else 1.0\ntitle_fontsize = max(8, round(12 * ratio))\n\nax.set_xlabel(\"Response Latency (ms)\", fontsize=10, color=INK)\nax.set_ylabel(\"Density\", fontsize=10, color=INK)\nax.set_title(title, fontsize=title_fontsize, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor spine in (\"left\", \"bottom\"):\n    ax.spines[spine].set_color(INK_SOFT)\nax.set_xlim(left=0)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8)\n\nplt.tight_layout()\n# No bbox_inches='tight' — preserves exact 3200×1800 canvas\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}