{"spec_id":"gain-curve","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\ngain-curve: Cumulative Gains Chart\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 94/100 | Updated: 2026-05-11\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\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 (first series always #009E73)\nOI_BRAND = \"#009E73\"\nOI_2 = \"#C475FD\"\nOI_3 = \"#4467A3\"\nOI_NEUTRAL = \"#1A1A1A\" if THEME == \"light\" else \"#E8E8E0\"\n\n# Configure seaborn theme\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.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Set seed for reproducibility\nnp.random.seed(42)\n\n# Generate synthetic classification data (customer churn scenario)\nn_samples = 1000\n\n# Create synthetic true labels with ~30% positive class (churners)\ny_true = np.random.choice([0, 1], size=n_samples, p=[0.7, 0.3])\n\n# Create correlated prediction scores (simulating a reasonably good model)\nnoise = np.random.normal(0, 0.15, n_samples)\ny_score = np.clip(0.3 + 0.4 * y_true + noise, 0, 1)\n\n# Calculate cumulative gains curve\nsorted_indices = np.argsort(y_score)[::-1]\ny_true_sorted = y_true[sorted_indices]\n\ntotal_positives = np.sum(y_true)\ncum_positives = np.cumsum(y_true_sorted)\ngains = cum_positives / total_positives * 100\n\n# Population percentage (x-axis)\npopulation_pct = np.arange(1, len(y_true) + 1) / len(y_true) * 100\n\n# Add origin point for complete curve\npopulation_pct = np.insert(population_pct, 0, 0)\ngains = np.insert(gains, 0, 0)\n\n# Calculate perfect model curve\npositive_rate = total_positives / len(y_true) * 100\nperfect_gains = np.minimum(population_pct / positive_rate * 100, 100)\n\n# Create figure\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Plot model gain curve using Okabe-Ito brand color\nsns.lineplot(x=population_pct, y=gains, ax=ax, color=OI_BRAND, linewidth=3.5, label=\"Churn Prediction Model\")\n\n# Plot random baseline (diagonal) using neutral color\nsns.lineplot(\n    x=[0, 100], y=[0, 100], ax=ax, color=OI_NEUTRAL, linewidth=2.5, linestyle=\"--\", label=\"Random Selection (Baseline)\"\n)\n\n# Plot perfect model curve using secondary Okabe-Ito color\nsns.lineplot(x=population_pct, y=perfect_gains, ax=ax, color=OI_2, linewidth=2.5, linestyle=\":\", label=\"Perfect Model\")\n\n# Fill area between model and baseline to show model lift\nax.fill_between(population_pct, gains, population_pct, alpha=0.2, color=OI_BRAND)\n\n# Styling\nax.set_xlabel(\"Customers Targeted (%)\", fontsize=20, color=INK)\nax.set_ylabel(\"Churners Captured (%)\", fontsize=20, color=INK)\nax.set_title(\"gain-curve · seaborn · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Set axis limits\nax.set_xlim(0, 100)\nax.set_ylim(0, 105)\n\n# Remove top and right spines\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)\n\n# Subtle grid\nax.yaxis.grid(True, alpha=0.1, linewidth=0.8, color=INK)\n\n# Legend in upper left to avoid data overlap\nax.legend(fontsize=16, loc=\"upper left\", framealpha=0.95)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}