ROC Curve & AUC
Plot a Receiver Operating Characteristic (ROC) curve from classifier scores and binary labels. The x-axis is sensitivity (true positive rate) and the y-axis is specificity (true negative rate). The AUC is computed via the trapezoidal rule and equals the standard ROC AUC.
Interactive example
Hover the intersection points to see the target specificity, the actual specificity achieved, the corresponding sensitivity, and the score cutoff in the data.
import polars as pl
from plotutils.auc import plot_roc_curve
df = pl.DataFrame({
"score": [...], # classifier probability / score (higher → more likely positive)
"label": [...], # ground-truth binary label (0 = negative, 1 = positive)
})
chart = plot_roc_curve(
df,
score_col="score",
label_col="label",
specificity_levels=[0.95, 0.90, 0.80],
)
chart.save("roc.html") # interactive; or .show() in a notebook
Specificity levels
specificity_levels annotates the curve at one or more target specificity values.
For each target a dashed cross-hair is drawn: a horizontal line from the y-axis to the
curve, then a vertical line down to the x-axis.
The actual specificity shown is always ≥ the requested level (conservative selection): the closest curve point at or above the target is chosen. When multiple points tie, the one with the highest sensitivity is preferred.
Hovering an intersection point shows:
| Tooltip field | Meaning |
|---|---|
| Target specificity | The value you passed in |
| Actual specificity | The specificity of the closest curve point |
| Sensitivity | Corresponding true positive rate |
| Cutoff | Score threshold in your data |
Computing the curve and AUC directly
The two compute helpers are available separately if you need the raw numbers:
from plotutils.auc import _compute_roc, _compute_auc
roc_df = _compute_roc(df, score_col="score", label_col="label")
# ┌───────────┬─────────────┬─────────────┐
# │ threshold ┆ sensitivity ┆ specificity │
# │ f64 ┆ f64 ┆ f64 │
# ╞═══════════╪═════════════╪═════════════╡
# │ null ┆ 0.0 ┆ 1.0 │ ← start boundary
# │ 0.97 ┆ 0.04 ┆ 1.0 │
# │ … ┆ … ┆ … │
# │ null ┆ 1.0 ┆ 0.0 │ ← end boundary
# └───────────┴─────────────┴─────────────┘
auc = _compute_auc(roc_df) # e.g. 0.823
_compute_roc is fully vectorised in Polars: scores are grouped by unique value
(ties handled correctly), sorted descending, and cumulative TP / FP counts are derived
with cum_sum — no Python loop over thresholds.
Reference
plotutils.auc.plot_roc_curve(df, score_col='score', label_col='label', specificity_levels=None, title='', width=400, height=400, curve_color='steelblue', id_col=None, **kwargs)
Plot a ROC curve with sensitivity on the x-axis and specificity on the y-axis.
The area under this curve is mathematically equal to the standard AUC (area under the FPR / TPR ROC curve).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing a score column and a binary label column. |
required |
score_col
|
str
|
Column with classifier scores (higher → more likely positive). |
'score'
|
label_col
|
str
|
Column with binary ground-truth labels (0 = negative, 1 = positive). |
'label'
|
specificity_levels
|
list[float] or None
|
Target specificity values to annotate on the curve. For each level a dashed horizontal line is drawn from the y-axis to the curve, then a dashed vertical line goes down to the x-axis. The intersection point shows a tooltip with the closest threshold (cutoff) in the data. |
None
|
title
|
str
|
Chart title. When empty, defaults to |
''
|
width
|
int
|
Chart dimensions in pixels. |
400
|
height
|
int
|
Chart dimensions in pixels. |
400
|
curve_color
|
str
|
CSS color for the ROC curve. |
'steelblue'
|
id_col
|
str or None
|
Optional column name containing patient / sample identifiers. When provided, hovering over any threshold step on the curve reveals the ID(s) of the patient(s) whose score equals that cutoff (ties are shown as a comma-separated list). |
None
|
**kwargs
|
Additional keyword arguments are passed to |
{}
|
Returns:
| Type | Description |
|---|---|
LayerChart
|
|
Source code in src/plotutils/auc.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
plotutils.auc._compute_roc(df, score_col, label_col, reverse_score=False)
AUROC computation with polars backend. Returns a DataFrame with columns (threshold, sensitivity, specificity).
Includes boundary points at (sensitivity=0, specificity=1) and (sensitivity=1, specificity=0) with null threshold.
Ties (multiple samples sharing the same score) are handled correctly: all tied samples are grouped into one threshold step before the cumulative TP/FP counts are computed.
The label_col can be any binary column: integer 0/1, boolean, or a string / categorical column with exactly two distinct values (the lexicographically larger value is treated as the positive class).
Source code in src/plotutils/auc.py
plotutils.auc._compute_auc(roc_df)
Trapezoidal AUC under the specificity-sensitivity curve.
Equivalent to the standard AUC of the ROC (see note in the docstring
of :func:plot_roc_curve).