import matplotlib.pyplot as plt
import pandas as pd
from mpl_ornaments.titles import set_title_and_subtitle

fig, ax = plt.subplots(nrows=1, ncols=1)

#1
df = pd.read_csv('data/age-distribution-usa-2022.csv', comment='#')
df['Percentage'] = 100*df['Population']/df['Population'].sum()

#2
df['labels'] = (df['Age_group'] + '\n' + 
                df['Percentage'].map('({:3.1f}%)'.format))
    
#3
textprops = {'ha':'center', 'va':'center'}
wedgeprops = {'linewidth':1.5, 'edgecolor':'white'}

#4
ax.pie(x=df['Population'], labels=df['labels'].to_list(), 
       wedgeprops=wedgeprops, textprops=textprops, labeldistance=1.18)

title = f'Population of the United States by age group (2022)'
subtitle = 'Source: US Census Bureau.'
set_title_and_subtitle(fig=fig, title=title, subtitle=subtitle, 
                       alignment='left', h_offset=25)

fig.savefig('charts/pie-chart.png', dpi=300, bbox_inches='tight')