how to use tight_layout() in Matplotlib

The tight_layout() function in Matplotlib is used to automatically adjust the spacing between axes and figures in a plot, so that the plot fits the size of the figure more tightly. This prevents overlapping of axes and text on the plot, and makes the plot more presentable. It is invoked by calling the tight_layout() function after plotting the figure. It is important to note that tight_layout() will not work on all plots, and it may be necessary to do some manual adjustments to the spacing of axes and text elements in the figure.


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.

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

x