seaborn:如何在分组的barplot上添加误差线 [英] seaborn: how to add error bars on a grouped barplot

查看:71
本文介绍了seaborn:如何在分组的barplot上添加误差线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

注意,通过简单地反转类别和子类别

  cat =样本集"subcat = "候选人"

您可以获得不同的分组:

I have a data frame df that has four columns: Candidate, Sample_Set, Values, and Error. The Candidate column has, say, three unique entries: [X, Y, Z] and we have three sample sets, such that Sample_Set has three unique values as well: [1,2,3]. The df would roughly look like this.

Candidate,Sample_Set,Values,Error
X,1,20,50
Y,1,10,50
Z,1,10,50
X,2,200,30
Y,2,101,30
Z,2,99,30
X,3,1999,10
Y,3,998,10
Z,3,1003,10

I am using to create a grouped barplot out of this with x="Candidate", y="Values", hue="Sample_Set". All's good, until I try to add an error bar along the y-axis using the values under the column named Error. I am using the following code.

import seaborn as sns

ax = sns.factorplot(x="Candidate", y="Values", hue="Sample_Set", data=df,
                    size=8, kind="bar")

How do I incorporate the error?

I would appreciate a solution or a more elegant approach on the task.

解决方案

As @ResMar pointed out in the comments, there seems to be no built-in functionality in seaborn to easily set individual errorbars.

If you rather care about the result than the way to get there, the following (not so elegant) solution might be helpful, which builds on matplotlib.pyplot.bar. The seaborn import is just used to get the same style.

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

def grouped_barplot(df, cat,subcat, val , err):
    u = df[cat].unique()
    x = np.arange(len(u))
    subx = df[subcat].unique()
    offsets = (np.arange(len(subx))-np.arange(len(subx)).mean())/(len(subx)+1.)
    width= np.diff(offsets).mean()
    for i,gr in enumerate(subx):
        dfg = df[df[subcat] == gr]
        plt.bar(x+offsets[i], dfg[val].values, width=width, 
                label="{} {}".format(subcat, gr), yerr=dfg[err].values)
    plt.xlabel(cat)
    plt.ylabel(val)
    plt.xticks(x, u)
    plt.legend()
    plt.show()

df = pd.read_csv("candf.txt")
print df

cat = "Candidate"
subcat = "Sample_Set"
val = "Values"
err = "Error"
grouped_barplot(df, cat, subcat, val, err )

Note that by simply inversing the category and subcategory

cat = "Sample_Set"
subcat = "Candidate"

you can get a different grouping:

这篇关于seaborn:如何在分组的barplot上添加误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