<jupyter_start><jupyter_text>Income Devoted to Housing (distribution)<jupyter_code>file = pd.read_csv('county_rent_mort_inc_units_5yr.csv')
cols = ['Year', 'FIPS', 'County', 'Med_Mort', 'Med_Rent', 'Med_Mort_Inc', 'Med_Rent_Inc', 'Mort_Share_Inc', 'Rent_Share_Inc', 'Total_Owned', 'Total_Rented']
last = file[cols][file.Year == 2016]
last['Rent_Share_Inc_Bin'] = last.Rent_Share_Inc.round().values
last['Mort_Share_Inc_Bin'] = last.Mort_Share_Inc.round().values
vals_dict = {}
for val in last.Rent_Share_Inc_Bin.dropna().unique():
    bin_fips = last.FIPS[last.Rent_Share_Inc_Bin == val].values
    vals_dict[val] = len(bin_fips)
df = pd.DataFrame(vals_dict.items())
df.columns = ['Bin', 'Count']
chart = alt.Chart(df, title='Renter Income Devoted to Housing (2014-2018)'
                 ).mark_bar(size=11, color='#cede54'
                           ).encode(x=alt.X('Bin', title='Income Devoted to Housing', axis=alt.Axis(titleY=35, titleOpacity=.8, titleFontSize=18, 
                                                                                                    labelOpacity=0, labelFontSize=16)), 
                                    y=alt.Y('Count', title='In most counties, the median share of renter income devoted to housing was', 
                                            axis=alt.Axis(values=list(range(100,300,100)), 
                                                          titleX=-30, titleY=-30, titleOpacity=.8, titleFontSize=18, 
                                                          labelOpacity=.8, labelPadding=30, labelFontSize=16), 
                                            scale=alt.Scale(domain=[0,305])))
y_title2 = alt.Chart(pd.DataFrame({'x':[10.2], 'y':[363], 'text':['between 20% and 40%']})).mark_text(font='lato', fontSize=18, opacity=.8).encode(x='x', y='y', text='text')
x_text_df = pd.DataFrame({'x':list(range(10,60,10)), 'y':[0]*5, 'text':['   10%'] + [str(i) for i in range(20,60,10)]})
x_text = alt.Chart(x_text_df).mark_text(dx=0, dy=17, fontSize=16, font='lato', opacity=.8).encode(x=alt.X('x'), y='y', text='text')
y_text_df = pd.DataFrame({'x':[10], 'y':[300], 'text':['300 counties']})
y_text = alt.Chart(y_text_df).mark_text(dx=-44, dy=0, fontSize=16, font='lato', opacity=.8).encode(x=alt.X('x', scale=alt.Scale(domain=[10,50])), y='y', text='text')
line = alt.Chart(pd.DataFrame({'x1':[10.2], 'x2':[55], 'y1':[300]})).mark_rule(opacity=.15).encode(x=alt.X('x1', scale=alt.Scale(domain=[10,50])), x2='x2',y='y1')

rent_inc_dist = (chart + line + x_text + y_text + y_title2).configure_title(fontSize=24)
rent_inc_dist
rent_inc_dist.save('rent_inc_share_dist.svg')
vals_dict = {}
for val in last.Mort_Share_Inc_Bin.dropna().unique():
    bin_fips = last.FIPS[last.Mort_Share_Inc_Bin == val].values
    vals_dict[val] = len(bin_fips)
df = pd.DataFrame(vals_dict.items())
df.columns = ['Bin', 'Count']
chart = alt.Chart(df, title='Homeowner Income Devoted to Housing (2014-2018)'
                 ).mark_bar(size=11, color='#97c2f2'
                           ).encode(x=alt.X('Bin', title='Income Devoted to Housing', axis=alt.Axis(titleY=35, titleOpacity=.8, titleFontSize=18, 
                                                                                                    labelOpacity=0, labelFontSize=16)),                                                                                                     
                                    y=alt.Y('Count', title='In most counties, the median share of homeowner income devoted to housing was', 
                                            axis=alt.Axis(values=list(range(100,500,100)), 
                                                          titleX=-30, titleY=-30, titleFontSize=18, titleOpacity=.8, 
                                                          labelOpacity=.8, labelPadding=30, labelFontSize=16), 
                                            scale=alt.Scale(domain=[0,501])))
