<jupyter_start><jupyter_text>Housing Share of Income (based on pop & housing density)*Split by homeowners & renters<jupyter_code>import pandas as pd
import numpy as np
%run chart_theme.py
rent_share_inc_pop_den5 = pd.read_csv('rent_share_inc_by_pop_den_5yr_0816.csv')
rent_share_inc_pop_den1 = pd.read_csv('rent_share_inc_by_pop_den_1yr_1018.csv')
rent_pop_den_df = pd.concat([rent_share_inc_pop_den5, rent_share_inc_pop_den1[rent_share_inc_pop_den1.Year > 2016]]).sort_values(['Home_Units', 'Year']).reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>*Adjusting 2017 & 2018 values for '10-25' & 'Less than 10' cols (since 1-yr data included few counties of low pop densities, resulting in distorted figures)<jupyter_code>rent_pop_den_df['pct_chg'] = rent_pop_den_df['Rent_Share_Inc'].pct_change()
for year in [2017, 2018]:
    yr_df = rent_pop_den_df[rent_pop_den_df.Year == year]
    for typ in ['100 to 250', '50 to 100', '25 to 50', '10 to 25', 'Less than 10']:
        index = yr_df[yr_df.Home_Units == typ].index[0]
        rent_pop_den_df.loc[index, 'Rent_Share_Inc'] = round(rent_pop_den_df['Rent_Share_Inc'][(rent_pop_den_df.Year == year-1) & (rent_pop_den_df.Home_Units == typ)].iloc[0] + rent_pop_den_df['Rent_Share_Inc'][(rent_pop_den_df.Year == year-1) & (rent_pop_den_df.Home_Units == typ)].iloc[0] * rent_pop_den_df.pct_chg[(rent_pop_den_df.Year == year) & (rent_pop_den_df.Home_Units == 'More than 1000')].iloc[0], 3)
dic = {}
for t in ['More than 1000', '250 to 1000', '100 to 250', '50 to 100', '25 to 50', '10 to 25', 'Less than 10']:
    dic[t] = t.split()[-1]
rent_pop_den_df['Order'] = rent_pop_den_df.Home_Units.map(dic).astype(int)
rent_pop_den_df = rent_pop_den_df.sort_values(['Order', 'Home_Units'], ascending=[False, False])
rent_pop_den = alt.Chart(rent_pop_den_df).mark_line(size=8, strokeCap='round', interpolate='basis').encode(
x=alt.X('Year:O', axis=alt.Axis(values=[2008, 2018], ticks=True, tickSize=10, titleFontSize=28, labelFontSize=26, labelOpacity=.8)),
y=alt.Y('Rent_Share_Inc', title='Counties grouped by population density', 
        scale=alt.Scale(domain=[10,40]),
        axis=alt.Axis(values=list(range(15,40,5)),
                      titleX=-35, titleY=-35, titleFontSize=26, titleOpacity=.8,
                      labelFontSize=26, labelOpacity=.8, labelPadding=35)),
color=alt.Color('Home_Units', scale=alt.Scale(domain=list(rent_pop_den_df.Home_Units.unique()), 
                                             range=['#b2182b','#ef8a62','#fddbc7','#e2e0d3','#d1e5f0','#67a9cf','#2166ac']), title='Residents / sq. mile')
)

y_text_df = pd.DataFrame({'x':[2008], 'y':[40], 'text':['40% of income']})
y_text = alt.Chart(y_text_df).mark_text(dx=21, dy=0, fontSize=26, font='lato', opacity=.8).encode(x=alt.X('x:O'), y='y', text='text')
line = alt.Chart(pd.DataFrame({'x':[2014], 'y':[40], 'text':['_'*72]})).mark_text(dx=15, dy=-4, opacity=.09, fontSize=11.5, fontWeight='bold').encode(x=alt.X('x:O', axis=None), y=alt.Y('y', axis=None), text='text')


