Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Create a Best Fit Line in Excel

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To create a best fit line in Excel, add a scatter chart to your data, then right-click a data point and choose Add Trendline. In the Format Trendline pane, pick Linear, and tick Display Equation on chart and Display R-squared value on chart so you can see the slope, intercept and how well the line matches your points. That is the whole job for most people: chart first, trendline second, equation third. The rest of this guide covers the details, the variants (exponential, logarithmic, polynomial, moving average) and the things that most often go wrong.

What You'll Need

  • Excel for Windows, Excel for Mac, or Excel for the web. The steps below are written for the desktop app; the web version supports trendlines on scatter charts but has a smaller Format Trendline pane, so some options (like the moving average period box) may be missing.
  • Two columns of numeric data: one for the independent variable (the X axis, such as time or temperature) and one for the dependent variable (the Y axis, such as sales or resistance).
  • Data sorted so the X values rise from smallest to largest. Excel does not require this, but an unsorted column makes a chart that is hard to read and can confuse a moving average trendline.
  • No blank cells or text inside the data range. A single empty cell can make Excel treat a column as text and break the chart.

Step-by-Step

  1. Lay out the data cleanly. Put your X values in column A and your Y values in column B, with a header in row 1 (for example "Hours" and "Output"). Keep one row per observation and do not merge cells.
  2. Select the data range. Click the top-left cell of the block and drag to the bottom-right, headers included. If your columns are not adjacent, select the first column, then hold Ctrl (Cmd on Mac) and select the second.
  3. Insert a scatter chart. Go to the Insert tab, find the Charts group, click the scatter icon and choose Scatter with Only Markers. A scatter chart is the right choice for a best fit line; a line chart treats the X values as categories and will space them evenly, which distorts the fit.
  4. Check the chart looks right. You should see one dot per row. If Excel plotted only one series or put the X values on the Y axis, your columns were selected in the wrong order or the headers were misread. Fix the range before continuing.
  5. Add the trendline. Click any data point once to select the series (all points highlight), then right-click and choose Add Trendline. Alternatively, use Chart Design > Add Chart Element > Trendline > Linear.
  6. Choose the trend type. In the Format Trendline pane that opens on the right, select Linear for a straight best fit line. The other options are Exponential, Logarithmic, Polynomial (with an Order box), Power and Moving Average. Leave Linear selected unless your data clearly curves.
  7. Show the equation and R-squared. At the bottom of the pane, tick Display Equation on chart and Display R-squared value on chart. The equation appears in the form y = mx + b, where m is the slope and b the intercept; R-squared runs from 0 to 1 and tells you the share of variation explained by the line.
  8. Format the line. Still in the pane, open the Fill & Line section to change colour, dash style and width so the trendline stands out from the gridlines. Use a solid, contrasting colour rather than the default grey.
  9. Extrapolate if you need to. In the Trendline Options section, set Forward or Backward forecast values (in units of your X axis) to extend the line beyond your data. Keep extrapolation short; a straight line projected far past the measured range is a guess, not a prediction.
  10. Read the result. A slope of 2.5 in y = 2.5x + 10 means Y rises 2.5 units for every one unit increase in X. An R-squared of 0.95 means the line explains 95% of the variation; 0.3 means it explains very little and you should look at a different trend type or at whether a straight line makes sense at all.
  11. Label and save. Add axis titles (Chart Design > Add Chart Element > Axis Titles), a chart title that states what is being fitted, and save the workbook. If you need the numbers elsewhere, retype the slope and intercept from the chart label into cells, or use the SLOPE, INTERCEPT, RSQ and TREND functions to calculate them directly.

Common Mistakes

Using a line chart instead of a scatter chart. This is the single most frequent error. A line chart spaces X values evenly regardless of their real values, so the trendline is fitted to the wrong positions. Always start from a scatter chart.

Fitting a straight line to curved data. If the points bend, a linear trendline will report a low R-squared and a slope that describes nothing. Try Polynomial (order 2 or 3) or Exponential, and compare R-squared values, but do not keep raising the polynomial order just to push R-squared toward 1.

Leaving text or blanks in the range. Excel silently drops or misreads those rows. Clear them or convert the column to numbers before charting.

Trusting R-squared alone. A high R-squared on six points proves very little, and a polynomial with many terms can fit almost anything. Look at the chart with your eyes as well as the number.

Forgetting units. The slope carries units (Y units per X unit). Quoting a slope without saying what it measures leads to wrong conclusions later.

Tips

Add data labels to the equation box by clicking it and dragging it clear of the points, so it does not overlap the markers. If you have several series, add a trendline to each and colour-match them. For a quick numeric check without a chart, type =SLOPE(B2:B20,A2:A20) and =INTERCEPT(B2:B20,A2:A20) in empty cells; =RSQ(B2:B20,A2:A20) gives the same R-squared the chart shows. Use =LINEST for the full set of statistics in one formula. If you expect the line to pass through the origin, tick Set Intercept and enter 0, but only when theory says Y must be zero at X = 0. Keep the raw data on its own sheet and the chart on a separate one for a cleaner workbook.

Troubleshooting

The Add Trendline option is greyed out. You have probably selected the chart area rather than a data series. Click directly on a data point first. Trendlines are also unavailable on some chart types (stacked, 3-D and radar charts), so switch to a 2-D scatter chart.

No equation appears. The Display Equation on chart box is unticked, or the label is hidden behind the plot area. Reopen the Format Trendline pane and tick it, then drag the label to an empty corner.

The trendline does not follow the points. Check that the X and Y columns were not swapped when you built the chart, and that the trend type matches the shape of the data. A logarithmic fit on data that includes zero or negative X values will fail entirely.

R-squared looks wrong or is missing. It is not calculated for a moving average trendline, and it can be misleading when there are fewer than about five points. Recheck the data range for stray values.

The chart shows dates as numbers. Excel stored the dates as serial numbers. Format the X column as Date and rebuild the chart.

If that did not work, delete the chart, confirm both columns are formatted as Number or General with no text, reselect the range including headers and insert a fresh scatter chart. Rebuilding from clean data resolves most stubborn chart problems faster than editing the existing one.

Frequently Asked Questions

What does a best fit line actually show?

It is the straight line that minimises the total squared vertical distance between itself and your data points, known as least squares. It summarises the overall direction and strength of the relationship between two variables.

What R-squared value is good enough?

There is no universal threshold; it depends on the field and the noise in the data. What matters is whether the line visibly follows the points and whether the fit makes sense for what you are measuring.

Can I add a trendline to a bar chart?

Yes, Excel allows trendlines on bar, column, area and line charts as well as scatter charts. For a true best fit line, though, a scatter chart is the correct choice because it plots real X and Y values.

How do I remove a trendline?

Click the trendline on the chart to select it and press Delete, or right-click it and choose Delete. You can also reopen the Format Trendline pane and click Clear at the bottom.

Does the trendline update when I change the data?

Yes, as long as the chart's source range still covers the edited cells. If you add rows beyond the original range, extend the range by dragging the blue outline on the worksheet or use a table so the chart expands automatically.

Can I show the equation with more decimal places?

Yes. Select the equation label, then use the Number section of the Format Trendline Label pane to set a fixed number of decimal places instead of the default General format.