Slope of a Line
The slope of a line measures how steep it is: how much changes for each step of 1 in . It is the single number that separates one direction of line from another, and outside geometry it has a more useful name: a rate of change, such as kilometres per hour, megabytes per second or euros per item.
Rise over run
Pick any two points and on a line. The slope is the vertical change divided by the horizontal change:
(, the Greek letter delta, means "change in".) For the points and :
Every step of 1 to the right raises the line by 3. Swapping the points gives again; what matters is that you subtract in the same order on top and bottom. On a straight line, any two points give the same slope, which is precisely what makes it straight.
The four kinds of slope
| Slope | Line goes | Example | Points |
|---|---|---|---|
| positive | up to the right | , | |
| negative | down to the right | , | |
| zero | horizontal | , | |
| undefined | vertical | division by 0 | , |
A larger means a steeper line. A slope of 0 means "no rise"; an undefined slope means "no run", because the formula would divide by .
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 20)
plt.figure(figsize=(6, 4))
for m, style in [(2, "-"), (0.5, "--"), (0, ":"), (-1, "-.")]:
plt.plot(x, m * x + 1, style, label=f"slope {m}")
# a rise/run triangle on the slope-2 line, from (0, 1) to (1, 3)
plt.plot([0, 1, 1], [1, 1, 3], color="gray")
plt.text(0.35, 0.55, "run 1"); plt.text(1.1, 1.9, "rise 2")
plt.axhline(0, color="black", linewidth=0.8); plt.axvline(0, color="black", linewidth=0.8)
plt.ylim(-4, 6); plt.grid(True, alpha=0.3); plt.legend()
plt.show()
print("four lines through (0, 1) with slopes 2, 0.5, 0, -1")four lines through (0, 1) with slopes 2, 0.5, 0, -1
Slope as a rate
When the axes carry units, the slope carries units too: (units of ) per (unit of ).
| -axis | -axis | Slope means |
|---|---|---|
| time in hours | distance in km | speed in km/h |
| time in seconds | downloaded MB | download rate in MB/s |
| number of items | total cost in € | price per item |
| horizontal metres | height in metres | road grade () |
A download shows 30 MB after 2 s and 105 MB after 7 s. The rate is . If the rate stays constant, the graph of downloaded MB against time is a straight line with slope 15.
The slope is also tied to the angle the line makes with the -axis: . A slope of 1 is 45°, a slope of 0.08 is about 4.6°. Game engines use this to decide whether a character can walk up a ramp: a "max walkable slope" of 45° means the ground's rise-over-run may be at most 1 (see Slopes, Steps and Ground Snapping).
In software: spotting a constant rate
A server reports its memory use every 10 minutes. Is it growing at a constant rate (a straight line), and if so, when will it hit the 1024 MB limit?
times = [0, 10, 20, 30, 40] # minutes
memory = [512, 542, 572, 602, 632] # MB
rates = [(memory[i + 1] - memory[i]) / (times[i + 1] - times[i]) for i in range(len(times) - 1)]
print("slope of each interval:", rates)
if len(set(rates)) == 1: # every interval has the same slope: a line
m = rates[0]
t_limit = (1024 - memory[0]) / m
print(f"constant leak of {m} MB/min, limit reached after {t_limit:.1f} min")slope of each interval: [3.0, 3.0, 3.0, 3.0]
constant leak of 3.0 MB/min, limit reached after 170.7 min
Equal slopes between consecutive points mean the data lie on one line. Real measurements are noisy, so in practice you fit the best line through them instead (Linear Models and Trend Lines). In machine learning, the slope of the loss curve tells gradient descent which way to step; that is the same idea in calculus clothing (Gradient Descent).
Tip: Slopes of parallel lines are equal; slopes of perpendicular lines multiply to −1 (2 and , for instance). Equations of Lines puts both facts to work.
Exercises
Exercise 1: Two points
Find the slope of the line through and .
Answer
.
Solution
The line falls 2 units for every unit to the right.
Exercise 2: Which kind?
Classify the slope of the line through each pair: (a) and , (b) and , (c) and .
Answer
(a) undefined (vertical), (b) zero (horizontal), (c) negative, .
Solution
Exercise 3: A rate
A delivery van's odometer reads 120 km at 9:00 and 330 km at 12:00. What is its average speed, and what does the odometer read at 13:30 if the speed stays the same?
Answer
70 km/h; 435 km at 13:30.
Solution
Exercise 4: Collinear points in code
Write a function collinear(p, q, r) that returns True when three points lie on one line, without dividing (so vertical lines cause no error). Test it on , , and on , , .
Answer
True for the first triple, False for the second.
Solution
Equal slopes can be cross-multiplied, which avoids dividing by zero:
def collinear(p, q, r):
return (q[1] - p[1]) * (r[0] - p[0]) == (r[1] - p[1]) * (q[0] - p[0])
print(collinear((1, 1), (3, 5), (6, 11)))
print(collinear((1, 1), (3, 5), (5, 10)))
print(collinear((2, 0), (2, 4), (2, 9))) # vertical line works tooTrue
False
True
Slopes: and for the first triple; but for the second.
Exercise 5: Road grade
A road rises 12 m over 150 m of horizontal distance. What is its slope as a decimal and as a percentage grade, and what angle does it make with the horizontal?
Answer
, an 8 % grade, about 4.57°.
Solution
import math
m = 12 / 150
print(m, f"{m:.0%}", round(math.degrees(math.atan(m)), 2))0.08 8% 4.57
Test yourself
What is the slope of the line through and ?
- 2
- −2
A graph shows cost in euros () against kilograms (). Its slope is 4.5. What does that mean?
- The fixed fee is €4.50
- Each extra kilogram costs €4.50
- 4.5 kg cost €1
- The total cost is €4.50
Why is the slope of a vertical line undefined?
- Its rise is zero
- Its run is zero, so rise/run divides by zero
- Vertical lines are not straight
- Its slope is infinitely negative only