Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/ageron/handson-ml3/llms.txt

Use this file to discover all available pages before exploring further.

Matplotlib is the workhorse plotting library for Python. In the ML notebooks, it is used to visualise training curves, decision boundaries, data distributions, weight matrices, and sample images. Most plotting is done through the pyplot interface, which mirrors MATLAB’s plotting API and keeps a global figure and axes state for quick interactive use.

First plot

import matplotlib.pyplot as plt

plt.plot([1, 2, 4, 9, 5, 3])
plt.show()
If only one array is passed, Matplotlib uses the array index as the x-axis. Pass two arrays for explicit x and y coordinates:
plt.plot([-3, -2, 5, 0], [1, 6, 4, 3])
plt.axis([-4, 6, 0, 7])   # [xmin, xmax, ymin, ymax]
plt.show()
In Jupyter, importing matplotlib.pyplot automatically registers Jupyter as a backend so plots render inline. You may still see %matplotlib inline in older notebooks — this was required in older Jupyter versions but is no longer necessary.

Labels, title, and grid

import numpy as np

x = np.linspace(-2, 2, 500)
y = x ** 2

plt.plot(x, y)
plt.title("Square function")
plt.xlabel("x")
plt.ylabel("y = x**2")
plt.grid(True)
plt.show()

Line style and color

The third argument to plt.plot sets the line style and color in one compact string. "g--" means green dashed line; "r-" means red solid:
plt.plot([0, 100, 100, 0, 0, 100, 50, 0, 100],
         [0, 0, 100, 100, 0, 100, 130, 100, 0],
         "g--")
plt.axis([-10, 110, -10, 140])
plt.show()

Scatter plots

Scatter plots are used extensively in ML to visualise feature relationships, cluster assignments, and classification results:
# Two-class scatter plot
np.random.seed(42)
X = np.random.randn(100, 2)
labels = (X[:, 0] + X[:, 1] > 0).astype(int)

plt.scatter(X[labels == 0, 0], X[labels == 0, 1], color="blue", label="class 0")
plt.scatter(X[labels == 1, 0], X[labels == 1, 1], color="red",  label="class 1")
plt.legend()
plt.show()

Histograms

Histograms help inspect the distribution of any numeric variable — feature values, prediction scores, or residuals:
data = np.random.randn(1000)

plt.hist(data, bins=30, edgecolor="black")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.title("Histogram of random normal data")
plt.show()

Subplots

plt.subplot(rows, cols, index) divides the figure into a grid of panels. In the ML notebooks this is used to compare multiple plots side by side, for instance to show training vs. validation loss:
fig, axes = plt.subplots(1, 2, figsize=(10, 4))

axes[0].plot(x, np.sin(x))
axes[0].set_title("Sine")

axes[1].plot(x, np.cos(x))
axes[1].set_title("Cosine")

plt.tight_layout()
plt.show()

Displaying images with imshow

plt.imshow renders a 2D array as a heatmap or a 3D (H × W × 3) array as an RGB image. It appears in the notebooks whenever MNIST digits, convolutional feature maps, or confusion matrices are displayed:
digit = np.random.rand(28, 28)  # simulate a grayscale image

plt.imshow(digit, cmap="binary")
plt.colorbar()
plt.axis("off")
plt.title("Sample digit")
plt.show()

Saving figures

plt.savefig("my_plot.png", dpi=150, bbox_inches="tight")
Call savefig before plt.show() — once show() is called the figure is cleared.

Figure size

Control figure dimensions (in inches) with the figsize parameter:
plt.figure(figsize=(8, 4))
plt.plot(x, y)
plt.show()

Build docs developers (and LLMs) love