【问题标题】:Stacked bar chart multiple binary variables with Seaborn使用 Seaborn 堆叠条形图多个二进制变量
【发布时间】:2018-03-20 10:22:16
【问题描述】:

我有一个包含大约 10 个二进制变量的 Pandas 数据框,我想使用 Seaborn 在堆积条形图中绘制零和一计数。任何人都可以帮助我如何做到这一点?

【问题讨论】:

    标签: python pandas data-visualization seaborn


    【解决方案1】:

    我认为ispossible 在 seaborn 中创建堆叠条,但真的很复杂。

    更简单的是使用DataFrame.plot.bar 和参数stacked=True

    from collections import Counter
    
    df = pd.DataFrame({'A':['1110','1111', '0000']})
    
    print (df)
          A
    0  1110
    1  1111
    2  0000
    
    #get counts of 0, 1
    x = Counter([a for x in df['A'] for a in list(x)])
    print (x)
    Counter({'1': 7, '0': 5})
    
    df = pd.DataFrame([x])
    print (df)
       0  1
    0  5  7
    
    df.plot.bar(stacked=True)
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2018-09-05
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多