Stop 05 · NumPy, Pandas & VisualizationTopics · 5/8

Plotting With Matplotlib

Part of the Machine Learning Roadmap

In short

Loss curves, histograms, scatter plots: you'll plot something in every notebook. Matplotlib is the base layer that Seaborn and pandas plotting sit on, so knowing it lets you fix any chart.

Matplotlib is Python's core plotting library for line charts, histograms, scatter plots and more. You will use it to look at data, loss curves and model errors throughout ML.

The purple blob paints a simple line chart on a canvas at a wooden easel.

Think of it like this

Painting a chart by hand

A painter starts with a blank canvas and adds every stroke, every label, every colour on purpose. Nothing appears unless they paint it. Matplotlib works the same way: more effort than a ready-made template, but you control every detail.

Watch it happen

What a matplotlib chart is made of

Figure
Axes
Line (ax.plot)
Title
X axis + label
Y axis + label

1/4The Figure is the whole canvas. It can hold one chart or a grid of them.

The big ideas

What you need to know

Use fig, ax = plt.subplots()

The object style is clearer than plt.plot everywhere, and it scales to multi-chart layouts.

Pick the chart for the question

Line for change over time, bar for comparing groups, histogram for one distribution, scatter for two numbers.

Always label

A chart without axis labels and units is a guess. Add a title that states the finding.

Save at good quality

fig.savefig('chart.png', dpi=150, bbox_inches='tight') gives a crisp image for reports and READMEs.

See it in code

Walk through the code

python
1import matplotlib.pyplot as plt2 3epochs = [1, 2, 3, 4, 5, 6]4train_loss = [0.92, 0.61, 0.45, 0.37, 0.31, 0.27]5val_loss = [0.95, 0.70, 0.58, 0.55, 0.56, 0.60]6 7fig, ax = plt.subplots(figsize=(6, 4))8ax.plot(epochs, train_loss, marker="o", label="train")9ax.plot(epochs, val_loss, marker="o", label="validation")10ax.set_title("Validation loss rises after epoch 4")11ax.set_xlabel("Epoch")12ax.set_ylabel("Loss")13ax.legend()14ax.grid(alpha=0.3)15fig.savefig("loss.png", dpi=150, bbox_inches="tight")16plt.show()

1/4Made-up loss values for six epochs of training.

You'll draw training curves like this constantly. When validation loss turns up while training loss keeps falling, the model is starting to overfit.

Save yourself the pain

Common mistakes

Oops

Using a pie chart with eight slices to compare categories.

Do this instead

Use a sorted bar chart. Lengths are much easier to compare than angles.

Oops

Calling plt.show() before plt.savefig and getting a blank image.

Do this instead

Save first, then show. Or call fig.savefig on the figure object directly.

Your mission

Three charts, one figure

~20 min

Quick check

You want to show how the marks of 300 students are spread out. Which chart fits best?

Go deeper

  1. 1Plot a histogram, a scatter plot and a line chart from one dataset.
  2. 2Use plt.subplots to put several charts in one figure.
  3. 3Always label axes and add a title before sharing a chart.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge