繁体   English   中英

如何并排显示 2 列 plt.imshow,Python

[英]How to display 2 columns of plt.imshow side by side, Python

我有 2 个包含 RGB 值的列表,用作 matplotlib 中的图像:

pred_rgb = [(58, 61, 55),
 (199, 138, 127),
 (176, 176, 176),
 (128, 118, 0),
 (71, 92, 24),
 (58, 255, 0),
 (69, 128, 0),
 (59, 55, 61),
 (61, 55, 61),
 (127, 145, 199),
 (0, 60, 128),
 (2, 32, 163),
 (128, 0, 69),
 (61, 58, 55),
 (176, 176, 176),
 (11, 176, 117),
 (76, 0, 128),
 (163, 2, 2),
 (58, 61, 55),
 (11, 73, 176),
 (255, 94, 0),
 (92, 58, 24),
 (92, 24, 50),
 (0, 128, 68),
 (0, 128, 87),
 (92, 68, 24),
 (81, 255, 0),
 (252, 252, 252),
 (117, 128, 0),
 (252, 252, 252),
 (59, 61, 55),
 (11, 176, 111),
 (92, 57, 24),
 (0, 55, 128),
 (122, 0, 128),
 (252, 252, 252)]

real_rgb = [(61, 55, 55),
 (127, 182, 199),
 (176, 176, 176),
 (128, 0, 0),
 (92, 24, 73),
 (235, 255, 0),
 (128, 0, 0),
 (61, 55, 55),
 (61, 55, 55),
 (127, 182, 199),
 (128, 0, 0),
 (44, 163, 2),
 (128, 0, 0),
 (61, 55, 55),
 (176, 176, 176),
 (176, 11, 70),
 (128, 0, 0),
 (44, 163, 2),
 (61, 55, 55),
 (176, 11, 70),
 (235, 255, 0),
 (92, 24, 73),
 (92, 24, 73),
 (128, 0, 0),
 (128, 0, 0),
 (92, 24, 73),
 (235, 255, 0),
 (252, 252, 252),
 (128, 0, 0),
 (252, 252, 252),
 (61, 55, 55),
 (176, 11, 70),
 (92, 24, 73),
 (128, 0, 0),
 (128, 0, 0),
 (252, 252, 252)] 

我已经成功地在 matplotlib 中使用 for 循环显示图像,如下所示:

# real color
for i in range(len(real_rgb)):
    plt.figure()
    plt.imshow([[real_rgb[i]]])
    plt.show()


# pred color
for i in range(len(pred_rgb)):
    plt.figure()
    plt.imshow([[pred_rgb[i]]])
    plt.show()

两者的示例 output 是: pred_output:

pred_output

真实输出:

真实输出

因为我需要比较这两个图,所以我需要将它们组合成一个 plot,其中包含左侧的 pred_plot 和右侧的 real_plot,所以我使用 matplotlib 中的子图和 for 循环......这是代码:

data_num = 36

fig, axes = plt.subplots(nrows=data_num, ncols=2, sharex=True, sharey=True)
for n in range(data_num):  #row index
    for i in range(data_num):
            plt.figure()
            plt.imshow([[pred_rgb[i, 0]]])
            plt.imshow([[real_rgb[i, 1]]])
            plt.show()

我已经运行了它,它给出了这样的错误![error_images

有谁知道如何垂直/向下显示 2 列子图?

最后我找到了解决方案...

这是代码:

plt.figure(figsize=(15, 40))
fig, ax = plt.subplots(36, 2, sharex='col', sharey='row',figsize=(15, 40))
# axes are in a two-dimensional array, indexed by [row, col]
ax[0, 0].title.set_text('Warna Real')
ax[0, 1].title.set_text('Warna Prediksi')

for i in range(36):
    ax[i, 1].imshow([[pred_rgb[i]]])
    ax[i, 0].imshow([[real_rgb[i]]])

现在是我希望是真实的最终 output: 最终输出

您可以只使用一个 for 循环并使用subplots中的子图。 我假设两个列表的大小相同。

for i in range(len(real_rgb)):
    fig, (ax1, ax2) = plt.subplots(1, 2)
    
    ax1.imshow([[real_rgb[i]]])
    ax1.set_title("real")
    ax2.imshow([[pred_rgb[i]]])
    ax2.set_title("pred")
    plt.show()

Output:

示例图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM