• <legend id='XIGUQ'><style id='XIGUQ'><dir id='XIGUQ'><q id='XIGUQ'></q></dir></style></legend><tfoot id='XIGUQ'></tfoot>
      <bdo id='XIGUQ'></bdo><ul id='XIGUQ'></ul>
    1. <small id='XIGUQ'></small><noframes id='XIGUQ'>

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

        如何使用海运为我的DataFrame创建堆叠条形图

        时间:2024-08-21
        <legend id='RXkK8'><style id='RXkK8'><dir id='RXkK8'><q id='RXkK8'></q></dir></style></legend>
      1. <small id='RXkK8'></small><noframes id='RXkK8'>

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

                  <tfoot id='RXkK8'></tfoot>
                1. 本文介绍了如何使用海运为我的DataFrame创建堆叠条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个DataFramedf

                  df = pd.DataFrame(columns=["App","Feature1", "Feature2","Feature3", "Feature4","Feature5", "Feature6","Feature7","Feature8"], data=[['SHA', 0, 0, 1, 1, 1, 0, 1, 0], ['LHA', 1, 0, 1, 1, 0, 1, 1, 0], ['DRA', 0, 0, 0, 0, 0, 0, 1, 0], ['FRA', 1, 0, 1, 1, 1, 0, 1, 1], ['BRU', 0, 0, 1, 0, 1, 0, 0, 0], ['PAR', 0, 1, 1, 1, 1, 0, 1, 0], ['AER', 0, 0, 1, 1, 0, 1, 1, 0], ['SHE', 0, 0, 0, 1, 0, 0, 1, 0]])
                  
                  # display(df)
                     App  Feature1  Feature2  Feature3  Feature4  Feature5  Feature6  Feature7  Feature8
                  0  SHA         0         0         1         1         1         0         1         0
                  1  LHA         1         0         1         1         0         1         1         0
                  2  DRA         0         0         0         0         0         0         1         0
                  3  FRA         1         0         1         1         1         0         1         1
                  4  BRU         0         0         1         0         1         0         0         0
                  5  PAR         0         1         1         1         1         0         1         0
                  6  AER         0         0         1         1         0         1         1         0
                  7  SHE         0         0         0         1         0         0         1         0
                  

                  我要创建堆叠条形图,以便每个堆栈与App相对应,而Y轴将包含1值的计数,而X轴将为Feature

                  它应该类似于此条形图,唯一的区别是现在我希望看到堆叠条形图和带颜色的图例:

                  df_c = df.iloc[:, 1:].eq(1).sum().rename_axis('Feature').reset_index(name='Count')
                  df_c = df_c.sort_values('Count')
                  plt.figure(figsize=(12,8))
                  ax = sns.barplot(x="Feature", y='Count', data=df_c, palette=sns.color_palette("GnBu", 10))
                  plt.xticks(rotation='vertical')
                  ax.grid(b=True, which='major', color='#d3d3d3', linewidth=1.0)
                  ax.grid(b=True, which='minor', color='#d3d3d3', linewidth=0.5)
                  plt.show()
                  

                  推荐答案

                  您可以按照@bharath的建议使用 pandas 图:

                  import seaborn as sns
                  sns.set()
                  df.set_index('App').T.plot(kind='bar', stacked=True)
                  

                  输出:

                  更新:

                  从matplotlib导入ListedColormap df.set_index(‘App’) .reindex_axis(df.set_index(‘App’).sum().sort_values().index,轴=1) .T.Plot(KIND=‘BAR’,STACKED=True, colormap=ListedColormap(sns.color_palette("GnBu",10)), FigSize=(12,6))

                  更新的 pandas 0.21.0+reindex_axis已弃用,请使用reindex

                  from matplotlib.colors import ListedColormap
                  
                  df.set_index('App')
                    .reindex(df.set_index('App').sum().sort_values().index, axis=1)
                    .T.plot(kind='bar', stacked=True,
                            colormap=ListedColormap(sns.color_palette("GnBu", 10)), 
                            figsize=(12,6))
                  

                  输出:

                  这篇关于如何使用海运为我的DataFrame创建堆叠条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何重用剧情艺术家(Line2D)? 下一篇:海运-根据色调名称更改条形图颜色

                  相关文章

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

                      <legend id='1WwcJ'><style id='1WwcJ'><dir id='1WwcJ'><q id='1WwcJ'></q></dir></style></legend>

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

                      <tfoot id='1WwcJ'></tfoot>
                        <bdo id='1WwcJ'></bdo><ul id='1WwcJ'></ul>