|
1 | | -# make plot of etopo bathymetry/topography data on |
2 | | -# lambert conformal conic map projection, drawing coastlines, state and |
3 | | -# country boundaries, and parallels/meridians. |
4 | | - |
5 | | -# the data is interpolated to the native projection grid. |
6 | | -import os |
7 | | -from mpl_toolkits.basemap import Basemap, shiftgrid |
| 1 | +from mpl_toolkits.basemap import Basemap |
| 2 | +import matplotlib.pyplot as plt |
8 | 3 | import numpy as np |
9 | | - |
10 | | -from pylab import title, colorbar, show, axes, cm, arange, figure, \ |
11 | | - text |
12 | | - |
13 | | -# read in topo data (on a regular lat/lon grid) |
14 | | -# longitudes go from 20 to 380. |
15 | | -# you can get this data from matplolib svn matplotlib/htdocs/screenshots/data/ |
16 | | -datadir = '/home/jdhunter/python/svn/matplotlib/trunk/htdocs/screenshots/data/' |
17 | | -if not os.path.exists(datadir): |
18 | | - raise SystemExit('You need to download the data with svn co https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/trunk/htdocs/screenshots/data/" and set the datadir variable in %s'%__file__) |
19 | | - |
20 | | -topoin = np.loadtxt(os.path.join(datadir, 'etopo20data.gz')) |
21 | | -lons = np.loadtxt(os.path.join(datadir, 'etopo20lons.gz')) |
22 | | -lats = np.loadtxt(os.path.join(datadir, 'etopo20lats.gz')) |
23 | | -# shift data so lons go from -180 to 180 instead of 20 to 380. |
24 | | -topoin,lons = shiftgrid(180.,topoin,lons,start=False) |
25 | | - |
26 | | -# setup of basemap ('lcc' = lambert conformal conic). |
27 | | -# use major and minor sphere radii from WGS84 ellipsoid. |
28 | | -m = Basemap(llcrnrlon=-145.5,llcrnrlat=1.,urcrnrlon=-2.566,urcrnrlat=46.352,\ |
29 | | - rsphere=(6378137.00,6356752.3142),\ |
30 | | - resolution='l',area_thresh=1000.,projection='lcc',\ |
31 | | - lat_1=50.,lon_0=-107.) |
32 | | -# transform to nx x ny regularly spaced native projection grid |
33 | | -nx = int((m.xmax-m.xmin)/40000.)+1; ny = int((m.ymax-m.ymin)/40000.)+1 |
34 | | -topodat,x,y = m.transform_scalar(topoin,lons,lats,nx,ny,returnxy=True) |
35 | | -# create the figure. |
36 | | -fig=figure(figsize=(6,6)) |
37 | | -# add an axes, leaving room for colorbar on the right. |
38 | | -ax = fig.add_axes([0.1,0.1,0.7,0.7]) |
39 | | -# plot image over map with imshow. |
40 | | -im = m.imshow(topodat,cm.jet) |
41 | | -# setup colorbar axes instance. |
42 | | -# for matplotlib 0.91 and earlier, could do l,b,w,h = ax.get_position() |
43 | | -# for post 0.91, pos = ax.get_position(); l,b,w,h = pos.bounds |
44 | | -# this works for both. |
45 | | -pos = ax.get_position() |
46 | | -l, b, w, h = getattr(pos, 'bounds', pos) |
47 | | -cax = axes([l+w+0.075, b, 0.05, h]) |
48 | | -colorbar(cax=cax) # draw colorbar |
49 | | -axes(ax) # make the original axes current again |
50 | | -# plot blue dot on boulder, colorado and label it as such. |
51 | | -xpt,ypt = m(-104.237,40.125) |
52 | | -m.plot([xpt],[ypt],'bo') |
53 | | -text(xpt+100000,ypt+100000,'Boulder') |
54 | | -# draw coastlines and political boundaries. |
55 | | -m.drawcoastlines() |
56 | | -m.drawcountries() |
57 | | -m.drawstates() |
58 | | -# draw parallels and meridians. |
59 | | -# label on left, right and bottom of map. |
60 | | -parallels = arange(0.,80,20.) |
61 | | -m.drawparallels(parallels,labels=[1,1,0,1]) |
62 | | -meridians = arange(10.,360.,30.) |
63 | | -m.drawmeridians(meridians,labels=[1,1,0,1]) |
64 | | -# set title. |
65 | | -title('ETOPO Topography - Lambert Conformal Conic') |
66 | | -show() |
| 4 | +# create figure |
| 5 | +fig = plt.figure(figsize=(8,8)) |
| 6 | +# set up orthographic map projection with |
| 7 | +# perspective of satellite looking down at 50N, 100W. |
| 8 | +# use low resolution coastlines. |
| 9 | +map = Basemap(projection='ortho',lat_0=50,lon_0=-100,resolution='l') |
| 10 | +# lat/lon coordinates of five cities. |
| 11 | +lats=[40.02,32.73,38.55,48.25,17.29] |
| 12 | +lons=[-105.16,-117.16,-77.00,-114.21,-88.10] |
| 13 | +cities=['Boulder, CO','San Diego, CA', |
| 14 | + 'Washington, DC','Whitefish, MT','Belize City, Belize'] |
| 15 | +# compute the native map projection coordinates for cities. |
| 16 | +xc,yc = map(lons,lats) |
| 17 | +# make up some data on a regular lat/lon grid. |
| 18 | +nlats = 73; nlons = 145; delta = 2.*np.pi/(nlons-1) |
| 19 | +lats = (0.5*np.pi-delta*np.indices((nlats,nlons))[0,:,:]) |
| 20 | +lons = (delta*np.indices((nlats,nlons))[1,:,:]) |
| 21 | +wave = 0.75*(np.sin(2.*lats)**8*np.cos(4.*lons)) |
| 22 | +mean = 0.5*np.cos(2.*lats)*((np.sin(2.*lats))**2 + 2.) |
| 23 | +# compute native map projection coordinates of lat/lon grid. |
| 24 | +# (convert lons and lats to degrees first) |
| 25 | +x, y = map(lons*180./np.pi, lats*180./np.pi) |
| 26 | +# draw map boundary |
| 27 | +map.drawmapboundary(color="0.9") |
| 28 | +# draw graticule (latitude and longitude grid lines) |
| 29 | +map.drawmeridians(np.arange(0,360,30),color="0.9") |
| 30 | +map.drawparallels(np.arange(-90,90,30),color="0.9") |
| 31 | +# plot filled circles at the locations of the cities. |
| 32 | +map.plot(xc,yc,'wo') |
| 33 | +# plot the names of five cities. |
| 34 | +for name,xpt,ypt in zip(cities,xc,yc): |
| 35 | + plt.text(xpt+100000,ypt+100000,name,fontsize=9,color='w') |
| 36 | +# contour data over the map. |
| 37 | +cs = map.contour(x,y,wave+mean,15,linewidths=1.5) |
| 38 | +# draw blue marble image in background. |
| 39 | +# (downsample the image by 50% for speed) |
| 40 | +map.bluemarble(scale=0.5) |
| 41 | +plt.show() |
0 commit comments