Labeling horizontal barplot with values in Seaborn

后端 未结 1 1660
北荒
北荒 2021-01-06 15:56

I have a horizontal barplot, for example, a simplified version of the example from the seaborn documentation: https://seaborn.pydata.org/examples/horizontal_barplot.html

1条回答
  •  孤城傲影
    2021-01-06 16:22

    Got it, thanks to @ImportanceOfBeingErnest

    This worked for me

    for p in ax.patches:
        width = p.get_width()    # get bar length
        ax.text(width + 1,       # set the text at 1 unit right of the bar
                p.get_y() + p.get_height() / 2, # get Y coordinate + X coordinate / 2
                '{:1.2f}'.format(width), # set variable to display, 2 decimals
                ha = 'left',   # horizontal alignment
                va = 'center')  # vertical alignment
    

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