0

I want to add titles to each of the subplots and get rid of the x-axis and y-axis in them.

fig = plt.figure(figsize=(6,6)) # specifying the overall grid size

for i in range(16):
    plt.subplot(4,4,i+1)    # the number of images in the grid is 6*6 (16)
    img = mpimg.imread(f'../input/cifar10-mu/train_images/{train["filename"][i]}')
    plt.imshow(img)
    
fig.suptitle('Sample Images')
plt.show()

Here is the output of the code above

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Replacing "plt.subplot(4,4,i+1)" with "plt.subplot(4, 4, i + 1, xticks = [], yticks = [], frameon = False)" should work. – Ori Yarden PhD Oct 27 '21 at 20:37
  • Does this answer your question? [How to plot images in subplots](https://stackoverflow.com/q/67595781/7758804) and [How to remove or hide y-axis ticklabels from a matplotlib / seaborn plot](https://stackoverflow.com/q/63756623/7758804) – Trenton McKinney Oct 27 '21 at 20:39

1 Answers1

0

you can add these two lines:

plt.text(0, 0, f'Label {train["label"][i]}')
plt.axis('off')

so that the whole code will look like this:

fig = plt.figure(figsize=(6,6)) # specifying the overall grid size

for i in range(16):
    plt.subplot(4,4,i+1)    # the number of images in the grid is 6*6 (16)
    img = mpimg.imread(f'../input/cifar10-mu/train_images/{train["filename"][i]}')
    plt.imshow(img)
    plt.text(0, 0, f'Label {train["label"][i]}')
    plt.axis('off')
    
fig.suptitle('Sample Images')
plt.show()