matplotlib imshow 错误的索引

matplotlib imshow wrong indices

我正在尝试显示图像网格,但遇到了索引问题。它仅适用于多行,我没有找到任何示例来阐明双循环中的用法

我使用二维中从 -2 到 5 的所有整数值来预测我的模型的输出。结果是一张小图片。

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()

如何在一张图上通过 7 张图片得到 7(-2 到 4)的网格?

这会生成一个 7 x 7 的子图,其中包含您的范围:

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()

这给你这个数字: Example