Reduce space between subplots

68 views Asked by At

Do you know how to reduce the space between these plots?

fig, axs = plt.subplots(2)
axs[0].imshow(np.random.randint(0,15,size=(70,714)))
axs[1].imshow(np.random.randint(0,15,size=(70,714)))
plt.show()

I´m expected to generate the plots with only a small distance between them.

2

There are 2 answers

0
Arne On

You can use fig.tight_layout and adjust the height padding (h_pad) parameter as needed. For example:

fig, axs = plt.subplots(2) 
fig.tight_layout(h_pad=-20)
axs[0].imshow(np.random.randint(0,15,size=(70,714))) 
axs[1].imshow(np.random.randint(0,15,size=(70,714))) 
plt.show()

example plot

1
RuthC On

Compressed layout was designed for fixed aspect ratio plots such as this:

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(2, layout="compressed")
axs[0].imshow(np.random.randint(0,15,size=(70,714)))
axs[1].imshow(np.random.randint(0,15,size=(70,714)))
plt.show()

enter image description here