Matplotlib tight_layout wspace and hspace in Python

I’ve encountered many situations where subplot layouts get messy. Whether it’s overlapping axis labels, titles, or crowded charts, handling subplot spacing is crucial to making your visualizations clear and professional.

In this article, I’ll share practical insights on how to use Matplotlib’s tight_layout() function along with the wspace and hspace parameters to control subplot spacing in Python.

These tools help you automatically or manually adjust the space between subplots so that your figures look clean and readable, something every data scientist or analyst working with Python will appreciate.

Matplotlib tight_layout in Python

Matplotlib’s tight_layout() is a handy function that automatically adjusts subplot parameters to give your plots enough space so that axis labels, titles, and ticks don’t overlap. This is especially useful when you have multiple subplots in a figure.

I use tight_layout() often in my projects because it saves time and makes plots look neat without manual tweaking. However, sometimes you need finer control over the spacing between subplots; this is where wspace and hspace parameters come in.

Control Horizontal Spacing (wspace) in Python Matplotlib

Let me explain to you how to control horizontal spacing in Python matplotlib.

Method 1: Adjust wspace with subplots_adjust()

One of the first methods I use to control horizontal spacing between subplots in Python is by using plt.subplots_adjust(). It allows you to set the width space (wspace) between subplots manually.

Here’s a simple example:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(1, 3, figsize=(12, 4))

for i, ax in enumerate(axs):
    ax.plot([1, 2, 3], [i+1, (i+1)*2, (i+1)*3])
    ax.set_title(f'Subplot {i+1}')

# Increase horizontal space between subplots
plt.subplots_adjust(wspace=0.5)

plt.show()

You can refer to the screenshot below to see the output.

Matplotlib tight_layout wspace

In this example, the wspace=0.5 parameter increases the horizontal gap between the three subplots. By default, wspace is set to a smaller value, which can cause labels or titles to overlap if your subplots contain detailed axis ticks or long titles.

Method 2: Use GridSpec for precise control of wspace

If you want even more control over subplot layout, I recommend using Matplotlib’s GridSpec. It lets you specify subplot positions and spacing precisely, including horizontal space (wspace).

Here’s how:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(12, 4))
gs = gridspec.GridSpec(1, 3, wspace=0.8)  # Set horizontal space

for i in range(3):
    ax = fig.add_subplot(gs[i])
    ax.plot([1, 2, 3], [i+1, (i+1)*2, (i+1)*3])
    ax.set_title(f'Subplot {i+1}')

plt.show()

You can refer to the screenshot below to see the output.

Matplotlib tight_layout wspace in Python

With GridSpec, the wspace parameter controls the width between subplots in a more flexible grid layout. I use this method when creating complex figures with varying subplot sizes.

Control Vertical Spacing (hspace) in Python Matplotlib

Here are methods to control vertical spacing in Python matplotlib.

Method 1: Adjust hspace with subplots_adjust()

Similar to horizontal spacing, vertical spacing between subplots can be controlled using the hspace parameter in plt.subplots_adjust(). This is useful when you have multiple rows of plots.

Here’s an example:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(3, 1, figsize=(6, 9))

for i, ax in enumerate(axs):
    ax.plot([1, 2, 3], [i+1, (i+1)*2, (i+1)*3])
    ax.set_title(f'Subplot {i+1}')

# Increase vertical space between subplots
plt.subplots_adjust(hspace=0.6)

plt.show()

You can refer to the screenshot below to see the output.

Matplotlib tight_layout hspace in Python

In this case, hspace=0.6 creates more vertical breathing room between the three stacked subplots. Without adjusting hspace, titles or x-axis labels can overlap, making the figure hard to read.

Method 2: Use GridSpec to control hspace precisely

Just like with wspace, GridSpec also allows you to control vertical spacing (hspace) with precision.

Example:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(6, 9))
gs = gridspec.GridSpec(3, 1, hspace=0.7)  # Set vertical space

for i in range(3):
    ax = fig.add_subplot(gs[i])
    ax.plot([1, 2, 3], [i+1, (i+1)*2, (i+1)*3])
    ax.set_title(f'Subplot {i+1}')

plt.show()

You can refer to the screenshot below to see the output.

Matplotlib tight_layout hspace

Here, hspace=0.7 increases the vertical gap between the rows of subplots. I prefer this approach when I want consistent spacing across subplots in a grid layout.

How tight_layout() Works with wspace and hspace in Python

While wspace and hspace give you manual control, tight_layout() automatically calculates the best spacing based on subplot contents.

You can call it simply:

fig, axs = plt.subplots(2, 2, figsize=(8, 6))

for i, ax in enumerate(axs.flat):
    ax.plot([1, 2, 3], [i+1, (i+1)*2, (i+1)*3])
    ax.set_title(f'Subplot {i+1}')

plt.tight_layout()
plt.show()

tight_layout() adjusts subplot parameters to minimize overlaps. However, sometimes it may not perfectly space subplots if you have complex elements like legends or colorbars.

In those cases, combining tight_layout() with manual adjustments to wspace and hspace through subplots_adjust() or GridSpec is the best practice.

Practical Tips from My Experience

  • Always start with plt.tight_layout() to quickly fix spacing issues. It handles most cases well.
  • If you see overlapping labels or titles, tweak wspace and hspace using plt.subplots_adjust().
  • For complex figures with uneven subplot sizes, use GridSpec for precise control.
  • Remember that tight_layout() and subplots_adjust() can be used together for fine-tuning.
  • When working on dashboards or reports for USA-based clients, clear and professional plots are critical — good spacing improves readability and impact.

Mastering subplot spacing in Matplotlib with tight_layout(), wspace, and hspace is essential for creating polished Python visualizations. With these tools, you can ensure your plots are not only informative but also visually appealing.

Related Articles You May Like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.