绘制来自 TensorFlow 数据集的数据图像,仅显示一张图像

原文标题Plotting images of data from TensorFlow datasets, shows one image only

我正在研究 TensorFlow 上的 Fashion MNIST 数据集,我试图绘制具有特定 test_label 的 train_data 的图像。我运行了以下代码,它可以工作,但即使有很多这样的图像,它也只显示一张图像。

for i in range (len(test_data)):
  if test_labels[i]==9:
    plt.imshow(test_data[i])

以下是我得到的输出:

enter image description here

原文链接:https://stackoverflow.com//questions/71901401/plotting-images-of-data-from-tensorflow-datasets-shows-one-image-only

回复

我来回复
  • Kaveh的头像
    Kaveh 评论

    每次迭代测试数据时,它都会覆盖上一个图。

    如果您想获得不同的图,请使用以下内容:

    j = 0
    n_rows = 2
    n_cols = 3
    for i in range(len(test_data)):
        if test_labels[i]==9:
            j += 1
            ax = plt.subplot(n_rows,n_cols,j)
            ax.imshow(test_data[i])
        if j >= (n_rows*n_cols):
            break
    

    结果:plotted image

    2年前 0条评论