from mpl_toolkits.basemap import Basemap #see http://matplotlib.org/basemap/ # see also http://matplotlib.org/basemap/users/examples.html # from matplotlib.colors import rgb2hex figure(figsize=(12,6)) # Lambert Conformal map of lower 48 states. m = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49, projection='lcc',lat_1=33,lat_2=45,lon_0=-95) # draw state boundaries. # data from U.S Census Bureau #http://www.census.gov/geo/cob/bdy/st/st90shp/st99_d90_shp.zip #http://www.census.gov/geo/www/cob/st1990.html shp_info = m.readshapefile('geodata/st99_d90','states',drawbounds=True) # population density by state from # http://en.wikipedia.org/wiki/List_of_U.S._states_by_population_density popdensity = { 'New Jersey': 438.00, 'Rhode Island': 387.35, 'Massachusetts': 312.68, 'Connecticut': 271.40, 'Maryland': 209.23, 'New York': 155.18, 'Delaware': 154.87, 'Florida': 114.43, 'Ohio': 107.05, 'Pennsylvania': 105.80, 'Illinois': 86.27, 'California': 83.85, 'Hawaii': 72.83, 'Virginia': 69.03, 'Michigan': 67.55, 'Indiana': 65.46, 'North Carolina': 63.80, 'Georgia': 54.59, 'Tennessee': 53.29, 'New Hampshire': 53.20, 'South Carolina': 51.45, 'Louisiana': 39.61, 'Kentucky': 39.28, 'Wisconsin': 38.13, 'Washington': 34.20, 'Alabama': 33.84, 'Missouri': 31.36, 'Texas': 30.75, 'West Virginia': 29.00, 'Vermont': 25.41, 'Minnesota': 23.86, 'Mississippi': 23.42, 'Iowa': 20.22, 'Arkansas': 19.82, 'Oklahoma': 19.40, 'Arizona': 17.43, 'Colorado': 16.01, 'Maine': 15.95, 'Oregon': 13.76, 'Kansas': 12.69, 'Utah': 10.50, 'Nebraska': 8.60, 'Nevada': 7.03, 'Idaho': 6.04, 'New Mexico': 5.79, 'South Dakota': 3.84, 'North Dakota': 3.59, 'Montana': 2.39, 'Wyoming': 1.96, 'Alaska': 0.42} ##print shp_info # choose a color for each state based on population density. colors={} statenames=[] cmap = cm.hot # use 'hot' colormap #try jet! or cool or winter, # see http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps vmin = 0; vmax = 450 # set range. ##print len(m.states_info),m.states_info[0].keys() for shapedict in m.states_info: statename = shapedict['NAME'] if statename != 'District of Columbia': # skip DC, it's not a state! pop = popdensity[statename] # calling colormap with value between 0 and 1 returns # rgba value. Invert color range (hot colors are high # population), take sqrt root to spread out colors more. colors[statename] = cmap(1.-math.sqrt((pop-vmin)/(vmax-vmin)))[:3] statenames.append(statename) # cycle through state names, color each one. for nshape,seg in enumerate(m.states): xx,yy = zip(*seg) if statenames[nshape] != 'District of Columbia': # skip DC color = rgb2hex(colors[statenames[nshape]]) fill(xx,yy,color,edgecolor=color) # draw meridians and parallels. m.drawparallels(arange(25,65,20),labels=[1,0,0,0]) m.drawmeridians(arange(-120,-40,20),labels=[0,0,0,1]) title('Filling State Polygons by Population Density') None