Seaborn boxplots changes (narrows) width of boxes when a hue is chosen, how might I remedy this?

Seaborn boxplots changes (narrows) width of boxes when a hue is chosen, how might I remedy this?

我正在使用 seaborn 创建箱线图。当我指定 group/color 框的列时,框的宽度变得很窄,很难看到。我所做的唯一更改是为 hue 指定一个参数,它指向传递的数据框中的一列。我尝试使用 'width' 参数(如 所述),它确实增加了箱线图的宽度,但也增加了它们分开的距离。

帮助:如何在指定色调参数时保持框的宽度?

我将在下面展示我的代码和结果:

我的数据框:

Out[3]: 
                   timestamp   room_number floor       floor_room  temperature
0  2016-01-19 09:00:00-05:00         11a06    11         11_11a06          23.0
1  2016-01-19 09:00:00-05:00    east-inner    11    11_east-inner          22.8
2  2016-01-19 09:00:00-05:00   east-window    11   11_east-window          22.9

使用奇数箱线图宽度的 seaborn,使用分组因子:

sns.boxplot(x=xunit, y=var, data=df, order=order, hue='floor')

使用具有合理箱线图宽度但没有分组因子的 seaborn:

sns.boxplot(x=xunit, y=var, data=df)

原来是 'hue' 参数导致了问题(我不确定为什么)。通过从函数中删除此 parameter/argument,问题就消失了,但您必须提供额外的信息,以便箱线图根据所需的条件进行颜色编码。以下代码行解决了我的问题:

sns.boxplot(x=xunit, y=var, data=df, order=order,palette=df[condition_column].map(palette_dir))

其中 palette_dir 是每个条件的颜色字典,映射到一列数据。

箱线图现在看起来很正常,但我正在努力添加图例。我希望解决此问题的人 可以指出他们的方法。

在版本 0.8(2017 年 7 月)中,添加了 dodge 参数

to boxplot, violinplot, and barplot to allow use of hue without changing the position or width of the plot elements, as when the hue varible is not nested within the main categorical variable.

(release notes v0.8.0)

您的代码如下所示:

sns.boxplot(x=xunit, y=var, data=df, order=order, hue='floor', dodge=False)