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

后端 未结 10 1593
既然无缘
既然无缘 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:25

    As mentioned by others, by default the tight layout does not take suptitle into account. However, I have found it is possible to use the bbox_extra_artists argument to pass in the suptitle as a bounding box that should be taken into account:

    st = fig.suptitle("My Super Title")
    plt.savefig("figure.png", bbox_extra_artists=[st], bbox_inches='tight')
    

    This forces the tight layout calculation to take the suptitle into account, and it looks as you would expect.

提交回复
热议问题