y_title2 = alt.Chart(pd.DataFrame({'x':[10.2], 'y':[570], 'text':['between 10% and 20%']})).mark_text(font='lato', fontSize=18, opacity=.8).encode(x='x', y='y', text='text')
x_text_df = pd.DataFrame({'x':list(range(5,30,5)), 'y':[0]*5, 'text':['   5%'] + [str(i) for i in range(10,30,5)]})
x_text = alt.Chart(x_text_df).mark_text(dx=0, dy=17, fontSize=16, font='lato', opacity=.8).encode(x=alt.X('x'), y='y', text='text')
y_text_df = pd.DataFrame({'x':[10], 'y':[500], 'text':['500 counties']})
y_text = alt.Chart(y_text_df).mark_text(dx=-44, dy=0, fontSize=16, font='lato', opacity=.8).encode(x=alt.X('x', scale=alt.Scale(domain=[10,50])), y='y', text='text')
line = alt.Chart(pd.DataFrame({'x1':[10.3], 'x2':[55], 'y1':[500]})).mark_rule(opacity=.15).encode(x=alt.X('x1', scale=alt.Scale(domain=[10,50])), x2='x2',y='y1')

mort_inc_dist = (line + chart + x_text + y_text + y_title2).configure_title(fontSize=24)
mort_inc_dist
mort_inc_dist.save('mort_inc_share_dist.svg')<jupyter_output><empty_output><jupyter_text>--- Digging deeper into income devoted to housing:<jupyter_code>rent_hi = last[last.Med_Rent > last.Med_Mort]
mort_hi = last[last.Med_Mort > last.Med_Rent]
len(rent_hi)
len(mort_hi)
rent_hi['Rent_Housing_Rent_Higher_Bin'] = rent_hi.Med_Rent.round().values
rent_hi['Rent_Housing_Rent_Higher_Inc_Bin'] = rent_hi.Med_Rent_Inc.round().values
rent_hi['Rent_Housing_Rent_Higher_Share_Inc_Bin'] = rent_hi.Rent_Share_Inc.round().values
mort_hi['Rent_Housing_Mort_Higher_Bin'] = mort_hi.Med_Rent.round().values
mort_hi['Rent_Housing_Mort_Higher_Inc_Bin'] = mort_hi.Med_Rent_Inc.round().values
mort_hi['Rent_Housing_Mort_Higher_Share_Inc_Bin'] = mort_hi.Rent_Share_Inc.round().values

rent_hi['Mort_Housing_Rent_Higher_Bin'] = rent_hi.Med_Mort.round().values
rent_hi['Mort_Housing_Rent_Higher_Inc_Bin'] = rent_hi.Med_Mort_Inc.round().values
rent_hi['Mort_Housing_Rent_Higher_Share_Inc_Bin'] = rent_hi.Mort_Share_Inc.round().values
mort_hi['Mort_Housing_Mort_Higher_Bin'] = mort_hi.Med_Mort.round().values
mort_hi['Mort_Housing_Mort_Higher_Inc_Bin'] = mort_hi.Med_Mort_Inc.round().values
mort_hi['Mort_Housing_Mort_Higher_Share_Inc_Bin'] = mort_hi.Mort_Share_Inc.round().values

