1

I am trying to create a figure with 2x10 subplots. I would like them all to be square with a thin white space in between them, but they are coming out as rectangles (longer in height than width). The images I'm putting in each cell of the grid are all square, but the cell itself is not square so the extra space just becomes white space which creates a giant gap between the top row and the bottom row. Here's the code that shows the rectangles:

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from PIL import Image

fig = plt.figure()
gs1 = GridSpec(2, 10)
for a in range(10):
    ax = plt.subplot(gs1[0, a])
    ax2 = plt.subplot(gs1[1, a])
plt.show()

output from above code

Imagine this but with little to no gaps in between cells and each cell is square instead of rectangular. Thanks in advance for any help!

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

0

You can use plt.tight_layout() to clean up your subplot figure. Also, play around with plt.rcParams for the figure size:

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from PIL import Image
plt.rcParams["figure.figsize"] = (20,10)
fig = plt.figure()
gs1 = GridSpec(2, 10)
for a in range(10):
    ax = plt.subplot(gs1[0, a])
    ax2 = plt.subplot(gs1[1, a])
plt.tight_layout()
plt.show()

Outputenter image description here

For more control, you can use fig,ax and turn off all the labels and ticks. Then you can remove the white space between the subplots.

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from PIL import Image
plt.rcParams["figure.figsize"] = (20,4)
fig, ax = plt.subplots(2,10)
gs1 = GridSpec(2, 10)
for x in range(2):
    for y in range(10):
        ax[x,y].plot()
        ax[x,y].tick_params(axis  = 'both', bottom= False, left  = False, 
                            labelbottom = False, labelleft   = False) 
ax[1,0].tick_params(axis  = 'both', bottom= True, left  = True, 
                    labelbottom = True, labelleft   = True) 

plt.subplots_adjust(wspace=0.05, hspace=0.05)
plt.show()

Output:

enter image description here

Michael S.
  • 3,050
  • 4
  • 19
  • 34