{"spec_id":"roc-curve","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nroc-curve: ROC Curve with AUC\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-09\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\nfrom sklearn.datasets import make_classification\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import auc, roc_curve\nfrom sklearn.tree import DecisionTreeClassifier\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\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\n\n# Okabe-Ito palette\nBRAND_1 = \"#009E73\"\nBRAND_2 = \"#C475FD\"\nNEUTRAL = \"#888888\"\n\n# Generate classification data\nnp.random.seed(42)\nX, y = make_classification(n_samples=500, n_features=10, n_informative=8, random_state=42, class_sep=1.5)\n\n# Train two classifiers\nlr = LogisticRegression(random_state=42, max_iter=1000)\nlr.fit(X, y)\ny_scores_lr = lr.predict_proba(X)[:, 1]\n\ndt = DecisionTreeClassifier(random_state=42, max_depth=5)\ndt.fit(X, y)\ny_scores_dt = dt.predict_proba(X)[:, 1]\n\n# Calculate ROC curves using sklearn\nfpr_lr, tpr_lr, _ = roc_curve(y, y_scores_lr)\nauc_lr = auc(fpr_lr, tpr_lr)\n\nfpr_dt, tpr_dt, _ = roc_curve(y, y_scores_dt)\nauc_dt = auc(fpr_dt, tpr_dt)\n\n# Create plot\nfig = go.Figure()\n\n# Random classifier baseline\nfig.add_trace(\n    go.Scatter(\n        x=[0, 1],\n        y=[0, 1],\n        mode=\"lines\",\n        name=\"Random Classifier\",\n        line={\"color\": NEUTRAL, \"width\": 3, \"dash\": \"dash\"},\n        showlegend=True,\n    )\n)\n\n# Logistic Regression ROC curve\nfig.add_trace(\n    go.Scatter(\n        x=fpr_lr,\n        y=tpr_lr,\n        mode=\"lines\",\n        name=f\"Logistic Regression (AUC = {auc_lr:.2f})\",\n        line={\"color\": BRAND_1, \"width\": 4},\n        fill=\"tozeroy\",\n        fillcolor=\"rgba(0, 158, 115, 0.15)\",\n    )\n)\n\n# Decision Tree ROC curve\nfig.add_trace(\n    go.Scatter(\n        x=fpr_dt,\n        y=tpr_dt,\n        mode=\"lines\",\n        name=f\"Decision Tree (AUC = {auc_dt:.2f})\",\n        line={\"color\": BRAND_2, \"width\": 4},\n    )\n)\n\n# Layout with theme-adaptive styling\nfig.update_layout(\n    title={\n        \"text\": \"roc-curve · plotly · anyplot.ai\",\n        \"font\": {\"size\": 28, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    xaxis={\n        \"title\": {\"text\": \"False Positive Rate\", \"font\": {\"size\": 22, \"color\": INK}},\n        \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n        \"range\": [0, 1],\n        \"dtick\": 0.2,\n        \"showgrid\": True,\n        \"gridcolor\": GRID,\n        \"gridwidth\": 1,\n        \"zeroline\": False,\n        \"linecolor\": INK_SOFT,\n        \"constrain\": \"domain\",\n    },\n    yaxis={\n        \"title\": {\"text\": \"True Positive Rate\", \"font\": {\"size\": 22, \"color\": INK}},\n        \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n        \"range\": [0, 1],\n        \"dtick\": 0.2,\n        \"showgrid\": True,\n        \"gridcolor\": GRID,\n        \"gridwidth\": 1,\n        \"zeroline\": False,\n        \"linecolor\": INK_SOFT,\n        \"scaleanchor\": \"x\",\n        \"scaleratio\": 1,\n        \"constrain\": \"domain\",\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    legend={\n        \"x\": 0.98,\n        \"y\": 0.02,\n        \"xanchor\": \"right\",\n        \"yanchor\": \"bottom\",\n        \"font\": {\"size\": 16, \"color\": INK_SOFT},\n        \"bgcolor\": ELEVATED_BG,\n        \"bordercolor\": INK_SOFT,\n        \"borderwidth\": 1,\n    },\n    margin={\"l\": 100, \"r\": 80, \"t\": 100, \"b\": 100},\n)\n\n# Save as PNG and HTML\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}