-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorize_svg.py
77 lines (61 loc) · 2.03 KB
/
colorize_svg.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
65
66
67
68
69
70
71
72
73
74
75
76
77
import csv
from BeautifulSoup import BeautifulSoup
# Read in unemployment rates
reader = csv.reader(open('unemployment-aug2010.txt', 'r'), delimiter=",")
# Load the SVG map
svg = open('counties.svg', 'r').read()
unemployment = {}
min_value = 100; max_value = 0; past_header = False
for row in reader:
if not past_header:
past_header = True
continue
try:
full_fips = row[1] + row[2]
rate = float( row[5].strip() )
unemployment[full_fips] = rate
except:
pass
# 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"] # Red-purple
# colors = ["#eff3ff", "#C6DBEF", "#9ECAE1", "#6BAED6", "#3182BD", "#08519C"] # Blue
# colors = ["#f2f0f7", "#cbc9e2", "#9e9ac8", "#6a51a3"] # Purple
# 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"]:
# pass
try:
rate = unemployment[p['id']]
except:
continue
# Linear scale
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
# Quantile scale
# if rate > 10.8:
# color_class = 3
# elif rate > 8.7:
# color_class = 2
# elif rate > 6.9:
# color_class = 1
# else:
# color_class = 0
color = colors[color_class]
p['style'] = path_style + color
print soup.prettify()