|
| 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 |
| 8 | +from pylab import title, colorbar, show, axes, cm, load, arange, figure, \ |
| 9 | + text |
| 10 | + |
| 11 | +# read in topo data (on a regular lat/lon grid) |
| 12 | +# longitudes go from 20 to 380. |
| 13 | +# you can get this data from matplolib svn matplotlib/htdocs/screenshots/data/ |
| 14 | +datadir = '/home/jdhunter/python/svn/matplotlib/htdocs/screenshots/data/' |
| 15 | +if not os.path.exists(datadir): |
| 16 | + 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__) |
| 17 | + |
| 18 | +topoin = load(os.path.join(datadir, 'etopo20data.gz')) |
| 19 | +lons = load(os.path.join(datadir, 'etopo20lons.gz')) |
| 20 | +lats = load(os.path.join(datadir, 'etopo20lats.gz')) |
| 21 | +# shift data so lons go from -180 to 180 instead of 20 to 380. |
| 22 | +topoin,lons = shiftgrid(180.,topoin,lons,start=False) |
| 23 | + |
| 24 | +# setup of basemap ('lcc' = lambert conformal conic). |
| 25 | +# use major and minor sphere radii from WGS84 ellipsoid. |
| 26 | +m = Basemap(llcrnrlon=-145.5,llcrnrlat=1.,urcrnrlon=-2.566,urcrnrlat=46.352,\ |
| 27 | + rsphere=(6378137.00,6356752.3142),\ |
| 28 | + resolution='l',area_thresh=1000.,projection='lcc',\ |
| 29 | + lat_1=50.,lon_0=-107.) |
| 30 | +# transform to nx x ny regularly spaced native projection grid |
| 31 | +nx = int((m.xmax-m.xmin)/40000.)+1; ny = int((m.ymax-m.ymin)/40000.)+1 |
| 32 | +topodat,x,y = m.transform_scalar(topoin,lons,lats,nx,ny,returnxy=True) |
| 33 | +# create the figure. |
| 34 | +fig=figure(figsize=(8,8)) |
| 35 | +# add an axes, leaving room for colorbar on the right. |
| 36 | +ax = fig.add_axes([0.1,0.1,0.7,0.7]) |
| 37 | +# plot image over map with imshow. |
| 38 | +im = m.imshow(topodat,cm.jet) |
| 39 | +# setup colorbar axes instance. |
| 40 | +# for matplotlib 0.91 and earlier, could do l,b,w,h = ax.get_position() |
| 41 | +# for post 0.91, pos = ax.get_position(); l,b,w,h = pos.bounds |
| 42 | +# this works for both. |
| 43 | +pos = ax.get_position() |
| 44 | +l, b, w, h = getattr(pos, 'bounds', pos) |
| 45 | +cax = axes([l+w+0.075, b, 0.05, h]) |
| 46 | +colorbar(cax=cax) # draw colorbar |
| 47 | +axes(ax) # make the original axes current again |
| 48 | +# plot blue dot on boulder, colorado and label it as such. |
| 49 | +xpt,ypt = m(-104.237,40.125) |
| 50 | +m.plot([xpt],[ypt],'bo') |
| 51 | +text(xpt+100000,ypt+100000,'Boulder') |
| 52 | +# draw coastlines and political boundaries. |
| 53 | +m.drawcoastlines() |
| 54 | +m.drawcountries() |
| 55 | +m.drawstates() |
| 56 | +# draw parallels and meridians. |
| 57 | +# label on left, right and bottom of map. |
| 58 | +parallels = arange(0.,80,20.) |
| 59 | +m.drawparallels(parallels,labels=[1,1,0,1]) |
| 60 | +meridians = arange(10.,360.,30.) |
| 61 | +m.drawmeridians(meridians,labels=[1,1,0,1]) |
| 62 | +# set title. |
| 63 | +title('ETOPO Topography - Lambert Conformal Conic') |
| 64 | +show() |
0 commit comments