# How to Plot an Equation in Python and Excel

To plot an equation, you generate a set of x values, compute the matching y values from the equation, and draw the pairs as a line or scatter chart. Both Python and Excel follow that same three-part recipe. This article shows how to plot an equation in each tool using $y = 2x + 3$ and $y = x^2$ as the running examples.

## Quick Answer

- Pick a range of x values, such as -5 to 5, and list them in order.
- Compute y for every x by substituting into the equation.
- In Python, use `numpy.linspace` for x and `matplotlib.pyplot.plot` for the chart [1].
- In Excel, put x in one column, write the y formula in the next column, then insert a Scatter chart with straight lines.
- Always plot enough points. A straight line needs few, a curve needs many.

## Before You Start

You need two things: a defined equation and a sensible x range. The equation tells you how to turn each x into a y. The range tells you which part of the curve you want to see.

Choose the range based on where the interesting behavior happens. For $y = 2x + 3$, any range works because the line is straight. For $y = x^2$, a range that includes negative and positive values shows the full U shape. A range like 0 to 5 would only show the right half.

Decide how many points you need. A line needs only two points to be drawn correctly, but a curve needs many so it looks smooth instead of angular. Ten to twenty points is a reasonable start for most classroom equations.

For Python, install NumPy and Matplotlib if you have not already. For Excel, no setup is needed beyond a blank sheet. If you are new to spreadsheet formulas, the basics of writing them are covered in [Excel Formula Basics: What Formulas Are and How to Write Them](/blog/data-analysis/excel-formula-basics).

## Step by Step

### Python

1. Import NumPy and Matplotlib.
2. Create your x values with `np.linspace(start, stop, count)`. This returns evenly spaced numbers.
3. Compute y by writing the equation directly against the x array.
4. Call `plt.plot(x, y)` to draw the line [1].
5. Add labels, a legend, and a grid, then call `plt.show()`.

The `plot` function takes x and y coordinates and draws a line through them by default [1]. You can pass a format string like `'bo'` for blue circle markers, or use keyword arguments such as `color` and `linestyle` for the same effect [1].

### Excel

1. Type your x values in column A, starting at A2.
2. In B2, write the formula for the first equation. For $y = 2x + 3$ with x in A2, that is `=2*A2+3`.
3. In C2, write the formula for the second equation. For $y = x^2$, that is `=A2^2`.
4. Select B2 and C2, then drag the fill handle down to match the last x value.
5. Select all three columns, go to Insert, and choose Scatter with Straight Lines.

Excel recalculates every y automatically when you change a formula or an x value. That makes it easy to test a new equation without rebuilding the sheet. If you want to compare the shape against raw data points, the steps in [How to Create a Scatter Plot in Excel (Step by Step)](/blog/data-analysis/how-to-create-scatter-plot-in-excel) apply here too.

## Worked Example

The dataset below holds the values for $y = 2x + 3$ and $y = x^2$ over x from -5 to 5, using 11 points spaced 1.0 apart.

| x | y_lin (2x + 3) | y_quad (x^2) |
|---|---|---|
| -5 | -7 | 25 |
| -4 | -5 | 16 |
| -3 | -3 | 9 |
| -2 | -1 | 4 |
| -1 | 1 | 1 |
| 0 | 3 | 0 |
| 1 | 5 | 1 |
| 2 | 7 | 4 |
| 3 | 9 | 9 |
| 4 | 11 | 16 |
| 5 | 13 | 25 |

The x range comes from `np.linspace(-5, 5, 11)`, which produces -5 to 5 with a step of 1.0. Substituting each x gives the two y columns. At $x = -5$, the linear equation gives $2(-5) + 3 = -7$ and the quadratic gives $(-5)^2 = 25$. At $x = 0$, they are 3 and 0. At $x = 5$, they are 13 and 25.

The two curves cross where $2x + 3 = x^2$, which happens at $x = -1$, where both equal 1, and at $x = 3$, where both equal 9. That crossing point is visible in the chart and is a common reason to plot two equations together.

Here is the Python code that produces the chart.

```python
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 11)
y_lin = 2 * x + 3
y_quad = x ** 2

plt.plot(x, y_lin, label='y = 2x + 3')
plt.plot(x, y_quad, label='y = x^2')
plt.xlabel('x'); plt.ylabel('y'); plt.legend()
plt.grid(True); plt.show()
print(f"x from {x.min():g} to {x.max():g} ({len(x)} points); y=2x+3 at x=5 -> {y_lin[-1]:g}; y=x^2 at x=5 -> {y_quad[-1]:g}")
```

Output:

```
x from -5 to 5 (11 points); y=2x+3 at x=5 -> 13; y=x^2 at x=5 -> 25
```

The figure shows a blue line for $y = 2x + 3$ and an orange parabola for $y = x^2$ across x from -5 to 5. The line crosses the parabola at $x = -1$ and $x = 3$.

## Other Ways to Do It

Python has more than one plotting library. Matplotlib is the most common starting point because `plot` accepts x and y arrays directly and handles multiple series with repeated calls [1]. Seaborn and Plotly build on similar ideas if you want statistical styling or interactive charts.

