Time for action – plotting a polynomial function

To illustrate how plotting works, let's display some polynomial graphs. We will use the NumPy polynomial function poly1d() to create a polynomial.

  1. Take the standard input values as polynomial coefficients. Use the NumPy poly1d() function to create a polynomial:
    func = np.poly1d(np.array([1, 2, 3, 4]).astype(float))
  2. Create the x values with the NumPy the linspace() function. Use the range -10 to 10 and create 30 even spaced values:
    x = np.linspace(-10, 10, 30)
  3. Calculate the polynomial values using the polynomial we created in the first step:
    y = func(x)
  4. Call the plot() function; this does not immediately display the graph:
    plt.plot(x, y)
  5. Add a label to the x axis with the xlabel() function:
    plt.xlabel('x') ...

Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.