Graphics Programs Reference
In-Depth Information
try:
full_fips = row[1] + row[2]
rate = float( row[8].strip() )
unemployment[full_fips] = rate
except:
pass
Next parse the SVG file with BeautifulSoup. Most tags have an opening and
closing tag, but there are a couple of self-closing tags in there, which you
need to specify. Then use the findAll() function to retrieve all the paths in
the map.
soup = BeautifulSoup(svg, selfClosingTags=['defs','sodipodi:namedview'])
paths = soup.findAll('path')
Then store the colors, which I got from ColorBrewer, in a Python list. This
is a sequential color scheme with multiple hues ranging from purple to red.
colors = [“#F1EEF6”, “#D4B9DA”, “#C994C7”, “#DF65B0”, “#DD1C77”, “#980043”]
You're getting close to the climax. Like I said earlier, you're going to
change the style attribute for each path in the SVG. You're just interested
in fill color, but to make things easier, you can replace the entire style
instead of parsing to replace only the color. I changed the hexadecimal
value after stroke to #ffffff , which is white. This changes the borders to
white instead of the current gray.
path_style = 'font-size:12px;fill-rule:nonzero;stroke:#fffff;stroke-
opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-
dasharray:none;stroke-linecap:butt;marker-start:none;stroke-
linejoin:bevel;fill:'
I also moved fill to the end and left the value blank because that's the
part that depends on each county's unemployment rate.
Finally, you're ready to change some colors! You can iterate through each
path (except for state boundary lines and the separator for Hawaii and
Alaska) and color accordingly. If the unemployment rate is greater than 10,
use a darker shade, and anything less than 2 has the lightest shade.
for p in paths:
if p['id'] not in [“State_Lines”, “separator”]:
# pass
Search WWH ::




Custom Search