In this blog post, we gonna have an overview of what Matplotlib is. Essentially it is the most famous plotting framework for Python and it actually provides you a full control across almost all aspects of a plot or figure. And it’s created to have a pretty similar to Matlab which is a different programming language and it has it’s own graphical plotting abilities.
Matplotlib runs pretty well with Pandas and Numpy which is a library for matrix operation and math. And in another post, we are going to see some other libraries like Seaborn that are designed based on Matplotlib. But in order to know those libraries, it’s necessary to understand Matplotlib first.
Feel free to access the code on Google Colab.
First of all, you need first to install Matplotlib using conda if you are using anaconda or pip3 if you are using another version of python, like if you want to use a text editor such as Sublime Text.
conda install matplotlib or pip3 install matplotlib
So after you have installed Matplotlib, we gonna start our code by importing the Matplotlib subpackage pyplot as an alias of plt so we don’t write matplotlib.pyplot every time we need it:
import matplotlib.pyplot as plt
In my case, I use Jupyter notebook and if you are the same, make sure to add this command %matplotlib inline because it allows you to see the plots inside this environment (Jupyter notebook):
But if you are using Sublime text editor or Pycharm or any text editor, consider putting plt.show() at the last of your code to see the plots:
4.Creating a Dataset And Start Plotting
In order to use Matplotlib, we need first to get data to work with, we are going to use Numpy which is a library that allows you to generate data so we can plot it later on using Matplolib. Make sure to install it first by using one of this command according to your environment:
conda install numpy or pip3 install numpy
Then make sure to import it as an alias of np and run the cell:
import numpy as np
Our dataset will be 11 random numbers ranging from 0 to 5. To do so, we use the linspace function that exists inside numpy, this will be our X values and for Y values it will be the power of 2 to the X values:
X = np.linspace(0, 5, 11) # create X values y = x ** 2 # create Y values
Now, there are two methods to create a plot, the first one is the functional method and the second is the object-oriented method.
4.1.The Functional Method
To do a simple plot in Matplotlib we use the function plt.plot() with two-argument (X and y values):
plt.plot(X, y) # create a simple plot
As you see from the graph above that is is so simple and we can add some information to it like:
- x_label: we use the plt.xlabel() function
- y_label: we use the plt.ylabel() function
- Title: we use the plt.title() function
make sure to run the plt.plot() command alongside these three arguments in the same cell, otherwise, it won’t work.
# Functional Method plt.plot(x, y) # add plot plt.xlabel('X Axis Title Here') # add X label plt.ylabel('Y Axis Title Here') # add Y label plt.title('String Title Here') # add Title
We can create multiple plots on the same canvas, to do that we use the plt.subplot() function and it takes a number of arguments such as:
- nrows: The number of rows in the canvas
- ncols: The number of columns in the canvas
- index: The index of every plot in the canvas
To make everything clear, we are going to change the color of every plot by adding a third argument to the plt.plot() function which is the color. Feel free to see the official documentation about the colors and what every character means. For example, “r” refers to the red color.
# plt.subplot(nrows, ncols, plot_number) plt.subplot(1,2,1) plt.plot(x, y, 'r') plt.subplot(1,2,2) plt.plot(y, x, 'g')
4.2.The Object-Oriented Method
Now, let’s jump to the other method of creating plots which are the Object-Oriented Method. The main idea for this process is by creating figure objects and then call methods off of this, and then add a set of axes to this object. See the code below:
# Object Oriented Method fig = plt.figure() # create an object axes = fig.add_axes() # add axes
As you see above, the that we can add many axes is by adding a list to the fig.add_axes() function which is: left, bottom, width, height argument ranging from 0 to 1, in other words, a percentage of that blank canvas that you want to take or reserve, feel free to play around with these numbers. Next, we add a plot using axes.plot() function, as you see it looks the same as earlier but we have more control over our plots, you see that in seconds! We can add some information to our plot which are:
- x_label: we use the axes.set_xlabel() function
- y_label: we use the axes.set_ylabel() function
- Title: we use the axes.set_title() function
# Object Oriented Method fig = plt.figure() # create an object axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # add axes axes.plot(x, y) # plotting axes.set_xlabel("X Axis Title Here") # add X label axes.set_ylabel("Y Axis Title Here") # add Ylabel axes.set_title("String Title Here") # add a title
You know what! let’s continue with this idea Object-Oriented programming and add two figures on the same canvas, See the code below:
fig = plt.figure() # create an object axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # add axes first figure axes1.plot(x, y) # add plot to the first axes axes1.set_title("Larger Plot") # add a title to the first figure axes2 = fig.add_axes([0.2, 0.5, 0.4, 0.3]) # add axes second figure axes2.plot(y, x) # add plot to the second axes axes2.set_title("Smaller Plot") # add a title to the second figure
The next thing to do is showing you how to create subplots withing the same canvas using the object-oriented method. See the code below:
fig, axes = plt.subplots(nrows=1, ncols=2) axes.plot(x, y) # index the first plot axes.set_title("The First Plot") # set title for the first plot axes.plot(y, x) # index the second plot axes.set_title("The Second Plot") # set title for the second plot plt.tight_layout() # remove overlapping
As you see the code above, we use plt.subplots() to create multiple plots on the same canvas and it takes two arguments:
- nrows: which is the number of rows
- ncols: which is the number of columns
And we can index each plot individually as you see axes and axes and change them like adding a title or plot some graphs on them. The last command plt.tight_layout() is recommended to use it at the last of the code so it removes any overlapping might occurs when creating many plots on the same canvas.
We can also control the size of plots by adding figsize=() argument to the plt.subplots():
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(8, 2)) axes.plot(x, y) # index the first plot axes.plot(y, x) # index the second plot plt.tight_layout() # remove overlapping
You can also save the figure as a png or jpg format using the fig.savefig() function and it takes two arguments:
- fname: which is the name of the picture
- dpi: which is the dots per inch (Quality)
fig.savefig(fname="picture.png", dpi=200) # save the figure
Object-Oriented is the method that we gonna use from now to create plots using Matplotlib because it gives us more control over plotting and creating a nice visualization. Hopefully, you have a basic understanding of what that line of codes mean, and I will see you at the next post. “Happy Coding ❤️”
Note: This is a guest post, and the opinion in this article is of the guest writer. If you have any issues with any of the articles posted at www.pythonlearning.org please contact at firstname.lastname@example.org