Exemplo n.º 1
0
def make_counties_no_territories():
    fips = {}
    with open('data/zip_fips.json', 'r') as read_obj:
        zips = json.load(read_obj)
    for key_zip in zips.keys():
        fip = zips[key_zip]['fips']
        state = zips[key_zip]['state']
        fips[fip] = state
    choropleth = choropleth_prep.Chorpleth('county')
    points = choropleth.points_dict
    final = {}
    for key in points.keys():
        state = fips.get(key)
        if state in ['GU', 'VI', 'PR', 'AS', 'MP', None]:
            continue
        l = choropleth.get_points(key)
        if point_is_territory(l):
            continue
        final[key] = {'x': [], 'y': []}
        for i in l:
            final[key]['x'] += i[0]
            final[key]['y'] += i[1]
            final[key]['state'] = state
    with open('data/counties_non_territories.json', 'w') as write_obj:
        json.dump(final, write_obj)
Exemplo n.º 2
0
def main():
    county, state = get_data()
    choropleth = choropleth_prep.Chorpleth(the_type='county')
    choropleth_state = choropleth_prep.Chorpleth(the_type='state')
    x, y = choropleth.get_points_flatten('KY')
    p1 = make_county(county, 2010, choropleth)
    p2 = make_county(county, 2011, choropleth)
    p3 = make_county(county, 2012, choropleth)
    p4 = make_county(county, 2013, choropleth)
    p5 = make_county(county, 2014, choropleth)
    p6 = make_county(county, 2015, choropleth)
    p7 = make_county(county, 2016, choropleth)
    p8 = make_county(county, 2017, choropleth)

    #grid = gridplot([p1, p2, p3, p4, p5, p6, p7, p8], ncols = 2, height=500,
    #width = 2.7 * 500)
    grid = gridplot([p1, p8], ncols=2, height=500, width=2.7 * 500)
    show(grid)
Exemplo n.º 3
0
def make_county(county, year, choropleth):
    choropleth_state = choropleth_prep.Chorpleth(the_type='state')
    x_state, y_state = choropleth_state.get_points_flatten('KY')
    x_state2, y_state2 = choropleth_state.get_points_flatten('OH')
    x_state3, y_state3 = choropleth_state.get_points_flatten('PA')
    x_state4, y_state4 = choropleth_state.get_points_flatten('VA')
    x_state5, y_state5 = choropleth_state.get_points_flatten('WV')
    x_state = x_state + x_state2 + x_state3 + x_state4 + x_state5
    y_state = y_state + y_state2 + y_state3 + y_state4 + y_state5
    nums = []
    for key in county[year].keys():
        nums.append(county[year][key])
    p85 = np.percentile(nums, 85)
    p85 = 200
    p85 = 150
    height = 250
    width = width = round(2.5 * height)
    p = figure(title=str(year), width=width, height=height)
    xs = []
    ys = []
    fill_colors = []
    min_x = 1e6
    max_x = -1e6
    min_y = 1e6
    max_y = -1e6
    for key in county[year].keys():
        num = county[year][key]
        x, y = choropleth.get_points_flatten(str(key))
        if x == None:
            continue
        if max(x) > max_x:
            max_x = max(x)
        if min(x) < min_x:
            min_x = min(x)
        if max(y) > max_y:
            max_y = max(y)
        if min(y) < min_y:
            min_y = min(y)
        xs.append(x)
        ys.append(y)
        fill_colors.append(get_color(num, p85))
    p.patches(xs,
              ys,
              fill_alpha=0.7,
              fill_color=fill_colors,
              line_color="white",
              line_width=0.001)
    p.patches([x_state], [y_state],
              fill_alpha=.01,
              fill_color='white',
              line_color="black",
              line_width=2)
    ratio = (max_x - min_x) / (max_y - min_y)
    return p
Exemplo n.º 4
0
def _main():
    choropleth = choropleth_prep.Chorpleth(the_type='state')
    xs, ys = choropleth.get_points_flatten('MA')
    xs2, ys2 = choropleth.get_points_flatten('NH')
    p = figure(title="test",
               toolbar_location="left",
               plot_width=1100,
               plot_height=700)
    p.patches([xs, xs2], [ys, ys2],
              fill_alpha=0.7,
              fill_color=['green', 'blue'],
              line_color="white",
              line_width=0.5)
Exemplo n.º 5
0
def resize_alaska_counties():
    choropleth = choropleth_prep.Chorpleth('state')
    points = choropleth.points_dict
    ak = points['AK']
    final = {'x': [], 'y': []}
    for i in ak:
        if max(i[0]) > 0:
            continue
        final['x'] += i[0]
        final['y'] += i[1]
    xs = [.5 * x - 30 for x in final['x']]
    ys = [.5 * x - 8 for x in final['y']]
    d = {'x': xs, 'y': ys}
    return d
Exemplo n.º 6
0
def move_hawaii():
    choropleth = choropleth_prep.Chorpleth('state')
    points = choropleth.points_dict
    ak = points['HI']
    final = {'x': [], 'y': []}
    for i in ak:
        if max(i[0]) > 0:
            continue
        final['x'] += i[0]
        final['y'] += i[1]
    xs = [x + 35 for x in final['x']]
    ys = [x + 5 for x in final['y']]
    d = {'x': xs, 'y': ys}
    return d
