New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

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.

Matplotlib plot components - Figure, Axis, Axes and Artist

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.

Note: The plt.show() command should be used only once per Python session and is most often seen at the very end of the script.

You cannot use multiple show() commands because if you will then it will lead to unpredictable backend-dependent behavior. Thus it should mostly be avoided.

Let us take an example of a file named myplt.py and the code inside the same is given below:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()

Run the above script in the command-line prompt, with the following command: $ python myplot.py and it will result in a window opening with your figure displayed.

Plotting from an IPython Shell

IPython is built to work well with Matplotlib if you specify the Matplotlib mode. If you want to enable this mode, you can use the %matplotlib magic command just after starting ipython:

%matplotlib

When you will run the above command, it will give:


Using matplotlib backend: Qt5Agg

After the above command, write the following code:

import matplotlib.pyplot as plt

Now any plt plot command will open a figure window, and then you can further use commands that can be run to update the plot.

Some changes (like modifying properties of lines that are already drawn) will not draw automatically; If you want to force an update then use plt.draw(). Using plt.show() in Matplotlib mode is not required.

Plotting from an IPython Notebook

Now IPython Notebook is basically a browser-based interactive data analysis tool that is used to combine narrative, code, graphics, HTML elements, and much more things into a single executable document.

If you want to plot interactively within an IPython notebook then use the %matplotlib command and it will work in a similar way to the IPython shell. In the IPython notebook, you also have the option of embedding graphics directly in the notebook, with two possible options:

  1. The command %matplotlib in the notebook will lead to interactive plots embedded within the notebook.

  2. The command %matplotlib inline will help to embed the static images of your plot in the notebook

First of all, run the command given below:

%matplotlib inline

After you run this command (it needs to be done only once per kernel/session), any cell within the notebook that creates a plot will embed a PNG image of the resulting graphic.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
fig = plt.figure()
plt.plot(x, np.sin(x), '-')
plt.plot(x, np.cos(x), '--')


Matplotlib sample plot

Summary:

So in this tutorial, we tried to cover the basics of Matplotlib module, components of the plot generated using Matplotlib and how to use Matplotlib covering 3 different ways to do so.