How to display the value of the bar on each bar with pyplot.barh()?

后端 未结 9 815
不知归路
不知归路 2020-11-22 08:10

I generated a bar plot, how can I display the value of the bar on each bar?

Current plot:

\"enter

9条回答
  •  忘掉有多难
    2020-11-22 08:44

    For pandas people :

    ax = s.plot(kind='barh') # s is a Series (float) in [0,1]
    [ax.text(v, i, '{:.2f}%'.format(100*v)) for i, v in enumerate(s)];
    

    That's it. Alternatively, for those who prefer apply over looping with enumerate:

    it = iter(range(len(s)))
    s.apply(lambda x: ax.text(x, next(it),'{:.2f}%'.format(100*x)));
    

    Also, ax.patches will give you the bars that you would get with ax.bar(...). In case you want to apply the functions of @SaturnFromTitan or techniques of others.

提交回复
热议问题