Member-only story
Matplotlib Tutorial: How to Get Started With Matplotlib
Hi everyone, welcome back. Matplotlib is a library for the Python programming language and is commonly used for data science and data visualization purposes. Matplotlib can be thought of a library that provides graph plotting functions that gives us visualization support. Let’s go over how you can get started with Matplotlib.
Installation
In order to work with Matplotlib, we will need to have Python and PIP installed onto our machines. Python can be downloaded and installed here and PIP can be downloaded and installed here. Once we have both Python and PIP installed, we can then install Matplotlib through the command line. Open up a command prompt or terminal and enter:
pip install matplotlibAlternatively, Matplotlib can be downloaded and used through a Python distribution such as Anaconda or Spyder.
Importing
Now we want to import Matplotlib into our project to use. We can do this the same way we handle other imports by adding this line to our Python file:
import matplotlibWe can also import individual modules of Matplotlib into our project, for instance if we only want to import the Pyplot module, we can do so by adding this line to our file:
from matplotlib import pyplotNumPy
NumPy is another library for Python that is commonly used for data science or statistical purposes. NumPy is short for “Numerical Python” and is used specifically to work with arrays and provides various functions regarding them. We will use NumPy arrays to store our plot points and use Pyplot to generate graphs based off of them. More about NumPy can read here.
Pyplot Basics
Let’s take a look at what Matplotlib can do. We’ll look at some of the basic functionality that Pyplot is capable of. Pyplot provides us with plotting and graph generating capabilities. Let’s see an example:
from matplotlib import pyplot
import numpy as np
x = np.array([0, 10])
y = np.array([0, 10])
pyplot.plot(x, y)
pyplot.show()Output:
