{"spec_id":"line-retention-cohort","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nline-retention-cohort: User Retention Curve by Cohort\nLibrary: matplotlib 3.11.0 | Python 3.13.14\nQuality: 90/100 | Updated: 2026-06-20\n\"\"\"\n\nimport os\nimport sys\n\n\n# Prevent this file (matplotlib.py) from shadowing the matplotlib package\n_d = os.path.dirname(os.path.abspath(__file__))\nwhile _d in sys.path:\n    sys.path.remove(_d)\n\nimport matplotlib.patheffects as pe\nimport matplotlib.pyplot as plt\nimport matplotlib.ticker as mticker\nimport numpy as np\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Imprint categorical palette — positions 1→5 in canonical order\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\"]\n\n# Data\nnp.random.seed(42)\n\ncohorts = {\n    \"Jan 2025\": {\"size\": 1245, \"base_rate\": 0.82, \"plateau\": 8},\n    \"Feb 2025\": {\"size\": 1102, \"base_rate\": 0.80, \"plateau\": 12},\n    \"Mar 2025\": {\"size\": 1380, \"base_rate\": 0.78, \"plateau\": 15},\n    \"Apr 2025\": {\"size\": 1510, \"base_rate\": 0.85, \"plateau\": 22},\n    \"May 2025\": {\"size\": 1423, \"base_rate\": 0.88, \"plateau\": 30},\n}\n\nweeks = np.arange(0, 13)\n\nretention_data = {}\nfor cohort, info in cohorts.items():\n    retention = [100.0]\n    for week in weeks[1:]:\n        decay = info[\"base_rate\"] ** week * 100\n        plateau = info[\"plateau\"]\n        value = max(decay, plateau) + np.random.normal(0, 1.0)\n        value = max(value, plateau - 2)\n        retention.append(round(value, 1))\n    retention_data[cohort] = retention\n\n# Plot — landscape 3200×1800 (figsize=(8,4.5) × dpi=400)\ntitle = \"line-retention-cohort · python · matplotlib · anyplot.ai\"\nn = len(title)\ntitle_fontsize = max(8, round(12 * 67 / n)) if n > 67 else 12\n\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Graduated styling: older cohorts thinner and more transparent to emphasise recent ones\nlinewidths = [2.0, 2.2, 2.5, 2.8, 3.2]\nalphas = [0.60, 0.65, 0.75, 0.88, 1.0]\nmarker_sizes = [5, 5.5, 6, 6.5, 7]\n\nfor i, (cohort, retention) in enumerate(retention_data.items()):\n    size = cohorts[cohort][\"size\"]\n    label = f\"{cohort} (n={size:,})\"\n    ax.plot(\n        weeks,\n        retention,\n        color=IMPRINT_PALETTE[i],\n        linewidth=linewidths[i],\n        alpha=alphas[i],\n        marker=\"o\",\n        markersize=marker_sizes[i],\n        markeredgecolor=PAGE_BG,\n        markeredgewidth=0.8,\n        label=label,\n        zorder=2 + i,\n        path_effects=[pe.Stroke(linewidth=linewidths[i] + 1.5, foreground=PAGE_BG), pe.Normal()],\n    )\n\n# Reference line at 20% retention benchmark\nax.axhline(y=20, color=INK_MUTED, linestyle=\"--\", linewidth=1.2, alpha=0.7, zorder=1)\nax.annotate(\n    \"20% retention target\",\n    xy=(12, 20),\n    xytext=(10.2, 25),\n    fontsize=8,\n    color=INK_MUTED,\n    fontstyle=\"italic\",\n    arrowprops={\"arrowstyle\": \"-\", \"color\": INK_MUTED, \"lw\": 0.8},\n)\n\n# Y-axis percentage formatter\nax.yaxis.set_major_formatter(mticker.FuncFormatter(lambda x, _: f\"{int(x)}%\"))\n\n# Style\nax.set_xlabel(\"Weeks Since Signup\", fontsize=10, color=INK)\nax.set_ylabel(\"Retained Users\", fontsize=10, color=INK)\nax.set_title(title, fontsize=title_fontsize, fontweight=\"medium\", color=INK, pad=10)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\n\nax.set_xlim(-0.3, 12.5)\nax.set_ylim(0, 105)\nax.set_xticks(weeks)\nax.set_yticks([0, 20, 40, 60, 80, 100])\n\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_color(INK_SOFT)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\n\nleg = ax.legend(fontsize=8, loc=\"upper right\", framealpha=0.9, fancybox=False)\nif leg:\n    leg.get_frame().set_facecolor(ELEVATED_BG)\n    leg.get_frame().set_edgecolor(INK_SOFT)\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"}