subplot() (Matplotlib)
plt.subplot() adds a single Axes to the current Figure at a position in a grid, specified as (rows, columns, index). It is the older pyplot way to build multi-panel figures one panel at a time.
Matplotlib
plt.subplot(2, 1, 1) selects the first cell of a 2-row, 1-column grid as the current Axes, and a later plt.subplot(2, 1, 2) selects the second; the digits can even be combined as plt.subplot(211). Each call returns the Axes it activated, and subsequent pyplot commands draw into it.
Because it works through pyplot's 'current axes' state and creates panels incrementally, subplot pairs with the pyplot interface. For most code the newer plt.subplots() (plural) is preferred — it builds the whole grid at once and returns all the Axes as explicit objects, which is clearer and less error-prone.
Reach for subplot when you are quickly sketching a couple of stacked panels in pyplot style; reach for subplots when you want the full object-oriented grid up front.
Syntax
plt.subplot(nrows, ncols, index)Examples
Python
import matplotlib.pyplot as plt
plt.figure()
ax = plt.subplot(2, 1, 1)
print(type(ax).__name__)Output:
Axes