代码之家  ›  专栏  ›  技术社区  ›  VERBOSE

如何在具有共享属性且没有分隔符的子地块中制作hbar?

  •  0
  • VERBOSE  · 技术社区  · 1 年前

    我的输入是一个数据帧 df (你可以在我的问题末尾找到一个片段),我试图在这里创建一个类似第二个“面板供应商条形图”的图: https://peltiertech.com/stacked-bar-chart-alternatives

    我编写的代码是这样的(顺便说一句,我对任何改进它的建议都持开放态度):

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.read_excel('my_folder/list_of_softwares.xlsx')
    
    sums = df.groupby('Software').sum().T
    
    fig, axes = plt.subplots(1, len(sums.index), sharey=True, figsize=(12, 3))
    plt.subplots_adjust(wspace=0)
    
    colors = ['#6caddf', '#f27077', '#9dd374', '#fab26a']
              
    for ax, software, parameter, color in zip(axes, sums.columns, sums.index, colors):
        ax.barh(sums.loc[parameter].index, sums.loc[parameter].values, color=color)
        ax.set(xticks=range(0, df.drop('Software', axis=1).max().max()+1, 2000), title=parameter)
    
    fig.suptitle('Summary of Softwares', y=1.05, fontweight='bold')
    
    plt.show()
    

    除了4个小问题外,它还能正常工作:

    1. 这个 y 应删除所有轴中的记号
    2. 轴的字幕应该在图形内部,而不是在图形外部
    3. 一些 x 刻度标签重叠
    4. 分开每把斧头的垂直线应该去掉

    我觉得这很容易,但我不知道怎么做。

    有什么想法吗,伙计们?

    enter image description here

    df 看起来像这样:

       Software  Parameters  Reports  Dashbords  Scorecards
    0   Tableau        7935       68        474         712
    1   Tableau          69      518        695         122
    2    Oracle         651      540        842         764
    3    Oracle         700       52        776         948
    4    Oracle         758      862        182         757
    5       IBM         999      271        847         338
    6       IBM         316      128        395         441
    7       IBM         915      199       1000         747
    8       IBM          44      685        818         427
    9     Tibco         500      575        876         450
    10    Board         748      936        367         771
    11    Board         304      700        856          77
    12    Board         623      974        120         802
    13    Board         131      151        395         410
    14    Board         217      645        996         537
    15  LogiXML         630      779        752         433
    16  LogiXML         947      391        280         109
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   jjsantoso    1 年前

    您只需调整 ax 对象,即每个单独的绘图:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.read_clipboard() # copy data in the SO question
    sums = df.groupby('Software').sum().T
    
    fig, axes = plt.subplots(1, len(sums.index), sharey=True, figsize=(12, 3))
    plt.subplots_adjust(wspace=0.2)
    
    colors = ['#6caddf', '#f27077', '#9dd374', '#fab26a']
              
    for ax, software, parameter, color in zip(axes, sums.columns, sums.index, colors):
        ax.barh(sums.loc[parameter].index, sums.loc[parameter].values, color=color)
        ax.set(xticks=range(0, df.drop('Software', axis=1).max().max()+1, 2000), )
        ax.set_title(parameter, y=0.9)
        ax.yaxis.set_tick_params(length=0)
        for d in ["top", "bottom", "right", "left"]:
            ax.spines[d].set_visible(False)
        
        
    fig.suptitle('Summary of Softwares', y=1.05, fontweight='bold')
    
    plt.show()
    

    final_ouput