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()
1
  • 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 matplotlib.org/matplotblog/posts/… Commented Sep 23, 2021 at 10:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

yeah, that I knew but in the code its + sign inside the function.can you please explain what's the + sign is for ty
It is explained in the documentation. I will edit my answer with the relevant section quoted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.