siremod
siremod

Reputation: 13

matplotlib imshow wrong indices

I am trying to show a grid of images, but encountering troubles with indices. It works only with several rows and I did not find any example that clarifies the usage in double for loops

I use all integer values from -2 to 5 in two dimensions to predict the output of my model. The result is a small picture.

fig=plt.figure()

for i in range (-2, 5):
    for j in range (-2, 5):
        current_value=[i, j]
        val=np.float32(np.reshape([current_value], (1,2)))
        y = model.predict(val)[0,:,:,:]
        # here I need help
        ax = fig.add_subplot(7,7,i+j+5)
        ax.imshow(y);
        np.vectorize(lambda ax:ax.axis('off'))(ax)
plt.show()

How to get a grid of 7 (-2 to 4 inclusive) by 7 pics on one plot?

Upvotes: 1

Views: 320

Answers (1)

DonFangzahn
DonFangzahn

Reputation: 128

This generates a 7 by 7 subplot with your ranges:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(7, 7, sharex='col', sharey='row')

# axes are in a two-dimensional array, indexed by [row, col]
for i in range(-2,5):
    for j in range(-2,5):
        ## ad your things here!
        ## My Example:
        ax[i, j].text(0.5, 0.5, str((i, j)),
                      fontsize=18, ha='center')

plt.show()

Which gives you this figure: Example

Upvotes: 1

Related Questions