-
Notifications
You must be signed in to change notification settings - Fork 0
/
20180826 color_map.py
64 lines (51 loc) · 1.64 KB
/
20180826 color_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
### color_map.py
import csv
from bs4 import BeautifulSoup
'''
# Read in unemployment rates
unemployment = {}
min_value = 100; max_value = 0
reader = csv.reader(open('unemployment09.csv'), delimiter=",")
for row in reader:
try:
full_fips = row[1] + row[2]
rate = float( row[8].strip() )
unemployment[full_fips] = rate
except:
pass
# Load the SVG map
svg = open('USA_Counties_with_FIPS_and_names.svg', 'r').read()
# Load into Beautiful Soup
soup = BeautifulSoup(svg, selfClosingTags=['defs','sodipodi:namedview'])
# Find counties
paths = soup.findAll('path')
# Map colors
colors = ["#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043"]
# County style
path_style = 'font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel;fill:'
# Color the counties based on unemployment rate
for p in paths:
if p['id'] not in ['State_Lines', 'separator']:
try:
rate = unemployment[p['id']]
except:
continue
if rate > 10:
color_class = 5
elif rate > 8:
color_class = 4
elif rate > 6:
color_class = 3
elif rate > 4:
color_class = 2
elif rate > 2:
color_class = 1
else:
color_class = 0
color = colors[color_class]
p['style'] = path_style + color
'''
print(soup.prettify())
f=open('beautifulsoup.txt','w')
f.write(string)
f.close()