rent_pop_den_chart = (rent_pop_den + y_text + line).properties(width=600, height=450)
rent_pop_den_chart
#rent_pop_den_chart.save('rent_pop_den.png', scale_factor=3)<jupyter_output><empty_output><jupyter_text>---<jupyter_code>mort_share_inc_pop_den5 = pd.read_csv('mort_share_inc_by_pop_den_5yr_0816.csv')
mort_share_inc_pop_den1 = pd.read_csv('mort_share_inc_by_pop_den_1yr_1018.csv')
mort_pop_den_df = pd.concat([mort_share_inc_pop_den5, mort_share_inc_pop_den1[mort_share_inc_pop_den1.Year > 2016]]).sort_values(['Home_Units', 'Year']).reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>*Adjusting 2017 & 2018 values for some cols (since 1-yr data included few counties of low pop densities, resulting in distorted figures)<jupyter_code>mort_pop_den_df['pct_chg'] = mort_pop_den_df['Mort_Share_Inc'].pct_change()
for year in [2017, 2018]:
    yr_df = mort_pop_den_df[mort_pop_den_df.Year == year]
    for typ in ['100 to 250', '50 to 100', '25 to 50', '10 to 25', 'Less than 10']:
        index = yr_df[yr_df.Home_Units == typ].index[0]
        mort_pop_den_df.loc[index, 'Mort_Share_Inc'] = round(mort_pop_den_df['Mort_Share_Inc'][(mort_pop_den_df.Year == year-1) & (mort_pop_den_df.Home_Units == typ)].iloc[0] + mort_pop_den_df['Mort_Share_Inc'][(mort_pop_den_df.Year == year-1) & (mort_pop_den_df.Home_Units == typ)].iloc[0] * mort_pop_den_df.pct_chg[(mort_pop_den_df.Year == year) & (mort_pop_den_df.Home_Units == 'More than 1000')].iloc[0], 3)
dic = {}
for t in ['More than 1000', '250 to 1000', '100 to 250', '50 to 100', '25 to 50', '10 to 25', 'Less than 10']:
    dic[t] = t.split()[-1]
mort_pop_den_df['Order'] = mort_pop_den_df.Home_Units.map(dic).astype(int)
mort_pop_den_df = mort_pop_den_df.sort_values(['Order', 'Home_Units'], ascending=[False, False])
mort_pop_den = alt.Chart(mort_pop_den_df).mark_line(size=8, strokeCap='round', interpolate='basis').encode(
x=alt.X('Year:O', axis=alt.Axis(values=[2008, 2018], ticks=True, tickSize=10, titleFontSize=28, labelFontSize=26, labelOpacity=.8)),
y=alt.Y('Mort_Share_Inc', title='Counties grouped by population density', 
        scale=alt.Scale(domain=[10,40]),
        axis=alt.Axis(values=list(range(15,40,5)),
                      titleX=-2, titleY=-35, titleFontSize=26, titleOpacity=.8,
                      labelFontSize=26, labelOpacity=0, labelPadding=0)),
color=alt.Color('Home_Units', scale=alt.Scale(domain=list(mort_pop_den_df.Home_Units.unique()), 
                                             range=['#b2182b','#ef8a62','#fddbc7','#e2e0d3','#d1e5f0','#67a9cf','#2166ac']), title='Residents / sq. mile')
)

line = alt.Chart(pd.DataFrame({'x':[2013], 'y':[40], 'text':['_'*94]})).mark_text(dx=0, dy=-4, opacity=.09, fontSize=11.5, fontWeight='bold').encode(x=alt.X('x:O', axis=None), y=alt.Y('y', axis=None), text='text')


