• <bdo id='1kkdp'></bdo><ul id='1kkdp'></ul>
    <legend id='1kkdp'><style id='1kkdp'><dir id='1kkdp'><q id='1kkdp'></q></dir></style></legend><tfoot id='1kkdp'></tfoot>

      1. <small id='1kkdp'></small><noframes id='1kkdp'>

      2. <i id='1kkdp'><tr id='1kkdp'><dt id='1kkdp'><q id='1kkdp'><span id='1kkdp'><b id='1kkdp'><form id='1kkdp'><ins id='1kkdp'></ins><ul id='1kkdp'></ul><sub id='1kkdp'></sub></form><legend id='1kkdp'></legend><bdo id='1kkdp'><pre id='1kkdp'><center id='1kkdp'></center></pre></bdo></b><th id='1kkdp'></th></span></q></dt></tr></i><div id='1kkdp'><tfoot id='1kkdp'></tfoot><dl id='1kkdp'><fieldset id='1kkdp'></fieldset></dl></div>
      3. 如何从列在分组条形图上添加错误条

        How to add error bars on a grouped barplot from a column(如何从列在分组条形图上添加错误条)
        <i id='1vZ80'><tr id='1vZ80'><dt id='1vZ80'><q id='1vZ80'><span id='1vZ80'><b id='1vZ80'><form id='1vZ80'><ins id='1vZ80'></ins><ul id='1vZ80'></ul><sub id='1vZ80'></sub></form><legend id='1vZ80'></legend><bdo id='1vZ80'><pre id='1vZ80'><center id='1vZ80'></center></pre></bdo></b><th id='1vZ80'></th></span></q></dt></tr></i><div id='1vZ80'><tfoot id='1vZ80'></tfoot><dl id='1vZ80'><fieldset id='1vZ80'></fieldset></dl></div>

      4. <tfoot id='1vZ80'></tfoot>

          <small id='1vZ80'></small><noframes id='1vZ80'>

          <legend id='1vZ80'><style id='1vZ80'><dir id='1vZ80'><q id='1vZ80'></q></dir></style></legend>
            • <bdo id='1vZ80'></bdo><ul id='1vZ80'></ul>
                  <tbody id='1vZ80'></tbody>
                  本文介绍了如何从列在分组条形图上添加错误条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个pandas数据框df,它有四列:CandidateSample_SetValuesError。比方说,Candidate列有三个唯一条目:[X, Y, Z],我们有三个样本集,因此Sample_Set也有三个唯一值:[1,2,3]。DF大致如下所示。

                  import pandas as pd
                  
                  data = {'Candidate': ['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z'],
                          'Sample_Set': [1, 1, 1, 2, 2, 2, 3, 3, 3],
                          'Values': [20, 10, 10, 200, 101, 99, 1999, 998, 1003],
                          'Error': [5, 2, 3, 30, 30, 30, 10, 10, 10]}
                  df = pd.DataFrame(data)
                  
                  # display(df)
                    Candidate  Sample_Set  Values  Error
                  0         X           1      20      5
                  1         Y           1      10      2
                  2         Z           1      10      3
                  3         X           2     200     30
                  4         Y           2     101     30
                  5         Z           2      99     30
                  6         X           3    1999     10
                  7         Y           3     998     10
                  8         Z           3    1003     10
                  

                  我使用seaborn用x="Candidate"y="Values"hue="Sample_Set"创建了一个分组条形图。一切都很好,直到我尝试使用名为Error的列下的值沿y轴添加一个误差栏。我正在使用以下代码。

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

                  如何合并错误?

                  我希望能为这项任务提供解决方案或更优雅的方法。

                  推荐答案

                  正如@Resmar在评论中指出的那样,Seborn中似乎没有用于轻松设置单个错误条的内置功能。

                  如果您更关心结果而不是实现目标的方式,以下(不是很优雅的)解决方案可能会有帮助,它建立在matplotlib.pyplot.bar的基础上。海运导入仅用于获取相同样式。

                  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()
                  
                  
                  cat = "Candidate"
                  subcat = "Sample_Set"
                  val = "Values"
                  err = "Error"
                  
                  # call the function with df from the question
                  grouped_barplot(df, cat, subcat, val, err )
                  

                  请注意,只需颠倒类别和子类别

                  cat = "Sample_Set"
                  subcat = "Candidate"
                  

                  您可以获得不同的分组:

                  这篇关于如何从列在分组条形图上添加错误条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)

                    <small id='3rR1n'></small><noframes id='3rR1n'>

                      1. <tfoot id='3rR1n'></tfoot>

                          • <bdo id='3rR1n'></bdo><ul id='3rR1n'></ul>
                            <i id='3rR1n'><tr id='3rR1n'><dt id='3rR1n'><q id='3rR1n'><span id='3rR1n'><b id='3rR1n'><form id='3rR1n'><ins id='3rR1n'></ins><ul id='3rR1n'></ul><sub id='3rR1n'></sub></form><legend id='3rR1n'></legend><bdo id='3rR1n'><pre id='3rR1n'><center id='3rR1n'></center></pre></bdo></b><th id='3rR1n'></th></span></q></dt></tr></i><div id='3rR1n'><tfoot id='3rR1n'></tfoot><dl id='3rR1n'><fieldset id='3rR1n'></fieldset></dl></div>
                              <tbody id='3rR1n'></tbody>

                            <legend id='3rR1n'><style id='3rR1n'><dir id='3rR1n'><q id='3rR1n'></q></dir></style></legend>