import numpy as np
import matplotlib.pyplot as plt
from collections import namedtuple
A = namedtuple('A', 'a d')
A.__add__ = lambda self, other: A(self.a + other.a, self.d + other.d)
t0009 = 716409+ 695923+ 694252+ 677136+ 689254+ 679327+ 697830+ 696484+ 684010+ 697726
t1019 = 722241+ 722852+ 735595+ 753292+ 786644+ 788875+ 808124+ 834306+ 825939+ 811944
t2029 = 828045+ 860603+ 903296+ 943635+1040072+1030503+1058649+1038497+1025737+1004003
t3039 = 1006078+1015013+1041919+1033777+1028891+ 980874+ 968335+ 959785+ 942439+ 917148
t4049 = 930754+ 939555+1020494+1126979+1172627+1261493+1322427+1358443+1397460+1401871
t5059 = 1428030+1415594+1369343+1349727+1304016+1267315+1194923+1166699+1132685+1095293
t6069 = 1072319+1034704+1030503+1004975+ 999121+ 962406+ 875830+ 819270+ 710420+ 622091
t70p = 825875+ 832191+ 800307+ 961770+ 998866+ 967580+ 882551+ 803194+ 735405+ 701395\
+ 622741+ 476399+ 449159+ 428799+ 419672+2140804
s = sum([t0009, t1019, t2029, t3039, t4049, t5059, t6069, t70p])
p = A( 7.6, t0009/s*100), A( 9.1, t1019/s*100), A(22.2, t2029/s*100), A(12.9, t3039/s*100),
A(12.4, t4049/s*100), A(12.7, t5059/s*100), A( 9.4, t6069/s*100), A(13.7, t70p/s*100)]
xoff = .47
ind = np.arange(2) + xoff # the x locations for the groups
width = .66 # the width of the bars
ax = plt.subplot(111)
ax.set_ylim([0,100])
# has to use '+' here because else wikipedia breaks, lol
cols = [[1,.7,.7], 1,.76,.64], 1,1,.4], .76,1,.64], .6,1,.8], .6,.8,1]] + '#D1BCF9', '#C69FE6'
labels = '0-9', '10-19', '20-29', '30-39', '40-49', '50-59', '60-69', '70+'
# plot the bars. iterate in reverse so the legend has '70+' on top, not '0-9'
for i, q in enumerate(p):
j = len(p) - i - 1
plt.bar(ind, pj], width, color=colsj], bottom=sum(p[:j], A(0,0)), label=labelsj])
for i, q in enumerate(p): # 'X%' labels
for k in 0,1]:
plt.text(xoff+k+width/2, sum(p[:i], A(0,0))[k + qk/2, str(round(qk]))+'%', ha='center', va='center')
plt.xticks(ind + width/2, ('Aachen', 'Germany'))
plt.yticks(np.arange(0, 101, 10))
major_ticks = np.arange(0, 101, 10)
ax.set_yticks(major_ticks)
ax.yaxis.grid(which='major', alpha=.8)
ax.set_axisbelow(True)
for tick in ax.xaxis.get_major_ticks():
tick.label.set_fontsize(16)
for tick in ax.yaxis.get_major_ticks():
tick.label.set_fontsize(16)
# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.7, box.height])
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.gcf().set_size_inches(5, 8)
plt.savefig('age_structure_aachen_vs_germany.svg')