繁体   English   中英

如何在0级(恒定)的海洋散点图中拟合回归线?

[英]How can I fit a regression line in a seaborn scatterplot of order 0 (constant)?

我有一份令人难以置信的散点图,其中包含跨发行版的不同组的指标。 我想为每组设置一条固定的线,以查看它们之间的性能比较。 我试图这样使用seaborn.lmplot:

 seaborn.lmplot(x="release", y=metric, palette="PRGn", hue=hue, data=data, order=0)

不幸的是,参数order的最小值似乎为1,因此它适合于order 1(y = ax + b)而不是order 0(y = c)的行。 有没有简单的方法可以使用seaborn拟合一条恒定线?

您可以获取lmplot绘制到的轴,然后使用axhline绘制一条水平线。 使用sns.color_palette()获取seaborn将使用的颜色列表,然后在axhline调用中使用相同的颜色:

import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")
g = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, markers=["o", "x"])

current_palette = sns.color_palette()  # get colors that seaborn uses

ax = g.axes[0][0]

# draw horizontal lines making sure they are the same color as seaborn uses
for i in range(len(ax.lines)):
    ax.axhline(i + 3, c=current_palette[i])

plt.show()

在此处输入图片说明

根据评论,最终的解决方案是使上面的示例适应以下条件:

g = sns.lmplot(x="release", y=variable, hue=hue, data=data, fit_reg=False)

current_palette = sns.color_palette()

medians = data.groupby([hue]).median()[variable].tolist()

for i in range(len(medians)):
    g.ax.axhline(medians[i], c=current_palette[i])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM