【问题标题】:Seaborn Lmplot one line per category (hue) and a overall lineSeaborn Lmplot 每个类别(色调)一条线和一条整体线
【发布时间】:2020-04-29 15:38:16
【问题描述】:

使用 lmplot 时,我可以为每个类别制作不同的线条,如下所示:

sns.lmplot( x='x', y='y', data=df, hue='z')

有没有办法汇总所有这些?我的意思是,如果我对 z (1,2,3) 有三个类别,我希望有一条线代表 1,一条线代表 2,一条线代表 3,以及整个数据集的整体趋势线。

df 类似于:

| x | y  | z |
|---|----|---|
| 1 | 2  | a |
| 2 | 6  | b |
| 3 | 6  | a |
| 4 | 12 | b |
| 5 | 10 | a |
| 6 | 18 | b |

这可能吗?

z=a 是 2 倍,z=b 是 3 倍。

【问题讨论】:

  • 您能否提供一个您的df 的示例,以便我们可以在这里进行讨论?
  • 用 df 更新了帖子

标签: seaborn


【解决方案1】:

一种可能性是返回坐标轴并使用regplot 重新绘制数据 请参考https://seaborn.pydata.org/generated/seaborn.regplot.html#seaborn.regplot

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

sns.set(style="white")
df = pd.DataFrame({'x': [1,2,3,4,5,6],
                   'y': [2,6,6,12,10,18],
                   'z': ['a', 'b', 'a', 'b','a', 'b']})
g = sns.lmplot(x='x', 
               y='y', 
               data=df, 
               hue='z',
               legend=False) # delegate legend to plt.legend further down
sns.regplot(x='x', y='y', data=df, 
            ax=g.axes.flat[0], 
            color="g",
            label='a+b',
            scatter=False,
            ci=68
            )
plt.legend()
g.savefig('/Users/massimopinto/Desktop/regplot.png', bbox_inches='tight')

生成

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 2018-11-19
    • 2018-11-20
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 2021-02-09
    相关资源
    最近更新 更多