While plt.subplots() provides a handy way to create grids of same-sized subplots, at times we may need to combine subplots of different sizes in a group. This is when plt.subplot2grid() comes into use.
plt.subplot2grid() takes in three to four parameters. The first tuple specifies the overall dimensions of the grid. The second tuple determines where in the grid the top left corner of a subplot starts. Finally we describe the subplot dimensions using the rowspan and colspan arguments.
Here is a code example to showcase the usage of this function:
import matplotlib.pyplot as plt
axarr = []
axarr.append(plt.subplot2grid((3,3),(0,0)))
axarr.append(plt.subplot2grid((3,3),(1,0)))
axarr.append(plt.subplot2grid((3,3),(0,2), rowspan=3))
axarr.append(plt.subplot2grid((3,3),(2,0), colspan=2))
axarr.append(plt.subplot2grid((3,3),(0,1), rowspan=2))
axarr[0].text(0.4,0.5,'0,0',fontsize=16)
axarr[1].text(0.4,0.5,'1,0',fontsize=16)
axarr[2].text(0.4,0.5,'0,2 3 rows',fontsize=16)
axarr[3].text(0.4,0.5,'2,0 2 cols',fontsize=16)
axarr[4].text(0.4,0.5,'0,1 2 rows',fontsize=16)
plt.show()
The following is the resultant plot. Notice how the subplots of different sizes are aligned: