我正在尝试显示图像网格,但遇到索引问题。它仅适用于几行,我没有找到任何示例来阐明双 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/