Matplotlib — Figure & Axes Explained in Detail
By: David Gladson
Tags: Python Programming, Data Science, Data Visualization, Matplotlib, Python

A quick recap of Day 2:

Summary plot of Day 2

Subplot definition

Fit multiple subplots using matrix technique

Exporting plot to PNG/JPEG

In case you missed the previous ones, find them here: day1, day2

Today’s topic is the most used one in Matplotlib, yet still a confusing one for many of us. It's about figure & axes, we’ll be covering the following:

What is figure & axes? How to use them for any kind of plot? How to use them especially for multiple subplots?

Figure: It is the topmost layer of the plot (kind of big-picture)

Figure constitutes of subplots, sub axis, titles, subtitles, legends, everything inside the plot but an overview.

Axes: It’s a part of the Figure, nothing but a subplot

Axes define a subplot, we can write our own x-axis limits, y-axis limits, their labels, the type of graph. It controls every detail inside the subplot.

Let’s get our hands dirty with the data:

#importing matplotlib to plot the graphs

import matplotlib.pyplot as plt #to avoid pop-ups & show graphs inline with the code

%matplotlib inline #pandas is required to read the input dataset

import pandas as pd

For this tutorial, we’ll be using Figure, Axes together using plt.subplots() function just because this is the most used way.

#subplot with 1 row & 2 cols

fig, ax = plt.subplots(1,2)

plt.subplots() has a default [0,1] x-y limits

We can rewrite the axis limits:

fig, ax = plt.subplots(1,2) x = [1,2,3,4]

y = [1,4,9,16] ax[0].plot(x,y)

plt.show()

plt.subplots() with defined axis limits on one side & default on the other side

From the previous article, we see that subplots were made very much easier using plt.subplot(xyz).

But why do we need Figure & Axes will they make our lives easier? YES, they do, let us see the difference between the two methods.

Method 1: The usual way

plt.figure(figsize = (10,6)) plt.subplot(121)

plt.text(0.5,0.5,’(1,2,1) Using plt.subplot’,ha = ‘center’, fontsize = 15) plt.subplot(122)

plt.text(0.5,0.5,’(1,2,2) Using plt.subplot’,ha = ‘center’, fontsize = 15) plt.show()

Plot using method 1

Method 2: Using figure & axes

fig, (ax1, ax2) = plt.subplots(1,2, figsize = (10,6)) ax1.text(0.5,0.5,’(1,2,1) Using Axes’,ha = ‘center’, fontsize = 15)

ax2.text(0.5,0.5,’(1,2,2) Using Axes’,ha = ‘center’, fontsize = 15) plt.show()

Or

fig, ax = plt.subplots(1,2, figsize = (10,6)) ax[0].text(0.5,0.5,’(1,2,1) Using Axes’,ha = ‘center’, fontsize = 15)

ax[1].text(0.5,0.5,’(1,2,2) Using Axes’,ha = ‘center’, fontsize = 15) plt.show()

plot using method 2 (using code1/code2)

That was simple, we can use ax1 & ax2 anywhere in the code while defining limits, labels, legends but for a conventional method this is not the case you need to define the plot details within each subplot.

Hence, Fig & Axes objects give us much comfort to deal with subplots & its details.

A small note: In case of plots with 2 rows or more axes should be called as matrices ax1, ax2, ax3, ax4= ax[0,0], ax[0,1], ax[1,0], ax[1,1]

fig, ax = plt.subplots(2,2, figsize = (10,6)) ax[0,0].text(0.5,0.5,’(2,2,1) Using Axes’,ha = ‘center’, fontsize = 15)

ax[1,1].text(0.5,0.5,’(2,2,4) Using Axes’,ha = ‘center’, fontsize = 15) plt.show()

That’s it for today! see you tomorrow with another fascinating topic in Matplotlib.

Sources: