subplots() (Matplotlib)
plt.subplots() creates a Figure and a grid of Axes in one call, returning both. It is the standard, recommended way to start a Matplotlib plot, especially when you want several panels arranged in rows and columns.
Matplotlib
The call fig, ax = plt.subplots() makes a Figure with a single Axes. Passing dimensions, plt.subplots(2, 3), makes a 2-by-3 grid and returns a NumPy array of Axes you index like axes[0, 1]. This is the cleanest entry point into the object-oriented interface.
Useful keywords include figsize to size the whole Figure, sharex/sharey to make panels share their scales (great for comparison), and constrained_layout=True to auto-space the panels so labels don't overlap. When you only need one panel, subplots() is still the idiomatic starting point.
It differs from the older plt.subplot() (singular), which adds one Axes at a time to the current Figure; subplots() builds the whole grid at once and hands you explicit objects to work with.
Syntax
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=None, sharex=False)Examples
Python — A 2x2 grid returns a 2x2 array of Axes.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2)
print(axes.shape)Output:
(2, 2)