rent_dict = {}
dfs = [rent_hi, rent_hi, rent_hi, mort_hi, mort_hi, mort_hi]*2
divs = [40, 200, 1]*2 + [40, 200, 1]*2
counter = 0
for col in ['Rent_Housing_Rent_Higher_Bin', 'Rent_Housing_Rent_Higher_Inc_Bin', 
            'Rent_Housing_Rent_Higher_Share_Inc_Bin', 'Rent_Housing_Mort_Higher_Bin', 
            'Rent_Housing_Mort_Higher_Inc_Bin', 'Rent_Housing_Mort_Higher_Share_Inc_Bin',
            'Mort_Housing_Rent_Higher_Bin', 'Mort_Housing_Rent_Higher_Inc_Bin', 
            'Mort_Housing_Rent_Higher_Share_Inc_Bin', 'Mort_Housing_Mort_Higher_Bin', 
            'Mort_Housing_Mort_Higher_Inc_Bin', 'Mort_Housing_Mort_Higher_Share_Inc_Bin']:
    df = dfs[counter]
    df[col] = [int(round(i/divs[counter])*divs[counter]) for i in df[col].values]
    vals_dict = {}
    for val in df[col].dropna().unique():
        bin_fips = df.FIPS[df[col] == val].values
        vals_dict[val] = len(bin_fips)
        
        dataframe = pd.DataFrame(vals_dict.items())
        dataframe.columns = ['Bin', 'Count']
    rent_dict[col] = dataframe
    counter += 1<jupyter_output>C:\Users\Mahmoud\Miniconda3\envs\data_analysis\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  """Entry point for launching an IPython kernel.
C:\Users\Mahmoud\Miniconda3\envs\data_analysis\lib\site-packages\ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  
C:\Users\Mahmoud\Miniconda3\envs\data_analysis\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .[...]<jupyter_text>Income Devoted to Housing  (1) Renters:<jupyter_code>%run chart_theme.py
charts_dict = {}
keys = ['Rent_Housing_Rent_Higher_Bin', 'Mort_Housing_Rent_Higher_Bin', 'Rent_Housing_Mort_Higher_Bin', 'Mort_Housing_Mort_Higher_Bin']
x_titles = ['Monthly Renter Housing Expense', 'Monthly Homeowner Housing Expense','','']
x_title_pads = [350,350,0,0]
y_titles1 = ['Renting More','','Owning More','']
y_titles2 = ['Expensive','','Expensive','']
y_titles1_pad = [-270,200]*2
y_titles2_pad = [-337,200]*2
opacities = [.8,0]*2
y_paddings = [50,0]*2
colors = ['#cede54','#97c2f2']*2
y_texts = [-2,500]*2
line_x = [20,-30]*2
line_len = [92,110]*2
counter = 0

for key in keys:
    df = rent_dict[key]
    df = df[df.Bin < 2200]
    
    # Calculating Median Bin (to display as text on chart)
    bins = array.array('i')
    for bn in df.Bin:
        count = df.Count[df.Bin == bn].iloc[0]
        bins.extend(np.full(count, bn))
        
    chart = alt.Chart(df).mark_bar(size=13, color=colors[counter]
                               ).encode(x=alt.X('Bin', title=x_titles[counter], 
                                                axis=alt.Axis(values=list(range(800,2400,400)), titleOpacity=.8, titleFontSize=40, titleX=x_title_pads[counter], titleY=-550, 
                                                              labelOpacity=.8, labelFontSize=28, format='a'), 
                                                scale=alt.Scale(domain=[400,2000])), 
                                        y=alt.Y('Count',
                                                axis=alt.Axis(values=list(range(80,240,80)),
                                                              labelPadding=y_paddings[counter], labelFontSize=28, labelOpacity=opacities[counter]),
                                                scale=alt.Scale(domain=[0,300])))

    text_df = pd.DataFrame({'x':[380,293,1300,2460], 'y':[0,240,240,270], 'text':['$400','240 counties','_'*line_len[counter],285]})
    line = alt.Chart(text_df.query('x==1300')).mark_text(dx=line_x[counter], dy=-4, opacity=.09, fontSize=11.5, fontWeight='bold').encode(x=alt.X('x', scale=alt.Scale(domain=[300,1000])), y='y', text='text')
    x_text = alt.Chart(text_df.query('x==380')).mark_text(dx=0, dy=24, fontSize=28, font='lato', opacity=.8).encode(x='x', y='y', text='text')
    y_text = alt.Chart(text_df.query('x==293')).mark_text(dx=y_texts[counter], dy=0, fontSize=28, font='lato', opacity=opacities[counter]).encode(x='x', y='y', text='text')
    y_title1 = alt.Chart(pd.DataFrame({'x':[y_titles1_pad[counter]], 'y':[130], 'text':y_titles1[counter]})).mark_text(font='lato', fontSize=30, opacity=.8).encode(x='x', y='y', text='text')
    y_title2 = alt.Chart(pd.DataFrame({'x':[y_titles2_pad[counter]], 'y':[110], 'text':y_titles2[counter]})).mark_text(font='lato', fontSize=30, opacity=.8).encode(x='x', y='y', text='text')    
    med_inc_text = alt.Chart(pd.DataFrame({'x':[1900], 'y':[30], 'text':['Median: $' + str(int(np.median(bins)))]})).mark_text(font='lato', fontSize=24, opacity=.8).encode(x='x', y='y', text='text')
   #chart_spacing = alt.Chart(text_df.query('x==2460')).mark_rule(opacity=0, size=3).encode(x='x', y='y', y2='text')

    combined = (line + chart + x_text + y_text + y_title1 + y_title2 + med_inc_text).properties(width=700, height=550)
    charts_dict[counter] = combined
    #combined.save(keys[counter] + str(counter) + '.png', scale_factor=6)
    counter += 1
renter_owner_hous_exp_charts1 = charts_dict[0] | charts_dict[1]
renter_owner_hous_exp_charts2 = charts_dict[2] | charts_dict[3]
renter_owner_hous_exp_charts1
renter_owner_hous_exp_charts2
renter_owner_hous_exp_charts1.save('renter_owner_hous_exp1.svg')
renter_owner_hous_exp_charts2.save('renter_owner_hous_exp2.svg')
#renter_owner_hous_exp_charts = (charts_dict[0] | charts_dict[3]) & (charts_dict[2] | charts_dict[1])
#renter_owner_hous_exp_charts<jupyter_output><empty_output><jupyter_text>---  (2) Homeowners<jupyter_code>charts_dict = {}
keys = ['Rent_Housing_Rent_Higher_Share_Inc_Bin', 'Mort_Housing_Rent_Higher_Share_Inc_Bin', 'Rent_Housing_Mort_Higher_Share_Inc_Bin', 'Mort_Housing_Mort_Higher_Share_Inc_Bin']
titles = ['Renter Income Devoted to Housing', 'Homeowner Income Devoted to Housing','','']
y_titles = ['Counties where renting is more expensive than owning']*2 + ['Counties where owning is more expensive than renting']*2
opacities = [.8,0]*2
colors = ['#cede54','#97c2f2']*2
vals = [list(range(10,60,10)), list(range(10,30,5))]*2
counter = 0
for key in keys:
    df = rent_dict[key]
    chart = alt.Chart(df, title=titles[counter]
                     ).mark_bar(size=14, color=colors[counter]
                           ).encode(x=alt.X('Bin', 
                                            axis=alt.Axis(values=vals[counter], labelFontSize=32, labelOpacity=.7),
                                            scale=alt.Scale(domain=[10,50])), 
                                    y=alt.Y('Count', title=y_titles[counter], 
                                            axis=alt.Axis(values=list(range(100,300,100)), 
                                                          titleX=-60, titleY=0, titleOpacity=.8, titleFontSize=36, 
                                                          labelPadding=60, labelFontSize=32, labelOpacity=opacities[counter]), 
                                            scale=alt.Scale(domain=[0,350])))

    text_df = pd.DataFrame({'x':[34,6,7,60], 'y':[300,0,300,0], 'text':['_'*94, '5%', '300 counties',-50]})
    line = alt.Chart(text_df.query('x==34')).mark_text(dx=-7, dy=-4, opacity=.09, fontSize=11.5, fontWeight='bold').encode(x=alt.X('x', scale=alt.Scale(domain=[10,50])), y='y', text='text')
    x_text = alt.Chart(text_df.query('x==6')).mark_text(dx=-5, dy=26.5, fontSize=32, font='lato', opacity=.7).encode(x='x', y='y', text='text')
    y_text = alt.Chart(text_df.query('x==7')).mark_text(dx=2, dy=0, fontSize=32, font='lato', opacity=opacities[counter]).encode(x='x', y='y', text='text')
    chart_spacing = alt.Chart(text_df.query('x==60')).mark_rule(opacity=0, size=3).encode(x='x', y='y', y2='text')
    
    combined = (line + chart + x_text + y_text + chart_spacing).properties(width=750, height=450)
    charts_dict[counter] = combined
    #combined.save(keys[counter] + str(counter) + '.png', scale_factor=6)
    counter += 1
inc_housing_charts = (charts_dict[0] | charts_dict[1]) & (charts_dict[2] | charts_dict[3])
inc_housing_charts
inc_housing_charts.save('renter_homeowner_inc_dev_hous.svg')
%run chart_theme.py
charts_dict = {}
keys = ['Rent_Housing_Rent_Higher_Inc_Bin', 'Mort_Housing_Rent_Higher_Inc_Bin', 'Rent_Housing_Mort_Higher_Inc_Bin', 'Mort_Housing_Mort_Higher_Inc_Bin']
x_titles = ['Monthly Renter Incomes', 'Monthly Homeowner Incomes','','']
x_title_pads = [350,350,0,0]
y_titles1 = ['Renting More','','Owning More','']
y_titles2 = ['Expensive','','Expensive','']
y_titles1_pad = [-2400,100]*2
y_titles2_pad = [-2780,100]*2
opacities = [.8,0]*2
x_paddings = [50,0]*2
colors = ['#cede54', '#97c2f2']*2
y_texts = [-2,500]*2
line_x = [0,-50]*2
line_len = [94,110]*2
counter = 0
    
for key in keys:
    df = rent_dict[key]
    df = df[df.Bin < 11000]  # Excluding outliers to minimize plot width
    
    # Calculating Median Bin (to display as text on chart)
    bins = array.array('i')
    for bn in df.Bin:
        count = df.Count[df.Bin == bn].iloc[0]
        bins.extend(np.full(count, bn))

    chart = alt.Chart(df).mark_bar(size=11.8, color=colors[counter]
                           ).encode(x=alt.X('Bin', title=x_titles[counter], 
                                            axis=alt.Axis(values=list(range(4000, 11000, 2000)), format='~s', 
                                                          titleOpacity=.8, titleFontSize=40, titleX=x_title_pads[counter], titleY=-580, 
                                                          labelFontSize=28, labelOpacity=.8),
                                            scale=alt.Scale(domain=[1000,10000])), 
                                    y=alt.Y('Count',
                                            axis=alt.Axis(values=list(range(100,300,100)),  
                                                          labelPadding=x_paddings[counter], labelFontSize=28, labelOpacity=opacities[counter]), 
                                            scale=alt.Scale(domain=[0,350])))

    text_df = pd.DataFrame({'x':[6300,1950,500,13200], 'y':[300,0,300,0], 'text':['_'*line_len[counter], '$2k', '300 counties',-40]})
    line = alt.Chart(text_df.query('x==6300')).mark_text(dx=line_x[counter], dy=-4, opacity=.09, fontSize=11.5, fontWeight='bold').encode(x=alt.X('x', scale=alt.Scale(domain=[1000,10000])), y='y', text='text')
    x_text = alt.Chart(text_df.query('x==1950')).mark_text(dx=-5, dy=24.5, fontSize=28, font='lato', opacity=.8).encode(x='x', y='y', text='text')
    y_text = alt.Chart(text_df.query('x==500')).mark_text(dx=y_texts[counter], dy=0, fontSize=28, font='lato', opacity=opacities[counter]).encode(x='x', y='y', text='text')
    y_title1 = alt.Chart(pd.DataFrame({'x':[y_titles1_pad[counter]], 'y':[170.5], 'text':y_titles1[counter]})).mark_text(font='lato', fontSize=30, opacity=.8).encode(x='x', y='y', text='text')
    y_title2 = alt.Chart(pd.DataFrame({'x':[y_titles2_pad[counter]], 'y':[145.5], 'text':y_titles2[counter]})).mark_text(font='lato', fontSize=30, opacity=.8).encode(x='x', y='y', text='text')    
    med_inc_text = alt.Chart(pd.DataFrame({'x':[9600], 'y':[35], 'text':['Median: $' + str(int(np.median(bins)))]})).mark_text(font='lato', fontSize=24, opacity=.8).encode(x='x', y='y', text='text')
    #chart_spacing = alt.Chart(text_df.query('x==13200')).mark_rule(opacity=0, size=3).encode(x='x', y='y', y2='text')
    
    combined = (line + chart + x_text + y_text + y_title1 + y_title2 + med_inc_text).properties(width=700, height=550)
    charts_dict[counter] = combined
    #combined.save(keys[counter] + str(counter) + '.png', scale_factor=6)
    counter += 1
incomes_charts1 = charts_dict[0] | charts_dict[1]
incomes_charts2 = charts_dict[2] | charts_dict[3]
incomes_charts1
incomes_charts2
incomes_charts1.save('renter_owner_incomes1.svg')
incomes_charts2.save('renter_owner_incomes2.svg')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1731958762886598, 'fraction_numerical': 0.04677156809549647, 'mean_word_length': 3.4920789666097978, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 71, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '503877', 'license': 'MIT', 'n_tokens_mistral': 7277, 'n_tokens_neox': 6651, 'n_words': 946, 'path': 'Notebooks/Housing_Dist_Charts.ipynb', 'repo_name': 'hassenmorad/Home-to-Income-Ratio', 'size': '18430'}