在seaborn中使用FacetGrid缩放构面以适合数据并使用最大可用空间

问题描述 投票:0回答:1

我正在使用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)

enter image description here

我已经为所有方面添加了值标签,我想进一步修改它以扩展每个方面,因为我有很多情况下值超出了方面的最大空间,有些情况下不可能由于数值不够密集而无法查看数据。第一组案例如下所示,您可以看到标签如何超越下一个方面,降低了图的清晰度。

enter image description here

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

python plot seaborn scaling facet-grid
1个回答
0
投票

我解决了它,一旦评论出下面的一行,小平面就不再重叠了:

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

此外,根据评论中的建议,我将值设置为正数以使小平面彼此更接近,但不引入相邻小平面的标签之间的重叠。我发现0.2的价值很好地满足了需求:

g.fig.subplots_adjust(hspace=.2)

然后在sharey=False构造函数中的FacetGrid参数正在工作。它看起来更干净,但价值并不是填满整个可用空间的所有方式,但这可能是有意的。

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.