I know it's an old thread, but I landed here several times via Google and think no given answer is really satisfying yet. Try using one of the following functions:
EDIT: As I'm getting some likes on this old thread, I wanna share an updated solution as well [basically putting my two previous functions together and automatically deciding whether it's a bar or hbar plot]:
def label_bars[ax, bars, text_format, **kwargs]:
"""
Attaches a label on every bar of a regular or horizontal bar chart
"""
ys = [bar.get_y[] for bar in bars]
y_is_constant = all[y == ys[0] for y in ys] # -> regular bar chart, since all all bars start on the same y level [0]
if y_is_constant:
_label_bar[ax, bars, text_format, **kwargs]
else:
_label_barh[ax, bars, text_format, **kwargs]
def _label_bar[ax, bars, text_format, **kwargs]:
"""
Attach a text label to each bar displaying its y value
"""
max_y_value = ax.get_ylim[][1]
inside_distance = max_y_value * 0.05
outside_distance = max_y_value * 0.01
for bar in bars:
text = text_format.format[bar.get_height[]]
text_x = bar.get_x[] + bar.get_width[] / 2
is_inside = bar.get_height[] >= max_y_value * 0.15
if is_inside:
color = "white"
text_y = bar.get_height[] - inside_distance
else:
color = "black"
text_y = bar.get_height[] + outside_distance
ax.text[text_x, text_y, text, ha='center', va='bottom', color=color, **kwargs]
def _label_barh[ax, bars, text_format, **kwargs]:
"""
Attach a text label to each bar displaying its y value
Note: label always outside. otherwise it's too hard to control as numbers can be very long
"""
max_x_value = ax.get_xlim[][1]
distance = max_x_value * 0.0025
for bar in bars:
text = text_format.format[bar.get_width[]]
text_x = bar.get_width[] + distance
text_y = bar.get_y[] + bar.get_height[] / 2
ax.text[text_x, text_y, text, va='center', **kwargs]
Now you can use them for regular bar plots:
fig, ax = plt.subplots[[5, 5]]
bars = ax.bar[x_pos, values, width=0.5, align="center"]
value_format = "{:.1%}" # displaying values as percentage with one fractional digit
label_bars[ax, bars, value_format]
or for horizontal bar plots:
fig, ax = plt.subplots[[5, 5]]
horizontal_bars = ax.barh[y_pos, values, width=0.5, align="center"]
value_format = "{:.1%}" # displaying values as percentage with one fractional digit
label_bars[ax, horizontal_bars, value_format]
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, we are going to see how to display the value of each bar in a bar chart using Matplotlib. There are two different ways to display the values of each bar in a bar chart in matplotlib –
- Using matplotlib.axes.Axes.text[] function.
- Use matplotlib.pyplot.text[] function.
Example 1: Using matplotlib.axes.Axes.text[] function:
This function is basically used to add some text to the location in the chart. This function return string, this is always used with the syntax “for index, value in enumerate[iterable]” with iterable as the list of bar values to access each index, value pair in iterable so at it can add the text at each bar.
Python3
import
os
import
numpy as np
import
matplotlib.pyplot as plt
x
=
[
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
]
y
=
[
160
,
167
,
17
,
130
,
120
,
40
,
105
,
70
]
fig, ax
=
plt.subplots[]
width
=
0.75
ind
=
np.arange[
len
[y]]
ax.barh[ind, y, width, color
=
"green"
]
for
i, v
in
enumerate
[y]:
ax.text[v
+
3
, i
+
.
25
,
str
[v],
color
=
'blue'
, fontweight
=
'bold'
]
plt.show[]
Output:
Example 2: Use matplotlib.pyplot.text[] function:
Call matplotlib.pyplot.barh[x, height] with x as a list of bar names and height as a list of bar values to create a bar chart. Use the syntax “for index, value in enumerate[iterable]” with iterable as the list of bar values to access each index, value pair in iterable. At each iteration, call matplotlib.pyplot.text[x, y, s] with x as value, y as index, and s as str[value] to label each bar with its size.
Python3
import
matplotlib.pyplot as plt
x
=
[
"A"
,
"B"
,
"C"
,
"D"
]
y
=
[
1
,
2
,
3
,
4
]
plt.barh[x, y]
for
index, value
in
enumerate
[y]:
plt.text[value, index,
str
[value]]
plt.show[]
Output: