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)

df = pd.read_csv('data/age-distribution-usa-2022.csv', comment='#')
df['Percentage'] = 100*df['Population']/df['Population'].sum()

labels = list()
for _, row in df.iterrows():
    labels.append(f'{row["Age_group"]}\n({row["Percentage"]:3.1f}%)')
    
#3
textprops = {'ha':'center', 'va':'center'}
wedgeprops = {'linewidth':1.5, 'edgecolor':'white', 'width':0.3}

ax.pie(x=df['Population'], labels=labels, 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, v_offset=15)

fig.savefig('charts/doughnut-chart.png', dpi=300, bbox_inches='tight')