|
6 | 6 | from matplotlib.patches import Polygon
|
7 | 7 | from matplotlib.colorbar import ColorbarBase
|
8 | 8 |
|
| 9 | +fig, ax = plt.subplots() |
| 10 | + |
9 | 11 | # Lambert Conformal map of lower 48 states.
|
10 | 12 | m = Basemap(llcrnrlon=-119,llcrnrlat=20,urcrnrlon=-64,urcrnrlat=49,
|
11 | 13 | projection='lcc',lat_1=33,lat_2=45,lon_0=-95)
|
|
93 | 95 | statenames.append(statename)
|
94 | 96 |
|
95 | 97 | #%% --------- cycle through state names, color each one. --------------------
|
96 |
| -fig = plt.gcf() # get current figure instance |
97 |
| -ax = plt.gca() # get current axes instance |
98 |
| - |
99 | 98 | for nshape,seg in enumerate(m.states):
|
100 | 99 | # skip DC and Puerto Rico.
|
101 | 100 | if statenames[nshape] not in ['Puerto Rico', 'District of Columbia']:
|
|
105 | 104 |
|
106 | 105 | AREA_1 = 0.005 # exclude small Hawaiian islands that are smaller than AREA_1
|
107 | 106 | 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 |
| 107 | +AK_SCALE = 0.19 # scale down Alaska to show as a map inset |
| 108 | +HI_OFFSET_X = -1900000 # X coordinate offset amount to move Hawaii "beneath" Texas |
| 109 | +HI_OFFSET_Y = 250000 # similar to above: Y offset for Hawaii |
| 110 | +AK_OFFSET_X = -250000 # X offset for Alaska (These four values are obtained |
| 111 | +AK_OFFSET_Y = -750000 # via manual trial and error, thus changing them is not recommended.) |
109 | 112 |
|
110 | 113 | for nshape, shapedict in enumerate(m_.states_info): # plot Alaska and Hawaii as map insets
|
111 | 114 | if shapedict['NAME'] in ['Alaska', 'Hawaii']:
|
112 | 115 | seg = m_.states[int(shapedict['SHAPENUM'] - 1)]
|
113 | 116 | if shapedict['NAME'] == 'Hawaii' and float(shapedict['AREA']) > AREA_1:
|
114 |
| - seg = list(map(lambda (x,y): (x-1900000, y+250000), seg)) |
| 117 | + seg = [(x + HI_OFFSET_X, y + HI_OFFSET_Y) for x, y in seg] |
115 | 118 | color = rgb2hex(colors[statenames[nshape]])
|
116 | 119 | 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)) |
| 120 | + seg = [(x*AK_SCALE + AK_OFFSET_X, y*AK_SCALE + AK_OFFSET_Y)\ |
| 121 | + for x, y in seg] |
118 | 122 | color = rgb2hex(colors[statenames[nshape]])
|
119 | 123 | poly = Polygon(seg, facecolor=color, edgecolor='gray', linewidth=.45)
|
120 | 124 | ax.add_patch(poly)
|
|
123 | 127 |
|
124 | 128 | #%% --------- Plot bounding boxes for Alaska and Hawaii insets --------------
|
125 | 129 | 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) |
| 130 | +x1,y1 = m_([-190,-183,-180,-180,-175,-171,-171],[29,29,26,26,26,22,20]) |
| 131 | +x2,y2 = m_([-180,-180,-177],[26,23,20]) # these numbers are fine-tuned manually |
| 132 | +m_.plot(x1,y1,color=light_gray,linewidth=0.8) # do not change them drastically |
| 133 | +m_.plot(x2,y2,color=light_gray,linewidth=0.8) |
140 | 134 |
|
141 | 135 | #%% --------- Show color bar ---------------------------------------
|
142 | 136 | ax_c = fig.add_axes([0.9, 0.1, 0.03, 0.8])
|
|
0 commit comments