1
1
import matplotlib .pyplot as plt
2
2
import numpy as np
3
3
4
- try :
5
- from mpl_toolkits .basemap import Basemap
6
- have_basemap = True
7
- except ImportError :
8
- have_basemap = False
4
+ from mpl_toolkits .basemap import Basemap
9
5
10
6
11
7
def plotmap ():
12
8
# create figure
13
- fig = plt .figure (figsize = (8 ,8 ))
9
+ fig = plt .figure (figsize = (8 , 8 ))
14
10
# set up orthographic map projection with
15
11
# perspective of satellite looking down at 50N, 100W.
16
12
# use low resolution coastlines.
17
- map = Basemap (projection = 'ortho' ,lat_0 = 50 ,lon_0 = - 100 ,resolution = 'l' )
13
+ map = Basemap (projection = 'ortho' , lat_0 = 50 , lon_0 = - 100 , resolution = 'l' )
18
14
# lat/lon coordinates of five cities.
19
- lats = [40.02 ,32.73 ,38.55 ,48.25 ,17.29 ]
20
- lons = [- 105.16 ,- 117.16 ,- 77.00 ,- 114.21 ,- 88.10 ]
21
- cities = ['Boulder, CO' ,'San Diego, CA' ,
22
- 'Washington, DC' ,'Whitefish, MT' ,'Belize City, Belize' ]
15
+ lats = [40.02 , 32.73 , 38.55 , 48.25 , 17.29 ]
16
+ lons = [- 105.16 , - 117.16 , - 77.00 , - 114.21 , - 88.10 ]
17
+ cities = ['Boulder, CO' , 'San Diego, CA' ,
18
+ 'Washington, DC' , 'Whitefish, MT' , 'Belize City, Belize' ]
23
19
# compute the native map projection coordinates for cities.
24
- xc ,yc = map (lons ,lats )
20
+ xc , yc = map (lons , lats )
25
21
# make up some data on a regular lat/lon grid.
26
- nlats = 73 ; nlons = 145 ; delta = 2. * np .pi / (nlons - 1 )
27
- lats = (0.5 * np .pi - delta * np .indices ((nlats ,nlons ))[0 ,:,:])
28
- lons = (delta * np .indices ((nlats ,nlons ))[1 ,:,:])
22
+ nlats = 73
23
+ nlons = 145
24
+ delta = 2. * np .pi / (nlons - 1 )
25
+ lats = (0.5 * np .pi - delta * np .indices ((nlats ,nlons ))[0 , :, :])
26
+ lons = (delta * np .indices ((nlats , nlons ))[1 , :, :])
29
27
wave = 0.75 * (np .sin (2. * lats )** 8 * np .cos (4. * lons ))
30
28
mean = 0.5 * np .cos (2. * lats )* ((np .sin (2. * lats ))** 2 + 2. )
31
29
# compute native map projection coordinates of lat/lon grid.
@@ -34,28 +32,18 @@ def plotmap():
34
32
# draw map boundary
35
33
map .drawmapboundary (color = "0.9" )
36
34
# draw graticule (latitude and longitude grid lines)
37
- map .drawmeridians (np .arange (0 ,360 ,30 ),color = "0.9" )
38
- map .drawparallels (np .arange (- 90 ,90 ,30 ),color = "0.9" )
35
+ map .drawmeridians (np .arange (0 , 360 , 30 ), color = "0.9" )
36
+ map .drawparallels (np .arange (- 90 , 90 , 30 ), color = "0.9" )
39
37
# plot filled circles at the locations of the cities.
40
- map .plot (xc ,yc ,'wo' )
38
+ map .plot (xc , yc , 'wo' )
41
39
# plot the names of five cities.
42
- for name ,xpt ,ypt in zip (cities ,xc ,yc ):
43
- plt .text (xpt + 100000 ,ypt + 100000 ,name ,fontsize = 9 ,color = 'w' )
40
+ for name , xpt , ypt in zip (cities , xc , yc ):
41
+ plt .text (xpt + 100000 , ypt + 100000 , name , fontsize = 9 , color = 'w' )
44
42
# contour data over the map.
45
- cs = map .contour (x ,y , wave + mean ,15 ,linewidths = 1.5 )
43
+ cs = map .contour (x , y , wave + mean , 15 , linewidths = 1.5 )
46
44
# draw blue marble image in background.
47
45
# (downsample the image by 50% for speed)
48
46
map .bluemarble (scale = 0.5 )
49
47
50
- def plotempty ():
51
- # create figure
52
- fig = plt .figure (figsize = (8 ,8 ))
53
- fig .text (0.5 , 0.5 , "Sorry, could not import Basemap" ,
54
- horizontalalignment = 'center' )
55
-
56
- if have_basemap :
57
- plotmap ()
58
- else :
59
- plotempty ()
48
+ plotmap ()
60
49
plt .show ()
61
-
0 commit comments