简体   繁体   English

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

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

I have a simple seaborn FacetGrid() with barplots inside.我有一个简单的 seaborn FacetGrid() ,里面有条形图。

I applied tight_layout() to my final plot, as xticks had to be properly positioned on the plot after rotation.我对我的最终图应用了tiny_layout() ,因为旋转后 xticks 必须正确定位在图上。 As result, when I want to add the title to the plot it is positioned in the wrong place, basically over the existing axes.结果,当我想将标题添加到绘图时,它被定位在错误的位置,基本上是在现有轴上。

So, my question is how should the title be manipulated in order to be properly positioned in case tight_layout() is applied?所以,我的问题是应该如何操作标题以便在应用tiny_layout() 时正确定位?

I reproduced the issue with the standard tips dataset:我用标准提示数据集重现了这个问题:

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()

在此处输入图片说明

Add (adjust the value to your taste)添加(根据您的口味调整值)

grid.fig.subplots_adjust(top=0.90)

after tight_laout() to make some room at the top of the plot for the suptitle()tight_laout()使一些空间在图的顶部为suptitle()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM