0

I am currently attempting to generate a choropleth of median home values by county with Kartograph.py. I was able to previously build a choropleth by way of a configuration and CSS string contained within my Notebook. (Note that tel is my variable of interest, and dbf_in is a pandas DataFrame containing the information in the .dbf table associated with my state shapefile.)

#Create color map
col_map=dict(zip(dbf_in['tel'],dbf_in['color']))

#Generate configuration dict
config={"proj":{
            "id":"laea-usa"},
        "layers":{
            "USA": {
                "src": shp_dir+"cb_2013_us_state_5m.shp",
                "attributes":"all"},
            "Lakes":{
                "src": shp_dir+"ne_10m_lakes.shp",
                "subtract-from": "USA",
                "filter":{
                    "scalerank":0}}},
        "bounds":{
            "mode":"bbox",
            "data":[-180,15,-65,55],}}

#Initialize Kartograph obj1ct
K=Kartograph()

#Generate style sheet
css_list=[]
for i in range(1,5):
    css_list.append('#USA[tel='+str(i)+']{fill: '+col_map[i]+'; stroke: #FFFFFF;}')

css='\n'.join(css_list)

#Generate blank map
K.generate(config, outfile='TEL_bind_states.svg',stylesheet=css)

Just for completeness, here is the CSS string:

#USA[tel=1]{fill: #fcbba1; stroke: #FFFFFF;}
#USA[tel=2]{fill: #fb694a; stroke: #FFFFFF;}
#USA[tel=3]{fill: #ca181d; stroke: #FFFFFF;}
#USA[tel=4]{fill: #67000d; stroke: #FFFFFF;}

In a nutshell, I was able to dynamically build my CSS string, and it yielded an SVG version of the following image:

choropleth by state

A similar protocol was followed to create a state level choropleth:

#Map colors to counties
col_map=dict(zip(acs['id5'],acs['color']))

#Generate style sheet
css_list=[]
for key in col_map.keys():
    css_list.append('#County[geoid='+key+']{fill: '+col_map[key]+'; stroke: #FFFFFF;}')

css='\n'.join(css_list[1:])

#Generate configuration dict
config={"proj":{
            "id":"laea-usa"},
        "layers":{
            "County": {
                "src": shp_dir+"tl_2014_us_county.shp",
                "attributes":"all",
                "simplify":3}},
        "bounds":{
            "mode":"bbox",
            "data":[-180,15,-65,55],}}

#Initialize Kartograph obj1ct
K=Kartograph()

#Generate blank map
K.generate(config, outfile='blank_US_cty.svg',stylesheet=css)

Here is the first section of the CSS string:

#County[geoid=16079]{fill: #fee0d2; stroke: #FFFFFF;}
#County[geoid=33017]{fill: #fdc5ae; stroke: #FFFFFF;}
#County[geoid=16073]{fill: #fee1d4; stroke: #FFFFFF;}
#County[geoid=16071]{fill: #fee3d7; stroke: #FFFFFF;}
#County[geoid=16077]{fill: #fee1d3; stroke: #FFFFFF;}
#County[geoid=16075]{fill: #fee1d3; stroke: #FFFFFF;}
#County[geoid=48093]{fill: #feeae0; stroke: #FFFFFF;}
#County[geoid=06115]{fill: #fdd0bc; stroke: #FFFFFF;}
#County[geoid=06111]{fill: #fa6648; stroke: #FFFFFF;}

This time, however, I can't seem to get the colors to bind to polygons like I could with the states. This time, it yields the following image:

Blank County Map

Any thoughts on why this is happening? I did consider that maybe a dictionary with over 3000 items was problematic, but I got the same results when I tried to color by a variable with only five unique values.

Marvin Ward Jr
  • 1,019
  • 1
  • 11
  • 30
  • Are you sure that `geoid` is the correct attribute name for counties? Also, you might try removing the `simplify` parameter from the configuration. – augurar Jan 19 '15 at 01:12
  • Thanks for taking a look. Yes, `geoid` is correct. I actually had to inspect the shapefile to capture the right format for the IDs, which I got from the `geoid` field. I started without the `simplify` factor, and I ended up adding it to make things move faster. In the end, it made no difference. – Marvin Ward Jr Jan 19 '15 at 01:33

0 Answers0