python - 如何注释堆积条形图
问题描述
我制作了一个堆积条形图,需要添加到每列的总数,以及每列中的各个块(百分比)。
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.
解决方案
推荐阅读
- excel - 使用索引将 FOR EACH 转换为 FOR
- java - 即使应用程序被杀死,如何防止警报被破坏?
- database-connection - Servicestack MySql 连接字符串
- mysql - 在 SELECT 的结果中替换具有相同属性的列
- r - R 修改默认函数行为 sys.date()
- javascript - Outlook Web 加载项在使用 displayInFrame:true 时出现错误
- css - 如何解决弹出覆盖问题
- html - 使屏幕阅读器可以访问自定义无线电控制
- angular - 在模板之外共享自定义 Angular 格式化函数
- neo4j - 如何仅显示连接到使用 Neo4J 的 Cypher 选择的节点的节点?