I have a bar chart of data from 8 separate buildings, the data is separated by year, I'm trying to place the growth each building went through in the last year on top of the bar chart.
I have this written currently:
n_groups = 8
numbers_2017 = (122,96,42,23,23,22,0,0)
numbers_2018 = (284,224,122,52,41,24,3,1)
fig, ax = plt.subplots(figsize=(15, 10))
index = np.arange(n_groups)
bar_width = 0.35
events2017 = plt.bar(index, numbers_2017, bar_width,
alpha=0.7,
color='#fec615',
label='2017')
events2018 = plt.bar(index + bar_width, numbers_2018, bar_width,
alpha=0.7,
color='#044a05',
label='2018')
labels = ("8 specific buildings passed as strings")
labels = [ '\n'.join(wrap(l, 15)) for l in labels ]
plt.ylabel('Total Number of Events', fontsize=18, fontweight='bold', color = 'white')
plt.title('Number of Events per Building By Year\n', fontsize=20, fontweight='bold', color = 'white')
plt.xticks(index + bar_width / 2)
plt.yticks(color = 'white', fontsize=12)
ax.set_xticklabels((labels),fontsize=12, fontweight='bold', color = 'white')
plt.legend(loc='best', fontsize='xx-large')
plt.tight_layout()
plt.show()
Looking through similar questions on here many of them split the total count across all the bars, whereas I'm just trying to get a positive (or negative) growth percentage placed on top of the most recent year, 2018 in this case.
I found this excellent example online, however it does exactly what I explained earlier, splits up the percentages across the chart:
totals = []
# find the values and append to list
for i in ax.patches:
totals.append(i.get_height())
# set individual bar lables using above list
total = sum(totals)
# set individual bar lables using above list
for i in ax.patches:
# get_x pulls left or right; get_height pushes up or down
ax.text(i.get_x()-.03, i.get_height()+.5, \
str(round((i.get_height()/total)*100, 1))+'%', fontsize=15,
color='dimgrey')
Please let me know if I can list any examples or images that would help, and if this is a dupe please don't hesitate to send me to a (RELEVANT) original and I can shut this question down, Thanks!