Open In Colab

Subplots

So far, we have only ever printed one individual chart. Often, however, it is necessary to plot two or more graphs at the same time (for comparisons, etc.). This can be done by defining so called subplots within a plot. Each new subplot is initialized by the plt.subplot() operator. This operator takes three basic parameters:

  • nrows: Number of rows we want our subplot to have
  • ncols: number of columns we want our sublot to have
  • index: specifies the position of the plot. For 1 as the index the position is top left, and it increases from left to right
In [ ]:
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
In [ ]:
#subplot 1
plt.subplot(1, 2, 1)
linear_data = [1,2,3,4,5,6,7,8]
plt.plot(linear_data, '-o')
plt.title("Linear Data")
exponential_data = [ x**2 for x in linear_data ]

#subplot 2
plt.subplot(1, 2, 2)
plt.plot(exponential_data, '-o')
plt.title("Exponential Data")
Out[ ]:
Text(0.5, 1.0, 'Exponential Data')

If we want to plot both graphs above each other, we just need to add a second row plt.subplot(2, 2, 1) and specify the index of the second subplot to be 3

In [ ]:
#subplot 1
plt.subplot(2, 2, 1)
linear_data = [1,2,3,4,5,6,7,8]
plt.plot(linear_data, '-o')


#subplot 2
plt.subplot(2, 2, 3)
plt.plot(exponential_data, '-o')
Out[ ]:
[<matplotlib.lines.Line2D at 0x7faaa7307990>]

Subplots with shared axes

In the example, both plots had different axes. The linear data ranges form 0-8 while the exponential data ranges from 0-70. However, visualizing two graphs with different axes next to each other may lead to confusion, which is why we need a way to assign the same axis to both of them.

To do so, we simply assign our plt.subplot() operators to variables ax1 & ax2. We can then use the axis of the first subplot within the second subplot using the sharey parameter.

In [ ]:
linear_data = [1,2,3,4,5,6,7,8]
exponential_data = [ x**2 for x in linear_data ]

#create first subplot and assign it to ax1
ax1 = plt.subplot(1, 2, 1)
plt.plot(linear_data, '-o')

# pass sharey=ax1 to ensure the two subplots share the same y axis
ax2 = plt.subplot(1, 2, 2, sharey=ax1)
plt.plot(exponential_data, '-x')
Out[ ]:
[<matplotlib.lines.Line2D at 0x7faaa7246450>]

Create 3x3 Plot

Okay, let's see how we can combine our knowledge of subplots to create a 3X3 matrix of individual plots.

As we don't want to write plt.subplot() for every single of the subplot we can specify a subplot once, and assign the returned axes to variables. In our case we specify that our subplot should have 3 rows and 3 columns and should both share the y- as well as the x-axis.

In [ ]:
#in order to understand the next code snippet it might be useful to have a look at the documentation
# We can see that plt.subplot returns the axes of the subplot which are specified as fig, (axes)
plt.subplot?
In [ ]:
linear_data = [1,2,3,4,5,6,7,8]
# create a 3x3 grid of subplots
fig, ((ax1,ax2,ax3), (ax4,ax5,ax6), (ax7,ax8,ax9)) = plt.subplots(3,3, sharex=True, sharey=True)
# plot the linear_data on the 5th subplot axes
ax5.plot(linear_data, '-')
Out[ ]:
[<matplotlib.lines.Line2D at 0x7faaa7021290>]
In [ ]:
# Task: Use this code-block to create a 4x4 subplot in which the diagonals are filled with a line