Open In App

How to set the spacing between subplots in Matplotlib in Python?

Last Updated : 16 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Let’s learn how to set the spacing between the subplots in Matplotlib to ensure clarity and prevent the overlapping of plot elements, such as axes labels and titles.

Let’s create a simple plot with the default spacing to see how subplots can sometimes become congested.

Python
import numpy as np
import matplotlib.pyplot as plt

x=np.array([1, 2, 3, 4, 5])
fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x*2)
ax[1, 0].plot(x, x*x)
ax[1, 1].plot(x, x*x*x)
plt.show()

Output:

Too much congested and very confusing

As, we can see that the above figure axes values are too congested and very confusing. To solve this problem we need to set the spacing between subplots. 

Setting the Space between Subplots using fig.tight_layout()

The fig.tight_layout() method automatically adjusts subplot parameters to give specified padding and prevent overlap of subplot elements such as axes labels and titles.

Example 1: Basic tight_layout without padding

Python
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4, 5])
fig, ax = plt.subplots(2, 2, figsize=(6, 6))  

ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x * 2)
ax[1, 0].plot(x, x * x)
ax[1, 1].plot(x, x * x * x)
# set the spacing between subplots
fig.tight_layout()
plt.show()

Output: 

Screenshot-2024-12-05-163756

Setting the Space between Subplots using fig.tight_layout()

Example 2: Using padding with tight_layout

Python
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5])

fig, ax = plt.subplots(2, 2, figsize=(6, 6))  

ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x * 2)
ax[1, 0].plot(x, x * x)
ax[1, 1].plot(x, x * x * x)
fig.tight_layout(pad=5.0)

plt.show()

Output:

Screenshot-2024-12-05-164037

Setting the Space between Subplots using fig.tight_layout() with padding

Here, pad=5.0 increases the space around the subplots. This will give more room between the subplots and the figure edges, preventing overlap of labels or titles.

Using plt.subplots_adjust() to set spacing between Subplots

plt.subplots_adjust() method provides fine control over the spacing between subplots and the positioning of the subplots within the figure overall. You can adjust the space using left, right, top, bottom, wspace, and hspace parameters.

Python
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5])
fig, ax = plt.subplots(2, 2, figsize=(6, 6))  

ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x * 2)
ax[1, 0].plot(x, x * x)
ax[1, 1].plot(x, x * x * x)

# Adjust spacing
plt.subplots_adjust(left=0.1, right=0.9, 
                    top=0.9, bottom=0.1, 
                    wspace=0.4, hspace=0.4)
plt.show()

Output: 

Using plt.subplots_adjust() to set spacing between Subplots

Using constrained_layout=True to set spacing between Subplots

For a more automated approach, constrained_layout automatically adjusts subplots and decorations like legends and colorbars to fit in the figure window.

Python
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots(2, 2, constrained_layout = True)

ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x*2)
ax[1, 0].plot(x, x*x)
ax[1, 1].plot(x, x*x*x)
plt.show()

Output: 

Screenshot-2024-12-05-165827

Using constrained_layout=True to set spacing between Subplots

By utilizing these methods, you can significantly improve the legibility and aesthetics of your plots in Matplotlib.



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg
  翻译: