Bird
0
0

You want to display 6 images in a 2x3 grid with titles on each subplot. Which code snippet correctly does this?

hard📝 Application Q15 of 15
Matplotlib - Image Display
You want to display 6 images in a 2x3 grid with titles on each subplot. Which code snippet correctly does this?
Afig, axes = plt.subplots(2, 3) for ax, img, i in zip(axes.flat, images, range(6)): ax.imshow(img) ax.set_title(f'Image {i+1}') ax.axis('off')
Bfig, axes = plt.subplots(3, 2) for i in range(6): axes[i].imshow(images[i]) axes[i].title(f'Image {i}') axes[i].axis('off')
Cfig, axes = plt.subplots(2, 3) for i in range(6): axes[i].imshow(images[i]) axes[i].set_title('Image') axes[i].axis('off')
Dfig, axes = plt.subplots(2, 3) for ax, img in zip(axes, images): ax.imshow(img) ax.set_title('Image') ax.axis('off')
Step-by-Step Solution
Solution:
  1. Step 1: Create correct subplot grid

    plt.subplots(2, 3) creates 2 rows and 3 columns, perfect for 6 images.
  2. Step 2: Loop through axes.flat and images with index

    Using axes.flat flattens the 2D axes array for easy looping. Adding index with range(6) helps set titles.
  3. Step 3: Set image, title, and turn off axes

    Each axis shows one image, sets a title with number, and hides axis ticks.
  4. Final Answer:

    fig, axes = plt.subplots(2, 3) for ax, img, i in zip(axes.flat, images, range(6)): ax.imshow(img) ax.set_title(f'Image {i+1}') ax.axis('off') -> Option A
  5. Quick Check:

    axes.flat + set_title + axis off = correct grid [OK]
Quick Trick: Use axes.flat and zip(images, range) for titles [OK]
Common Mistakes:
  • Indexing 2D axes as 1D without flat
  • Using wrong subplot shape for 6 images
  • Calling non-existent title() method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes