将拆分图(点图)添加到分组箱线图 - Pandas 和 Seaborn

Adding splitplot (dotplot) to grouped boxplot - Panda and Seaborn

我是第一次使用 seaborn,并尝试制作一个嵌套(分组)箱线图,并将数据点添加为点。这是我的代码:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.set(style="ticks")
## Draw a nested boxplot to show bills by day and sex
sns.boxplot(x="day", y="total_bill", hue="smoker",data=tips,width=0.5,palette="PRGn",linewidth=1)

## Draw a split strip plot
sns.stripplot(x="day", y="total_bill", hue="smoker",palette="PRGn",data=tips,size=4,edgecolor="gray",
             split=True)
sns.despine(offset=10, trim=True)
plt.show()

和图:

由于箱线图中使用了 'width' 参数,您会看到点没有以框为中心。有什么办法可以将点与框对齐? boxplot 命令中的宽度参数是未对齐点的原因。

p.s。 - 我已经添加了汤姆提到的 MCVE。

巴德

组间距离是自动计算的,据我所知,没有简单的方法可以更改它,但您在箱线图中使用间接方式修改它:关键字 width。使用默认值,一切都会对齐。

sns.set(style="ticks")
sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips,
            palette="PRGn", linewidth=1)
sns.stripplot(x="day", y="total_bill", hue="smoker", data=tips,
              palette="PRGn", size=4, edgecolor="gray", split=True)
sns.despine(offset=10, trim=True)