"""Render the article's charts from its free county_comparison.csv (matplotlib 3.10.7).""" import csv from pathlib import Path import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt GROUPS = ['Declined', '0 to <5%', '5 to <10%', '10% or more'] COLORS = ['#647d8a','#b38653','#598c7b','#315140'] EXAMPLES = {'12105':'Polk, FL','37183':'Wake, NC','48491':'Williamson, TX'} def render(data_file, destination): destination=Path(destination);destination.mkdir(exist_ok=True,parents=True) with open(data_file,newline='',encoding='utf-8') as file: data=[r for r in csv.DictReader(file) if r['main_comparison']=='yes'] plt.rcParams.update({'font.family':'DejaVu Sans','font.size':11,'axes.labelsize':11, 'axes.edgecolor':'#bcc7ba','axes.labelcolor':'#26352e','text.color':'#26352e', 'xtick.color':'#58665f','ytick.color':'#58665f','svg.fonttype':'none', 'svg.hashsalt':'acd-county-housing-v1','figure.facecolor':'#f7f8f0','axes.facecolor':'#f7f8f0'}) for mobile in (False,True): for kind in ('growth-permits','permits-prices'): fig,ax=plt.subplots(figsize=(4.2,6.5) if mobile else (8.4,6.2)) fig.subplots_adjust(left=.25 if mobile else .105,bottom=(.36 if mobile else .32) if kind=='permits-prices' else .24,right=.97,top=.79) ax.spines[['top','right']].set_visible(False);ax.set_axisbelow(True);ax.grid(alpha=.3,color='#aab6aa',linewidth=.6) xkey='population_growth_pct' if kind=='growth-permits' else 'authorized_units_per_1000_2020_residents' ykey='authorized_units_per_1000_2020_residents' if kind=='growth-permits' else 'house_price_growth_pct' title = ('Population growth and\nhousing authorizations' if mobile else 'Population growth and housing authorizations') if kind=='growth-permits' else ('Housing authorizations\nand house-price growth' if mobile else 'Housing authorizations and house-price growth') fig.text(.06,.96,title,fontsize=15 if mobile else 19,fontweight='bold',va='top') fig.text(.06,.85,f'{len(data):,} counties · at least 100,000 residents in July 2020',fontsize=8 if mobile else 11) footer = 'Permits: calendar 2021–2025. Population: July 2020–2025.\nSources: U.S. Census Bureau; Actually Clean Data calculations.' if kind=='growth-permits' else 'Permits: calendar 2021–2025. Prices: annual 2020–2025.\nSources: Census, FHFA HPI®; Actually Clean Data calculations.' if mobile: footer=footer.replace('. Population:', '.\nPopulation:').replace('. Prices:', '.\nPrices:').replace('; Actually Clean Data calculations.', ';\nActually Clean Data calculations.') fig.text(.06,.035,footer,fontsize=7 if mobile else 9,linespacing=1.5,va='bottom',color='#58665f') for g,c in zip(GROUPS,COLORS): group=[r for r in data if r['population_growth_group']==g] ax.scatter([float(r[xkey]) for r in group],[float(r[ykey]) for r in group],s=18 if mobile else 23,color=('#315140' if kind=='growth-permits' else c),alpha=.65,linewidths=0,label=g) if kind=='growth-permits': ax.set_xlabel('Population change, 2020–2025 (%)',labelpad=12) ax.set_ylabel('Authorized housing units\nper 1,000 residents in 2020',labelpad=12) ax.axvline(0,color='#9ba89e',linewidth=.8);ax.set_ylim(bottom=0) else: ax.set_xlabel('Authorized housing units per\n1,000 residents in 2020',labelpad=12) ax.set_ylabel('House-price change, 2020–2025 (%)',labelpad=12) ax.set_xlim(left=0) ax.legend(title='Population change, 2020–2025',loc='upper center',bbox_to_anchor=(.5,-.25),frameon=False,ncol=2 if mobile else 4,fontsize=9,title_fontsize=9,columnspacing=1.3) for code,label in EXAMPLES.items(): r=next(r for r in data if r['county_fips']==code);x,y=float(r[xkey]),float(r[ykey]);ax.scatter([x],[y],s=40,color='#1e2925',edgecolors='white',linewidths=.8,zorder=4) offset={'12105':(15,23),'37183':(-72,9),'48491':(12,-22)}[code] ax.annotate(label,(x,y),xytext=offset,textcoords='offset points',fontsize=9, bbox={'facecolor':'#f7f8f0','edgecolor':'none','pad':1.5},arrowprops={'arrowstyle':'-','color':'#526257','lw':.7}) ax.tick_params(labelsize=10) suffix='-mobile' if mobile else '' fig.savefig(destination/f'{kind}{suffix}.svg',metadata={'Date':None,'Creator':'Actually Clean Data; matplotlib 3.10.7'}) if not mobile:fig.savefig(destination/f'{kind}.png',dpi=180,metadata={'Software':'Actually Clean Data; matplotlib 3.10.7'}) plt.close(fig) if __name__=='__main__': render('county_comparison.csv','figures')