How to set the spacing between subplots in Matplotlib in Python?
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.
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:
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
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:

Example 2: Using padding with tight_layout
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:

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.
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()
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 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.
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()
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:

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