Seaborn Lmplot每个类别(色相)一行,总行

时间:2020-04-24 16:33:21

标签: seaborn

使用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。

1 个答案:

答案 0 :(得分:0)

一种可能性是回到轴并用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')

生成

overlay with .map over the facetgrid axes