October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Create Multiple Plots in Matplotlib

Use plt.subplots to arrange Matplotlib plots in a figure, then choose shared axes, GridSpec, or subplot_mosaic to fit the layout and comparison you need.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.