【问题标题】:How to create a grouped bar plot如何创建分组条形图
【发布时间】:2018-05-27 12:49:14
【问题描述】:

这里的目标是创建一个分组条形图,而不是像下图这样的子图

有没有一种简单的方法可以在 Python 中创建分组条形图?现在我得到单独的条形图,而不是一个图上的单独条形图。

import pandas as pd

df = pd.DataFrame([['g1', 'c1', 10], ['g1', 'c2', 12], ['g1', 'c3', 13], ['g2', 'c1', 8], ['g2', 'c2', 10], ['g2', 'c3', 12]], columns=['group', 'column', 'val'])

  group column  val
0    g1     c1   10
1    g1     c2   12
2    g1     c3   13
3    g2     c1    8
4    g2     c2   10
5    g2     c3   12
    

df.groupby(['group']).plot(kind='bar')

【问题讨论】:

    标签: python pandas matplotlib seaborn bar-chart


    【解决方案1】:

    Pandas 将按列显示分组条形。每行但不同列中的条目将在结果图中构成一个组。因此,您需要“重塑”您的数据框以将“组”作为列。 在这种情况下,您可以像这样旋转

    df.pivot("column", "group", "val")
    

    生产

    group   g1  g2
    column        
    c1      10   8
    c2      12  10
    c3      13  12
    

    绘制此图将生成一个分组条形图。

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame([['g1','c1',10],['g1','c2',12],['g1','c3',13],['g2','c1',8],
                       ['g2','c2',10],['g2','c3',12]],columns=['group','column','val'])
    
    df.pivot("column", "group", "val").plot(kind='bar')
    
    plt.show()
    

    【讨论】:

      【解决方案2】:

      您可以使用下面给出的代码简单地做到这一点:

      import pandas as pd
      import matplotlib.pyplot as plt
      
      positive_values = [20, 17.5, 40]
      negative_values = [15, 8, 70]
      index = ['Precision', 'Recall', 'f1-score',]
      df = pd.DataFrame({'Positive Values': positive_values,
                          'Negative Values': negative_values}, index=index)
      ax = df.plot.bar(rot=0, color={"Positive Values": "green", "Negative Values": "red"})
      

      输出:

      【讨论】:

        【解决方案3】:
        • 给定一个长(整齐)数据的数据帧,如 OP 中所示,不需要转换数据帧的实现是使用 seaborn.barplothue 参数。
        • seabornmatplotlib 的高级 API
        • seaborn 0.11.1matplotlib 3.4.2 测试
        import pandas as pd
        import seaborn as sns
        
        # the sample dataframe from the OP
        df = pd.DataFrame([['g1', 'c1', 10], ['g1', 'c2', 12], ['g1', 'c3', 13], ['g2', 'c1', 8], ['g2', 'c2', 10], ['g2', 'c3', 12]], columns=['group', 'column', 'val'])
        
        # plot with seaborn barplot
        sns.barplot(data=df, x='column', y='val', hue='group')
        

        【讨论】:

          【解决方案4】:

          Plotly express 是我最近用过的最好的可视化包之一。它使您无需执行大量数据转换即可生成可视化。

          # initial dataframe
          df = pd.DataFrame([['g1','c1',10],['g1','c2',12],['g1','c3',13],['g2','c1',8],
                             ['g2','c2',10],['g2','c3',12]],columns=['group','column','val'])
          
          df.head()
          
             group column val
          0   g1   c1     10
          1   g1   c2     12
          2   g1   c3     13
          3   g2   c1      8
          4   g2   c2     10
          5   g2   c3     12
          

          无需转换数据,直接使用plotly express:

          import plotly.express as px
          fig = px.bar(df, x="column", y="val",
                       color='group', barmode='group',text="val",
                       height=400)
          fig.show()
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-14
            • 1970-01-01
            • 1970-01-01
            • 2022-11-16
            相关资源
            最近更新 更多