Skip to content

Commit cfff23e

Browse files
author
jsh9
committed
Some changed according to suggestions
1 parent cf8a3cb commit cfff23e

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

examples/fillstates.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from matplotlib.patches import Polygon
77
from matplotlib.colorbar import ColorbarBase
88

9+
fig, ax = plt.subplots()
10+
911
# Lambert Conformal map of lower 48 states.
1012
m = Basemap(llcrnrlon=-119,llcrnrlat=20,urcrnrlon=-64,urcrnrlat=49,
1113
projection='lcc',lat_1=33,lat_2=45,lon_0=-95)
@@ -93,9 +95,6 @@
9395
statenames.append(statename)
9496

9597
#%% --------- cycle through state names, color each one. --------------------
96-
fig = plt.gcf() # get current figure instance
97-
ax = plt.gca() # get current axes instance
98-
9998
for nshape,seg in enumerate(m.states):
10099
# skip DC and Puerto Rico.
101100
if statenames[nshape] not in ['Puerto Rico', 'District of Columbia']:
@@ -105,16 +104,21 @@
105104

106105
AREA_1 = 0.005 # exclude small Hawaiian islands that are smaller than AREA_1
107106
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.)
109112

110113
for nshape, shapedict in enumerate(m_.states_info): # plot Alaska and Hawaii as map insets
111114
if shapedict['NAME'] in ['Alaska', 'Hawaii']:
112115
seg = m_.states[int(shapedict['SHAPENUM'] - 1)]
113116
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]
115118
color = rgb2hex(colors[statenames[nshape]])
116119
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]
118122
color = rgb2hex(colors[statenames[nshape]])
119123
poly = Polygon(seg, facecolor=color, edgecolor='gray', linewidth=.45)
120124
ax.add_patch(poly)
@@ -123,20 +127,10 @@
123127

124128
#%% --------- Plot bounding boxes for Alaska and Hawaii insets --------------
125129
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)
140134

141135
#%% --------- Show color bar ---------------------------------------
142136
ax_c = fig.add_axes([0.9, 0.1, 0.03, 0.8])

0 commit comments

Comments
 (0)