如何用每个条形的总和来注释堆积条形图(Matplotlib)?

时间:2019-05-13 23:55:43

标签: python matplotlib annotations stacked-chart stackedbarseries

我有一个分组的条形图,每个条形图都是堆叠的。

我已经用单独的值注释了堆栈的每个部分,现在我想对这些值求和并注释每个条的总值(高度)。我希望此注释位于每个栏的顶部。

这是我正在使用的两个数据框之一:

             df_title = pd.DataFrame(index=['F','M'], 
             data={'<10':[2.064897, 1.573255], '10-12':[3.933137, 4.326450], '13-17':[9.242871, 16.715831],
                    '18-24':[10.226155, 12.487709], '18-24':[8.161259, 10.717797], '35-44':[5.801377, 4.916421],
                    '45-54':[3.539823, 2.851524], '55+':[1.671583, 1.769912]})

在绘制之前,我将两个数据帧(df_title和df_comps)都转换为numpy数组。

df_title_concat = np.concatenate((np.zeros((len,1)), df_title.T.values), axis=1)

这是完整的代码:

    df_title 
    df_comps
    len  = df_title.shape[1]
    df_title_concat = np.concatenate((np.zeros((len,1)), df_title.T.values), axis=1)
    df_comps_concat = np.concatenate((np.zeros((len,1)), df_comps.T.values), axis=1)

    fig = plt.figure(figsize=(20,10))
    ax = plt.subplot()
    title_colors = ['skyblue', 'royalblue']
    comps_colors = ['lightgoldenrodyellow', 'orange']

    for i in range(1,3):
        for j in list(range(0, df_title.shape[1]-1)):
            j += 1 

            ax_1 = ax.bar(j, df_title_concat[j,i], width=-0.4, bottom=np.sum(df_title_concat[j,:i]), color = title_colors[i-1], 
                          edgecolor='black', linewidth=3, align='edge')

            for p in ax_1.patches:
                width, height = p.get_width(), p.get_height()
                x, y = p.get_xy() 
                if height > 2:
                    ax.annotate('{:.2f}%'.format(height), (p.get_x()+0.875*width, p.get_y()+.4*height), 
                                fontsize=16, fontweight='bold', color='black')

            ax_2 = ax.bar(j, df_comps_concat[j,i], width=0.4, bottom=np.sum(df_comps_concat[j,:i]), color = comps_colors[i-1], 
                          edgecolor='black', linewidth=3, align='edge')

            for p in ax_2.patches:
                width, height = p.get_width(), p.get_height()
                x, y = p.get_xy() 
                if height > 2:
                    ax.annotate('{:.2f}%'.format(height), (p.get_x()+0.15*width, p.get_y()+.4*height), 
                                fontsize=16, fontweight='bold', color='black')

My Stacked Bar Chart

1 个答案:

答案 0 :(得分:0)

这是一个解决方案:

df_title = pd.DataFrame(index=['F','M'], 
             data={'<10':[2.064897, 1.573255], '10-12':[3.933137, 4.326450], '13-17':[9.242871, 16.715831],
                    '18-24':[10.226155, 12.487709], '18-24':[8.161259, 10.717797], '35-44':[5.801377, 4.916421],
                    '45-54':[3.539823, 2.851524], '55+':[1.671583, 1.769912]})


df_title_concat = np.concatenate((np.zeros((len(df_title),1)), df_title.T.values), axis=1)


fig = plt.figure(figsize=(12,8))
ax = plt.subplot()
title_colors = ['skyblue', 'royalblue']

for i in range(1,3):
    for j in list(range(0, df_title.shape[1]-1)):
        j += 1 
        bottom=np.sum(df_title_concat[j,:i])
        ax_1 = ax.bar(j, df_title_concat[j,i], width=-0.4, bottom=bottom, color = title_colors[i-1], 
                      edgecolor='black', linewidth=3, align='edge')

        for p in ax_1.patches:
            width, height = p.get_width(), p.get_height()
            if bottom != 0:
                ax.annotate('{:.2f}%'.format(height+bottom), (p.get_x()+0.875*width, (height+bottom)+0.3), 
                            fontsize=16, fontweight='bold', color='black')

output

但是,我建议您重新考虑您要遵循的整个方法,并将情节更改为:

plt.bar(df_title.columns,df_title.loc['M'])
plt.bar(df_title.columns,df_title.loc['F'],bottom=df_title.loc['M'])

enter image description here