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

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

    2. <tfoot id='Hfisv'></tfoot>

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

      1. pandas :我如何对堆叠的条形图进行分组?

        时间:2024-08-20
          <bdo id='vUeKI'></bdo><ul id='vUeKI'></ul>
          <legend id='vUeKI'><style id='vUeKI'><dir id='vUeKI'><q id='vUeKI'></q></dir></style></legend>

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

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

                <tbody id='vUeKI'></tbody>

                  本文介绍了 pandas :我如何对堆叠的条形图进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建分组的堆叠条形图。

                  目前我有以下DataFrame:

                  >>> df
                                                         Value                     
                  Rating                                 1          2         3
                  Context Parameter                                
                  Total   1                          43.312347   9.507902  1.580367
                          2                          42.862649   9.482205  1.310549
                          3                          43.710651   9.430811  1.400488
                          4                          43.209559   9.803418  1.349094
                          5                          42.541436  10.008994  1.220609
                          6                          42.978286   9.430811  1.336246
                          7                          42.734164  10.317358  1.606064
                  User    1                          47.652348  11.138861  2.297702
                          2                          47.102897  10.589411  1.848152
                          3                          46.853147  10.139860  1.848152
                          4                          47.252747  11.138861  1.748252
                          5                          45.954046  10.239760  1.448551
                          6                          46.353646  10.439560  1.498501
                          7                          47.102897  11.338661  1.998002
                  

                  我希望将每个参数总计用户栏组合在一起。

                  这是生成的图表,包含df.plot(kind='bar', stacked=True)

                  条本身看起来是正确的,但是如何使总计用户的条彼此相邻,对于每个参数,最好在参数之间留一些空白处?

                  推荐答案

                  以下方法允许同时分组和堆叠条形图。 首先,数据帧按parameter, context排序。然后将context从索引中取出,为每个context, value对创建新列。 最后,在彼此之间绘制三个条形图,以可视化堆叠的条形图。

                  import pandas as pd
                  from matplotlib import pyplot as plt
                  
                  df = pd.DataFrame(columns=['Context', 'Parameter', 'Val1', 'Val2', 'Val3'],
                                    data=[['Total', 1, 43.312347, 9.507902, 1.580367],
                                          ['Total', 2, 42.862649, 9.482205, 1.310549],
                                          ['Total', 3, 43.710651, 9.430811, 1.400488],
                                          ['Total', 4, 43.209559, 9.803418, 1.349094],
                                          ['Total', 5, 42.541436, 10.008994, 1.220609],
                                          ['Total', 6, 42.978286, 9.430811, 1.336246],
                                          ['Total', 7, 42.734164, 10.317358, 1.606064],
                                          ['User', 1, 47.652348, 11.138861, 2.297702],
                                          ['User', 2, 47.102897, 10.589411, 1.848152],
                                          ['User', 3, 46.853147, 10.139860, 1.848152],
                                          ['User', 4, 47.252747, 11.138861, 1.748252],
                                          ['User', 5, 45.954046, 10.239760, 1.448551],
                                          ['User', 6, 46.353646, 10.439560, 1.498501],
                                          ['User', 7, 47.102897, 11.338661, 1.998002]])
                  df.set_index(['Context', 'Parameter'], inplace=True)
                  df0 = df.reorder_levels(['Parameter', 'Context']).sort_index()
                  
                  colors = plt.cm.Paired.colors
                  
                  df0 = df0.unstack(level=-1) # unstack the 'Context' column
                  fig, ax = plt.subplots()
                  (df0['Val1']+df0['Val2']+df0['Val3']).plot(kind='bar', color=[colors[1], colors[0]], rot=0, ax=ax)
                  (df0['Val2']+df0['Val3']).plot(kind='bar', color=[colors[3], colors[2]], rot=0, ax=ax)
                  df0['Val3'].plot(kind='bar', color=[colors[5], colors[4]], rot=0, ax=ax)
                  
                  legend_labels = [f'{val} ({context})' for val, context in df0.columns]
                  ax.legend(legend_labels)
                  
                  plt.tight_layout()
                  plt.show()
                  

                  这篇关于 pandas :我如何对堆叠的条形图进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何更改图例在海运中的位置? 下一篇:将字符串数据传递给matplotlib API时会绘制什么?

                  相关文章

                    <bdo id='lhpB5'></bdo><ul id='lhpB5'></ul>
                  <legend id='lhpB5'><style id='lhpB5'><dir id='lhpB5'><q id='lhpB5'></q></dir></style></legend>

                • <small id='lhpB5'></small><noframes id='lhpB5'>

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