Excel offers a few routes. A Scatter chart with straight lines is the closest match to plotting a function. A Line chart also works, but it treats the x values as categories, which distorts spacing when the x values are uneven. For evenly spaced x values like the ones here, the difference is small, but a Scatter chart is the safer habit.

You can also plot equations without code by using a graphing calculator or an online grapher. These accept the equation directly and choose their own x range. They are fast for a quick look but give you less control over the exact points and styling.

If your equation comes from fitting a trendline to real data, the approach is slightly different because the equation is estimated from points. That workflow is covered in [How to Make a Scatter Plot with a Trendline, Equation and R-squared](/blog/research-skills/how-to-make-a-scatter-plot).

## Troubleshooting

If the curve looks jagged, you have too few points. Increase the count in `np.linspace` or extend the Excel column with more x values.

If the line is missing in Excel, check that the x column is numeric. Numbers stored as text will not plot correctly.

If the chart shows one flat line, your formula may be referencing the wrong cell. Click the formula cell and confirm it points at the x value in the same row.

If Python throws a shape error, your x and y arrays have different lengths. Print `len(x)` and `len(y)` to compare.

If the legend is missing, you forgot the `label` argument in `plt.plot` or the `plt.legend()` call.

## Common Mistakes

- Using too few points for a curve. A parabola drawn with three points looks like a V. Use at least ten points across the range.
- Mixing up the x and y columns in Excel. The first column you select becomes the x axis. Check the chart after inserting it.
- Forgetting that Excel's `^` operator needs no extra parentheses for simple powers. `=A2^2` is correct, and `=A2^2` with a negative x still returns a positive value.
- Choosing a Line chart instead of a Scatter chart. Line charts space x values evenly and can misrepresent the shape.
- Leaving the x range too narrow. If the curve's interesting part sits outside your range, the chart looks flat or incomplete.
- Hardcoding y values instead of using a formula. A formula lets you change the equation and see the new curve instantly.

## Limitations

Plotting an equation shows the shape of the relationship over the range you chose. It does not prove anything about behavior outside that range. A line that looks straight between -5 and 5 may curve sharply at 50, and a chart cannot tell you which.

Charts also hide the exact numbers. Two curves that look identical at screen resolution may differ by small amounts. When the precise values matter, read them from the table or compute them directly. For equations with discontinuities, such as division by zero, the plotted line may jump across the gap and suggest a connection that does not exist.

## Frequently Asked Questions

### How many points do I need to plot an equation?

It depends on the shape. A straight line needs two points to be defined, but five to ten makes a cleaner chart. A curve needs more, usually fifteen to fifty, so the bends look smooth. When in doubt, add points and check whether the shape changes.

### Can I plot an equation in Excel without a data table?

Not directly. Excel charts read from cells, so you need at least a small table of x and y values. You can keep it short, but the chart will only be as smooth as the points you provide.

### Why does my Excel chart show the x values as 1, 2, 3 instead of my numbers?

You likely used a Line chart, which labels the x axis with category numbers. Switch to a Scatter chart so Excel uses your actual x values on the axis.

### How do I plot two equations on the same chart?

In Python, call `plt.plot` once per equation before `plt.show()` [1]. In Excel, put each equation in its own column and select all the columns before inserting the chart. Both series then share the same axes.

### Can I plot an equation with negative and positive x values?

Yes. Just include negative numbers in your x range. For $y = x^2$, negative x values produce the same y as their positive counterparts, which is why the parabola is symmetric around zero.

## References

1. [matplotlib.pyplot.plot, Matplotlib 3.11.2 documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html)

## Further Reading

- [8.12.1 Graphing a Quadratic Data Set Using Technology - Algebra 1 | OpenStax](https://openstax.org/books/algebra-1/pages/8-12-1-graphing-a-quadratic-data-set-using-technology)
- [Weissgerber TL, Milic NM, Winham SJ et al. (2015). Beyond Bar and Line Graphs: Time for a New Data Presentation Paradigm. PLOS Biology](https://doi.org/10.1371/journal.pbio.1002128)
- [Rougier NP, Droettboom M, Bourne PE (2014). Ten Simple Rules for Better Figures. PLoS Computational Biology](https://doi.org/10.1371/journal.pcbi.1003833)
- [NIST/SEMATECH e-Handbook: Graphical Techniques](https://www.itl.nist.gov/div898/handbook/index.htm)
- [Cleveland WS, McGill R (1984). Graphical Perception: Theory, Experimentation, and Application to the Development of Graphical Methods. Journal of the American Statistical Association](https://doi.org/10.1080/01621459.1984.10478080)

## Related Articles

- [How to Make a Box Plot in Excel (Step by Step)](/blog/data-analysis/how-to-make-box-plot-excel)
- [How to Flip the X and Y Axis in Excel (Step by Step)](/blog/data-analysis/flip-x-y-axis-excel)
- [How to Calculate Correlation Coefficient in Excel (Step by Step)](/blog/data-analysis/calculate-correlation-coefficient-excel)
- [How to Create a Scatter Plot in Excel (Step by Step)](/blog/data-analysis/how-to-create-scatter-plot-in-excel)
- [How to Make a Chart in Excel (Step by Step)](/blog/data-analysis/how-to-make-chart-in-excel)
- [How to Make a Scatter Plot with a Trendline, Equation and R-squared](/blog/research-skills/how-to-make-a-scatter-plot)