Seaborn:在箱线图顶部覆盖散点图

2024-04-20 04:56:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在箱线图的顶部覆盖一个简单的散点图。当我分别绘制这两个图时,一切正常:

boxplot soloscatterplot solo

但当我尝试将它们组合起来时,几乎看起来散射图的x值都被2除:

together

我很确定这是因为箱线图将x轴值视为分类的(即使它们在我的数据帧中是浮动的),而散点图将它们视为连续的。但我不知道解决办法是什么。以下是我正在使用的代码:

sns.scatterplot(data=old_data, x="x-axis", y="y-axis", s=200, color="red", label="Old Data")
sns.boxplot(data=new_data, x="x-axis", y="y-axis", color="blue")
plt.plot([], [], label="New Data", color='blue') # this just adds the boxplot label to the legend
plt.legend()

附加问题:如果您知道如何更好地将箱线图标签添加到图例中,我很想听听


Tags: thedata绘制分类pltbluelabelcolor