plotting a grid of png with matplotlib

Viewed 764

I have found multiple similar questions with this subject but so far I couldn't adapt any solution to my needs, so I'm sorry for reposting.

I'm trying to plot a grid of png images using matplotlib, the closest I've got to what I want is using the code below, which can be found here https://matplotlib.org/stable/gallery/axes_grid1/simple_axesgrid.html .

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np

im1 = np.arange(100).reshape((10, 10))
im2 = im1.T
im3 = np.flipud(im1)
im4 = np.fliplr(im2)

fig = plt.figure(figsize=(4., 4.))
grid = ImageGrid(fig, 111,  # similar to subplot(111)
                 nrows_ncols=(2, 2),  # creates 2x2 grid of axes
                 axes_pad=0.1,  # pad between axes in inch.
                 )

for ax, im in zip(grid, [im1, im2, im3, im4]):
    # Iterating over the grid returns the Axes.
    ax.imshow(im)

plt.show()

My question is, how do I get rid of the x and y ticks/labels and also give each image a title?

Again, I'm sorry for repeating the question.

1 Answers

This code

import matplotlib.pyplot as plt

image = plt.imread("sample.png")

fig, axes = plt.subplots(2, 3)

for row in [0, 1]:
    for column in [0, 1, 2]:
        ax = axes[row, column]
        ax.set_title(f"Image ({row}, {column})")
        ax.axis('off')
        ax.imshow(image)

plt.show()

is going to produce

enter image description here

Related