Annotate bars with values on Pandas bar plots

后端 未结 3 1202
甜味超标
甜味超标 2020-11-22 06:26

I was looking for a way to annotate my bars in a Pandas bar plot with the rounded numerical values from my DataFrame.

>>> df=pd.DataFrame({\'A\':np.         


        
3条回答
  •  醉酒成梦
    2020-11-22 06:51

    You get it directly from the axes' patches:

    for p in ax.patches:
        ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
    

    You'll want to tweak the string formatting and the offsets to get things centered, maybe use the width from p.get_width(), but that should get you started. It may not work with stacked bar plots unless you track the offsets somewhere.

提交回复
热议问题