Computer scienceData scienceInstrumentsVisualizationMatplotlib overview

Introduction to matplotlib

13 minutes read

Anyone who has worked with data knows how much it depends on the way you present it. One illustrative figure is worth a thousand words. We have good news for you — Python has a wonderful tool for visualization, it's called the matplotlib library. We are going to cover the outlines of this library, but even the basics alone will take you to another level in your day-to-day work.

The matplotlib library contains several modules. For example, the matplotlib.table module includes tools for constructing tables, and the matplotlib.colorbar module allows you to visualize a set of scalar values in color. Today, we will focus on one module, matplotlib.pyplot. It is designed to create colorful plots.

Main objects

First, we need to import the library and numpy for numerical examples.

import matplotlib.pyplot as plt
import numpy as np

We will deal mostly with figures and axes. Let's create a toy example to see what we can do.

fig, ax = plt.subplots()  # this creates a figure paired with axes

Matplotlib employs a hierarchical structure, with a Figure object on top. A Figure is a container that consists of many elements, including axes. A Figure can change the graph size, manage titles, and figure legends among other things. An Axes is an object that takes your data and visualizes it on the plot; one figure can have several axes that correspond to different subgraphs. There is another object, Axis, which generates the scales and the ticks. In the case of 2D plots there are 2 Axis objects in the Axes. Here is the illustration for the distinction between Figure, an Axes, and an Axis:

Matplotlib object hierarchy: Figure, Axes, Axis

It is always a good idea to create figures and axes explicitly, as we have done above. The names fig and ax are conventional, but you can use whichever you like.

Now let's create our first graph:

x = np.linspace(0, 10, 200)  # x data
y = np.sin(x) + x            # y data
ax.plot(x, y)                # creates the plot
fig.show()                   # this will show your plot in case your environment is not interactive

This produces the following plot:

Create a line plot with matplotlib

We will discuss what we have done in detail later.

Another way

There is also another way to create a figure in matplotlib:

plt.figure()

This code looks a bit simpler, yes, but that's the only positive side. Numerous times, you need to draw several plots in succession. Matplotlib employs an "active figure" concept. Suppose, we need to implement plt.figure(num) in our program. In this case, it creates a new figure, assigns it with a num number (if you have no figures with that number), and makes it active. If we want to go back to one of the previous charts, we must refer to the specific figure instead of creating a new one. To do so, we need to track its number — say, 5 — and activate the desired figure with the plt.figure(5) command. This is not very convenient.

Besides, the plt.subplots() function is crucial when you want to create a plot consisting of several parts; we will see later how to do this.

Plot settings

Now let's customize our plot and learn some basic settings.

The figsize argument sets the figure length and width:

fig, ax = plt.subplots(figsize=(15,10))

The suptitle method sets the figure's title.

fig.suptitle('Our beautiful plot', fontsize=40)

You may pass any text keyword arguments (like we did with the fontsize above) to customize it.

We can also set line color, style, and width. You can find the full list of named colors in the official documentation. The documentation also describes ways to change the line appearance (the Linestyles paragraph). Additional width will make our line a bit thicker for a better visual appearance.

color = 'y'
linestyle = 'dotted'
linewidth = 4

Finally, that's our plot:

ax.plot(x, y, c=color, linestyle=linestyle, linewidth=linewidth)

Note that the plot parameters are specified as keywords. This is the visual representation of our plot:

Add a centered title to a line plot with suptitle

Looks good, but we could make it even better.

Axes settings

We will get a glance at some Axes class properties. The official documentation will tell you more if you're interested.

First, we need to name our x and y axes. You can pass any keyword arguments:

ax.set_xlabel('argument', fontsize = 30)
ax.set_ylabel('function', fontsize = 30)

The Axis object (Axes contains Axis, among others) has two child objects: xaxis and yaxis. Each of them has its ticks with bound labels — in our case, the numbers under the axis line. We can subsequently change their size:

ax.tick_params(axis='both', which='major', labelsize=18)

Our new graph is shown in the picture below:

Setting the font sizes of objects inside the Axes

Looks nice, isn't it?

Note that to change some properties of an axis, we need to refer to the attributes of the specific Axes object. This prevents confusion and allows us to control the properties of every next subplot.

It is a good practice to close the figure windows when we don't need them; it keeps the code clean and saves a lot of memory. To do this, we can use the plt.close(fig) command. Use theplt.close() command to close all open figure windows. If you need to delete a particular figure including associated axes with data, use fig.clf(). It deletes the figure content but leaves the figure window open.

Multiple subplots

The plt.subplots() method is the best way to handle several subplots at once. Let's create a plot in two parts:

fig, axes = plt.subplots(1, 2, figsize=(16,8))
ax1, ax2 = axes

As we have seen above, plt.subplots() creates a single pair of figure and axes objects when no arguments are given. When you specify the rows and columns — here 1 is the number of rows and 2 is the number of columns — this function returns a single figure (remember, it's a scalable frame) and an array of an Axes object. To refer to a single axis, use the standard array indexing.

Now let's add the data:

x = np.linspace(0,10,200)
y1 = np.sin(x)
y2 = np.cos(x)

Each y array will stand for the respective subplot. The x axis remains for both plots.

We may also want to specify axes labels individually for each of the subplots:

ax1.set_ylabel('sin(x)', fontsize=30)
ax1.set_xlabel('x', fontsize=30)
ax2.set_ylabel('cos(x)', fontsize=30)
ax2.set_xlabel('x', fontsize=30)

We can also use a trick that will provide some space between subplots to make them neat. You can try to disable this line and see what happens:

fig.tight_layout(pad=2)

Finally, let's create our plot. Take a look at how we set the line width and color:

ax1.plot(x, y1, c='r', linewidth=4)
ax2.plot(x, y2, c='g', linewidth=4)

That's how the result looks like:

Make subplots with matplotlib

We can control the settings of each axis separately by addressing its attributes!

Conclusion

We have covered the basic features of the matplotlib library and learned how to manage some of their properties. Furthermore, we recommend that you study the documentation — the library contains a good number of different settings. In the following topics, we will look at other types of figures that occur in real-world problems.

Read more on this topic in SQL and Python: applying programming languages on Hyperskill Blog.

47 learners liked this piece of theory. 4 didn't like it. What about you?
Report a typo