如何在python堆叠栏中的列顶部添加标签?

如何在python堆叠栏中的列顶部添加标签?,python,matplotlib,label,stacked-chart,Python,Matplotlib,Label,Stacked Chart,我已经从熊猫数据帧(df)创建了一个堆叠条: 如何在栏中的列顶部显示标签 最新答复: 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(he

我已经从熊猫数据帧(df)创建了一个堆叠条:

如何在栏中的列顶部显示标签

最新答复:

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')
您可以使用
sum()
函数对数据帧中每x列的值求和:

df = pd.DataFrame({
    'keyword':  [.2, .08, .02, 0, .15],
    'research': [.2, .12, .09, .17, 0]
})

dy = sum(df[c] for c in df.keys())
然后,
dy
中的结果值可以用作标签的y偏移

print(dy)

0    0.40
1    0.20
2    0.11
3    0.17
4    0.15
dtype: float64
您可以使用
sum()
函数对数据帧中每x列的值求和:

df = pd.DataFrame({
    'keyword':  [.2, .08, .02, 0, .15],
    'research': [.2, .12, .09, .17, 0]
})

dy = sum(df[c] for c in df.keys())
然后,
dy
中的结果值可以用作标签的y偏移

print(dy)

0    0.40
1    0.20
2    0.11
3    0.17
4    0.15
dtype: float64

请看我对类似问题的回答:请看我对类似问题的回答:谢谢,我使用您的方法计算每个项目的总和,然后使用注释显示值(见下文)。谢谢,我使用您的方法计算每个项目的总和,然后使用注释显示值(见下文)