Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Poly1d with Matplotlib

I'm trying to plot a function which is an object of numpy.poly1d. In my case it is y = -x^2 + 7x -7. So now I'm trying to plot it like a nice parabola, however when I plot it, it looks like this:

1

So I wondered if anybody could tell me how to make this line smooth.

This is my code:

t = np.poly1d([-1, 7, -7])

plt.plot(t)
plt.show()
like image 592
Steven Avatar asked Jan 10 '16 15:01

Steven


People also ask

What is poly1d in Python?

poly1d(c_or_r, r=False, variable=None)[source] A one-dimensional polynomial class. Note. This forms part of the old polynomial API. Since version 1.4, the new polynomial API defined in numpy.

How do you use the poly function in Python?

poly() function in the Sequence of roots of the polynomial returns the coefficient of the polynomial. Parameters : Seq : sequence of roots of the polynomial roots, or a matrix of roots. Return: 1D array having coefficients of the polynomial from the highest degree to the lowest one.


1 Answers

np.poly1d() creates a polynomial. If you plot that, you only get its coefficient values, of which you have 3. So effectively you plot the values -1, 7 and -7.

You want to pass some x values to your polynomial to get the corresponding y values.

p = np.poly1d([-1, 7, -7])
x = np.arange(20)
y = p(x)
plt.plot(x, y)
plt.show()
like image 126
Reti43 Avatar answered Sep 27 '22 21:09

Reti43