# Get average income at key percentile groups from WID
pct_groups = ['p0p50', 'p50p90', 'p90p100']
abs_countries = ['US', 'FR', 'DE', 'SE', 'GB', 'DK', 'NO']
target_year = 2020
avg_income = wid.get_average_income(
abs_countries, income_concept='pretax',
percentiles=pct_groups,
start_year=target_year - 2, end_year=target_year + 2,
)
if not avg_income.empty:
avg_income['year_diff'] = abs(avg_income['year'] - target_year)
latest = (avg_income.sort_values('year_diff')
.groupby(['country', 'percentile']).first().reset_index())
pivot = latest.pivot(index='country', columns='percentile', values='value')
fig, ax = plt.subplots(figsize=(12, 6))
pct_labels = {'p0p50': 'Bottom 50%', 'p50p90': 'Middle 40%', 'p90p100': 'Top 10%'}
pct_colors = {'p0p50': '#4878a8', 'p50p90': '#d98c3e', 'p90p100': '#c75b5b'}
x = np.arange(len(pivot))
width = 0.25
for i, pct in enumerate(pct_groups):
if pct in pivot.columns:
ax.bar(x + i * width, pivot[pct].values, width,
label=pct_labels.get(pct, pct), color=pct_colors.get(pct, '#666'))
ax.set_xticks(x + width)
ax.set_xticklabels([country_name(c) for c in pivot.index], rotation=45, ha='right')
ax.set_ylabel('Average pre-tax income (local currency units)')
ax.set_title(f'Average Income by Distribution Group (~{target_year})', fontsize=14)
ax.legend()
ax.yaxis.set_major_formatter(mtick.StrMethodFormatter('{x:,.0f}'))
plt.tight_layout()
save_figure(fig, '03_income_by_percentile')
plt.show()
print('\nNote: WID expresses values in EUR PPP for European countries and USD for the US.')
else:
print('WID average income data not available.')