本文介绍了如何在Python中绘制多个级别的groupby直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数据框,

I have a dataframe like this,

col1    col2     col3
A       east        5
A       west        7
A       east        1
A       east        6
B       east        2
B       west        9
B       east        8
...
Z       west        4

我知道如何使用

df[df.col1.isin(ix)].groupby('col1').hist()

如何使用上面的数据框获得2级分组和绘制直方图?对于每个col1组直方图,我希望它们在单独的图中.但是对于每个col2组.我希望他们像这样.

How can I get a 2 levels groupby and draw histograms by using the dataframe above?For each col1 group histogram I want them in a separate plot. But for each col2 group. I hope them in one like this.

例如,最终结果将是来自A-Z的26个直方图.每个内部都有2个图(east&west).

For example, the final results will be 26 histograms from A-Z. Each inside has 2 plots(east&west).

推荐答案

使用 seaborn :

  • pandas.DataFrame.sort_values col1上的打印顺序也将从AZ
  • Using seaborn:

    • pandas.DataFrame.sort_values on col1 will also order the plot sequence from A to Z
    • import string
      import numpy as np
      import pandas as pd
      import random
      
      alpha_list = [random.choice(list(string.ascii_uppercase)) for _ in range(10_000)]
      coor_list = [random.choice(['east', 'west']) for _ in range(10_000)]
      rand_val = [np.random.randint(10) for _ in range(10_000)]
      df = pd.DataFrame({'col1': alpha_list, 'col2': coor_list, 'col3': rand_val})
      df.sort_values(by='col1', inplace=True)
      
      col1  col2  col3
         A  west     1
         A  east     3
         A  west     9
         A  west     5
         A  west     7
         A  east     1
         A  east     5
         A  west     2
         A  east     2
         A  west     2
      

      情节:

      • 使用col_wrap
      • 选择每行显示的数字
      • 构建结构化的多图网格
      • seaborn.FacetGrid
      • seaborn.distplot
      • Plot:

        • Select the number displayed per row with col_wrap
        • Building structured multi-plot grids
        • seaborn.FacetGrid
        • seaborn.distplot
        • g = sns.FacetGrid(df, col='col1', hue='col2', col_wrap=7)
          g.map(sns.distplot, 'col3', hist_kws=dict(edgecolor='black'), bins=range(0, 11, 1), kde=False)
          plt.xlabel('Value Range')
          plt.ylabel('Frequency')
          plt.legend()
          plt.xticks(range(1, 11, 1))
          plt.show()
          
          • 如果要在每个图形上打勾,请在plt.show()之前的行上放置以下代码:
          • if you want tick labels on each graph, put the following code on the line before plt.show():
          for ax in g.axes.flatten():
              ax.tick_params(labelbottom=True)
          
          

          这篇关于如何在Python中绘制多个级别的groupby直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:58