Matplotlib Tutorial
Learn Matplotlib by Examples.
Instructions are clear, concise and effective
Theory, Principles and Practice
Matplotlib is a Multiplatform visualization library for data that is built on NumPy Arrays. This Library is designed to work with the broader SciPy stack, which includes different modules of Python used for machine learning and data science.
Matplotlib is the default (sort of) Python Data Visualization Package and is being used extensively in the market for creating plots, charts, graphs for datasets for better data analysis and visualization.
In this tutorial of Matplotlib, we will start with the basics of Matplotlib and will cover all the different types of Plots available in Matplotlib.
-------------------------------------------------
TAB W. KEITH
Copyright 2021 by Su Tran. All Right Reserved.
Table of Contents
Matplotlib Basics
Introduction to Matplotlib
In this tutorial, we will cover the basic introduction of the Matplotlib library in Python , important modules within Matplotlib, how to install Matplotlib module and we will understand how this library is useful for data visualization .
What is Matplotlib?
Matplotlib is basically a Multiplatform visualization library for data that is built on NumPy Arrays . This Library is designed to work with the broader SciPy stack , which includes different modules of Python used for machine learning and data science.
- Matplotlib is the default(sort of) Python Data Visualization Package .
- As Matplotlib is a visualization library, these visualizations allow us to represent huge amount of data in visual form like charts and plots .
- Matplotlib is useful in creating 2D plots from the data in Arrays .
- Matplotlib library is inspired by MATLAB programming language and it also provides a similar interface like MATLAB for graphics .
- This library gets easily integrated with the Pandas package which is used for data manipulation .
- With the combination of Pandas and Matplotlib data wrangling can be done along with the visualization and one can get valuable insights out of the data .
- Matplotlib library in Python is mainly known as Grammer of Graphics and it is the most used library for creating charts and plots in Python.
- Matplotlib can be used with Jupyter Notebook , web server Applications , and IPython shells .
It was written by John D. Hunter in 2003 and it was born with its 0.1 version. Matplotlib received an early boost at the time when it was adopted as the plotting package of choice by the Space Telescope Science Institute .
The current stable version is 2.2.0 and it was released in 2018.
Important Modules in Matplotlib
There are two important modules in Matplotlib Library and these are given below:
1. pyplot
- It is an important module in Matplotlib. In our further tutorials in code examples , you will often see matplotlib.pyplot being used.
- This module mainly provides us an interface that allows us to automatically and implicitly create figures and their axes to achieve the desired plot .
- This is a great module when you quickly want to plot something without the instantiation of any figure or any axes .
2. pylab
- It is another important module of Matplotlib.
- You need to install this module alongside the matplotlib module.
- The Module pylab helps to import NumPy and pyplot and it is recommended when you need to work with arrays, perform mathematical operations , and you want to access the plotting features .
- It is not recommended in case you are using IPython Kernel.
Installing Matplotlib
The dependent packages of Matplotlib and the matplotlib itself are available in the standard Python package repositories in the form of a wheel package. Thus it can be easily installed on MacOS, Window, Linux, etc. using the pip package manager .
Note: You must have Python installed on your machine, to install matplotlib module. Here is our Python Installation Guide.
You just need to open your command prompt write the following given command:
python -m pip install -U matplotlib
If you are using Jupyter Notebook, then it is important to note that Jupyter Notebook comes with many preinstalled libraries like Numpy, Pandas, Matplotlib, Scikit-Learn, so you don't have to worry about installing these modules/libraries separately.
General Concepts in Matplotlib
In this tutorial, we will cover some general concepts in Matplotlib and some tips for you to remember, with the help of which visualization becomes easy .
As we know that Matplotlib is a powerful library which is used to visualize data in form of plots. You will see in our tutorial that with the help of this library one can create many types of plots like line, bar, histogram, contour, scatter, violin, and many more . It is used for creating 2D plots of Arrays .
Here are some general concepts which you must understand first before moving on to the further topics. There are many parts of a figure created using matplotlib and here we are going to discuss those parts, which are:
First of all, we have used the word "figure"; let us understand what is a figure in Matplotlib?
1. Figure:
Figure is the canvas on which different plots are created. The Matplotlib Figure is a canvas that contains one or more than one axes/plots.
2. Axis:
Axis in a Matplotlib figure is used for generating the graph limit and it is basically like a number line . There can be an X axis, Y axis and Z axis for plots upto 3 dimensional.
3. Axes:
Axes is generally considered as a plot . In the case of 3D , there are three-axis objects but there are many axes in a figure.
4. Artist
Everything which can be seen on a figure is an Artist . Most of the artists are tied with the axes . These are like text objects , collection objects and Line2D objects , and many more.
Before diving into creating visualizations with the help of matplotlib; Let us discuss a few things related to the Matplotlib Library:
1. Importing the Matplotlib Library
We will use some standard shorthand while importing the Matplotlib in our code:
import matplotlib as mpl
import matplotlib.pyplot as plt
The plt interface mentioned above is used often in our further tutorials.
2. Setting Styles
So, we will use the plt.style directive to choose appropriate aesthetic styles for our figures.
Now, let us set the classic style , which ensures that the plots we create use the classic Matplotlib style:
plt.style.use('classic')
3. Displaying the Plots
How the plots are displayed, depends on how you are using the Matplotlib library.
There are three applicable contexts using which you can create plots using Matplotlib: In a script , an IPython Notebook , or an IPython Shell .
Plotting from a script:
If you are using matplotlib from within a script, then plt.show() function is your good friend. This function starts an event loop , then looks for all currently active figure objects , and opens one or more interactive windows that display your figure or figures .