0

Can't understand pyplot.subplot(330+1+i)

# plot first few images
for i in range(9):
    #define subplot
    pyplot.subplot(330 + 1 + i)
    # plot raw pixel data
    pyplot.imshow(trainX[i], cmap=pyplot.get_cmap('gray'))

# show the figure
pyplot.show()
godfather
  • 1
  • 2
  • Note that this is a very old way to use matplotlib. When working with subplots, it is highly recommended to use `fig, axs = plt.subplots(ncols=3, nrows=3)` and `for trainX_i, ax in zip(trainX, axs.flatten()): ax.imshow(trainX_i, ...)`. See https://matplotlib.org/matplotblog/posts/pyplot-vs-object-oriented-interface/ – JohanC Sep 23 '21 at 10:43

1 Answers1

1

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.

George Sun
  • 968
  • 8
  • 21