Matplotlib subplots_adjust hspace so titles and xlabels don't overlap?

前端 未结 3 1915
南笙
南笙 2020-12-08 00:13

With, say, 3 rows of subplots in matplotlib, xlabels of one row can overlap the title of the next. One has to fiddle with pl.subplots_adjust(hspace)

相关标签:
3条回答
  • 2020-12-08 00:38

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

    subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
    
    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
    
    0 讨论(0)
  • 2020-12-08 00:39

    The link posted by Jose has been updated and pylab now has a tight_layout() function that does this automatically (in matplotlib version 1.1.0).

    http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.tight_layout

    http://matplotlib.org/users/tight_layout_guide.html#plotting-guide-tight-layout

    0 讨论(0)
  • 2020-12-08 00:45

    I find this quite tricky, but there is some information on it here at the MatPlotLib FAQ. It is rather cumbersome, and requires finding out about what space individual elements (ticklabels) take up...

    Update: The page states that the tight_layout() function is the easiest way to go, which attempts to automatically correct spacing.

    Otherwise, it shows ways to acquire the sizes of various elements (eg. labels) so you can then correct the spacings/positions of your axes elements. Here is an example from the above FAQ page, which determines the width of a very wide y-axis label, and adjusts the axis width accordingly:

    import matplotlib.pyplot as plt
    import matplotlib.transforms as mtransforms
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(range(10))
    ax.set_yticks((2,5,7))
    labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))
    
    def on_draw(event):
       bboxes = []
       for label in labels:
           bbox = label.get_window_extent()
           # the figure transform goes from relative coords->pixels and we
           # want the inverse of that
           bboxi = bbox.inverse_transformed(fig.transFigure)
           bboxes.append(bboxi)
    
       # this is the bbox that bounds all the bboxes, again in relative
       # figure coords
       bbox = mtransforms.Bbox.union(bboxes)
       if fig.subplotpars.left < bbox.width:
           # we need to move it over
           fig.subplots_adjust(left=1.1*bbox.width) # pad a little
           fig.canvas.draw()
       return False
    
    fig.canvas.mpl_connect('draw_event', on_draw)
    
    plt.show()
    
    0 讨论(0)
提交回复
热议问题