0

i am trying to plot 2 rows of 4 pictures in the jupyter output, here is my code

for name in names_pred:

    onlyfiles2 = [ f for f in listdir(os.path.join(TOP_DATA,names_supcetcs)) ]


    posibles = plt.figure(figsize = (20,20))

    for i in range(1,9):
        plt.subplot(2,4,i)        
        plt.subplots_adjust(wspace=None)
        img = mpimg.imread(TOP_DATA+names_supcetcs+ '/'+ onlyfiles2[i-1])
        plt.imshow(img)
    plt.show()

and the output is an iteration of the next pic but when the i=2,3,4... starts, there is no skipped space

enter image description here

how can i delete this space? i already tried

Improve subplot size/spacing with many subplots in matplotlib

but it make it worst some pictures are not shown

mimus
  • 367
  • 4
  • 21

2 Answers2

0

Did you try incorporating plt.tight_layout() in your code?

Wrote a trivial example because I don't have access to the data you're using, but it should be enough for you to get rolling.

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

ax = {i: fig.add_subplot(2,4,i) for i in range(1,9)}

x = np.linspace(1,10,100)
plots = [ax[i].plot(x,i*x) for i in range(1,9)]

plt.tight_layout()

plt.show()
Michael Green
  • 719
  • 6
  • 15
0

thanks for the help, i actually manage it with an if in the nested for loop, it is not the most fancy way but it works

 for i in range(1,9):
        if i == 1:
            fig.add_subplot(2,4,i)
            plt.imshow(img0)

        else:    

            fig.add_subplot(2,4,i)
            img = mpimg.imread(TOP_DATA+names_supcetcs+ '/'+ onlyfiles2[i-1])
            plt.imshow(img)
mimus
  • 367
  • 4
  • 21