HOWTO · Python
How to Calculate the Slope in Python
This tutorial discusses how to calculate the slope of a line in Python.
On this page
The slope describes how much a line’s y-coordinate changes for each unit change in its x-coordinate. For two points, calculate it with m = (y2 - y1) / (x2 - x1). In Python, use a small function for a direct two-point calculation; use SciPy or NumPy when you want to fit a line to paired data. The fitting functions also work with two points, but their main benefit is handling a dataset with more observations.
Calculate the Slope Between Two Points with a Python Function
For points (x1, y1) and (x2, y2), the numerator is the vertical change and the denominator is the horizontal change. Keep each x-coordinate paired with its corresponding y-coordinate when passing values to the function.
Run this example:
def slope(x1, y1, x2, y2):
return (y2 - y1) / (x2 - x1)
print(slope(4, 5, 8, 10))
1.25
The function returns the quotient of the change in y and the change in x. For these coordinates, the changes are 5 vertically and 4 horizontally, so the slope is 5/4. A positive result means y increases as x increases; a negative result means y decreases.
Calculate a Regression Slope with SciPy
scipy.stats.linregress fits a straight line to paired x and y values and returns regression statistics. Its slope is the fitted line’s gradient; the other returned values include the intercept, correlation coefficient, p-value, and standard error. With two points, the fitted line passes through both. With more points, it gives a linear fit to the dataset rather than calculating a separate slope for each pair.
This example uses the same two points as the direct function. For a dataset, replace the lists with equally sized sequences of observations.
from scipy.stats import linregress
x = [4, 8]
y = [5, 10]
result = linregress(x, y)
print(result.slope)
1.25
The imported function is specifically linregress; it is not a method on the whole SciPy library. Install SciPy in the Python environment before running it.
Fit a Line with NumPy
np.polyfit finds polynomial coefficients that fit the supplied data. Set the degree to 1 for a straight line. The returned coefficients are ordered from the highest power to the constant term, so the first value is the slope and the second is the intercept.
import numpy as np
x = [4, 8]
y = [5, 10]
slope, intercept = np.polyfit(x, y, 1)
print(slope)
1.2499999999999991
As with regression, using more than two observations fits one line to the dataset. Floating-point arithmetic can make the computed coefficient display as a value very close to the mathematical slope instead of an exact decimal. Install NumPy in the Python environment before running it.
Handle Vertical Lines and Choose an Approach
The direct formula divides by x2 - x1. When both points have the same x-coordinate, that difference is zero, so a vertical line has no finite slope and the function cannot return one using ordinary division. Handle this case according to your application—for example, report that the slope is undefined or represent the line separately—instead of treating it as a numeric slope.
Choose the direct function when you have exactly two points and want their slope. Choose linregress when you want a regression slope and related statistics for paired observations. Choose np.polyfit(x, y, 1) when you need the coefficients of a degree-1 polynomial fit. Both library approaches describe a best-fit line for a larger dataset; they do not change the vertical-line limitation of representing a line by a finite slope.