For several charts in one figure, start with fig, axs = plt.subplots(rows, columns), then plot on each Axes. Use shared axes when panels should use the same scale, and choose GridSpec or subplot_mosaic when a regular grid is not enough.
How to create multiple plots in one Matplotlib figure
A Matplotlib Figure is the container for the chart; each Axes inside it holds a plot, its labels, title, and annotations. plt.subplots creates both the Figure and a regular grid of Axes in one call. See Matplotlib’s guide to Axes and subplots.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2, figsize=(8, 6), layout="constrained")
axs[0, 0].plot(x, y1)
axs[0, 1].scatter(x, y2)
axs[1, 0].bar(categories, values)
axs[1, 1].hist(samples)
fig.suptitle("Four related views")
plt.show()
Replace x, y1, y2, categories, values, and samples with your data. In a two-by-two grid, axs[row, column] selects a panel: row and column indexes start at zero. Each Axes can use a different plotting method, as in this example. The figsize value sets the overall Figure size in inches; layout="constrained" asks Matplotlib to arrange elements to reduce overlap. The official multiple-subplots example shows grid creation and layout options.
How to index the Axes returned by plt.subplots
The shape of axs depends on the number of rows and columns. For two panels in one row, unpack the two Axes directly:
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →#1 Best Overall
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot(x, y1)
ax2.plot(x, y2)
For larger grids, keep the returned collection as axs and index it by row and column. With one row or one column, the default result is one-dimensional; with a single subplot, it is a single Axes rather than an array. If you want the result to always be a two-dimensional array—even for one row or column—pass squeeze=False:
fig, axs = plt.subplots(1, 2, squeeze=False)
axs[0, 0].plot(x, y1)
axs[0, 1].plot(x, y2)
Matplotlib’s subplots API documents the return shape and sharing options. Its naming convention is ax for one Axes and axs for multiple Axes.
Rank #2
When should you share an axis between subplots?
Share an axis when aligned scales make comparisons easier. For example, vertically stacked time-series panels often benefit from a common x-axis; side-by-side measurements in the same units may benefit from a common y-axis. Sharing synchronizes the relevant scale and limits. It is usually unsuitable when panels have different units or need independent ranges.
fig, axs = plt.subplots(2, 1, sharex=True)
axs[0].plot(time, temperature)
axs[1].plot(time, pressure)
axs[1].set_xlabel("Time")
The sharex and sharey arguments accept True or the modes 'all', 'row', 'col', and 'none'. These let you share limits across every panel, within rows or columns, or not at all. With shared axes, Matplotlib hides redundant interior tick labels by default. To show the bottom labels on a particular Axes, for example, use axs[0].tick_params(labelbottom=True). The subplots guide demonstrates shared axes and label control.
How to control subplot spacing and panel sizes
For a simple grid, layout="constrained" is a convenient starting point. If you need particular relative panel sizes, pass width_ratios or height_ratios to plt.subplots. For more exact control over row heights, column widths, and gaps, create a GridSpec.
fig = plt.figure(layout="constrained")
gs = fig.add_gridspec(2, 2, width_ratios=[2, 1], height_ratios=[1, 1])
ax_left = fig.add_subplot(gs[:, 0])
ax_top_right = fig.add_subplot(gs[0, 1])
ax_bottom_right = fig.add_subplot(gs[1, 1])
Here, the left Axes spans both rows, while the right column contains two separate panels. GridSpec is also useful when you need to set spacing explicitly. Matplotlib’s Figure API examples show GridSpec layouts, spacing, and label_outer(), which keeps only outer tick labels in shared grids.
Rank #4
When to use subplot_mosaic instead
Use fig.subplot_mosaic when an irregular layout is easier to describe with named regions—for example, a large main chart beside two smaller charts. Names make later plotting code easier to read, and a named Axes can occupy more than one grid cell.
fig, axd = plt.subplot_mosaic([
["main", "top"],
["main", "bottom"],
], layout="constrained")
axd["main"].plot(x, y1)
axd["top"].scatter(x, y2)
axd["bottom"].hist(samples)
In this layout, main spans the two rows in the first column. Choose plt.subplots for an ordinary grid, GridSpec for precise cell proportions or spacing, and subplot_mosaic for a labeled composition with panels that span cells. Matplotlib explains this approach in its subplot_mosaic guide.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsQuick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




