0

I have the following dataset and I have plotted a Stacked Bar Chart using Matplotlib.

industry    Distribution    Manufacturing   Retail  Services
2017-09          1                4           12       7
2017-10          3                2            3       4
2017-11          1                0            2       1
2018-02          1                0            0       0

the following is the code to generate the Graph:

fig, ax = plt.subplots(1, figsize=(17,6))

part1 = ax.bar(industry_split.index.values, 'Retail', data=industry_split, color = 'darkblue', width=0.5, edgecolor=edgecolor, linewidth=linewidth)
part2 = ax.bar(industry_split.index.values, 'Services', data=industry_split, color = 'dodgerblue', edgecolor=edgecolor, linewidth=linewidth, width=0.5, bottom = industry_split.Retail)
part3 = ax.bar(industry_split.index.values, 'Manufacturing', data=industry_split, color = 'green', width=0.5, edgecolor=edgecolor, linewidth=linewidth, bottom = industry_split.Retail + industry_split.Services)
part4 = ax.bar(industry_split.index.values, 'Distribution', data=industry_split, color = 'orange', width=0.5, edgecolor=edgecolor, linewidth=linewidth, bottom = industry_split.Retail + industry_split.Services + industry_split.Manufacturing)

enter image description here

I need to annotate the values in the graph. For instance, I need the value for each bar to appear inside the bar of the graph. See example below

enter image description here

0

1 Answer 1

2

Worked with the following code:

# Adding Data Labels
for i, label in enumerate(list(industry_split.index.values)):
score1 = industry_split.loc[label]['Retail']
if score1 == 0:
    None
else:
    ax.annotate(str(score1), (i, score1 - 0.7), color='white', fontsize=12, weight='semibold')

score2 = industry_split.loc[label]['Services']
if score2 == 0:
    None
else:
    ax.annotate(str(score2), (i, score1 + score2 - 0.7), color='white', fontsize=12, weight='semibold')

score3 = industry_split.loc[label]['Manufacturing']
if score3 == 0:
    None
else:
    ax.annotate(str(score3), (i, score1 + score2 + score3 - 0.7), color='white', fontsize=12, weight='semibold')

score4 = industry_split.loc[label]['Distribution']
if score4 == 0:
    None
else:
    ax.annotate(str(score4), (i, score1 + score2 + score3 + score4 - 0.7), color='white', fontsize=12, weight='semibold')
Sign up to request clarification or add additional context in comments.

1 Comment

I had a solution but couldn't find it. Yours looks way more xomplicated. But if it works why not!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.