<bdo id='R9Nbl'></bdo><ul id='R9Nbl'></ul>

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

      <small id='R9Nbl'></small><noframes id='R9Nbl'>

      <legend id='R9Nbl'><style id='R9Nbl'><dir id='R9Nbl'><q id='R9Nbl'></q></dir></style></legend>
      1. <tfoot id='R9Nbl'></tfoot>
      2. 如何按行计算百分比并注释 100% 堆积条

        时间:2023-09-14

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

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

                • <legend id='Yjvrp'><style id='Yjvrp'><dir id='Yjvrp'><q id='Yjvrp'></q></dir></style></legend>
                    <tbody id='Yjvrp'></tbody>

                  本文介绍了如何按行计算百分比并注释 100% 堆积条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要帮助在从数据帧中的交叉表创建的 pandas 堆积条形图的每个部分中添加总数的百分比分布(无小数).

                  I need help adding the percent distribution of the total (no decimals) in each section of a stacked bar plot in pandas created from a crosstab in a dataframe.

                  这里是示例数据:

                  data = {
                      'Name':['Alisa','Bobby','Bobby','Alisa','Bobby','Alisa',
                              'Alisa','Bobby','Bobby','Alisa','Bobby','Alisa'],
                      'Exam':['Semester 1','Semester 1','Semester 1','Semester 1','Semester 1','Semester 1',
                              'Semester 2','Semester 2','Semester 2','Semester 2','Semester 2','Semester 2'],
                       
                      'Subject':['Mathematics','Mathematics','English','English','Science','Science',
                                 'Mathematics','Mathematics','English','English','Science','Science'],
                     'Result':['Pass','Pass','Fail','Pass','Fail','Pass','Pass','Fail','Fail','Pass','Pass','Fail']}
                  df = pd.DataFrame(data)
                  
                  # display(df)
                       Name        Exam      Subject Result
                  0   Alisa  Semester 1  Mathematics   Pass
                  1   Bobby  Semester 1  Mathematics   Pass
                  2   Bobby  Semester 1      English   Fail
                  3   Alisa  Semester 1      English   Pass
                  4   Bobby  Semester 1      Science   Fail
                  5   Alisa  Semester 1      Science   Pass
                  6   Alisa  Semester 2  Mathematics   Pass
                  7   Bobby  Semester 2  Mathematics   Fail
                  8   Bobby  Semester 2      English   Fail
                  9   Alisa  Semester 2      English   Pass
                  10  Bobby  Semester 2      Science   Pass
                  11  Alisa  Semester 2      Science   Fail
                  

                  这是我的代码:

                  #crosstab
                  pal = ["royalblue", "dodgerblue", "lightskyblue", "lightblue"]
                  ax= pd.crosstab(df['Name'], df['Subject']).apply(lambda r: r/r.sum()*100, axis=1)
                  ax.plot.bar(figsize=(10,10),stacked=True, rot=0, color=pal)
                  display(ax)
                      
                  plt.legend(loc='best', bbox_to_anchor=(0.1, 1.0),title="Subject",)
                  
                  plt.xlabel('Name')
                  plt.ylabel('Percent Distribution')
                  
                  plt.show()
                  

                  我知道我需要以某种方式添加 plt.text,但无法弄清楚.我希望将总数的百分比嵌入堆叠的条形图中.

                  I know I need to add a plt.text some how, but can't figure it out. I would like the percent of the totals to be embedded within the stacked bars.

                  推荐答案

                  我们试试吧:

                  # crosstab
                  pal = ["royalblue", "dodgerblue", "lightskyblue", "lightblue"]
                  ax= pd.crosstab(df['Name'], df['Subject']).apply(lambda r: r/r.sum()*100, axis=1)
                  ax_1 = ax.plot.bar(figsize=(10,10), stacked=True, rot=0, color=pal)
                  display(ax)
                  
                  plt.legend(loc='upper center', bbox_to_anchor=(0.1, 1.0), title="Subject")
                  
                  plt.xlabel('Name')
                  plt.ylabel('Percent Distribution')
                  
                  for rec in ax_1.patches:
                      height = rec.get_height()
                      ax_1.text(rec.get_x() + rec.get_width() / 2, 
                                rec.get_y() + height / 2,
                                "{:.0f}%".format(height),
                                ha='center', 
                                va='bottom')
                      
                  plt.show()
                  

                  输出:

                  
                  Subject English Mathematics Science
                  Name            
                  Alisa   33.333333   33.333333   33.333333
                  Bobby   33.333333   33.333333   33.333333
                  

                  这篇关于如何按行计算百分比并注释 100% 堆积条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Seaborn Catplot 在条形图上设置值 下一篇:Python 和 Matplotlib 以及鼠标悬停注释

                  相关文章

                  最新文章

                    <small id='movyt'></small><noframes id='movyt'>

                    <legend id='movyt'><style id='movyt'><dir id='movyt'><q id='movyt'></q></dir></style></legend>

                      • <bdo id='movyt'></bdo><ul id='movyt'></ul>

                      <i id='movyt'><tr id='movyt'><dt id='movyt'><q id='movyt'><span id='movyt'><b id='movyt'><form id='movyt'><ins id='movyt'></ins><ul id='movyt'></ul><sub id='movyt'></sub></form><legend id='movyt'></legend><bdo id='movyt'><pre id='movyt'><center id='movyt'></center></pre></bdo></b><th id='movyt'></th></span></q></dt></tr></i><div id='movyt'><tfoot id='movyt'></tfoot><dl id='movyt'><fieldset id='movyt'></fieldset></dl></div>
                    1. <tfoot id='movyt'></tfoot>