Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何用每个条形图的总和(Matplotlib)注释堆叠条形图?_Python_Matplotlib_Annotations_Stacked Chart_Stackedbarseries - Fatal编程技术网

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

Python 如何用每个条形图的总和(Matplotlib)注释堆叠条形图?,python,matplotlib,annotations,stacked-chart,stackedbarseries,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.242

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

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

这是我使用的两个数据帧之一:

             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
    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')
这里有一个解决方案:

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')


性别、年龄、dfs
未定义,抱歉。我已经编辑了这篇文章并添加了一个数据框。
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')

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