"""Render the free spending_changes.csv with matplotlib 3.10.7.""" import csv from pathlib import Path import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from matplotlib.lines import Line2D def render(data_file, destination): with open(data_file, newline='', encoding='utf-8') as f: rows = list(csv.DictReader(f)) destination = Path(destination); destination.mkdir(parents=True, exist_ok=True) plt.rcParams.update({'font.family':'DejaVu Sans','font.size':11,'text.color':'#26352e', 'axes.labelcolor':'#26352e','xtick.color':'#526358','ytick.color':'#526358', 'svg.fonttype':'none','svg.hashsalt':'acd-spending-v1', 'figure.facecolor':'#f7f8f0','axes.facecolor':'#f7f8f0'}) for mobile in (False, True): for kind in ('spending-growth','budget-shares'): fig, axes=plt.subplots(3,1,figsize=(4.2,13) if mobile else (9,10)) fig.subplots_adjust(left=.27 if mobile else .19,right=.88,top=.76,bottom=.18,hspace=.65) growth=kind=='spending-growth' title=('Spending growth before\nand after inflation' if mobile else 'Spending growth before and after inflation') if growth else ('Housing takes the\nlargest share' if mobile else 'Housing takes the largest share') fig.text(.055,.97,title,fontsize=18 if mobile else 23,weight='bold',va='top') sub='Mean annual spending per consumer unit\n2019–2024 · U.S. income quintiles' if growth else 'Share of total annual expenditures per consumer unit\n2019 and 2024 · U.S. income quintiles' if mobile and not growth:sub='Share of total annual expenditures\nper consumer unit · U.S. income quintiles\n2019 and 2024' fig.text(.055,.895 if growth else (.895 if not mobile else .91),sub,fontsize=11 if mobile else 12,va='top',linespacing=1.5) labels=['Nominal dollars','CPI-adjusted dollars'] if growth else ['2019','2024'] fig.legend([Line2D([0],[0],marker='o',color=c,linestyle='',markersize=7) for c in ['#af743c','#315b49']],labels,loc='upper left',bbox_to_anchor=(.045,.84 if not mobile else .85),frameon=False,ncol=1 if mobile else 2,fontsize=11 if mobile else 10) for ax,category in zip(axes,['Food','Housing','Transportation']): rs=[r for r in rows if r['category']==category] k1,k2=('nominal_change_pct','cpi_adjusted_change_pct') if growth else ('share_2019_pct','share_2024_pct') for i,r in enumerate(rs): x1,x2=float(r[k1]),float(r[k2]);ax.plot([x1,x2],[i,i],color='#b5c0b5',lw=2,zorder=1) ax.scatter([x1],[i],color='#af743c',s=45,zorder=3);ax.scatter([x2],[i],color='#315b49',s=45,zorder=3) ax.annotate(f'{x2:+.1f}%' if growth else f'{x2:.1f}%',(x2,i),xytext=(0,-13),textcoords='offset points',ha='center',fontsize=12 if mobile else 9,color='#315b49') ax.set_yticks(range(5),['Q1 · lowest','Q2','Q3','Q4','Q5 · highest']);ax.invert_yaxis();ax.set_ylim(5.0,-.5) ax.set_title(category,loc='left',fontsize=14,weight='bold',pad=10) ax.set_xlim(-15,40) if growth else ax.set_xlim(0,50) ax.set_xticks([-10,0,10,20,30,40] if growth else [0,10,20,30,40,50]) ax.grid(axis='x',alpha=.2);ax.spines[['top','right','left']].set_visible(False);ax.spines['bottom'].set_color('#b5c0b5');ax.tick_params(axis='y',length=0,labelsize=12 if mobile else 10);ax.tick_params(axis='x',labelsize=12 if mobile else 11) if growth:ax.axvline(0,color='#839187',lw=.8) axes[-1].set_xlabel('Change, 2019–2024 (%)' if growth else 'Share of total expenditures (%)',labelpad=10) footer='All five before-tax income quintiles; changing groups, not a household panel.\n' + ('General CPI-U adjustment; not category-specific quantities. ' if growth else 'Shares are of spending, not income. ') + 'Point estimates; no significance tests.\nSources: BLS Consumer Expenditure Surveys' + (' and CPI-U' if growth else '') + '; Actually Clean Data calculations.' if mobile:footer=('Groups change each year; not a household panel.\n'+('General CPI-U adjustment, not consumption quantities.\n' if growth else 'Shares are of spending, not income.\n')+'Point estimates; no significance tests.\nSources: BLS CE'+(' and CPI-U' if growth else '')+';\nActually Clean Data calculations.') fig.text(.055,.022,footer,fontsize=10 if mobile else 9,linespacing=1.5) name=kind+('-mobile' if mobile else '') fig.savefig(destination/(name+'.svg'),metadata={'Date':None,'Creator':'Actually Clean Data; matplotlib 3.10.7'}) if not mobile:fig.savefig(destination/(name+'.png'),dpi=160) plt.close(fig) if __name__ == '__main__': render('spending_changes.csv','figures')