Objective
Learning objectives
- Identify the x-axis, y-axis, and coordinate pairs on a graph
- Describe a graph's slope informally as its steepness, and locate its intercepts
- Plot a function's output values with matplotlib
- Read specific values off a graph and describe its overall shape in words
Hook
Here's cost(tokens) from last lesson as a table:
| tokens | cost |
|---|
| 0 | $0.000 |
| 2,000 | $0.004 |
| 4,000 | $0.008 |
| 6,000 | $0.012 |
| 8,000 | $0.016 |
| 10,000 | $0.020 |
Technically complete, genuinely correct — and still slower to make sense of than a single glance at the same six points connected into a line. A graph doesn't add new information the table didn't have; it just makes the pattern in that information visible instead of buried in a column of digits.
See it
Picture each row of that table as a point: (0, 0), (2000, 0.004), (4000, 0.008), and so on. The first number tells you how far right to go; the second tells you how far up. Plot all six points and connect them, and a shape appears that no single row could show on its own: a straight line, climbing at a perfectly steady rate, starting exactly at the origin.
The graph is the function — just drawn instead of computed
Every point on that line is an input/output pair of cost(tokens), nothing more. The line isn't a separate object that happens to relate to the function; it's a picture of the exact same rule from Lesson 01, with every possible input plotted at once instead of one at a time.
Name it
A graph lives on two perpendicular number lines: the x-axis (horizontal, usually the input) and the y-axis (vertical, usually the output). Every point on the graph is a coordinate pair, written (x, y) — go x units along the horizontal axis, then y units up or down.
Slope, informally, is how steep the graph is — how fast y climbs (or drops) as x increases. cost(tokens) climbs steadily: every added 2,000 tokens raises the cost by the same $0.004, which is exactly what "constant steepness" looks like on a graph. (Lesson 03 turns this informal idea into an exact number.)
An intercept is where the graph crosses an axis. cost(tokens) crosses the y-axis at (0, 0) — zero tokens costs nothing, which is both the y-intercept and the x-intercept here, since this particular line happens to pass through the origin. That's not true of every graph; it's a property of this specific function worth noticing, not a rule.
Code it
import matplotlib.pyplot as plt
def cost(tokens):
return (tokens / 1000) * 0.002
xs = [0, 2000, 4000, 6000, 8000, 10000]
ys = [cost(x) for x in xs]
for x, y in zip(xs, ys):
print(x, round(y, 4))
plt.plot(xs, ys, marker="o")
plt.xlabel("tokens")
plt.ylabel("cost ($)")
plt.title("cost(tokens)")
plt.savefig("cost_graph.png", dpi=150, bbox_inches="tight")
0 0.0
2000 0.004
4000 0.008
6000 0.012
8000 0.016
10000 0.02
plt.plot(xs, ys) is doing exactly the "connect the points" step from See It — xs and ys are just the same table from the Hook, split into two matching lists. This is a deliberately minimal first look at plotting; a full treatment of matplotlib (styling, multiple series, subplots) is Course 04's job. Right now the only goal is connecting "a graph" to "a function's own output values," concretely.
def g(x):
return x * x
xs = [-2, -1, 0, 1, 2]
ys = [g(x) for x in xs]
print(list(zip(xs, ys)))
Unlike cost(tokens), this graph wouldn't be a straight line — both ends curve back up to 4 while the middle dips to 0, symmetric around x = 0. That's because squaring a negative number and squaring its positive counterpart give the same result: g(-2) and g(2) both land on 4. A table can hide that symmetry in plain sight; a graph makes it obvious immediately.