如果应用 tight_layout,如何在 seaborn.FacetGrid 中重新定位标题?

How to reposition title in seaborn.FacetGrid if tight_layout is applied?

我有一个简单的 seaborn FacetGrid(),里面有条形图。

我将 tight_layout() 应用于我的最终绘图,因为 xticks 必须在旋转后正确定位在绘图上。 结果,当我想将标题添加到绘图时,它位于错误的位置,基本上是在现有轴上。

所以,我的问题是应该如何操作标题以便在 tight_layout()[=28= 的情况下正确定位] 已应用?

我用标准提示数据集重现了这个问题:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")

days_dict = {day:day+', a long name' for day in tips['day'].unique()}
tips['day_long'] = tips['day'].map(lambda x: days_dict[x])

grid = sns.FacetGrid(tips,col='size',col_wrap=3,height=4,sharex=False)

grid.map(sns.barplot, 'day_long', 'total_bill').set_titles('{col_name}')
grid.fig.set_size_inches(10,10)
grid.fig.suptitle('Title (it should be placed higher)',fontsize=16)

for ax in grid.axes.flat:
    for label in ax.get_xticklabels():
        label.set_rotation(90)

plt.tight_layout()
plt.show()

添加(根据您的喜好调整值)

grid.fig.subplots_adjust(top=0.90)

tight_laout() 之后在地块顶部为 suptitle()

腾出一些空间