HOWTO · Matplotlib

How to Change Space Between Subplots in Matplotlib

Use tight_layout(), subplots_adjust(), subplot_tool(), or constrained_layout=True to improve spacing in Matplotlib figures with many subplots.

On this page

To make a Matplotlib figure with many subplots easier to read, choose the layout control that fits your workflow: call tight_layout() to fit subplot decorations automatically, use plt.subplots_adjust() to set spacing and figure margins, open plt.subplot_tool() for interactive tuning, or enable constrained_layout=True when creating the subplots. These controls adjust the layout; if each axes needs more room, also consider increasing the figure size or changing the number of rows and columns.

Use tight_layout() to prevent subplot overlap

Call fig.tight_layout() after adding plots, labels, and titles. It adjusts subplot positions to make room for decorations such as axis labels and titles. It is a useful first choice when the default arrangement is crowded, though it may not resolve every layout, especially when figures use complex decorations.

This complete example creates four titled plots in a 2-by-2 grid and then applies the automatic adjustment:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-3, 3, 100)
series = [
    (np.sin(x), "Sine function"),
    (np.cos(x), "Cosine function"),
    (1 / (1 + np.exp(-x)), "Sigmoid function"),
    (np.exp(x), "Exponential function"),
]

fig, axes = plt.subplots(2, 2)
for ax, (values, title) in zip(axes.flat, series):
    ax.plot(x, values)
    ax.set_title(title)

fig.tight_layout()
plt.show()

Run the example in a Python environment with NumPy and Matplotlib installed. Its layout is adjusted after the titles and plotted data have been added.

Set subplot spacing and margins with plt.subplots_adjust()

Use plt.subplots_adjust() when you want direct control over the gaps between axes or the space around the grid. wspace sets the horizontal gap as a fraction of the average axes width; hspace sets the vertical gap as a fraction of the average axes height. The left, right, bottom, and top values set the subplot area’s edges as fractions of the figure width or height.

The following example uses the same four-plot arrangement and gives the rows extra separation:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-3, 3, 100)
series = [
    (np.sin(x), "Sine function"),
    (np.cos(x), "Cosine function"),
    (1 / (1 + np.exp(-x)), "Sigmoid function"),
    (np.exp(x), "Exponential function"),
]

fig, axes = plt.subplots(2, 2)
for ax, (values, title) in zip(axes.flat, series):
    ax.plot(x, values)
    ax.set_title(title)

fig.subplots_adjust(left=0.12, right=0.95, bottom=0.12, top=0.9, wspace=0.25, hspace=0.4)
plt.show()

Increase wspace or hspace to widen the corresponding gaps. Adjust the four edge values to reserve more room around the subplot grid—for example, when labels need extra space. These settings control the axes layout within the figure; change figsize in plt.subplots() if the entire figure needs more room.

Adjust subplot layout interactively with plt.subplot_tool()

plt.subplot_tool() opens a Matplotlib window with controls for adjusting subplot spacing and margins. It is useful when you want to tune a figure visually and then transfer the chosen settings to your script. The tool requires an interactive Matplotlib backend and a graphical environment; it is not a way to adjust a static image or a headless plotting session.

In a GUI-backed interactive session, create the figure and call plt.subplot_tool(fig) with that figure. This call depends on a Matplotlib canvas manager with an associated toolbar; non-interactive backends such as the one used for headless rendering do not provide that toolbar, so the tool cannot launch there. The interactive example is intentionally not presented as a runnable headless script.

Use the tool’s controls to change the subplot parameters while viewing the figure. Once the arrangement looks right, apply the corresponding wspace, hspace, and margin values with plt.subplots_adjust() in code for repeatable output.

Enable constrained_layout=True when creating subplots

For a figure that should arrange space automatically as it is drawn, pass constrained_layout=True when calling plt.subplots(). The layout engine accounts for subplot decorations such as titles and labels. Enable it during figure creation; it is intended to manage the layout as the figure is drawn, but complex decorations can still need manual adjustment. See the Matplotlib constrained layout guide for details.

import numpy as np
import matplotlib.pyplot as plt

a = np.linspace(0, 5, 100)
figure, axes = plt.subplots(2, 2, constrained_layout=True)

axes[0, 0].plot(a, np.exp(a))
axes[0, 1].plot(a, np.sin(a))
axes[1, 0].plot(a, np.cos(a))
axes[1, 1].plot(a, a ** 2)

for index, ax in enumerate(axes.flat, start=1):
    ax.set_title(f"Subplot {index}")

plt.show()

All four approaches help manage subplot layout, but they serve different workflows: use tight_layout() for a quick automatic fit after adding content, plt.subplots_adjust() for explicit values, plt.subplot_tool() for interactive tuning, and constrained_layout=True when creating a figure that should handle spacing automatically.