在 seaborn 中使用 FacetGrid 缩放面以适应数据并使用最大可用 space

Scaling facets to fit the data and use maximum available space using FacetGrid in seaborn

我正在使用 FacetGrid 提供的示例 here,其中 FacetGrid 对象使用下面的代码片段初始化,结果如下图所示。

# Initialize the FacetGrid object
g = sns.FacetGrid(df, row="g", hue="g", height=2, aspect=10, palette=sns.color_palette("Blues", 1))

# Draw the densities in a few steps
g.map(sns.kdeplot, "x", clip_on=False, shade=True, alpha=1, lw=1.5, bw=.2)
g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw=.2)
g.map(plt.axhline, y=0, lw=2, clip_on=False)

我已经为所有方面添加了值标签,我想进一步修改它以缩放每个方面,因为我有很多情况下值超出了方面的最大值 space ,以及由于值不够密集而无法查看数据的某些情况。下面介绍第一组案例,可以看到标签是如何超出下一个方面的,降低了情节的清晰度。

我想实现的是每个方面的单独缩放,标记值,并将最大值放在可用方面的顶部 space。

我解决了,一旦注释掉下面的行,面就不再重叠了:

# Set the subplots to overlap
g.fig.subplots_adjust(hspace=-.25)

此外,根据评论中的建议,我将值设置为正数以拉近切面之间的距离,但不会引入相邻切面标签之间的重叠。我发现 0.2 的值很适合需求:

g.fig.subplots_adjust(hspace=.2)

然后 FacetGrid 构造函数中的 sharey=False 参数起作用了。它看起来更干净,但这些值并没有完全填满整个可用 space,但这可能是有意的。

g = sns.FacetGrid(df, row="g", hue="g", height=2, aspect=10, sharey=False, palette=sns.color_palette("Blues", 1))