不访问 DataFrame 的解决方案是使用 patch 属性:
ax = df.plot.bar(title="Scores")
for p in ax.patches:
ax.annotate(str(p.get_height()), xy=(p.get_x(), p.get_height()))
请注意,您必须使用 xy kwarg (第二个参数)才能获得您想要的标签位置。
竖条
我发现这种格式一般来说是最好的:
ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()), ha='center', va='center', xytext=(0, 10), textcoords='offset points')
单杠
我发现以下格式可以很好地与水平条配合使用:
ax.annotate("%.2f" % p.get_width(), (p.get_x() + p.get_width(), p.get_y()), xytext=(5, 10), textcoords='offset points')