Seaborn:基于箱线图的线性回归 [英] Seaborn : linear regression on top of a boxplot

查看:148
本文介绍了Seaborn:基于箱线图的线性回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 seaborn,我如何使用 sns.boxplot 和 sns.lmplot 从相同数据中获得带有回归线的箱线图?这不起作用:

With seaborn, how I can use sns.boxplot and sns.lmplot to obtain a boxplot with a regression line from the same data ? This does not work :

tips = sns.load_dataset("tips")
ax = sns.boxplot(x="size", y="tip", data=df)
ax = sns.lmplot(x="size", y="tip", data=tips, x_estimator=np.mean); 

推荐答案

改用 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()

这篇关于Seaborn:基于箱线图的线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