Matplotlib tight_layout() doesn't take into account figure suptitle

后端 未结 10 1595
既然无缘
既然无缘 2020-11-28 18:02

If I add a subtitle to my matplotlib figure it gets overlaid by the subplot\'s titles. Does anybody know how to easily take care of that? I tried the tight_layout()

10条回答
  •  悲哀的现实
    2020-11-28 18:15

    Tight layout doesn't work with suptitle, but constrained_layout does. See this question Improve subplot size/spacing with many subplots in matplotlib

    I found adding the subplots at once looked better, i.e.

    fig, axs = plt.subplots(rows, cols, constrained_layout=True)
    
    # then iterating over the axes to fill in the plots
    

    But it can also be added at the point the figure is created:

    fig = plt.figure(constrained_layout=True)
    
    ax1 = fig.add_subplot(cols, rows, 1)
    # etc
    

    Note: To make my subplots closer together, I was also using

    fig.subplots_adjust(wspace=0.05)
    

    and constrained_layout doesn't work with this :(

提交回复
热议问题