11import matplotlib .pyplot as plt
22import numpy as np
33
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
95
106
117def plotmap ():
128 # create figure
13- fig = plt .figure (figsize = (8 ,8 ))
9+ fig = plt .figure (figsize = (8 , 8 ))
1410 # set up orthographic map projection with
1511 # perspective of satellite looking down at 50N, 100W.
1612 # 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' )
1814 # 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' ]
2319 # compute the native map projection coordinates for cities.
24- xc ,yc = map (lons ,lats )
20+ xc , yc = map (lons , lats )
2521 # 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 , :, :])
2927 wave = 0.75 * (np .sin (2. * lats )** 8 * np .cos (4. * lons ))
3028 mean = 0.5 * np .cos (2. * lats )* ((np .sin (2. * lats ))** 2 + 2. )
3129 # compute native map projection coordinates of lat/lon grid.
@@ -34,28 +32,18 @@ def plotmap():
3432 # draw map boundary
3533 map .drawmapboundary (color = "0.9" )
3634 # 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" )
3937 # plot filled circles at the locations of the cities.
40- map .plot (xc ,yc ,'wo' )
38+ map .plot (xc , yc , 'wo' )
4139 # 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' )
4442 # 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 )
4644 # draw blue marble image in background.
4745 # (downsample the image by 50% for speed)
4846 map .bluemarble (scale = 0.5 )
4947
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 ()
6049plt .show ()
61-
0 commit comments