df = df[df['executive_leader'] != 'No lead data']
sm_df = pd.DataFrame(df.groupby(['executive_leader','ethnicity'])['employee_number'].count())
sm_df.reset_index(inplace=True)
sm_df.columns = ['executive_leader', 'ethnicity', 'count']
sm_df
sm_df_p = sm_df.pivot_table('count', ['executive_leader'], 'ethnicity')
sm_df_p = sm_df_p.fillna(0)
# sm_df_p['Total'] = sm_df_p.sum(axis=1)
result = sm_df_p.copy()
og_sm_df_p = sm_df_p.copy()
sm_df_p = result.div(result.sum(axis=1), axis=0)
chart = sm_df_p.plot.bar(stacked=True)
plt.legend(loc='upper right', bbox_to_anchor=(2, 1))
chart.set_xticklabels(chart.get_xticklabels(), rotation=45, horizontalalignment='right')
chart.spines['right'].set_visible(False)
chart.spines['top'].set_visible(False)
counter = -1
# for num in range(8):
for i,p in enumerate(chart.patches):
# print(p)
width, height = p.get_width(), p.get_height()
total_idx = i%len(sm_df_p)
if total_idx == 0:
counter += 1
# print(og_sm_df_p.iloc[total_idx][0])
# percentage = (height/df_total[total_idx]) *100
# print(i,p)
x, y = p.get_xy()
# print(y, i-(len(sm_df_p)))
lookup = og_sm_df_p.iloc[total_idx][counter]
print(total_idx, counter, lookup)
if lookup > 2:
chart.text(x+width/2,
y+height/2,
'{}'.format(lookup),
horizontalalignment='center',
verticalalignment='center')
# if percentage > 5:
# ax.text(x+width/2,
# y+height/2,
# '{:.0f}%'.format(percentage),
# horizontalalignment='center',
# verticalalignment='center')