1

I'm trying to reproduce an example by Thucydides411 on How to remove gaps between subplots in matplotlib? using the following code:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8,8)) # Notice the equal aspect ratio
ax = [fig.add_subplot(2,2,i+1) for i in range(4)]

for a in ax:
    a.set_xticklabels([])
    a.set_yticklabels([])
    a.set_aspect('equal')

fig.subplots_adjust(wspace=0, hspace=0)

plt.show()

Since the figure size has an equal aspect ratio, I would expect the subplots to have no gaps between them. However, what I see is the following:

enter image description here

Although there is no vertical gap, there is a small horizontal gap. Any ideas why this is not working as it did for Thucydides411?

Community
  • 1
  • 1
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526

1 Answers1

1

One way to solve it is to use tight_layout instead of subplots_adjust:

fig.tight_layout(h_pad=0, w_pad=0)

The resulting figure now has no gaps:

enter image description here

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526