Improve subplot size/spacing with many subplots in matplotlib

前端 未结 6 1526
一整个雨季
一整个雨季 2020-11-22 12:56

Very similar to this question but with the difference that my figure can be as large as it needs to be.

I need to generate a whole bunch of vertically-stacked plots

相关标签:
6条回答
  • 2020-11-22 13:03

    I found that subplots_adjust(hspace = 0.001) is what ended up working for me. When I use space = None, there is still white space between each plot. Setting it to something very close to zero however seems to force them to line up. What I've uploaded here isn't the most elegant piece of code, but you can see how the hspace works.

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.ticker as tic
    
    fig = plt.figure()
    
    x = np.arange(100)
    y = 3.*np.sin(x*2.*np.pi/100.)
    
    for i in range(5):
        temp = 510 + i
        ax = plt.subplot(temp)
        plt.plot(x,y)
        plt.subplots_adjust(hspace = .001)
        temp = tic.MaxNLocator(3)
        ax.yaxis.set_major_locator(temp)
        ax.set_xticklabels(())
        ax.title.set_visible(False)
    
    plt.show()
    

    enter image description here

    0 讨论(0)
  • 2020-11-22 13:03

    Similar to tight_layout matplotlib now (as of version 2.2) provides constrained_layout. In contrast to tight_layout, which may be called any time in the code for a single optimized layout, constrained_layout is a property, which may be active and will optimze the layout before every drawing step.

    Hence it needs to be activated before or during subplot creation, such as figure(constrained_layout=True) or subplots(constrained_layout=True).

    Example:

    import matplotlib.pyplot as plt
    
    fig, axes = plt.subplots(4,4, constrained_layout=True)
    
    plt.show()
    

    constrained_layout may as well be set via rcParams

    plt.rcParams['figure.constrained_layout.use'] = True
    

    See the what's new entry and the Constrained Layout Guide

    0 讨论(0)
  • 2020-11-22 13:09
    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.

    0 讨论(0)
  • 2020-11-22 13:18

    Try using plt.tight_layout

    As a quick example:

    import matplotlib.pyplot as plt
    
    fig, axes = plt.subplots(nrows=4, ncols=4)
    fig.tight_layout() # Or equivalently,  "plt.tight_layout()"
    
    plt.show()
    

    Without Tight Layout

    enter image description here


    With Tight Layout enter image description here

    0 讨论(0)
  • 2020-11-22 13:21

    You could try the subplot_tool()

    plt.subplot_tool()
    
    0 讨论(0)
  • 2020-11-22 13:28

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

    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

    0 讨论(0)
提交回复
热议问题