如何向每个面添加水平平均线和注释

ZKI*_*ING 2 python matplotlib pandas seaborn facet-grid

我有一个 2 行 1 列的简单 FacetGrid,其中的线图表示构面的不同类别 - 如下图。

# lineplot for each Category over the last three years
g = sns.FacetGrid(df, row="Category", sharey=False, sharex=False, height=2.5, aspect = 3)
g = g.map(plt.plot, 'Date', 'Count')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如何添加显示每个方面的平均计数的参考线和注释?

样本数据

  • 读取示例数据框
# lineplot for each Category over the last three years
g = sns.FacetGrid(df, row="Category", sharey=False, sharex=False, height=2.5, aspect = 3)
g = g.map(plt.plot, 'Date', 'Count')
Run Code Online (Sandbox Code Playgroud)

df.head()

import pandas as pd

data = {'Category': ['Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1',
                     'Group 1', 'Group 1', 'Group 1', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2',
                     'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2', 'Group 2'],
        'Date': ['2017-01-31', '2017-02-28', '2017-03-31', '2017-04-30', '2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31', '2017-09-30', '2017-10-31', '2017-11-30', '2017-12-31', '2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30', '2018-05-31', '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31', '2018-11-30', '2018-12-31',
                 '2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31', '2019-08-31', '2019-09-30', '2017-01-31', '2017-02-28', '2017-03-31', '2017-04-30', '2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31', '2017-09-30', '2017-10-31', '2017-11-30', '2017-12-31', '2018-01-31', '2018-02-28', '2018-03-31',
                 '2018-04-30', '2018-05-31', '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31', '2018-11-30', '2018-12-31', '2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31', '2019-08-31', '2019-09-30'],
        'Count': [226, 235, 236, 221, 187, 218, 225, 221, 248, 224, 204, 224, 218, 241, 196, 246, 256, 217, 229, 230, 222, 215, 226, 227, 232, 233, 224, 214, 243, 214, 235, 218, 208, 208, 254, 223, 227, 245, 222, 226, 235, 225, 226, 258, 234, 257, 224, 228, 222, 227, 256, 217, 243, 230, 250, 197, 232, 248, 232, 259, 259, 229, 228, 234, 218, 231]}

df = pd.DataFrame(data)
df.Date = pd.to_datetime(df.Date)
Run Code Online (Sandbox Code Playgroud)

Imp*_*est 6

g = sns.relplot(data=df, kind='line', x='Date', y='Count', row='Category', height=2.5, aspect=3, facet_kws={'sharey': True, 'sharex': False})
g.fig.tight_layout()


def custom(y, **kwargs):
    ym = y.mean()
    plt.axhline(ym, color="orange", linestyle="dashed")
    plt.annotate(f"mean: {y.mean():.3f}", xy=(1,ym), 
                 xycoords=plt.gca().get_yaxis_transform(), ha="right")
    

g = g.map(custom, 'Count')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


只需一行就足够了

g = sns.FacetGrid(df, row="Category", sharey=False, sharex=False, height=2.5, aspect = 3)
g = g.map(plt.plot, 'Date', 'Count')

# add this to get a horizontal line
g = g.map(lambda y, **kw: plt.axhline(y.mean(), color="k"), 'Count')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

要使线条橙色和虚线并添加注释,您可以这样做

def custom(y, **kwargs):
    ym = y.mean()
    plt.axhline(ym, color="orange", linestyle="dashed")
    plt.annotate(f"mean: {y.mean():.3f}", xy=(1,ym), 
                 xycoords=plt.gca().get_yaxis_transform(), ha="right")
    

g = sns.FacetGrid(df, row="Category", sharey=False, sharex=False, height=2.5, aspect = 3)
g = g.map(plt.plot, 'Date', 'Count')
    
g = g.map(custom, 'Count')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述