简体   繁体   中英

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

I have 2 lists that contain RGB value to be used as images in 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)] 

I have successfully show the images using for loop in matplotlib like this:

# 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()

the example output of both are: pred_output:

pred_output

real_output:

真实输出

Because I need to compare the 2 plots I need to combine them to be a plot that contains pred_plot on the left and real_plot on the right, so I use the subplots in matplotlib with for loop... Here's the code:

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()

I've run it and it gives an error like this![error_images

Does anyone know how to display 2 columns of subplots vertically/downward?

Finally I found the solution...

Here's the code:

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]]])

and now the final output that I expect to be real: 最终输出

You could use only one for loop and use subplots from matplotlib. I assume that both of the lists are equal in size.

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:

示例图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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