To illustrate how plotting works, let's display some polynomial graphs. We will use the NumPy polynomial function poly1d()
to create a polynomial.
poly1d()
function to create a polynomial:func = np.poly1d(np.array([1, 2, 3, 4]).astype(float))
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)
y = func(x)
plot()
function; this does not immediately display the graph:plt.plot(x, y)
x
axis with the xlabel()
function:plt.xlabel('x')
y
axis with the ylabel()
function:plt.ylabel('y(x)')
show()
function to display the graph:plt.show()
The following is a plot with polynomial coefficients 1, 2, 3, and 4:
We displayed a polynomial graph on our screen. We added labels to the x
and y
axes (see polyplot.py
):
import numpy as np import matplotlib.pyplot as plt func = np.poly1d(np.array([1, 2, 3, 4]).astype(float)) x = np.linspace(-10, 10, 30) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y(x)') plt.show()