Note at the beginning: if you want to have full control over the interval, avoid using plt.tight_layout() , as it will try to change the graphics in your drawing equally and nicely distributed. This is mostly beautiful and gives nice results, but adjusts the interval as desired.
The reason the GridSpec example that you quote in the Matplotlib example gallery works so well because the aspect of the subheadings is not predefined. That is, the subnets will simply expand on the grid and leave the specified distance (in this case wspace=0.0, hspace=0.0 ) regardless of the size of the figure.
In contrast, you draw images using imshow , and the default image size is equal to (equivalent to ax.set_aspect("equal") ). However, you can of course put set_aspect("auto") on each plot (and optionally add wspace=0.0, hspace=0.0 as arguments to GridSpec, as in the gallery example), which will lead to the creation of a graph without spaces .
However, when using images, it makes sense to maintain the same aspect ratio so that each pixel is as large as possible and the square array is shown as a square image.
Then you will need to play with the image size and margins to get the expected result. The figsize argument for a shape is a number (width, height) in inches, and here you can play the ratio of two numbers. And the parameters of the subtask wspace, hspace, top, bottom, left can be manually configured to give the desired result. The following is an example:
import numpy as np import matplotlib.pyplot as plt from matplotlib import gridspec nrow = 10 ncol = 3 fig = plt.figure(figsize=(4, 10)) gs = gridspec.GridSpec(nrow, ncol, width_ratios=[1, 1, 1], wspace=0.0, hspace=0.0, top=0.95, bottom=0.05, left=0.17, right=0.845) for i in range(10): for j in range(3): im = np.random.rand(28,28) ax= plt.subplot(gs[i,j]) ax.imshow(im) ax.set_xticklabels([]) ax.set_yticklabels([])

Edit:
Of course, it is advisable not to manually configure the settings. Thus, it is possible to calculate some optimal by the number of rows and columns.
nrow = 7 ncol = 7 fig = plt.figure(figsize=(ncol+1, nrow+1)) gs = gridspec.GridSpec(nrow, ncol, wspace=0.0, hspace=0.0, top=1.-0.5/(nrow+1), bottom=0.5/(nrow+1), left=0.5/(ncol+1), right=1-0.5/(ncol+1)) for i in range(nrow): for j in range(ncol): im = np.random.rand(28,28) ax= plt.subplot(gs[i,j]) ax.imshow(im) ax.set_xticklabels([]) ax.set_yticklabels([]) plt.show()