且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Seaborn:基于箱线图的线性回归

更新时间:2022-06-22 06:59:25

改用 sns.regplot(),它是一个图形级别的函数,可以让两个图都放在同一个图形中.

Use sns.regplot() instead, it's a figure-level function that lets both plots be placed in the same figure.

下面的代码将为您提供一个箱形图,上面带有回归线.它还从回归中消除了分散.您可以根据需要更改回归的顺序.当箱线图和regplot使用相同的数据时,这将起作用.如果您要定义另一个数据集 df,请在您的问题中加以说明,我会更新此答案.

The code below will give you a boxplot with regression line over it. It also removes the scatter from the regression. You can change the order of the regression as you see fit. This will work when the boxplot and regplot are using the same data. If you are defining another dataset, df, then please clarify in your question and I'll update this answer.

tips = sns.load_dataset("tips")

fig, ax = plt.subplots()
sns.boxplot(x="size", y="tip", data=tips, ax=ax)
sns.regplot(x="size", y="tip", data=tips, ax=ax, scatter=False)

plt.show()