【发布时间】:2016-06-16 07:15:43
【问题描述】:
我从 pandas 数据框 (df) 创建了一个堆叠条:
plt.style.use('ggplot')
ax= df.plot.bar(stacked=True, title=title,figsize=(11, 7));
ax.set_ylabel("Score")
ax.set_xlabel("Item")
ax.set_ylim([0,1.4])
ax.set_xticklabels(labels=df.index,rotation=0,minor=False)
labels = ["label%d" % i for i in range(9)]
rects = ax.patches
for rect, label in zip(rects, labels):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2, height, label, ha='center', va='bottom')
更新答案:
dy= sum(dfsub[c] for c in dfsub.keys())
for i, label in enumerate(list(dfsub.index)):
score = dfsub.ix[label]
height = dy.ix[label]
height = np.round(height,3)
ax.annotate(height, (i, height),ha='center')
【问题讨论】:
-
看看我对类似问题的回答:stackoverflow.com/a/37822413/2666859
标签: python matplotlib label stacked-chart