|
2 | 2 | import numpy as np
|
3 | 3 | import matplotlib.pyplot as plt
|
4 | 4 | from mpl_toolkits.basemap import Basemap as Basemap
|
5 |
| -from matplotlib.colors import rgb2hex |
| 5 | +from matplotlib.colors import rgb2hex, Normalize |
6 | 6 | from matplotlib.patches import Polygon
|
| 7 | +from matplotlib.colorbar import ColorbarBase |
7 | 8 |
|
8 | 9 | # Lambert Conformal map of lower 48 states.
|
9 |
| -m = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49, |
| 10 | +m = Basemap(llcrnrlon=-119,llcrnrlat=20,urcrnrlon=-64,urcrnrlat=49, |
10 | 11 | projection='lcc',lat_1=33,lat_2=45,lon_0=-95)
|
11 |
| -# draw state boundaries. |
12 |
| -# data from U.S Census Bureau |
13 |
| -# http://www.census.gov/geo/www/cob/st2000.html |
14 |
| -shp_info = m.readshapefile('st99_d00','states',drawbounds=True) |
15 |
| -# population density by state from |
16 |
| -# http://en.wikipedia.org/wiki/List_of_U.S._states_by_population_density |
| 12 | + |
| 13 | +# Mercator projection, for Alaska and Hawaii |
| 14 | +m_ = Basemap(llcrnrlon=-190,llcrnrlat=20,urcrnrlon=-143,urcrnrlat=46, |
| 15 | + projection='merc',lat_ts=20) # do not change these numbers |
| 16 | + |
| 17 | +#%% --------- draw state boundaries ---------------------------------------- |
| 18 | +## data from U.S Census Bureau |
| 19 | +## http://www.census.gov/geo/www/cob/st2000.html |
| 20 | +shp_info = m.readshapefile('st99_d00','states',drawbounds=True, |
| 21 | + linewidth=0.45,color='gray') |
| 22 | +shp_info_ = m_.readshapefile('st99_d00','states',drawbounds=False) |
| 23 | + |
| 24 | +## population density by state from |
| 25 | +## http://en.wikipedia.org/wiki/List_of_U.S._states_by_population_density |
17 | 26 | popdensity = {
|
18 | 27 | 'New Jersey': 438.00,
|
19 | 28 | 'Rhode Island': 387.35,
|
|
65 | 74 | 'Montana': 2.39,
|
66 | 75 | 'Wyoming': 1.96,
|
67 | 76 | 'Alaska': 0.42}
|
68 |
| -print(shp_info) |
69 |
| -# choose a color for each state based on population density. |
| 77 | + |
| 78 | +#%% -------- choose a color for each state based on population density. ------- |
70 | 79 | colors={}
|
71 | 80 | statenames=[]
|
72 |
| -cmap = plt.cm.hot # use 'hot' colormap |
| 81 | +cmap = plt.cm.hot_r # use 'reversed hot' colormap |
73 | 82 | vmin = 0; vmax = 450 # set range.
|
74 |
| -print(m.states_info[0].keys()) |
| 83 | +norm = Normalize(vmin=vmin, vmax=vmax) |
75 | 84 | for shapedict in m.states_info:
|
76 | 85 | statename = shapedict['NAME']
|
77 | 86 | # skip DC and Puerto Rico.
|
|
80 | 89 | # calling colormap with value between 0 and 1 returns
|
81 | 90 | # rgba value. Invert color range (hot colors are high
|
82 | 91 | # population), take sqrt root to spread out colors more.
|
83 |
| - colors[statename] = cmap(1.-np.sqrt((pop-vmin)/(vmax-vmin)))[:3] |
| 92 | + colors[statename] = cmap(np.sqrt((pop-vmin)/(vmax-vmin)))[:3] |
84 | 93 | statenames.append(statename)
|
85 |
| -# cycle through state names, color each one. |
| 94 | + |
| 95 | +#%% --------- cycle through state names, color each one. -------------------- |
| 96 | +fig = plt.gcf() # get current figure instance |
86 | 97 | ax = plt.gca() # get current axes instance
|
| 98 | + |
87 | 99 | for nshape,seg in enumerate(m.states):
|
88 | 100 | # skip DC and Puerto Rico.
|
89 |
| - if statenames[nshape] not in ['District of Columbia','Puerto Rico']: |
90 |
| - color = rgb2hex(colors[statenames[nshape]]) |
| 101 | + if statenames[nshape] not in ['Puerto Rico', 'District of Columbia']: |
| 102 | + color = rgb2hex(colors[statenames[nshape]]) |
91 | 103 | poly = Polygon(seg,facecolor=color,edgecolor=color)
|
92 | 104 | ax.add_patch(poly)
|
93 |
| -# draw meridians and parallels. |
94 |
| -m.drawparallels(np.arange(25,65,20),labels=[1,0,0,0]) |
95 |
| -m.drawmeridians(np.arange(-120,-40,20),labels=[0,0,0,1]) |
96 |
| -plt.title('Filling State Polygons by Population Density') |
| 105 | + |
| 106 | +AREA_1 = 0.005 # exclude small Hawaiian islands that are smaller than AREA_1 |
| 107 | +AREA_2 = AREA_1 * 30.0 # exclude Alaskan islands that are smaller than AREA_2 |
| 108 | +AK_SCALE = 0.18 # scale down Alaska to show as a map inset |
| 109 | + |
| 110 | +for nshape, shapedict in enumerate(m_.states_info): # plot Alaska and Hawaii as map insets |
| 111 | + if shapedict['NAME'] in ['Alaska', 'Hawaii']: |
| 112 | + seg = m_.states[int(shapedict['SHAPENUM'] - 1)] |
| 113 | + if shapedict['NAME'] == 'Hawaii' and float(shapedict['AREA']) > AREA_1: |
| 114 | + seg = list(map(lambda (x,y): (x-1900000, y+250000), seg)) |
| 115 | + color = rgb2hex(colors[statenames[nshape]]) |
| 116 | + elif shapedict['NAME'] == 'Alaska' and float(shapedict['AREA']) > AREA_2: |
| 117 | + seg = list(map(lambda (x,y): (AK_SCALE*x-200000, AK_SCALE*y-650000), seg)) |
| 118 | + color = rgb2hex(colors[statenames[nshape]]) |
| 119 | + poly = Polygon(seg, facecolor=color, edgecolor='gray', linewidth=.45) |
| 120 | + ax.add_patch(poly) |
| 121 | + |
| 122 | +ax.set_title('United states population density by state') |
| 123 | + |
| 124 | +#%% --------- Plot bounding boxes for Alaska and Hawaii insets -------------- |
| 125 | +light_gray = [0.8]*3 # define light gray color RGB |
| 126 | +m_.plot(np.linspace(170,177),np.linspace(29,29),linewidth=1., |
| 127 | + color=light_gray,latlon=True) |
| 128 | +m_.plot(np.linspace(177,180),np.linspace(29,26),linewidth=1., |
| 129 | + color=light_gray,latlon=True) |
| 130 | +m_.plot(np.linspace(180,180),np.linspace(26,23),linewidth=1., |
| 131 | + color=light_gray,latlon=True) |
| 132 | +m_.plot(np.linspace(-180,-177),np.linspace(23,20),linewidth=1., |
| 133 | + color=light_gray,latlon=True) |
| 134 | +m_.plot(np.linspace(-180,-175),np.linspace(26,26),linewidth=1., |
| 135 | + color=light_gray,latlon=True) |
| 136 | +m_.plot(np.linspace(-175,-171),np.linspace(26,22),linewidth=1., |
| 137 | + color=light_gray,latlon=True) |
| 138 | +m_.plot(np.linspace(-171,-171),np.linspace(22,20),linewidth=1., |
| 139 | + color=light_gray,latlon=True) |
| 140 | + |
| 141 | +#%% --------- Show color bar --------------------------------------- |
| 142 | +ax_c = fig.add_axes([0.9, 0.1, 0.03, 0.8]) |
| 143 | +cb = ColorbarBase(ax_c,cmap=cmap,norm=norm,orientation='vertical', |
| 144 | + label=r'[population per $\mathregular{km^2}$]') |
| 145 | + |
97 | 146 | plt.show()
|
| 147 | + |
0 commit comments