{"spec_id":"roc-curve","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nroc-curve: ROC Curve with AUC\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 93/100 | Updated: 2026-05-09\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, HoverTool, Legend\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\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\"]\n\n# Data - Simulated ROC curves for three classifiers with different performance levels\nnp.random.seed(42)\n\n# Generate ROC curve points using sklearn-like simulation\n# Using the parametric approach: FPR = t, TPR = t^(1/k) where k controls curve shape\nn_points = 200\nt = np.linspace(0, 1, n_points)\n\n# Model 1: Strong classifier (AUC ~0.95) - curve bows far towards top-left\nk1 = 0.15\nfpr_1 = t\ntpr_1 = np.power(t, k1)\nauc_1 = np.trapezoid(tpr_1, fpr_1)\n\n# Model 2: Medium classifier (AUC ~0.82) - moderate curve\nk2 = 0.35\nfpr_2 = t\ntpr_2 = np.power(t, k2)\nauc_2 = np.trapezoid(tpr_2, fpr_2)\n\n# Model 3: Weak classifier (AUC ~0.68) - closer to diagonal\nk3 = 0.6\nfpr_3 = t\ntpr_3 = np.power(t, k3)\nauc_3 = np.trapezoid(tpr_3, fpr_3)\n\n# Random classifier reference line\nfpr_random = np.array([0, 1])\ntpr_random = np.array([0, 1])\n\n# Create ColumnDataSources\nsource_1 = ColumnDataSource(data={\"fpr\": fpr_1, \"tpr\": tpr_1, \"model\": [\"Model A\"] * len(fpr_1)})\nsource_2 = ColumnDataSource(data={\"fpr\": fpr_2, \"tpr\": tpr_2, \"model\": [\"Model B\"] * len(fpr_2)})\nsource_3 = ColumnDataSource(data={\"fpr\": fpr_3, \"tpr\": tpr_3, \"model\": [\"Model C\"] * len(fpr_3)})\nsource_random = ColumnDataSource(data={\"fpr\": fpr_random, \"tpr\": tpr_random})\n\n# Create figure - Square format preferred for equal aspect ratio\np = figure(\n    width=3600,\n    height=3600,\n    title=\"roc-curve · bokeh · anyplot.ai\",\n    x_axis_label=\"False Positive Rate\",\n    y_axis_label=\"True Positive Rate\",\n    x_range=(-0.02, 1.02),\n    y_range=(-0.02, 1.02),\n    tools=\"pan,wheel_zoom,box_zoom,reset,save\",\n    toolbar_location=\"right\",\n)\n\n# Plot random classifier reference line (diagonal)\nrandom_line = p.line(\n    x=\"fpr\", y=\"tpr\", source=source_random, line_width=3, line_dash=\"dashed\", line_color=INK_SOFT, alpha=0.5\n)\n\n# Plot ROC curves with Okabe-Ito colors\nline_1 = p.line(x=\"fpr\", y=\"tpr\", source=source_1, line_width=5, line_color=IMPRINT[0], alpha=0.9)\nline_2 = p.line(x=\"fpr\", y=\"tpr\", source=source_2, line_width=5, line_color=IMPRINT[1], alpha=0.9)\nline_3 = p.line(x=\"fpr\", y=\"tpr\", source=source_3, line_width=5, line_color=IMPRINT[2], alpha=0.9)\n\n# Add HoverTool for interactivity\nhover = HoverTool(tooltips=[(\"FPR\", \"@fpr{0.00}\"), (\"TPR\", \"@tpr{0.00}\")])\np.add_tools(hover)\n\n# Create legend with AUC scores\nlegend = Legend(\n    items=[\n        (f\"Model A (AUC = {auc_1:.2f})\", [line_1]),\n        (f\"Model B (AUC = {auc_2:.2f})\", [line_2]),\n        (f\"Model C (AUC = {auc_3:.2f})\", [line_3]),\n        (\"Random Classifier\", [random_line]),\n    ],\n    location=\"center\",\n)\n\np.add_layout(legend)\nlegend.label_text_font_size = \"22pt\"\nlegend.glyph_height = 30\nlegend.glyph_width = 30\nlegend.spacing = 15\nlegend.padding = 20\nlegend.background_fill_color = ELEVATED_BG\nlegend.background_fill_alpha = 0.9\nlegend.border_line_color = INK_SOFT\nlegend.border_line_width = 1\nlegend.label_text_color = INK_SOFT\n\n# Style the plot\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.title.align = \"center\"\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\n\n# Grid styling - subtle\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\np.xgrid.grid_line_width = 0.8\np.ygrid.grid_line_width = 0.8\n\n# Background and border\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\np.outline_line_width = 1\n\n# Axis styling\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.axis_line_width = 1\np.yaxis.axis_line_width = 1\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\np.xaxis.major_tick_line_width = 1\np.yaxis.major_tick_line_width = 1\n\n# Save as HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome — Selenium 4 / Selenium Manager\nW, H = 3600, 3600\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}