Python Matplotlib - How to plot and graph the quadratic function

Python Matplotlib plots and draw a graph. This article shows how to graph a quadratic function. Importing NumPy and Matplotlib is required.

import numpy
from matplotlib import pyplot

x = numpy.arange(-5, 5, 0.1)
y = x * x

pyplot.plot(x, y)
pyplot.savefig('plot.png')

First, set the domain (x). Second, get the range (y). In the above, x ranges from -5 to 5 and the step is 0.1, that is x = [-5, -4.9, -4.8, ..., 5].

pyplot module in matplotlib has plot method plotting (x, y) and savefig saves the graph.

Matplotlib Plot

Matplotlib Tutorial