mort_pop_den_chart = (mort_pop_den + line).properties(width=600, height=450)
comb_pop_den_chart = (rent_pop_den_chart | mort_pop_den_chart).configure_legend(orient='none', legendX=1250, legendY=0, titleFont='Lato', titleFontSize=26, titleOpacity=.85, titleLimit=300, labelFont='Lato', labelFontSize=26, labelOpacity=.85, labelLimit=300, symbolSize=400, symbolStrokeWidth=8)
comb_pop_den_chart
comb_pop_den_chart.save('comb_hous_share_pop_den.svg')<jupyter_output><empty_output><jupyter_text>---<jupyter_code>mort_share_inc_hous_den5 = pd.read_csv('mort_share_inc_by_hous_den_5yr_0816.csv')
mort_share_inc_hous_den1 = pd.read_csv('mort_share_inc_by_hous_den_1yr_1018.csv')
mort_hous_den_df = pd.concat([mort_share_inc_hous_den5, mort_share_inc_hous_den1[mort_share_inc_hous_den1.Year > 2016]]).sort_values(['Hous_Den', 'Year']).reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>*Adjusting 2017 & 2018 values for most cols (since 1-yr data included few counties of low pop densities, resulting in distorted figures)<jupyter_code>mort_hous_den_df['pct_chg'] = mort_hous_den_df['Mort_Share_Inc'].pct_change()
for year in [2017, 2018]:
    yr_df = mort_hous_den_df[mort_hous_den_df.Year == year]
    for typ in ['30 to 75', '20 to 30', '10 to 20', '5 to 10', 'Less than 5']:
        index = yr_df[yr_df.Hous_Den == typ].index[0]
        mort_hous_den_df.loc[index, 'Mort_Share_Inc'] = round(mort_hous_den_df['Mort_Share_Inc'][(mort_hous_den_df.Year == year-1) & (mort_hous_den_df.Hous_Den == typ)].iloc[0] + mort_hous_den_df['Mort_Share_Inc'][(mort_hous_den_df.Year == year-1) & (mort_hous_den_df.Hous_Den == typ)].iloc[0] * mort_hous_den_df.pct_chg[(mort_hous_den_df.Year == year) & (mort_hous_den_df.Hous_Den == '75 to 200')].iloc[0], 3)
dic = {}
for t in ['More than 200', '75 to 200', '30 to 75', '20 to 30', '10 to 20', '5 to 10', 'Less than 5']:
    dic[t] = t.split()[-1]
mort_hous_den_df['Order'] = mort_hous_den_df.Hous_Den.map(dic).astype(int)
mort_hous_den_df = mort_hous_den_df.sort_values(['Order', 'Hous_Den'], ascending=[False, False])
mort_hous_den = alt.Chart(mort_hous_den_df, title='Homeowner Income Devoted to Housing').mark_line(size=8, strokeCap='round', interpolate='basis').encode(
x=alt.X('Year:O', axis=alt.Axis(values=[2008, 2018], ticks=True, tickSize=10, labelFontSize=26, labelOpacity=.8)),
y=alt.Y('Mort_Share_Inc', title='Counties grouped by housing density', 
        scale=alt.Scale(domain=[10,40]),
        axis=alt.Axis(values=list(range(15,40,5)),
                      titleX=-2, titleY=-35, titleFontSize=26, titleOpacity=.8,
                      labelFontSize=26, labelOpacity=0, labelPadding=0)),
color=alt.Color('Hous_Den', scale=alt.Scale(domain=list(mort_hous_den_df.Hous_Den.unique()), 
                                             range=['#b2182b','#ef8a62','#fddbc7','#e2e0d3', '#d1e5f0','#67a9cf','#2166ac']), title='Housing Units / sq. mile')
)

line = alt.Chart(pd.DataFrame({'x':[2013], 'y':[40], 'text':['_'*94]})).mark_text(dx=0, dy=-4, opacity=.09, fontSize=11.5, fontWeight='bold').encode(x=alt.X('x:O', axis=None), y=alt.Y('y', axis=None), text='text')


