How to Plot Charts in Python with Matplotlib

Originally published at: https://www.sitepoint.com/plot-charts-python-matplotlib/

You generate a huge amount of data on a daily basis. A critical part of data analysis is visualization. A variety of graphing tools have developed over the past few years. Given the popularity of Python as a language for data analysis, this tutorial focuses on creating graphs using a popular Python library — Matplotlib.

Matplotlib is a huge library, which can be a bit overwhelming for a beginner — even if one is fairly comfortable with Python. While it is easy to generate a plot using a few lines of code, it may be difficult to comprehend what actually goes on in the back-end of this library. This tutorial explains the core concepts of Matplotlib so that one can explore its full potential.

Let's get started!

Prerequisites

The library that we will use in this tutorial to create graphs is Python's matplotlib. This post assumes you are using version 3.0.3. To install it, run the following pip command in the terminal.

pip install matplotlib==3.0.3

To verify the version of the library that you have installed, run the following commands in the Python interpreter.

>>> import matplotlib
>>> print(matplotlib.__version__)
'3.0.3'

If you are using Jupyter notebooks, you can display Matplotlib graphs inline using the following magic command.

%matplotlib inline

Pyplot and Pylab: A Note

During the initial phases of its development, Mathworks' MATLAB influenced John Hunter, the creator of Matplotlib. There is one key difference between the use of commands in MATLAB and Python. In MATLAB, all functions are available at the top level. Essentially, if you imported everthing from matplotlib.pylab, functions such as plot() would be available to use.

This feature was convenient for those who were accustomed to MATLAB. In Python, though, this could potentially create a conflict with other functions.

Therefore, it is a good practice to use the pyplot source.

from matplotlib import pyplot as plt

All functions such as plot() are available within pyplot. You can use the same plot() function using plt.plot() after the import earlier.

Dissecting a Matplotlib Plot

The Matplotlib documentation describes the anatomy of a plot, which is essential in building an understanding of various features of the library.

Source

The major parts of a Matplotlib plot are as follows:

  • Figure: The container of the full plot and its parts
  • Title: The title of the plot
  • Axes: The X and Y axis (some plots may have a third axis too!)
  • Legend: Contains the labels of each plot

Each element of a plot can be manipulated in Matplotlib's, as we will see later.

Without further delay, let's create our first plot!

Create a Plot

Creating a plot is not a difficult task. First, import the pyplot module. Although there is no convention, it is generally imported as a shorter form &mdash plt. Use the .plot() method and provide a list of numbers to create a plot. Then, use the .show() method to display the plot.

from matplotlib import pyplot as plt
plt.plot([0,1,2,3,4])
plt.show()

Notice that Matplotlib creates a line plot by default. The numbers provided to the .plot() method are interpreted as the y-values to create the plot. Here is the documentation of the .plot() method for you to further explore.

Now that you have successfully created your first plot, let us explore various ways to customize your plots in Matplotlib.

Customize Plot

Let us discuss the most popular customizations in your Matplotlib plot. Each of the options discussed here are methods of pyplot that you can invoke to set the parameters.

1 Like