The Python Oracle

Improve subplot size/spacing with many subplots

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Quiet Intelligence

--

Chapters
00:00 Question
00:49 Accepted answer (Score 670)
01:28 Answer 2 (Score 471)
02:13 Answer 3 (Score 84)
03:17 Answer 4 (Score 83)
04:23 Thank you

--

Full question
https://stackoverflow.com/questions/6541...

Accepted answer links:
[matplotlib: Tight Layout guide]: https://matplotlib.org/stable/tutorials/...
[matplotlib.pyplot.tight_layout]: https://matplotlib.org/stable/api/_as_ge...
[matplotlib.figure.Figure.tight_layout]: https://matplotlib.org/stable/api/figure...
[image]: https://i.stack.imgur.com/U7agc.png
[image]: https://i.stack.imgur.com/G4NNT.png

Answer 2 links:
[plt.subplots_adjust]: https://matplotlib.org/stable/api/_as_ge...

Answer 3 links:
[image]: https://i.stack.imgur.com/6pJTd.png
[image]: https://i.stack.imgur.com/Wqltt.png

Answer 4 links:
[constrained_layout]: https://matplotlib.org/tutorials/interme...
[image]: https://i.stack.imgur.com/HcF1b.png
[what's new entry]: https://matplotlib.org/users/whats_new.h...
[Constrained Layout Guide]: https://matplotlib.org/tutorials/interme...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #matplotlib #seaborn #subplot

#avk47



ACCEPTED ANSWER

Score 763


Please review matplotlib: Tight Layout guide and try using matplotlib.pyplot.tight_layout, or matplotlib.figure.Figure.tight_layout

As a quick example:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=4, ncols=4, figsize=(8, 8))
fig.tight_layout() # Or equivalently,  "plt.tight_layout()"

plt.show()

Without Tight Layout

enter image description here


With Tight Layout

enter image description here




ANSWER 2

Score 547


You can use plt.subplots_adjust to change the spacing between the subplots.

call signature:

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

The parameter meanings (and suggested defaults) are:

left  = 0.125  # the left side of the subplots of the figure
right = 0.9    # the right side of the subplots of the figure
bottom = 0.1   # the bottom of the subplots of the figure
top = 0.9      # the top of the subplots of the figure
wspace = 0.2   # the amount of width reserved for blank space between subplots
hspace = 0.2   # the amount of height reserved for white space between subplots

The actual defaults are controlled by the rc file




ANSWER 3

Score 99


Using subplots_adjust(hspace=0) or a very small number (hspace=0.001) will completely remove the whitespace between the subplots, whereas hspace=None does not.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tic

fig = plt.figure(figsize=(8, 8))

x = np.arange(100)
y = 3.*np.sin(x*2.*np.pi/100.)

for i in range(1, 6):
    temp = 510 + i
    ax = plt.subplot(temp)
    plt.plot(x, y)
    plt.subplots_adjust(hspace=0)
    temp = tic.MaxNLocator(3)
    ax.yaxis.set_major_locator(temp)
    ax.set_xticklabels(())
    ax.title.set_visible(False)

plt.show()

hspace=0 or hspace=0.001

enter image description here

hspace=None

enter image description here




ANSWER 4

Score 37


import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10,60))
plt.subplots_adjust( ... )

The plt.subplots_adjust method:

def subplots_adjust(*args, **kwargs):
    """
    call signature::

      subplots_adjust(left=None, bottom=None, right=None, top=None,
                      wspace=None, hspace=None)

    Tune the subplot layout via the
    :class:`matplotlib.figure.SubplotParams` mechanism.  The parameter
    meanings (and suggested defaults) are::

      left  = 0.125  # the left side of the subplots of the figure
      right = 0.9    # the right side of the subplots of the figure
      bottom = 0.1   # the bottom of the subplots of the figure
      top = 0.9      # the top of the subplots of the figure
      wspace = 0.2   # the amount of width reserved for blank space between subplots
      hspace = 0.2   # the amount of height reserved for white space between subplots

    The actual defaults are controlled by the rc file
    """
    fig = gcf()
    fig.subplots_adjust(*args, **kwargs)
    draw_if_interactive()

or

fig = plt.figure(figsize=(10,60))
fig.subplots_adjust( ... )

The size of the picture matters.

"I've tried messing with hspace, but increasing it only seems to make all of the graphs smaller without resolving the overlap problem."

Thus to make more white space and keep the sub plot size the total image needs to be bigger.