【问题标题】:How to generate a group bar plot with different numbers of bars in each group?如何生成每组中具有不同数量条形的组条形图?
【发布时间】:2021-03-29 08:29:11
【问题描述】:

我知道如何使用 matplotlib 生成一个常见的组条形图,如下所示: A Grouped bar chart from matplotlib

每组有 2 个小节。

但是如何生成一个组条形图,每个组中的条数不同,例如 Group1 中有 2 个条,Group2 中有 3 个条?

谢谢。

【问题讨论】:

    标签: python matplotlib bar-chart


    【解决方案1】:

    您可以使用catplot,例如如下:

    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame({'group': ['group 1', 'group 1', 'group 2', 'group 2', 'group 2'],
                       'bar': ['bar 1', 'bar 2', 'bar 1', 'bar 2', 'bar 3'],
                       'value': [1, 2, 3, 4, 5]})
    g = sns.catplot(kind='bar', data=df, col='group', x='bar', y='value',
                    hue='bar', palette='rocket', dodge=False, sharex=False)
    plt.tight_layout()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 2020-09-24
      • 2020-03-27
      相关资源
      最近更新 更多