Graphics Programs Reference
In-Depth Information
were colored with six shades, and every 2 percentage points was a new
class. every county with an unemployment rate greater than 10 percent was
one class; then counties with rates between 8 and 10, then 6 and 8, and so
forth. Another common way to define thresholds is by quartiles, where you
use four colors, and each color represents a quarter of the regions.
For example, the lower, middle, and upper quartiles for these unemploy-
ment rates are 6.9, 8.7, and 10.8 percent, respectively. This means that
a quarter of the counties have rates below 6.9 percent, another quarter
between 6.9 and 8.7, one between 8.7 and 10.8, and the last quarter is
greater than 10.8 percent. To do this, change the colors list in your script
to something like the following. It's a purple color scheme, with one shade
per quarter.
colors = [“#f2f0f7”, “#cbc9e2”, “#9e9ac8”, “#6a51a3”]
Then modify the color conditions in the for loop, using the preceding
quartiles.
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
Run the script and save like before, and you get Figure 8-19. Notice how
there are more counties colored lightly.
To increase the usability of your code, you can calculate quartiles pro-
grammatically instead of hard-coding them. This is straightforward in
Python. You store a list of your values, sort them from least to great-
est, and find the values at the one-quarter, one-half, and three-quarters
marks. More concretely, as it pertains to this example, you can modify the
first loop in colorize_svg.py to store just unemployment rates.
unemployment = {}
rates_only = [] # To calculate quartiles
min_value = 100; max_value = 0; past_header = False
for row in reader:
if not past_header:
Search WWH ::




Custom Search