RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Python for AI — Zero to Useful
  6. /Ch. 25
Python for AI — Zero to Useful

25. Plotting AI Metrics

Chapter 25 of 36 · 15 min
KEY INSIGHT

For AI metrics, prefer `sklearn.metrics` plotting helpers—they handle axis scaling, legends, and formatting correctly. Plot your metrics as soon as you generate them, not at the end of long training runs where failures hide.

AI work has specific visualization needs: confusion matrices, ROC curves for classification, precision-recall tradeoffs, embedding projections. Matplotlib handles all of these with the right approach.

Here's a confusion matrix visualization:

import matplotlib.pyplot as plt
import numpy as np

def plot_confusion_matrix(cm, class_names):
    """Plot a confusion matrix with nice formatting."""
    fig, ax = plt.subplots(figsize=(8, 6))
    
    # Use a colormap (lower values = white, higher = dark blue)
    im = ax.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
    ax.figure.colorbar(im, ax=ax)
    
    # Labels
    ax.set(xticks=np.arange(cm.shape[1]),
           yticks=np.arange(cm.shape[0]),
           xticklabels=class_names,
           yticklabels=class_names)
    
    # Rotate labels for readability
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
    
    # Annotate cells
    thresh = cm.max() / 2
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], 'd'),
                   ha="center", va="center",
                   color="white" if cm[i, j] > thresh else "black")
    
    ax.set_xlabel('Predicted Label')
    ax.set_ylabel('True Label')
    ax.set_title('Confusion Matrix')
    
    plt.tight_layout()
    return fig

# Simulate confusion matrix (rows=true, cols=predicted)
# Classes: ['Negative', 'Neutral', 'Positive']
cm = np.array([
    [142, 8, 5],
    [12, 118, 10],
    [3, 9, 143]
])

fig = plot_confusion_matrix(cm, ['Negative', 'Neutral', 'Positive'])
plt.show()

For ROC curves and other sklearn metrics, the sklearn.metrics module has direct plotting support:

from sklearn.metrics import roc_curve, auc, RocCurveDisplay

# Simulated predictions and ground truth
y_true = [0, 0, 0, 1, 1, 1, 1]
y_scores = [0.1, 0.35, 0.4, 0.6, 0.65, 0.8, 0.95]

fpr, tpr, thresholds = roc_curve(y_true, y_scores)
roc_auc = auc(fpr, tpr)

fig, ax = plt.subplots()
RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=roc_auc).plot(ax=ax)
plt.show()

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Create a figure with two subplots side by side: one showing an ROC curve for a good classifier (high AUC ~0.95) and one for a near-random classifier (AUC ~0.55). Use simulated data. Add AUC values as text annotations.

← Chapter 24
Data Visualization with Matplotlib
Chapter 26 →
Performance Profiling