【问题标题】:How to Increase subplot text size and add custom bar plot annotations如何增加子图文本大小并添加自定义条形图注释
【发布时间】:2021-10-08 20:47:32
【问题描述】:

我有以下数据框,代表每个地区每个部门/区域的员工总数。

               Finance   HR   IT  Marketing  Medical  Operations  Sales
Business Unit                                                          
Cardiology           0    2    1          0        3           0      0
Genetics             1    4    3          1        3           1      1
Imaging             34   74   70         38       68          18     33
Labs                63  130  131         66      130          32     68
Pathology            2    5   10          4        8           3      6

使用此数据框,我使用此代码生成了波纹图:

#Plot the graph
fig, ax = plt.subplots(1, 5, figsize=(30, 15), sharey = True)
iax = iter(ax.flatten())
for n, g in df.groupby('Business Unit'):
    g.loc[n, :].plot.bar(ax=next(iax),  title=f'{n}', stacked=True, legend = True, fontsize = 30)

如您所见,通知业务部门的子图的文本大小以及图例大小(在顶角)太小了。我怎样才能增加他们两个的文本?

另外,有没有一种方法可以在这段代码中添加一种方法来显示每列中总数的百分比?

非常感谢您的帮助!

【问题讨论】:

    标签: python pandas matplotlib seaborn bar-chart


    【解决方案1】:
    import pandas as pd
    
    # load dataframe
    data = {'Cardiology': {'Finance': 0, 'HR': 2, 'IT': 1, 'Marketing': 0, 'Medical': 3, 'Operations': 0, 'Sales': 0}, 'Genetics': {'Finance': 1, 'HR': 4, 'IT': 3, 'Marketing': 1, 'Medical': 3, 'Operations': 1, 'Sales': 1}, 'Imaging': {'Finance': 34, 'HR': 74, 'IT': 70, 'Marketing': 38, 'Medical': 68, 'Operations': 18, 'Sales': 33}, 'Labs': {'Finance': 63, 'HR': 130, 'IT': 131, 'Marketing': 66, 'Medical': 130, 'Operations': 32, 'Sales': 68}, 'Pathology': {'Finance': 2, 'HR': 5, 'IT': 10, 'Marketing': 4, 'Medical': 8, 'Operations': 3, 'Sales': 6}}
    df = pd.DataFrame.from_dict(data, orient='index')
    
    # get the total for each business unit; used to calculate percent
    bu_total = df.sum(axis=1)
    
    # get the total for each division; used to calculate percent
    div_total = df.sum(axis=0)
    
    # plot
    axes = df.T.plot(kind='bar', subplots=True, layout=(1, 5), figsize=(22, 6), sharey=True, ylabel='Expenditures ($)')
    
    # iterate through axes subplots
    for ax in axes[0]:
        # title is used to get the total from bu_total
        title = ax.get_title()
        
        ax.legend(fontsize=15)
        ax.set_xticklabels(ax.get_xticklabels(), fontdict={'fontsize':24})
        ax.set_title(title, fontdict={'fontsize':24})
        ax.set_ylabel(ax.get_ylabel(), fontdict={'fontsize':24})
        
        # customized labels for % business unit total
        bu_labels = [f'{(v.get_height() / bu_total[title])*100 :.0f}%' for v in ax.containers[0]]
        
        # customized labels for % division total
        div_labels = [f'{(v.get_height() / div_total[i])*100 :.0f}%' for i, v in enumerate(ax.containers[0])]
        
        # annotate as desired: use bu_lables or div_labels
        ax.bar_label(ax.containers[0], labels=bu_labels, label_type='edge', fontsize=10)
    
        # pad the spacing between the number and the edge of the figure
        ax.margins(y=0.1)
    

    【讨论】:

    • 再次感谢,非常感谢!
    猜你喜欢
    • 2020-12-27
    • 2022-08-03
    • 2017-10-30
    • 2016-10-11
    • 2020-08-26
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    相关资源
    最近更新 更多