Exemplo n.º 7
0
def make_us_map(the_type,
                title="test map",
                plot_width=1100,
                plot_height=700,
                line_color="white",
                line_width=0.5,
                colors_dict=None,
                default_color="white",
                map_extent='continental_us',
                data=None,
                palette=Blues8,
                reverse_palette=True):
    if reverse_palette:
        palette.reverse()
    assert colors_dict == None or data == None
    choropleth = choropleth_prep.Chorpleth(the_type=the_type)
    d = _main_init_dict(_filter_points(choropleth.points_dict))
    add_data(d, 'data', data)
    add_data(d, 'legend', _make_legends(data, palette))
    xs, ys, data, legends = _sort_all_data(flatten(d, 'xs'), flatten(d, 'ys'),
                                           flatten(d, 'data'),
                                           flatten(d, 'legend'))
    color_mapper = LinearColorMapper(palette=palette)
    color_mapper.nan_color = default_color
    assert len(data) == len(xs)
    source = ColumnDataSource(data=dict(
        x=xs,
        y=ys,
        data=data,
        legend=legends,
    ))
    p = figure(title=title,
               toolbar_location="left",
               plot_width=plot_width,
               plot_height=plot_height)
    p.patches('x',
              'y',
              source=source,
              fill_color={
                  'field': 'data',
                  'transform': color_mapper
              },
              fill_alpha=0.7,
              line_color=line_color,
              line_width=line_width,
              legend='legend')

    show(p)
Exemplo n.º 8
0
def resize_alaska():
    choropleth = choropleth_prep.Chorpleth('state')
    points = choropleth.points_dict
    ak = points['AK']
    final = {'x': [], 'y': []}
    for i in ak:
        if max(i[0]) > 0:
            continue
        final['x'] += i[0]
        final['y'] += i[1]
    xs = [.5 * x - 30 for x in final['x']]
    ys = [.5 * x - 8 for x in final['y']]
    d = {'x': xs, 'y': ys}
    with open('data/alaska_resized.json', 'w') as write_obj:
        json.dump(final, write_obj)
    return d
Exemplo n.º 9
0
def make_states_no_territories():
    choropleth = choropleth_prep.Chorpleth('state')
    points = choropleth.points_dict
    final = {}
    for key in points.keys():
        if key in ['GU', 'VI', 'PR', 'AS', 'MP']:
            continue
        final[key] = {'x': [], 'y': []}
        l = choropleth.get_points(key)
        for i in l:
            if max(i[0]) > 0:
                continue
            final[key]['x'] += i[0]
            final[key]['y'] += i[1]
    with open('data/states_non_territories.json', 'w') as write_obj:
        json.dump(final, write_obj)
Exemplo n.º 10
0
def states_alaska_resize():
    ak = resize_alaska()
    hi = move_hawaii()
    choropleth = choropleth_prep.Chorpleth('state')
    points = choropleth.points_dict
    final = {}
    for key in points.keys():
        if key in ['GU', 'VI', 'PR', 'AS', 'MP', 'AK', 'HI']:
            continue
        final[key] = {'x': [], 'y': []}
        l = choropleth.get_points(key)
        for i in l:
            if max(i[0]) > 0:
                continue
            final[key]['x'] += i[0]
            final[key]['y'] += i[1]
    final['AK'] = ak
    final['HI'] = hi
    with open('data/states_states_resize.json', 'w') as write_obj:
        json.dump(final, write_obj)
Exemplo n.º 11
0
def get_alaska_hawaii_counties(needed_state):
    fips = {}
    final = {}
    with open('data/zip_fips.json', 'r') as read_obj:
        zips = json.load(read_obj)
    for key_zip in zips.keys():
        fip = zips[key_zip]['fips']
        state = zips[key_zip]['state']
        fips[fip] = state
    choropleth = choropleth_prep.Chorpleth('county')
    points = choropleth.points_dict
    for key in points.keys():
        state = fips.get(key)
        if state != needed_state:
            continue
        l = choropleth.get_points(key)
        if point_is_territory(l):
            continue
        final[key] = {'x': [], 'y': []}
        for i in l:
            final[key]['x'] += i[0]
            final[key]['y'] += i[1]
            final[key]['state'] = state
    return final
Exemplo n.º 12
0
import pprint
import choropleth_prep
import bokeh
from bokeh.plotting import figure, show, output_file

pp = pprint.PrettyPrinter(indent=4)
choropleth = choropleth_prep.Chorpleth(the_type='state')
x, y = choropleth.get_points_flatten('MA')
width = 1000
height = int(round((max(y) - min(y)) / (max(x) - min(x)) * width))
p = figure(title='test', width=width, height=height)
p.patches([x], [y], fill_alpha=0.7, line_color="white", line_width=0.5)
x2 = [min(x), max(x), min(x), max(x)]
y2 = [min(y), max(y), max(y), min(y)]
p.scatter(x2, y2, fill_color="red", size=10)
show(p)