Reputation:
What is the significance of i+1 in plt.subplot(3, 3, i + 1) ? in the following code:
for i in range(9):
plt.subplot(3, 3, i + 1)
img = plt.imread(os.path.join(img_dir, random_images[i]))
plt.imshow(img, cmap='gray')
plt.axis('off')
Upvotes: 1
Views: 420
Reputation: 39062
i+1
is the counter for the subplot in your 3x3 subplot grid.
Why add 1?
The subplot numbering starts from 1 but the range(9)
starts from 0, so i+1
is used here to add 9 subplots, starting from 1, 2, 3, ..., 8, 9
Upvotes: 2
Reputation: 185
matplotlib starts counting at 1, while the range function starts at 0. range(9) will return 0...8, while matplotlib needs 1...9, the i+1 moves the range(9) results to numbers matplotlib expects
Upvotes: 1