增加FacetGrid图上行之间的空间

Rod*_*d0n 4 matplotlib seaborn

我有以下代码创建了可以在图片中看到的图:

g = sns.FacetGrid(data, col="Provincia",col_wrap=6,size=2.5)
g.map(sns.barplot, "Anio", "Diff");
g.set_axis_labels("Año", "Porcentaje de aumento");

for ax in g.axes.flat:
    _ = plt.setp(ax.get_yticklabels(), visible=True)
    _ = plt.setp(ax.get_xticklabels(), visible=False)
    _ = plt.setp(ax.get_xticklabels()[0], visible=True)    
    _ = plt.setp(ax.get_xticklabels()[-1], visible=True)
Run Code Online (Sandbox Code Playgroud)

如您在图片中所看到的,问题是x刻度折叠起来并带有下面的col名称。为了解决此问题,增加此空间的正确方法是什么?

输出

Imp*_*est 10

布局紧凑

您可以tight_layout用来自动调整间距

g.fig.tight_layout()
Run Code Online (Sandbox Code Playgroud)

或者,如果您已将matplotlib.pyplot导入为plt

plt.tight_layout()
Run Code Online (Sandbox Code Playgroud)

子图调整

You can use plt.subplots_adjust to manually set the spacings between subplots,

plt.subplots_adjust(hspace=0.4, wspace=0.4)
Run Code Online (Sandbox Code Playgroud)

where hspace is the space in height, and wspace is the space width direction.

gridspec keyword arguments

You could also use gridspec_kws in the FacetGrid initialization,

g = sns.FacetGrid(data, ... , gridspec_kws={"wspace":0.4})
Run Code Online (Sandbox Code Playgroud)

However, this can only be used if col_wrap is not set. (So it might not be an option in the particular case from the question).