Saving in one unique plot a series of subplots

Viewed 36

I'm currently plotting 3 subplots consecutively with a for loop in the following way:

for i in range(3):  
    fig, (ax1, ax2, ax3 , ax4) = plt.subplots(figsize=(20, 5), ncols=4)



    ax1.set_title('test 1')
    ax1.imshow(test_1)
    ax2.set_title('test 2')
    ax2.imshow(test_2[i])
    ax3.set_title('test 3')
    ax3.imshow(test_3[i])
    ax4.set_title('test 4')
    ax4.imshow(test_4[i])

    plt.show()

This is currently plotting one after the others 3 subplots with 4 images side by side where test_1 is always the same figure (since it does not have any index), while the other 3 images changes.

What I would like is to have the same plot but in a unique panel , so techincally having a 4(columns) x 3 (rows)

Maybe the idea of a plot made of subplots is not the right one. Any suggestion would be very helpful. Thanks!

1 Answers

I've solved like this and it worked:

plt.figure(figsize=(20,15))
num_cols = 4
num_class = 3
for i in range(num_class):

    plt.subplot(num_class,num_cols,i*num_cols+1)
    plt.imshow(test_1)
    plt.subplot(num_class,num_cols,i*num_cols+2)
    plt.imshow(test_2[i])
    plt.subplot(num_class,num_cols,i*num_cols+3)
    plt.imshow(test_3[i])
    plt.subplot(num_class,num_cols,i*num_cols+4)
    plt.imshow(test_4[i])

plt.tight_layout()  
plt.savefig("prova", dpi = 600)

And gave me the correct output in one image

Related