【问题标题】:matplotlib imshow wrong indicesmatplotlib imshow 错误的索引
【发布时间】:2019-10-11 14:34:51
【问题描述】:

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

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

【问题讨论】:

    标签: python arrays for-loop matplotlib imshow


    【解决方案1】:

    这会生成一个包含您的范围的 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

    【讨论】:

    • 效果很好!
    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 2022-09-23
    • 1970-01-01
    • 2023-01-21
    • 2019-01-22
    • 2015-07-30
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多