首页 > 解决方案 > 如何注释堆积条形图

问题描述

我制作了一个堆积条形图,需要添加到每列的总数,以及每列中的各个块(百分比)。

df_data_2.round(2)
#df_data_2

#graph1 =df_data_2.T.plot.bar(stacked = True)


#ax=graph1
plt.xlabel("QTR")
plt.ylabel("Revenue")
plt.title("On Prem Tran Rev: Quarterly Performance $M (% of Total)")
#plt.legend()


#For percentages-

counter = df_data_2.unstack().unstack()


percentage_dist = 100 * counter.divide(counter.sum(axis = 1), axis = 0)
#percentage_dist.plot.bar(stacked=True)


ax = percentage_dist.plot.bar(stacked=True)
for p in ax.patches:
    width, height = p.get_width(), p.get_height()
    x, y = p.get_xy() 
    ax.text(x+width/2+.45, 
            y+height/2, 
            '{:.1f} %'.format(height), 
            horizontalalignment='center', 
            verticalalignment='center')```


So far I have a stacked graph with the percentages for each individual box.

标签: pythongraphannotatestacked

解决方案


推荐阅读