Line of Best Fit in Excel: Add a Trendline, Equation and Slope – LicenceHouse guide

To add a line of best fit in Excel, make a scatter chart of your data, click the chart, click the Chart Elements (+) button and tick Trendline. For the equation, right-click the line, choose Format Trendline and tick Display Equation on chart. To calculate the slope in a cell, use =SLOPE(known_y's, known_x's).

  1. Select your x and y data (x in the left column).
  2. Go to Insert > Charts > Insert Scatter (X, Y) or Bubble Chart > Scatter.
  3. Click the chart, click + (Chart Elements) and tick Trendline.
  4. Right-click the trendline > Format Trendline, choose the type, and tick Display Equation on chart and Display R-squared value on chart.

How do I add a line of best fit in Excel?

A line of best fit is what Excel calls a linear trendline. It works best on a scatter chart, because a scatter chart plots your real x values. Here is a worked example using study hours and test scores.

Five steps to add a line of best fit: select A1:B7, insert a Scatter chart, click the + Chart Elements button and tick Trendline.

A (Hours studied) B (Score)
1 Hours Score
2 1 52
3 2 58
4 3 61
5 4 67
6 5 72
7 6 75
  1. Select A1:B7.
  2. Go to Insert > Charts, open the Insert Scatter (X, Y) or Bubble Chart menu and choose the first Scatter option.
  3. Click the chart. Click the + button at its top-right corner and tick Trendline. Excel adds a straight dotted line through the points.
  4. To format it, right-click the line and choose Format Trendline. In the pane, Linear is selected by default.

On a Mac, there is no + button. Click the chart, then go to Chart Design > Add Chart Element > Trendline > Linear. That route also works on Windows. Microsoft's article on how to add a trend or moving average line to a chart shows both routes. If you haven't built the chart yet, our guide on how to make a scatter plot in Excel walks through setting up the x and y columns.

How do I add an equation to a graph in Excel?

Right-click the trendline, choose Format Trendline, and tick Display Equation on chart at the bottom of the Trendline Options. Excel places a text box on the chart in the form y = mx + c.

For the example data, the label reads approximately y = 4.6571x + 47.867. That means each extra hour of study adds about 4.66 points, and the line crosses the y-axis at about 47.87. Tick Display R-squared value on chart as well to see how well the line fits; here R² is about 0.991, which is a very close fit.

The label is a normal chart text box, so you can drag it to a clearer spot. To show more decimal places, right-click the label, choose Format Trendline Label, and set Number to a format such as 0.0000. Pair the equation with clear axis titles; our guide to adding axis titles and a legend in Excel covers that.

How do I calculate the slope in Excel?

Use the SLOPE function. Its syntax is =SLOPE(known_y's, known_x's), with the y values first. For the example:

SLOPE formula =SLOPE(B2:B7, A2:A7) explained with y values first; it returns 4.657 points per hour, with INTERCEPT 47.87 and R² 0.991.

=SLOPE(B2:B7, A2:A7)      returns 4.657142857
=INTERCEPT(B2:B7, A2:A7)  returns 47.86666667
=RSQ(B2:B7, A2:A7)        returns 0.991442254

These match the trendline equation, but with full precision, and they update when the data changes. Getting the arguments the wrong way round is the most common slip, and it gives a different answer without any error. Microsoft's SLOPE function reference notes that it returns #N/A when the two ranges contain different numbers of points, and that text and empty cells are ignored.

To predict a value from the line, use FORECAST.LINEAR (Excel 2016 and later) or plain arithmetic. For 8 hours of study:

=FORECAST.LINEAR(8, B2:B7, A2:A7)          returns about 85.1
=SLOPE(B2:B7,A2:A7)*8 + INTERCEPT(B2:B7,A2:A7)

Which trendline types can Excel draw?

Excel offers six trendline types in the Format Trendline pane. Linear is the line of best fit; the others suit curved data.

Table of six Excel trendline types: Linear, Exponential, Logarithmic, Polynomial, Power and Moving Average, with when to use each and limits.

Type Use when Limits
Linear Data rises or falls at a steady rate None
Exponential Values rise or fall at an increasing rate No zero or negative y values
Logarithmic Change is fast at first, then levels off x values must be above zero
Polynomial Data has hills and valleys Order 2 to 6
Power Measurements grow at a specific rate No zero or negative values
Moving Average You want to smooth noisy data No equation or R²

