Annotate bars with values on Pandas bar plots

后端 未结 3 1199
甜味超标
甜味超标 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:48

    The ax gives us the size of the box.

    x_position=##define a value
    y_position=##define a value
    for patch in ax.patches:
        b= patch.get_bbox()
        y_value=b.y1-b.y0
        ax.annotate(y_value, "x_position" , "y_position"))
    plt.show()
    

    for more clarity::
    Bbox(x0=3.75, y0=0.0, x1=4.25, y1=868.0)
    Bbox(x0=4.75, y0=0.0, x1=5.25, y1=868.0)
    Bbox(x0=5.75, y0=0.0, x1=6.25, y1=1092.0)
    Bbox(x0=6.75, y0=0.0, x1=7.25, y1=756.0)
    Bbox(x0=7.75, y0=0.0, x1=8.25, y1=756.0)
    Bbox(x0=8.75, y0=0.0, x1=9.25, y1=588.0)
    Bbox(x0=3.75, y0=868.0, x1=4.25, y1=3724.0)
    Bbox(x0=4.75, y0=868.0, x1=5.25, y1=3528.0)
    Bbox(x0=5.75, y0=1092.0, x1=6.25, y1=3948.0)
    Bbox(x0=6.75, y0=756.0, x1=7.25, y1=2884.0)
    Bbox(x0=7.75, y0=756.0, x1=8.25, y1=3024.0)
    Bbox(x0=0.75, y0=4004.0, x1=1.25, y1=4396.0)
    Bbox(x0=1.75, y0=3668.0, x1=2.25, y1=4060.0)
    Bbox(x0=2.75, y0=3864.0, x1=3.25, y1=4060.0)

    this is the output of patch.get_bbox() in my program.
    we can extract the bounding box details from here and manipulate for our requirement

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 07:00

    Solution which also handles the negative values with sample float formatting.

    Still needs tweaking offsets.

    df=pd.DataFrame({'A':np.random.rand(2)-1,'B':np.random.rand(2)},index=['val1','val2'] )
    ax = df.plot(kind='bar', color=['r','b']) 
    x_offset = -0.03
    y_offset = 0.02
    for p in ax.patches:
        b = p.get_bbox()
        val = "{:+.2f}".format(b.y1 + b.y0)        
        ax.annotate(val, ((b.x0 + b.x1)/2 + x_offset, b.y1 + y_offset))
    

    0 讨论(0)
提交回复
热议问题