Plotting results with Matplotlib

Matplotlib is the standard Python library for drawing charts; in ML you use it mostly to see loss curves, data and predictions.

Why it matters

Training prints thousands of numbers, and you can't spot a trend by reading them. Is the loss still falling? Did it jump to a huge value at step 400? Is the model overfitting? A picture answers those questions in a second. Many training bugs are found by looking at a plot, not by reading the code.

The idea

A plot turns numbers into a picture, so you can see patterns and bugs. In ML, the most important plot is the loss curve.

A car dashboard. You don't read engine data line by line. You glance at the gauges.

A dashboard shows only the present. A loss curve shows the whole history of training, and that history is where the clues are.

How it works

  1. Keep a list during training: losses.append(loss) once per step, or once per epoch.
  2. plt.plot(losses) draws a line: the position in the list goes on the x-axis and the value on the y-axis.
  3. Label it: plt.xlabel("step"), plt.ylabel("loss") and a title. An unlabelled plot is easy to misread a week later.
  4. For training and validation curves, call plt.plot twice with label="train" and label="validation", then plt.legend() to add the key.
  5. If the loss spans several powers of 10, call plt.yscale("log") so the late progress is visible.
  6. plt.show() opens the window; plt.savefig("loss.png", dpi=150) saves it for your README or report.
  7. For several charts side by side, fig, axes = plt.subplots(1, 2) gives you two drawing areas: axes[0].plot(...) and axes[1].plot(...).

Key points

Common mistakes

Not true: A normal, evenly spaced y-axis is the clearest way to see late improvements in a loss.
In fact: On a normal axis, a loss that falls from 1,000 to 0.01 looks flat at the end. A log scale makes the late progress visible.

Not true: Two runs can be compared fairly even when their plots use different y-axis ranges.
In fact: Different ranges make the same curve look steeper or flatter. Fix the range with plt.ylim, or draw both runs on the same axes.

Part of Unit 0: Your toolkit in the free ML Foundations course.