【问题标题】:How to add average line per group in seaborns如何在seaborns中添加每组的平均线
【发布时间】:2020-03-03 17:16:01
【问题描述】:

我有下面的图表:

使用以下代码创建

subset1=data1[feats_to_explore+['wine_class']]
f, (ax1, ax2, ax3) = plt.subplots(3, figsize=(7,14), sharey=True)

l=[ax1,ax2,ax3]

for graph in l:
    for i in (subset1['wine_class']).unique():
        df =subset1[subset1.wine_class == i]
        sns.distplot(df[feats_to_explore[l.index(graph)]],  kde=False, label=i, ax=graph)
        graph.legend(title='Wine Class')

我想为每个直方图添加与相应直方图相同颜色的平均线。换句话说,每个子图三行。

有人可以帮帮我吗?

提前谢谢你。

【问题讨论】:

    标签: python histogram data-visualization seaborn


    【解决方案1】:

    您应该添加一个plt.axvline 呼叫:

    for graph in l:
        for i in (subset1['wine_class']).unique():
            df =subset1[subset1.wine_class == i]
            plt.axvline(df[feats_to_explore[l.index(graph)]].mean())  ## Here
            sns.distplot(df[feats_to_explore[l.index(graph)]],  kde=False, label=i, ax=graph)
            graph.legend(title='Wine Class')
    

    您正在绘制的变量的语法有点奇怪,所以我不确定我写的内容是否会称为您的组的平均值,因此请随意将 df[feats_to_explore[l.index(graph)]].mean() 替换为称为平均值的任何语法你正在策划的小组。

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 2021-10-08
      • 1970-01-01
      • 2017-12-04
      • 2021-07-03
      • 2017-04-24
      • 2020-12-11
      • 1970-01-01
      相关资源
      最近更新 更多