Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c653e8

Browse files
committed
remove ImportError if Basemap not installed
1 parent 407f83a commit 6c653e8

1 file changed

Lines changed: 44 additions & 41 deletions

File tree

doc/pyplots/plotmap.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
1-
from mpl_toolkits.basemap import Basemap
2-
import matplotlib.pyplot as plt
3-
import numpy as np
1+
try:
2+
from mpl_toolkits.basemap import Basemap
3+
import matplotlib.pyplot as plt
4+
import numpy as np
45

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

0 commit comments

Comments
 (0)