I’ve been trying to annotate each sub-amount of a stacked bar chart with its values like the picture shown above (values not accurate, just an example).
JavaScript
x
3
1
df.iloc[1:].T.plot(kind='bar', stacked=True)
2
plt.show()
3
The linked post is somewhat similar to my question but I do not understand the code given in that answer nor were there any explanations given.
Annotating Values in Stacked Bar Chart Matplotlib
Advertisement
Answer
Imports and DataFrame
JavaScript
1
14
14
1
import pandas as pd
2
import matplotlib.pyplot as plt
3
4
data = {'var': ['TR', 'AC', 'F&B'], '2019 1Q': [6600, 1256, 588], '2019 2Q': [6566, 1309, 586], '2019 3Q': [7383, 1525, 673]}
5
df = pd.DataFrame(data)
6
df.set_index('var', inplace=True)
7
8
# display(df)
9
2019 1Q 2019 2Q 2019 3Q
10
var
11
TR 6600 6566 7383
12
AC 1256 1309 1525
13
F&B 588 586 673
14
Update as of matplotlib v3.4.2
- Use
matplotlib.pyplot.bar_label
- See How to add value labels on a bar chart for additional details and examples with
.bar_label
. - Tested with
pandas v1.2.4
, which is usingmatplotlib
as the plot engine.
JavaScript
1
7
1
ax = df.T.plot.bar(stacked=True, figsize=(6, 5), rot=0)
2
3
for c in ax.containers:
4
ax.bar_label(c, label_type='center')
5
6
ax.legend(title='Categories', bbox_to_anchor=(1.05, 1), loc='upper left')
7
Original Answer – prior to matplotlib v3.4.2
- Transpose the dataframe and then use
pandas.DataFrame.plot.bar
withstacked=True
. - An
ndarray
is returned with onematplotlib.axes.Axes
per column withsubplots=True
.- In the case of this figure,
ax.patches
contains 9matplotlib.patches.Rectangle
objects, one for each segment of each bar.- By using the associated methods for this object, the
height
,width
,x
, andy
locations can be extracted, and used to annotate the rectangles.
- By using the associated methods for this object, the
- In the case of this figure,
- The difference this question has from How to annotate a stacked bar chart with word count and column name? is the other question needs to extract and use alternate text for labels, and this dataframe needed to be transposed.
JavaScript
1
20
20
1
ax = df.T.plot.bar(stacked=True)
2
plt.legend(title='Categories', bbox_to_anchor=(1.05, 1), loc='upper left')
3
4
for i, rect in enumerate(ax.patches):
5
# Find where everything is located
6
height = rect.get_height()
7
width = rect.get_width()
8
x = rect.get_x()
9
y = rect.get_y()
10
11
# The height of the bar is the count value and can used as the label
12
label_text = f'{height:.0f}'
13
14
label_x = x + width / 2
15
label_y = y + height / 2
16
17
# don't include label if it's equivalently 0
18
if height > 0.001:
19
ax.text(label_x, label_y, label_text, ha='center', va='center', fontsize=8)
20