In [1]:
import matplotlib.pyplot as plt
%matplotlib inline

import numpy as np

1. Creating axes by using plt.subplots()

In [2]:
fig, axes = plt.subplots(nrows=3, ncols=2)

# You can call fig.tight_layout() for more good looking figure.
# fig.tight_layout()
In [3]:
print(type(fig))
<class 'matplotlib.figure.Figure'>
In [4]:
print(type(axes))
<class 'numpy.ndarray'>
In [5]:
print(type(axes[0, 0]))
<class 'matplotlib.axes._subplots.AxesSubplot'>

Simple Plot

In [6]:
x = np.linspace(0, 10, 100)
y = x ** 2

fig, ax1 = plt.subplots(1,1)
ax1.plot(x, y)
Out[6]:
[<matplotlib.lines.Line2D at 0x2242057ac88>]

2. Creating axes by using fig.add_subplot()

In [7]:
x = np.linspace(0, 10, 100)
y = x ** 2

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1) # this is the same as fig.add_subplot(111)
ax1.plot(x, y)
Out[7]:
[<matplotlib.lines.Line2D at 0x224205af2c8>]

3. Creating axes by using fig.add_axes()

In [8]:
x = np.linspace(0, 10, 100)
y = x ** 2
y2 = np.sqrt(x)

fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # [left, bottom, width, height]
ax1.plot(x, y)

ax2 = fig.add_axes([0.2, 0.5, 0.4, 0.3])
ax2.plot(x, y2)
Out[8]:
[<matplotlib.lines.Line2D at 0x22420684f88>]