Essentials of Data Visualization with Python Matplotlib (I): Basic Concepts and Process

Data Visualization with Matplotlib is very convenient

From this article, I will start to talk about plotting with Python Matplotlib library with the following three parts:

Part I: Basic Concepts and Process
Part II: The MATLAB-style Interface
Part III: The Object-oriented Interface

1. Python Plotting Libraries

There are many data visualization libraries in Python, but here I only list some well-know ones as follows.

  • Matplotlib: a popular and comprehensive open source library to create static, animated, and interactive visualizations in Python.
  • Seaborn:a Python data visualization library based on Matplotlib. It provides a high-level interface for creating attractive and informative statistical graphics.
  • Bokeh: a Python library for creating interactive visualizations for modern web browsers. With Bokeh, you can create JavaScript-powered visualizations without writing any JavaScript.
  • Altair: a declarative statistical visualization library for Python, built on top of the powerful Vega-Lite visualization grammar, which enables you to produce beautiful and effective visualizations with a minimal amount of code.
  • Plotly:an interactive, open-source, and browser-based graphing library for Python, which is a more sophisticated data visualization tool more suited to efficiently create elaborate plots
  • GGplot: It allows you to create data visualizations using a grammar of graphics, a high-level tool letting you create data plots in an efficient and consistent way.
  • Pyecharts: a Python Echarts Plotting Library, Echarts is an easy-to-use, highly interactive and highly performant JavaScript visualization library under Apache license.

2. Set up Environment

In these article, I will use Jupyter notebook to code and display you the results. You can use JupyterLab and any Python IDE.

(1) Installation of Matplotlib Libraries

Open your Command-line Shell, and run the following commands:

using pip

pip install matplotlib

Besides, we can also use %pip magic or !pip to install it inside your Jupyter notebook, which we have already learned.
using %pip

%pip install matplotlib

using !pip

!pip install matplotlib

(2) Check the Version

(i) Only check the version

Run the following code in terminal or inside the code cell of a Jupyter notebook to check the versions and information of the installed libraries, by which we can also check if the libraries have been successfully installed.

import matplotlib as mpl
print(mpl.__version__) # Double Underscore
3.4.3

(ii) check detailed information

We have learned that %pip show packageName magic or !pip show packageName can be used to check package information in Chapter 6. For example, let’s use the magic to check the information of pandas and Matplotlib packages as follows:

%pip show matplotlib
Name: matplotlib
Version: 3.4.3
Summary: Python plotting package
Home-page: https://matplotlib.org
Author: John D. Hunter, Michael Droettboom
Author-email: matplotlib-users@python.org
License: PSF
Location: c:userssigmundappdatalocalprogramspythonpython39libsite-packages
Requires: cycler, kiwisolver, numpy, pillow, pyparsing, python-dateutil
Required-by: jupyterthemes
Note: you may need to restart the kernel to use updated packages.

(3) Create working directory

For example, let’s create two folders in the working directory. You can use any names for the two folders, but I use “data” and “plots” , which will be used to store the data or plots in the next two parts.

3. Matplotlib Plotting Interfaces

Matplotlib offers dual interfaces to do the similar thing, a simple convenient Matlab-style interface, and a more powerful object-oriented interface.

(1) MATLAB-style Interface

The original purpose of writing Matplotlib was to create a Python alternative library for Matlab. The Matlab-style Interface tools, contained in matplotlib.pyplot module.

(2) Object-oriented interface

The object-oriented interface is suitable for more complicated situations, which allows you control more over the figures through two core objects, i.e. fig and axes. This interface uses pyplot.subplots to create a figure and a grid of subplots with a single call.

This bi-interface feature is also the most confusing thing of Matplotlib to most users. We will highlight the differences between these two interface with concrete examples.

4. Basic Plots Process with Matplotlib

Here, I generally divide the process of a basic plot into the following five steps:

Step 1: Import required package
Step 2: Prepare required data
Step 3: Plot required diagram
Step 4: Add necessary elements
Step 5: Save the diagram

5. Online Course

If you are interested in learning Python data analysis in details, you are welcome to enroll one of my courses:

Master Python Data Analysis and Modelling Essentials

Bookmark
ClosePlease login
0 - 0

Thank You For Your Vote!

Sorry You have Already Voted!

Please follow and like me:

Leave a Reply

Your email address will not be published. Required fields are marked *