使用 matplotlib 绘制子图

Subplotting image using matplotlib

我使用文件夹中的 cv2.imread 将几张图像存储为数组。我现在想使用子图显示文件夹中的几张图像。这是我试过的

w=10
h=10
fig=plt.figure(figsize=(8, 8))
columns = 4
rows = 5
for i in range(1, columns*rows +1):
    img = malaria_images[i]
    fig.add_subplot(rows, columns, i)
    plt.imshow(img)
plt.show()

然而结果看起来是这样的。我不确定哪里出错了。任何帮助,将不胜感激。谢谢

我已经根据评论中的内容修改了您的解决方案。这个不显示吗?

import matplotlib.pyplot as plt

w=10
h=10
#fig = plt.figure(figsize=(8, 8))
columns = 4
rows = 5

fig, axes = plt.subplots(rows, columns, figsize=(8, 8))

i = 0
for r in range(rows):
    for c in range(columns):
        axes[r,c].imshow(malaria_images[i])
        i += 1

plt.show()