{"spec_id":"roc-curve","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nroc-curve: ROC Curve with AUC\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 93/100 | Updated: 2026-05-09\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom sklearn.metrics import auc, roc_curve\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\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\"]\n\n# Data\nnp.random.seed(42)\nn_samples = 1000\n\n# Generate classification data with three models of different quality\ny_true = np.random.binomial(1, 0.5, n_samples)\ny_scores_1 = y_true * np.random.beta(5, 2, n_samples) + (1 - y_true) * np.random.beta(2, 6, n_samples)\ny_scores_2 = y_true * np.random.beta(4, 2, n_samples) + (1 - y_true) * np.random.beta(2, 4, n_samples)\ny_scores_3 = y_true * np.random.beta(2.5, 2, n_samples) + (1 - y_true) * np.random.beta(2, 2.5, n_samples)\n\n# Compute ROC curves using sklearn\nfpr1, tpr1, _ = roc_curve(y_true, y_scores_1)\nauc1 = auc(fpr1, tpr1)\n\nfpr2, tpr2, _ = roc_curve(y_true, y_scores_2)\nauc2 = auc(fpr2, tpr2)\n\nfpr3, tpr3, _ = roc_curve(y_true, y_scores_3)\nauc3 = auc(fpr3, tpr3)\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nax.plot(fpr1, tpr1, color=IMPRINT[0], linewidth=3.5, label=f\"Random Forest (AUC = {auc1:.2f})\")\nax.plot(fpr2, tpr2, color=IMPRINT[1], linewidth=3.5, label=f\"Logistic Regression (AUC = {auc2:.2f})\")\nax.plot(fpr3, tpr3, color=IMPRINT[2], linewidth=3.5, label=f\"Decision Tree (AUC = {auc3:.2f})\")\n\nax.plot([0, 1], [0, 1], color=INK_SOFT, linewidth=2.5, linestyle=\"--\", label=\"Random Classifier (AUC = 0.50)\")\n\nax.fill_between(fpr1, tpr1, alpha=0.12, color=IMPRINT[0])\nax.fill_between(fpr2, tpr2, alpha=0.08, color=IMPRINT[1])\n\n# Style\nax.set_xlabel(\"False Positive Rate\", fontsize=20, color=INK)\nax.set_ylabel(\"True Positive Rate\", fontsize=20, color=INK)\nax.set_title(\"roc-curve · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\nax.set_xlim([-0.02, 1.0])\nax.set_ylim([0.0, 1.02])\n\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\n\nax.grid(True, alpha=0.15, linewidth=0.8, color=INK, axis=\"y\")\n\nleg = ax.legend(loc=\"lower right\", fontsize=16)\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=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}