Free graph maker & data converters

Line of Best Fit Calculator

Paste your X and Y values and get the least-squares line y = mx + b, with the slope, intercept, their uncertainties, R² and a chart. Predict y from x, or x from y.

Data (X, Y)

Paste two columns: X then Y (first row = names). Sample: hours studied vs exam score.

Updates as you type. Your data stays in this browser.
Results
Equationy = 4.352x + 47.27
Slope (m)4.352 ± 0.11995% CI 4.08 to 4.63
Intercept (b)47.27 ± 0.74195% CI 45.6 to 49
R²0.99401share of the variation in y explained by the line
Correlation (r)0.997
Standard error of estimate1.084typical distance of a point from the line, in y units
Points (n)108 degrees of freedom

x = 11 → y = 95.13 outside your data range (extrapolation)

Fitted values and residuals
Hours studiedExam scoreFitted ŷResidual
15251.62+ 0.382
25555.97− 0.97
36160.32+ 0.679
46464.67− 0.673
57069.02+ 0.976
67273.38− 1.38
77977.73+ 1.27
88382.08+ 0.921
98586.43− 1.43
109190.78+ 0.218
Chart
02468105060708090100Hours studiedExam scoreExam scorey = 4.352x + 47.27, R2 = 0.9940

How to use the Line of Best Fit Calculator

  1. Paste two columns of numbers, X first and Y second. The first row can hold the column names.
  2. Read the equation, slope, intercept and R² in the results panel. They update as you type.
  3. Enter an x value to predict y, or a y value to find x.
  4. Open “Fitted values and residuals” to check how far each point is from the line.

What the calculator computes

It fits the straight line that minimises the sum of the squared vertical distances between your points and the line, which is the ordinary least-squares line of best fit. The slope is the sum of (x − mean x)(y − mean y) divided by the sum of (x − mean x)², and the intercept is mean y minus the slope times mean x, so the line always passes through the centre of the data.

Beyond the equation you get the standard error of the slope and intercept, with 95% confidence intervals from Student’s t distribution, the correlation r, R², and the standard error of the estimate. Those uncertainties are what turn “the slope is 4.35” into “the slope is 4.35 ± 0.12”, which is what a lab report or a paper needs.

Worked example: hours studied and exam score

The sample is ten students: hours studied from 1 to 10 and the score each earned. The calculator returns y = 4.352x + 47.27, with R² = 0.9940. In plain words, each extra hour of study goes with about 4.4 more marks, and a student who studied zero hours would be predicted to score about 47. Asking for x = 11 predicts a score of about 95.1.

That prediction carries a warning: 11 hours is outside the data (1 to 10), so it is an extrapolation. The calculator flags this, because lines rarely keep going. A score cannot pass 100, so the straight line must bend somewhere.

ResultValueMeaning
Slope4.352Marks gained per extra hour
Intercept47.27Predicted score at 0 hours
R²0.994099.4% of the variation in scores is explained by hours
Std. error of estimate1.08Typical miss of about one mark

Reading the results correctly

A line of best fit describes a relationship; it does not prove one variable causes the other. Check the chart for curvature, and the residuals for patterns: if the residuals are all positive in the middle and negative at the ends, the data are curved and a straight line is the wrong model. A single point far from the others can also tilt the line, so look for outliers before trusting the slope.

The standard error of the estimate is in the units of y and tells you how large the typical miss is, which R² does not. The guide to line of best fit and R² explains these checks, and the scatter plot maker draws the same fit as a publication-quality figure.

Doing it by hand or in Excel

To check the calculator, compute the two means, then Sxx = Σ(x − x̄)² and Sxy = Σ(x − x̄)(y − ȳ). For the sample data x̄ = 5.5, ȳ = 71.2, Sxx = 82.5 and Sxy = 359, so the slope is 359 / 82.5 = 4.352. In Excel use =SLOPE(y_range, x_range), =INTERCEPT(y_range, x_range) and =RSQ(y_range, x_range); note that the Y range comes first. The trendline guide shows the chart method as well.

Frequently asked questions

How do I find the line of best fit?
Calculate the slope as Sxy / Sxx and the intercept as mean y minus slope times mean x, or paste your data into this calculator, which does it instantly and shows the uncertainties.
What is the difference between R and R²?
r is the correlation coefficient, from −1 to 1, and shows direction. R² is r squared, from 0 to 1, and is the fraction of variation in y explained by the line.
How many points do I need?
At least three for uncertainties to exist, and ideally ten or more. With two points a line always fits perfectly, which says nothing.
Can I force the line through zero?
This calculator fits the intercept, which is the safer default. Forcing a zero intercept changes the slope and inflates R², so only do it when the physics demands it.
Is my data private?
No. The calculation runs in your browser, so the numbers you enter are never sent to a server.

Guides that go with this tool

Last updated: · by Dev Patel ·Read our guides