Example #1
0
def plot_states(states,
                values,
                title="",
                legend="",
                colors=DEFAULT_COLORS,
                complements=DEFAULT_COMPLEMENTS,
                font={},
                spacing={},
                decimals=1,
                shape="hex",
                quantile=False):
    states = [_get_state(state) for state in states]
    states, values = zip(*[(state, value)
                           for state, value in zip(states, values)
                           if state in STATE_ABBREVS.values()])
    if quantile:
        values = pd.qcut(values, 6, [-20, -10, -1, 1, 10, 20])

    # Colors
    cbin = Colorbin(values, colors, proportional=True, decimals=None)
    cbin.set_decimals(decimals)
    cbin.recalc(fenceposts=True)
    cbin.calc_complements(0.0, *complements)

    # Choropleth
    cg = Chorogrid(STATE_FILEPATH, states, cbin.colors_out)
    cg.set_title(title, font_dict=font)
    if quantile:
        labels = _quantile_labels(len(colors))
    else:
        labels = cbin.labels
    cg.set_legend(cbin.colors_in, labels, title=legend)

    # Draw
    draw_fn = {
        "hex": cg.draw_hex,
        "square": cg.draw_squares,
        "multihex": cg.draw_multihex,
        "multisquare": cg.draw_multisquare,
        "map": cg.draw_map
    }[shape]
    draw_fn(spacing_dict=spacing)
    return cg.done(show=True)
Example #2
0
def plot_counties(fips,
                  values,
                  title="",
                  legend="",
                  colors=DEFAULT_COLORS,
                  complements=DEFAULT_COMPLEMENTS,
                  font={},
                  spacing={},
                  decimals=1,
                  quantile=False,
                  statelines=False):
    fips, values = zip(*[(fip, value) for fip, value in zip(fips, values)
                         if fip in COUNTY_IDS])
    if quantile:
        values = pd.qcut(values, 6, [-20, -10, -1, 1, 10, 20])

    # Colors
    cbin = Colorbin(values, colors, proportional=True, decimals=None)
    cbin.set_decimals(decimals)
    cbin.recalc(fenceposts=True)
    cbin.calc_complements(0.0, *complements)

    # Choropleth
    cg = Chorogrid(COUNTY_FILEPATH,
                   fips,
                   cbin.colors_out,
                   id_column="fips_integer")
    cg.set_title(title, font_dict=font)
    if quantile:
        labels = _quantile_labels(len(colors))
    else:
        labels = cbin.labels
    cg.set_legend(cbin.colors_in, labels, title=legend)

    # Draw
    cg.draw_map(spacing_dict=spacing)
    if statelines:
        with open(STATELINES_FILEPATH, 'r') as f:
            statelines = f.read()
        cg.add_svg(statelines)
    return cg.done(show=True)
listings['dates_object'] = listings.ListingStartDate.map(lambda x: try_parsing_date(x))
listings['Year'] = listings.dates_object.map(lambda x:  x.year)

## data visualization --------------------------------------------------------------------------------------

# 1.1 number of loans issued by states, normalized by state population -  choropleth map 
counts = listings.groupby(['BorrowerState']).ListingAmountFunded.count()
population.index = population['state']
df = population.join(counts, how = 'right')
df['percentage'] = df['ListingAmountFunded']/df['population']*10000

mycolors = ['#e0e9f0', '#c1d4e2',  '#a3bed4', '#84a9c6','#6694b8', '#517693', '#3d586e']
mybin = Colorbin(df['percentage'], mycolors, proportional=True, decimals=None)
mybin.set_decimals(1)
mybin.recalc(fenceposts=True)
mybin.fenceposts
mybin.calc_complements(0.5, '#e0e0e0', '#101010')
states = list(df.index)
colors_by_state = mybin.colors_out
font_colors_by_state = mybin.complements
legend_colors = mybin.colors_in
legend_labels = mybin.labels
cg = Chorogrid('chorogrid/databases/usa_states.csv', states, colors_by_state)
cg.set_title('Number of P2P Loans Normalized by State Population', font_dict={'font-size': 16})
cg.set_legend(legend_colors, legend_labels, title='# of Loans scaled by population')
cg.draw_hex(spacing_dict={'margin_right': 210}, font_colors=font_colors_by_state)
cg.done(show=True)

# 1.2 number of loans issued by states -  choropleth map 
df = listings.groupby(['BorrowerState']).ListingAmountFunded.count()
Example #4
0
dfStateUnEmp = dfFred[dfFred['title'].str.contains("Unemployment Rate in ")&~dfFred['title'].str.contains("Census")&dfFred['seasonal_adjustment_short'].str.match('SA')&~dfFred['title'].str.contains('DISCONTINUED')]

srsStateUnIDs = dfStateUnEmp['id'][:]

dictStateUN = {}
for i in srsStateUnIDs:
    dictStateUN[i[:2]] = apiFred.get_series(i,observation_start='2007-01-01').mean()

dfStateUN = pd.DataFrame(list(dictStateUN.items()),columns=['State', 'Unemployment'])


##Make the Map
colors = ['#fff5eb', '#fee6ce', '#fdd0a2', '#fdae6b', '#fd8d3c', '#f16913', '#d94801', '#8c2d04']
chBin = Colorbin(dfStateUN['Unemployment'], colors, proportional=True, decimals=None)
chBin.set_decimals(1)
chBin.recalc(fenceposts=True)
chBin.fenceposts
colors_out = chBin.colors_out
legend_colors = chBin.colors_in
legend_labels = chBin.labels
cg = Chorogrid('X:\\Documents\\Research\\Tools\\Python\\chorogrid-master\\chorogrid\\databases\\usa_states.csv', list(dfStateUN['State']), colors_out)
cg.set_title('Average Unemployment by State (%)', font_dict={'font-size': 16})
cg.set_legend(legend_colors, legend_labels, title='% Unemployment')
cg.draw_map(spacing_dict={'margin_right': 400})
cg.done(show=True)


####### Import the LC Data #######

path = 'D:\\Andrew\\VM_Storage\\LendingClub\\LoanStats3.csv'
Example #5
0
        i, observation_start='2007-01-01').mean()

dfStateUN = pd.DataFrame(list(dictStateUN.items()),
                         columns=['State', 'Unemployment'])

##Make the Map
colors = [
    '#fff5eb', '#fee6ce', '#fdd0a2', '#fdae6b', '#fd8d3c', '#f16913',
    '#d94801', '#8c2d04'
]
chBin = Colorbin(dfStateUN['Unemployment'],
                 colors,
                 proportional=True,
                 decimals=None)
chBin.set_decimals(1)
chBin.recalc(fenceposts=True)
chBin.fenceposts
colors_out = chBin.colors_out
legend_colors = chBin.colors_in
legend_labels = chBin.labels
cg = Chorogrid(
    'X:\\Documents\\Research\\Tools\\Python\\chorogrid-master\\chorogrid\\databases\\usa_states.csv',
    list(dfStateUN['State']), colors_out)
cg.set_title('Average Unemployment by State (%)', font_dict={'font-size': 16})
cg.set_legend(legend_colors, legend_labels, title='% Unemployment')
cg.draw_map(spacing_dict={'margin_right': 400})
cg.done(show=True)

####### Import the LC Data #######

path = 'D:\\Andrew\\VM_Storage\\LendingClub\\LoanStats3.csv'