Microsoft explains when each type fits in trendline options in Office. Compare R² values between types, but don't pick a high-order polynomial just because it scores higher; it can bend wildly outside your data range.

Can I extend the line of best fit or force it through zero?

Yes. In Format Trendline, under Forecast, enter a number of units in Forward or Backward to extend the line past your data. Tick Set Intercept and enter 0 to force the line through the origin, which suits data where zero input must mean zero output, such as a calibration curve.

Forcing the intercept changes both the slope and R². In a formula, the matching calculation is =LINEST(B2:B7, A2:A7, FALSE), where FALSE sets the intercept to zero.

Which charts support trendlines, and does it vary by version?

You can add trendlines to unstacked 2-D scatter, line, column, bar, area, stock and bubble charts. Pie, doughnut, radar, stacked and 3-D charts don't offer the option. If Trendline is missing from Chart Elements, change the chart type first.

Trendlines, SLOPE, INTERCEPT and RSQ work the same in Excel 2016, 2019, 2021, 2024 and Microsoft 365, on Windows and Mac. FORECAST.LINEAR needs Excel 2016 or later; older files may use FORECAST, which gives the same result. Any current desktop edition, including those in our Office 2024 collection, has the full Format Trendline pane.

What we commonly see: people add a trendline to a line chart of dates or labels and then wonder why the equation doesn't match SLOPE. In a line chart, Excel ignores your x values and uses 1, 2, 3 for the points. So the slope is "per point", not "per day" or "per hour". Switch to a scatter chart whenever the x values are real numbers.

Advanced nuance: the trendline label rounds coefficients to a few significant figures. If you copy the equation into cells to make predictions, small rounding errors grow quickly, especially with polynomial trendlines or large x values. Use SLOPE and INTERCEPT, or LINEST for polynomials (for example =LINEST(B2:B7, A2:A7^{1,2}) for a second-order fit), to get full-precision coefficients. To show uncertainty around the line, add error bars to the chart.

Frequently asked questions

Why is the Trendline option greyed out?

Trendline is unavailable for chart types that don't support it, such as pie, doughnut, radar, stacked and 3-D charts. It can also be missing when the chart has several series and none is selected; Excel then asks which series to use. Change to a scatter, line or column chart, or click a single data series first, and the option appears.

What does R-squared mean on an Excel trendline?

R-squared is a number from 0 to 1 that shows how closely the trendline matches your data. A value near 1 means the points sit close to the line; a value near 0 means the line explains little of the variation. It does not prove cause and effect, and a high value on very few points can be misleading.

Can I add more than one trendline to a chart?

Yes. Each data series can have its own trendline, and you can add several trendlines to one series, for example a linear and a polynomial one, to compare the fit. Select the series, open Chart Elements or Add Chart Element, and add another trendline. Give each a distinct colour or dash style so readers can tell them apart.

How do I find the line of best fit without a chart?

Use formulas. SLOPE gives the gradient, INTERCEPT gives where the line crosses the y-axis, and RSQ gives the R-squared value. LINEST returns slope and intercept together, plus optional statistics. In Excel 2021, 2024 and Microsoft 365 LINEST spills its results automatically; in older versions you enter it as an array formula with Ctrl+Shift+Enter.

How do I remove a trendline?

Click the trendline on the chart and press Delete. Alternatively, click the chart, open Chart Elements (+) and untick Trendline, which removes all trendlines from the selected series. Deleting the trendline also removes its equation and R-squared label.

Related guides

Links in this guide

Link Type Why it helps
Microsoft Support: Add a trend or moving average line to a chart External Official steps for adding a trendline
Microsoft Support: SLOPE function External Syntax and error behaviour of SLOPE
Microsoft Support: Trendline options in Office External When to use each trendline type
Scatter plot in Excel Internal Build the chart the trendline sits on
Axis titles and legend in Excel Internal Label the chart clearly
Error bars in Excel Internal Show uncertainty around the data
Office 2024 collection Internal Editions with the desktop Format Trendline pane
Excel 2024 vs Excel in Microsoft 365 Internal Version differences for charting
Data analysis, Excel, Excel charts, How-to