You can view the arguments in the documentation for subplot. The basic arguments are subplot(nrows, ncols, index) where nrows is the number of rows of plots, ncols is the number of columns of plots, and index is the plot number when counting across the grid of plots from left to right, top to bottom.
Another way to specify subplots is with a three-digit integer as in your example.
A 3-digit integer. The digits are interpreted as if given separately as three single-digit integers, i.e. fig.add_subplot(235) is the same as fig.add_subplot(2, 3, 5). Note that this can only be used if there are no more than 9 subplots.
In your example where i ranges from 0 to 8, inclusive, the argument for subplot will range from 331 to 339. Following to the documentation, your subplots will be over 3 rows and 3 columns with in indices 1 to 9.
fig, axs = plt.subplots(ncols=3, nrows=3)andfor trainX_i, ax in zip(trainX, axs.flatten()): ax.imshow(trainX_i, ...). See matplotlib.org/matplotblog/posts/…