2

I am trying to have many subplots (7*4 rows*col) using following code:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(100)
y = np.arange(100)

for i in range(28):
    plt.subplot(4, 7, i+1)
    plt.scatter(x, y)

plt.tight_layout()
plt.show()

It produces all subplots together but with wide margins around individual figures:

enter image description here

If I omit tight_layout() then they are closer together but have a large margin at top, bottom, right and left:

enter image description here

How can I reduce or remove extra space from between as well as around the figures?

rnso
  • 23,686
  • 25
  • 112
  • 234

1 Answers1

1

You can use the subplots_adjust method, adjusting the wspace and hspace arguments as you see fit (width space and height space respectively):

for i in range(28):
    plt.subplot(4, 7, i+1)
    plt.scatter(x, y)

plt.subplots_adjust(wspace=1, hspace=1)

enter image description here

From the documentation, these are "expressed as a fraction of the average axis width/height"

sacuL
  • 49,704
  • 8
  • 81
  • 106
  • Setting `wspace=0.1, hspace=0.1` worked for me. But significant margins at top, bottom, right and especially left still remain. Is there any way to minimize them? – rnso Mar 27 '18 at 18:05
  • You can mess around with the `top` and `bottom` arguments. For instance, try `plt.subplots_adjust(wspace=0.1, hspace=0.1, top=0.95, bottom=0.05)` – sacuL Mar 27 '18 at 18:12
  • I see that even 0.01 is working! – rnso Mar 27 '18 at 18:15
  • Glad I could help! – sacuL Mar 27 '18 at 18:15