How to Use tight_layout() in Matplotlib


You can use the tight_layout() function in Matplotlib to automatically adjust the padding between and around subplots.

The following example shows how to use this function in practice.

Example: How to Use tight_layout() in Matplotlib

Suppose we use Matplotilb to create four subplots in a 2×2 grid:

import matplotlib.pyplot as plt

#define data
x = [1, 2, 3]
y = [7, 13, 24]

#define layout for subplots
fig, ax = plt.subplots(2, 2)

#define subplot titles
ax[0, 0].plot(x, y, color='red')
ax[0, 1].plot(x, y, color='blue')
ax[1, 0].plot(x, y, color='green')
ax[1, 1].plot(x, y, color='purple')

#add title to each subplot
ax[0, 0].set_title('First Subplot')
ax[0, 1].set_title('Second Subplot')
ax[1, 0].set_title('Third Subplot')
ax[1, 1].set_title('Fourth Subplot')

Notice that there is minimal padding between the subplots, which causes the titles to overlap in some places.

By specifying fig.tight_layout() we can automatically adjust the padding between and around subplots:

import matplotlib.pyplot as plt

#define data
x = [1, 2, 3]
y = [7, 13, 24]

#define layout for subplots
fig, ax = plt.subplots(2, 2)

#specify a tight layout
fig.tight_layout()

#define subplot titles
ax[0, 0].plot(x, y, color='red')
ax[0, 1].plot(x, y, color='blue')
ax[1, 0].plot(x, y, color='green')
ax[1, 1].plot(x, y, color='purple')

#add title to each subplot
ax[0, 0].set_title('First Subplot')
ax[0, 1].set_title('Second Subplot')
ax[1, 0].set_title('Third Subplot')
ax[1, 1].set_title('Fourth Subplot')

matplotlib tight_layout example

Notice that the padding between and around the subplots has been adjusted so that the plots no longer overlap in any area.

Note that the tight_layout() function takes a pad argument to specify the padding between the figure edge and the edges of subplots, as a fraction of the font size.

The default value for pad is 1.08. However, we can increase this value to increase the padding around the plots:

import matplotlib.pyplot as plt

#define data
x = [1, 2, 3]
y = [7, 13, 24]

#define layout for subplots
fig, ax = plt.subplots(2, 2)

#specify a tight layout with increased padding
fig.tight_layout(pad=5)

#define subplot titles
ax[0, 0].plot(x, y, color='red')
ax[0, 1].plot(x, y, color='blue')
ax[1, 0].plot(x, y, color='green')
ax[1, 1].plot(x, y, color='purple')

#add title to each subplot
ax[0, 0].set_title('First Subplot')
ax[0, 1].set_title('Second Subplot')
ax[1, 0].set_title('Third Subplot')
ax[1, 1].set_title('Fourth Subplot')

Matplotlib tight_layout with increased padding

Notice that the padding around the plots has increased noticeably.

Feel free to adjust the value for the pad argument to increase the padding around the plots as much as you’d like.

Additional Resources

The following tutorials explain how to perform other common tasks in  Matplotlib:

How to Add Title to Subplots in Matplotlib
How to Adjust Subplot Size in Matplotlib
How to Adjust Spacing Between Matplotlib Subplots

Leave a Reply

Your email address will not be published. Required fields are marked *