# Plot a Polynomial

**URL:** <https://discuss.python.org/t/plot-a-polynomial/8945>\
**Category:** Python Help\
**Created:** [May 26, 2021, 11:43pm UTC](https://discuss.python.org/t/plot-a-polynomial/8945 "2021-05-26T23:43:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![basil106](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@basil106](https://discuss.python.org/u/basil106)\
**Post date:** [May 26, 2021, 11:43pm UTC](https://discuss.python.org/t/plot-a-polynomial/8945/1 "2021-05-26T23:43:58Z")

</div>

Plot a Polynomial  
I created a GUI to accept a Polynomial and stored it into a variable called ‘equation’.  
I used that ‘equation’ variable and used the ‘solve’ function to solve it and it worked just fine.  
However, when I try to use the same variable, ‘equation’, along with the ‘linspace’, in the ‘plot’ function to plot the graph of the function, it gives me an ‘Exception in Tkinter call back’ and rejects the plotting exercise.  
If instead of using the ‘equation’ variable, I enter the actual function, like x\*\*2+5\*x-6, directly into the ‘plot’ function, it works fine.  
I have extensive experience using Assembler language and being able to get complete control of the machine down to the bit level.  
Using APIs is new to me and very unfriendly

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [May 27, 2021, 5:21am UTC](https://discuss.python.org/t/plot-a-polynomial/8945/2 "2021-05-27T05:21:17Z")

</div>

I looks like you are trying to apply a SymPy expression to a NumPy array as if the expression were callable. A SymPy expression is symbolic. It can contain several symbols, like `x **2 + y** 2 + x*y`. It is meant for symbolic computations, not numeric computations. You cannot apply it to an array directly. However, SymPy supports turning its expressions into numeric functions, as explained here:

[https://docs.sympy.org/latest/modules/numeric-computation.html#lambdify](https://docs.sympy.org/latest/modules/numeric-computation.html#lambdify)

You want something like this:

```auto
import numpy as np
import sympy
from sympy.abc import x
import matplotlib.pyplot as plt

f = x**2 + x + 1

X = np.linspace(-10, 10)
# Won't work -- f is not a function.
# Y = f(X)
# Works.
Y = sympy.lambdify(x, f, "numpy")(X)

plt.plot(X, Y)
plt.show()

```

---

<div class="post-metadata">

**Author:** ![basil106](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@basil106](https://discuss.python.org/u/basil106)\
**Post date:** [May 28, 2021, 12:24am UTC](https://discuss.python.org/t/plot-a-polynomial/8945/3 "2021-05-28T00:24:51Z")

</div>

Thanks a lot.  
I’ll study what you gave me and introduce it to my code.

---

<div class="post-metadata">

**Author:** ![basil106](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@basil106](https://discuss.python.org/u/basil106)\
**Post date:** [May 28, 2021, 12:34am UTC](https://discuss.python.org/t/plot-a-polynomial/8945/4 "2021-05-28T00:34:11Z")

</div>

I entered it exactly the way you gave it to me and it almost worked.  
It generated the y values from -10 to 10 but under the entry just before the 10.0, it gives me an error message  
SyntaxError: invalid syntax  
I placed a lot of print statements in the code to trace it and apparently it is the  
y = sympy.lambdify(x, f, “numpy”)(x)  
That is causing the error.  
In any case, as I said, it is very, very close to give me the plot

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [May 28, 2021, 3:47pm UTC](https://discuss.python.org/t/plot-a-polynomial/8945/5 "2021-05-28T15:47:21Z")

</div>

The traceback message should point you to the error. Could you paste it completely? I don’t see a syntax error in the line you quoted, so make sure to post the traceback literally (so the quotes for example are not transformed into curly quotes). In Discourse, here is the way to do this:

````auto
```
Three backticks above.
Your code or error message goes here.
Three backticks at the end.
```

````

---

<div class="post-metadata">

**Author:** ![basil106](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@basil106](https://discuss.python.org/u/basil106)\
**Post date:** [June 6, 2021, 11:47am UTC](https://discuss.python.org/t/plot-a-polynomial/8945/6 "2021-06-06T11:47:54Z")

</div>

your code works beautifully, thanks
