如何在seaborn.lmplot中为整个数据添加回归线?

时间:2020-10-26 10:48:53

标签: python-3.x matplotlib seaborn

我正在尝试绘制散点图,其中每个点都带有变量Points上色。此外,我想添加回归线。

import pandas as pd
import urllib3
import seaborn as sns

decathlon = pd.read_csv("https://raw.githubusercontent.com/leanhdung1994/Deep-Learning/main/decathlon.txt", sep='\t')

g = sns.lmplot(
    data = decathlon,
    x="100m", y="Long.jump",
    hue = 'Points', palette = 'viridis'
)

enter image description here

在我看来,有2条回归线,每组数据对应一条。这不是我想要的。我想对整个数据使用回归线。此外,如何隐藏图例的右侧?

您能否详细说明如何做?

1 个答案:

答案 0 :(得分:1)

除非应该使用FacetGrid将数据集划分为多个子图,否则不应使用lmplot

由于显示的示例未使用FacetGrid提供的任何功能,因此您应该使用scatterplot()regplot()的组合来创建图

tips = sns.load_dataset('tips')
ax = sns.scatterplot(data=tips, x="total_bill", y="tip", hue="day")
sns.regplot(data=tips, x="total_bill", y="tip", scatter=False, ax=ax)

enter image description here