close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to display the value of the bar on each bar with pyplot.barh() in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

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

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

    The y-values v are both the x-location and the string values for ax.text, and conveniently the barplot has a metric of 1 for each bar, so the enumeration i is the y-location.

  2. display the value of the bar on each bar with pyplot.barh()

    The y-values v are both the x-location and the string values for ax.text, and conveniently the barplot has a metric of 1 for each bar, so the enumeration i is the y-location.

Method 1

Add:

for i, v in enumerate(y):
    ax.text(v + 3, i + .25, str(v), color='blue', fontweight='bold')

result:

enter image description here

The y-values v are both the x-location and the string values for ax.text, and conveniently the barplot has a metric of 1 for each bar, so the enumeration i is the y-location.

Method 2

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.

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read