Syllabus Lesson 15 of 246 · Module 1: Python for this course
Module 1: Python for this course

Plotting a function

Why it matters You will read loss curves all through this course. First, learn to draw one yourself.

matplotlib draws charts. The usual pattern: make a figure and an axis, then draw on the axis.

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 61)        # 61 evenly spaced points from -3 to 3
fig, ax = plt.subplots()
ax.plot(x, x ** 2, label="x squared")
ax.set_xlabel("x"); ax.set_ylabel("y"); ax.legend()

linspace: evenly spaced numbers between two ends, the usual way to draw a smooth curve.

We check what you plotted, the numbers on the line, not how it looks.

Recap Points in x, values in y, one ax.plot.

Words and symbols in this lesson
axis
which direction an operation runs along: axis=0 down the rows, axis=1 along each row
Your turn

Plot the curve y = (x - 1) ** 2 for 101 evenly spaced x values from -2 to 4, on an axis named ax, with the label "loss". Label the x axis "weight".

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.