Next, we can adjust the alignment. We may want to adjust the margin between each subplot, or leave no margin instead of having rows and columns of discrete boxes. In this case, we can use the plt.tight_layout() function. By default, it fits all subplots into the figure area when no parameters are supplied. It takes the keyword arguments pad, w_pad, and h_pad to control the padding around subplots. Let's look at the following code example:
import matplotlib.pyplot as plt
fig, axarr = plt.subplots(3,4,sharex=True,sharey=True)
for i in range(3):
for j in range(4):
axarr[i][j].text(0.3,0.5,str(i)+','+str(j),fontsize=18)
plt.tight_layout(pad=0, w_pad=-1.6, h_pad=-1)
We see from the following figure that the now there is no space between subplots, but there's some overlap of axis ticks.
We will learn how to adjust tick properties or to remove ticks in a later section: