Hands-on with matplotlib

We will start with simple examples to explore matplotlib's functionality. The first thing we do usually is import matplotlib into our Python script:

import matplotlib.pyplot as plt

Notice we imported pyplot as a short name, plt, to be used inside our script. Now, we will use the plot() method inside it to plot our data, which consists of two lists. The first list represents the values of the x-axis while the second list represents the values of the y-axis:

plt.plot([0, 1, 2, 3, 4], [0, 10, 20, 30, 40])

Now, the values are dropped into the plot.

The last step is to show that plot as a window using the show() method:

plt.show()
You may need to install Python-tk in Ubuntu in order to view the graph. Use apt install ...

Get Hands-On Enterprise Automation with Python. 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.