Get a Linear Equation from an Excel Graph
In this article
To get a linear equation from a graph in Excel, add a trendline to your chart and display its equation: click the data series, choose Add Trendline, pick Linear, and tick "Display Equation on chart." Excel then shows the slope and intercept in the form y = mx + b. If you also need the exact numbers in cells, use the LINEST function or the SLOPE and INTERCEPT functions. The steps below cover both the chart route and the formula route, plus what to do when the equation does not appear or looks wrong.
What You'll Need
- Excel on Windows, Mac, or the web (the chart trendline option is available in all current versions; menu labels can differ slightly by version and region).
- A worksheet with two columns of numeric data: one for the x-values and one for the y-values.
- Data that actually follows a straight-line pattern. If the points curve, a linear trendline will fit poorly and the equation will be misleading.
Step-by-Step
- Lay out your data in two columns. Put x-values in one column and y-values in the column to the right. Use a header row (for example, "Hours" and "Score") so Excel labels the axes correctly. Remove blank rows and any text in the numeric cells.
- Select the data range including the headers, then go to Insert > Scatter (the scatter chart icon in the Charts group). A scatter chart is the right choice for finding an equation; a line chart treats x-values as categories and can distort the trendline.
- Click any data point in the chart to select the series. All points in the series should highlight.
- Right-click the selected series and choose Add Trendline. On Mac, the same option appears in the right-click menu or under Chart Design > Add Chart Element > Trendline.
- In the Format Trendline pane, choose Linear. Leave "Set Intercept" unchecked unless you have a specific reason to force the line through zero.
- Tick "Display Equation on chart" (and optionally "Display R-squared value on chart"). The equation appears on the chart, usually as y = mx + b, where m is the slope and b is the intercept.
- Read the equation carefully. Excel may show it in scientific notation or with many decimal places. Right-click the equation label, choose Format Trendline Label, and set Number to a fixed number of decimal places (2 or 3 is usually enough) to make it readable.
- If you need the coefficients in cells rather than on the chart, use formulas. For a single x column and y column,
=SLOPE(known_ys, known_xs)returns m and=INTERCEPT(known_ys, known_xs)returns b. For the full equation with statistics, select a 2-cell wide by 1-cell tall range, type=LINEST(known_ys, known_xs), and press Ctrl+Shift+Enter in older versions (in Microsoft 365 it spills automatically). The first cell is the slope and the second is the intercept. - Check the fit. If you displayed R-squared, a value close to 1 means the line fits well; a low value means the linear model is weak and you should reconsider the chart type or the data.
Common Mistakes
- Using a line chart instead of a scatter chart. A line chart spaces x-values evenly regardless of their actual values, so the trendline equation will not match your data.
- Selecting the wrong series or the whole chart. The trendline option applies to a data series; if nothing highlights when you click, you have selected the chart area instead.
- Swapping x and y in the formulas. SLOPE and INTERCEPT take the y-range first, then the x-range. Reversing them gives the reciprocal slope and a different intercept.
- Leaving text or blanks in the data. Non-numeric cells are ignored or cause errors, which shifts the equation.
- Trusting the equation without checking R-squared. A linear equation will be produced even for curved data, but it may describe the pattern poorly.
- Rounding too early. If you copy a rounded equation from the chart and use it for predictions, small errors accumulate. Use the cell formulas for precise work.
Tips
- Format the trendline label to 3 decimal places for a balance of readability and accuracy.
- Use named ranges or a table so the chart and formulas update automatically when you add data.
- If you need to predict a y-value for a new x, put the slope and intercept in separate cells and use
=slope*x + intercept. - Add the equation as a text box if you need to move it away from the data points.
- For multiple series, add a trendline to each series separately and label them clearly.
Troubleshooting
- The equation does not appear. Reopen Format Trendline and confirm "Display Equation on chart" is ticked. If the label is hidden behind the plot, drag it to an empty area.
- The trendline option is greyed out. Make sure you selected a data series, not the chart area or a blank chart. If the chart is a 3-D chart, switch to a 2-D scatter chart; trendlines are not supported on some 3-D chart types.
- The equation looks wrong. Check that x and y columns are in the order you expect and that no cells contain text. Also confirm you chose Linear, not Exponential or Polynomial.
- LINEST returns a #VALUE! error. The x and y ranges must be the same size and contain only numbers. Remove blanks or text and try again.
- The chart shows a date axis. Excel may treat dates as categories. Convert dates to serial numbers or use a scatter chart with date values formatted as numbers.
- Numbers show as scientific notation. Change the trendline label number format to Number with fixed decimals.
Frequently Asked Questions
Can I get the equation without adding a trendline to the chart?
Yes. Use the SLOPE and INTERCEPT functions, or LINEST, directly on your data columns. This gives you the coefficients in cells you can reference in other formulas.
What do m and b mean in y = mx + b?
m is the slope, or the change in y for each one-unit increase in x. b is the y-intercept, the value of y when x is zero.
Why is my R-squared low?
A low R-squared means the straight line does not explain much of the variation in your data. The relationship may be curved or noisy; consider a different trendline type or more data.
Does this work in Excel for the web?
Yes, the scatter chart and trendline options are available in Excel for the web, though the exact menu location may differ slightly from the desktop app.
Can I force the line through the origin?
Yes. In Format Trendline, tick "Set Intercept" and enter 0. Use this only when theory or context says y must be zero when x is zero.
How many decimal places should I use?
Two or three is usually enough for display. For calculations, reference the SLOPE and INTERCEPT cells rather than retyping a rounded equation.