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:
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:
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.