python - matplotlib imshow 错误的索引

标签 python arrays for-loop matplotlib imshow

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

我使用二维中从 -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()

如何在一 block 地 block 上获得 7 个(-2 到 4,包括)× 7 张图片的网格?

最佳答案

这会生成一个 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

关于python - matplotlib imshow 错误的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56301977/

相关文章:

控制到达 C 中非 void 函数的末尾

java - 如何制作一个 for 循环来在 javaFx 中显示 8 个 TextFields

python循环索引

python - 使用 Python OpenCV 将图像捕获为数组

C# 从 array.sum 中排除元素

c - x86-如何使用内联汇编中的for循环将值插入数组

for-loop - CFSCRIPT - For 循环递增索引错误

Python - = 和 .extend for 循环中的区别

python - os.mkdir 的权限分配没有按预期工作

python - 使用 pip install 安装 ssl 时遇到问题 : "SyntaxError: Missing parentheses in call to ' print'"