Аннотирование значений в столбчатой ​​диаграмме с накоплением Matplotlib

У меня есть следующий набор данных, и я построил гистограмму с накоплением, используя 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

Ниже приведен код для создания графика:

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)

введите здесь описание изображения

Мне нужно аннотировать значения на графике. Например, мне нужно, чтобы значение каждой полосы отображалось внутри полосы графика. См. Пример ниже

введите здесь описание изображения


person Stats DUB01    schedule 22.02.2018    source источник
comment
что ты уже испробовал? Где ваши попытки потерпели неудачу? Другими словами, прочтите Как задать вопрос   -  person Diziet Asahi    schedule 22.02.2018
comment
Хотя это и не точная копия, этот пост должен получить ты начал. Если у вас есть конкретный вопрос по реализации, не стесняйтесь редактировать свой вопрос соответствующим образом.   -  person Diziet Asahi    schedule 22.02.2018


Ответы (1)


Работал со следующим кодом:

# 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')
person Stats DUB01    schedule 22.02.2018
comment
У меня было решение, но я не мог его найти. Ваш выглядит намного сложнее. Но если работает, почему бы и нет! - person Anton vBR; 22.02.2018