Set the Spacing Between Subplots in Python Matplotlib

Recently, I was working on a Python data visualization project where I had to create multiple charts in one figure. The issue I faced was that the subplots were too close to each other, making the labels and titles overlap. It looked messy and unprofessional.

If you have worked with Matplotlib in Python, you might have faced the same problem. By default, Matplotlib tries to manage subplot spacing, but often it needs manual adjustments.

In this tutorial, I will show you step-by-step how I set the spacing between subplots in Python Matplotlib.

I will cover multiple methods, explain each with easy-to-follow examples, and share my firsthand experience so you can apply them in your own projects.

Method 1 – Use plt.subplots_adjust() in Python

The first method I usually try is the plt.subplots_adjust() function. It gives me full control over the spacing between subplots.

This function allows me to adjust parameters like left, right, top, bottom, hspace (height space), and wspace (width space).

Here is a simple Python example:

import matplotlib.pyplot as plt
import numpy as np

# Sample data for USA sales performance
months = np.arange(1, 13)
sales_2024 = np.random.randint(200, 500, size=12)
sales_2025 = np.random.randint(250, 550, size=12)

# Create subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6))

# Plot first subplot
ax1.plot(months, sales_2024, marker='o', color='blue')
ax1.set_title("Monthly Sales in USA - 2024")
ax1.set_xlabel("Month")
ax1.set_ylabel("Sales ($)")

# Plot second subplot
ax2.bar(months, sales_2025, color='green')
ax2.set_title("Monthly Sales in USA - 2025")
ax2.set_xlabel("Month")
ax2.set_ylabel("Sales ($)")

# Adjust spacing between subplots
plt.subplots_adjust(hspace=0.5, wspace=0.3)

plt.show()

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

Spacing Between Subplots in Python Matplotlib

In this code, I used hspace=0.5 to increase the vertical spacing between the two subplots. You can also use wspace when you have multiple columns of subplots, and you want to adjust the horizontal spacing.

Method 2 – Use tight_layout() in Python

Another method I often use is tight_layout(). This function automatically adjusts subplot parameters so that labels, titles, and ticks fit nicely. It’s very handy when you don’t want to manually calculate spacing values.

Here is an example in Python:

import matplotlib.pyplot as plt
import numpy as np

# Sample data for USA unemployment rate
years = np.arange(2015, 2025)
rate_A = np.random.uniform(3, 8, size=10)
rate_B = np.random.uniform(4, 9, size=10)

# Create subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6))

# Plot first subplot
ax1.plot(years, rate_A, marker='s', color='red')
ax1.set_title("Unemployment Rate in USA - Group A")
ax1.set_xlabel("Year")
ax1.set_ylabel("Rate (%)")

# Plot second subplot
ax2.plot(years, rate_B, marker='^', color='purple')
ax2.set_title("Unemployment Rate in USA - Group B")
ax2.set_xlabel("Year")
ax2.set_ylabel("Rate (%)")

# Automatically adjust spacing
plt.tight_layout()

plt.show()

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

Set the Spacing Between Subplots in Python Matplotlib

With just one line of code plt.tight_layout(), the subplots look cleaner. This method works well for most cases, especially when you have many subplots in one figure.

Method 3 – Combine tight_layout() with rect in Python

Sometimes, I need extra space for a main title or a legend outside the plot. In such cases, I combine tight_layout() with the rect parameter. The rect parameter defines the portion of the figure area that the subplots will occupy.

Here’s a Python example:

import matplotlib.pyplot as plt
import numpy as np

# Sample data for USA population growth
years = np.arange(2010, 2025)
population = np.random.randint(300, 350, size=15)

fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6))

ax1.plot(years, population, color='orange')
ax1.set_title("USA Population Growth - Chart 1")

ax2.bar(years, population, color='cyan')
ax2.set_title("USA Population Growth - Chart 2")

# Adjust layout with rect to leave space for main title
plt.tight_layout(rect=[0, 0, 1, 0.95])

# Add a main title
fig.suptitle("USA Population Growth Analysis (2010-2025)", fontsize=14)

plt.show()

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

Python Matplotlib Set the Spacing Between Subplots

By using rect=[0, 0, 1, 0.95], I left some space at the top for the main title. This method is useful when preparing professional reports or presentations in Python.

Method 4 – Use constrained_layout=True in Python

Recently, I started using constrained_layout=True when creating subplots. It’s a newer option in Matplotlib that automatically adjusts spacing more intelligently than tight_layout().

Here’s how it works in Python:

import matplotlib.pyplot as plt
import numpy as np

# Sample data for USA inflation rate
years = np.arange(2010, 2020)
inflation = np.random.uniform(1, 5, size=10)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5), constrained_layout=True)

ax1.plot(years, inflation, color='brown')
ax1.set_title("Inflation Rate (Line Chart)")

ax2.bar(years, inflation, color='teal')
ax2.set_title("Inflation Rate (Bar Chart)")

plt.show()

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

MatplotlibSet the Spacing Between Subplots in Python

When I set constrained_layout=True, Matplotlib automatically manages spacing between subplots, legends, and titles. This is one of the easiest methods, and I recommend it if you are using the latest version of Matplotlib in Python.

Method 5 – Adjust Spacing Dynamically in Python

There are times when I want to adjust subplot spacing dynamically based on figure size. I usually combine fig.subplots_adjust() with conditions.

Here’s a Python example:

import matplotlib.pyplot as plt
import numpy as np

# Sample data for USA GDP growth
years = np.arange(2010, 2020)
gdp_growth = np.random.uniform(1.5, 4.5, size=10)

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

for i, ax in enumerate(axs.flat):
    ax.plot(years, gdp_growth + i, marker='o')
    ax.set_title(f"GDP Growth Chart {i+1}")

# Adjust spacing based on figure size
fig.subplots_adjust(hspace=0.4, wspace=0.4)

plt.show()

This method gives me flexibility when I have a grid of subplots, and I want consistent spacing.

Key Notes from My Experience

  • Use tight_layout() for quick automatic adjustments.
  • Use subplots_adjust() when you need manual fine-tuning.
  • Use constrained_layout=True if you are on a newer Matplotlib version.
  • Always check how your plots look before finalizing them for presentations or reports.

Conclusion

In this tutorial, I showed you different methods to set spacing between subplots in Python Matplotlib. I started with subplots_adjust(), then explained tight_layout(), followed by using rect for extra space, and finally covered constrained_layout=True.

All these methods are simple and practical. I have personally used them in real-world Python projects for USA-specific datasets like sales, unemployment, and GDP growth.

Now, whenever you face overlapping subplot issues in Python Matplotlib, you know exactly what to do.

You may also read other Matplotlib articles:

Leave a Comment

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.