Annotate bars with values on Pandas bar plots

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

提交回复
热议问题