mort_hous_den_chart = (mort_hous_den + line).properties(width=600, height=450)
mort_hous_den_chart<jupyter_output><empty_output><jupyter_text>---<jupyter_code>rent_share_inc_hous_den5 = pd.read_csv('rent_share_inc_by_hous_den_5yr_0816.csv')
rent_share_inc_hous_den1 = pd.read_csv('rent_share_inc_by_hous_den_1yr_1018.csv')
rent_hous_den_df = pd.concat([rent_share_inc_hous_den5, rent_share_inc_hous_den1[rent_share_inc_hous_den1.Year > 2016]]).sort_values(['Hous_Den', 'Year']).reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>*Adjusting 2017 & 2018 values for most cols (since 1-yr data included few counties of low pop densities, resulting in distorted figures)<jupyter_code>rent_hous_den_df['pct_chg'] = rent_hous_den_df['Rent_Share_Inc'].pct_change()
for year in [2017, 2018]:
    yr_df = rent_hous_den_df[rent_hous_den_df.Year == year]
    for typ in ['30 to 75', '20 to 30', '10 to 20', '5 to 10', 'Less than 5']:
        index = yr_df[yr_df.Hous_Den == typ].index[0]
        rent_hous_den_df.loc[index, 'Rent_Share_Inc'] = round(rent_hous_den_df['Rent_Share_Inc'][(rent_hous_den_df.Year == year-1) & (rent_hous_den_df.Hous_Den == typ)].iloc[0] + rent_hous_den_df['Rent_Share_Inc'][(rent_hous_den_df.Year == year-1) & (rent_hous_den_df.Hous_Den == typ)].iloc[0] * rent_hous_den_df.pct_chg[(rent_hous_den_df.Year == year) & (rent_hous_den_df.Hous_Den == '75 to 200')].iloc[0], 3)
dic = {}
for t in ['More than 200', '75 to 200', '30 to 75', '20 to 30', '10 to 20', '5 to 10', 'Less than 5']:
    dic[t] = t.split()[-1]
rent_hous_den_df['Order'] = rent_hous_den_df.Hous_Den.map(dic).astype(int)
rent_hous_den_df = rent_hous_den_df.sort_values(['Order', 'Hous_Den'], ascending=[False, False])
rent_hous_den = alt.Chart(rent_hous_den_df, title='Renter Income Devoted to Housing').mark_line(size=8, strokeCap='round', interpolate='basis').encode(
x=alt.X('Year:O', axis=alt.Axis(values=[2008, 2018], ticks=True, tickSize=10, labelFontSize=26, labelOpacity=.8)),
y=alt.Y('Rent_Share_Inc', title='Counties grouped by housing density', 
        scale=alt.Scale(domain=[10,40]),
        axis=alt.Axis(values=list(range(15,40,5)),
                      titleX=-35, titleY=-30, titleFontSize=26, titleOpacity=.8,
                      labelFontSize=26, labelOpacity=.8, labelPadding=35)),
color=alt.Color('Hous_Den', scale=alt.Scale(domain=list(rent_hous_den_df.Hous_Den.unique()), 
                                             range=['#b2182b','#ef8a62','#fddbc7','#e2e0d3', '#d1e5f0','#67a9cf','#2166ac']), title='Housing Units / sq. mile')
)

y_text_df = pd.DataFrame({'x':[2008], 'y':[40], 'text':['40% of income']})
y_text = alt.Chart(y_text_df).mark_text(dx=21, dy=0, fontSize=26, font='lato', opacity=.8).encode(x=alt.X('x:O'), y='y', text='text')
line = alt.Chart(pd.DataFrame({'x':[2014], 'y':[40], 'text':['_'*72]})).mark_text(dx=13, dy=-4, opacity=.09, fontSize=11.5, fontWeight='bold').encode(x=alt.X('x:O', axis=None), y=alt.Y('y', axis=None), text='text')


rent_hous_den_chart = (rent_hous_den + y_text + line).properties(width=600, height=450)
comb_hous_den_chart = (rent_hous_den_chart | mort_hous_den_chart).configure_legend(orient='none', legendX=1250, legendY=0, titleFont='Lato', titleFontSize=26, titleOpacity=.85, titleLimit=300, labelFont='Lato', labelFontSize=26, labelOpacity=.85, labelLimit=300, symbolSize=400, symbolStrokeWidth=8)
comb_hous_den_chart
comb_hous_den_chart.save('comb_hous_share_hous_den.svg')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17428019945311243, 'fraction_numerical': 0.06200739906707415, 'mean_word_length': 6.267679719462302, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '374895', 'license': 'MIT', 'n_tokens_mistral': 5556, 'n_tokens_neox': 4966, 'n_words': 737, 'path': 'Notebooks/Den_Charts_Hous_Share_Inc.ipynb', 'repo_name': 'hassenmorad/Home-to-Income-Ratio', 'size': '12434'}