def plot_map(data, rlon=None, rlat=None, use_pcolor=0, **keywords): """ display the data in a map keywords: dict which can include the following keywords minv=0.0, maxv=0.0, dv=0.0, show_map=0, map_proj='cyl', show_lat=0, show_lon=0, lat_0=0.0, lon_0=0.0, minlat=-90.0, maxlat=90.0, minlon=0.0, maxlon=360.0, use_log=0, level=level """ minv = 0.0 if ('minv' in keywords): minv = keywords['minv'] maxv = 0.0 if ('maxv' in keywords): maxv = keywords['maxv'] dv = 0.0 if ('dv' in keywords): dv = keywords['dv'] if ('dv' in keywords): dv = keywords['dv'] if (maxv > minv): rlvl = arange(minv, maxv + dv, dv) rlvl[0] = -999.0 rlvl[size(rlvl) - 1] = 999. stitle = "" add_str = "" if ('title' in keywords): add_str = keywords['title'] add_str = add_str.strip() stitle = stitle + ' ' + add_str if ('unit' in keywords): add_str = keywords['unit'] add_str = add_str.strip() stitle = stitle + '(' + add_str + ')' cbar_vert = 1 if ('cbar_vert' in keywords): cbar_vert = keywords['cbar_vert'] if (cbar_vert == 1): orientation = 'vertical' else: orientation = 'horizontal' show_lat = 1 if ('show_lat' in keywords): show_lat = keywords['show_lat'] show_lon = 1 if ('show_lon' in keywords): show_lon = keywords['show_lon'] nlon, nlat = shape(data) vals = array(data) map_proj = 'cyl' if ('map_proj' in keywords): map_proj = keywords['map_proj'] lat_0 = 0.0 if ('lat_0' in keywords): lat_0 = keywords['lat_0'] minlat = -90.0 maxlat = 90.0 if ('minlat' in keywords): minlat = keywords['minlat'] if ('maxlat' in keywords): maxlat = keywords['maxlat'] if (rlat == None): dlat = (maxlat - minlat) / nlat rlat = arange(minlat, maxlat, dlat) minlon = -180 maxlon = 180.0 if ('minlon' in keywords): minlon = keywords['minlon'] if ('maxlon' in keywords): maxlon = keywords['maxlon'] if (rlon == None): dlon = (maxlon - minlon) / nlon rlon = arange(minlon, maxlon, dlon) lon_0 = 0 do_bdr = 0 if ('do_bdr' in keywords): do_bdr = keywords['do_bdr'] if ('lon_0' in keywords): lon_0 = keywords['lon_0'] boundinglat = 45 if ('boundinglat' in keywords): boundinglat = keywords['boundinglat'] if (map_proj == 'npstere' or map_proj == 'spstere'): m = Basemap(projection=map_proj, lon_0=lon_0, boundinglat=boundinglat) elif (map_proj == 'ortho'): m = Basemap(projection=map_proj, lon_0=lon_0, lat_0=lat_0) else: if (maxlon - minlon > 180): m=Basemap(llcrnrlon=minlon, llcrnrlat=minlat, \ urcrnrlon=maxlon, urcrnrlat=maxlat,projection=map_proj, lon_0=lon_0, lat_0=lat_0, resolution='l') else: m=Basemap(llcrnrlon=minlon, llcrnrlat=minlat, \ urcrnrlon=maxlon, urcrnrlat=maxlat,projection=map_proj, lon_0=lon_0, lat_0=lat_0, resolution='i') if (rlon[-1] < rlon[0] + 360.0): rlon = resize(rlon, nlon + 1) rlon[-1] = rlon[0] + 360.0 vals = squeeze(vals) vals = resize(vals, [nlon + 1, nlat]) x, y = m(*meshgrid(rlon, rlat)) cmap = cm.Paired if ('cmap' in keywords): print 'cmap included' cmap = keywords['cmap'] m.drawcoastlines(color='k', linewidth=0.5) if (maxv > minv): if (use_pcolor == 1): cs0 = m.pcolormesh(x, y, transpose(vals), shading='flat', vmin=minv, vmax=maxv, cmap=cmap) # cs0=m.imshow(x, y, transpose(vals), shading='flat', vmin=minv, vmax=maxv, cmap=cmap) else: cs0 = m.contourf(x, y, transpose(vals), rlvl, cmap=cmap) else: if (use_pcolor == 1): cs0 = m.pcolor(x, y, transpose(vals), shading='flat', cmap=cmap) else: cs0 = m.contourf(x, y, transpose(vals), cmap=cmap) # info(m.drawcoastlines) # m.drawcountries(color=white) m.drawmapboundary() if (show_lat == 1): if (maxlat - minlat >= 90): m.drawparallels(arange(minlat, maxlat + 30.0, 30.), labels=[1, 0, 0, 0], color='grey') else: m.drawparallels(arange(minlat, maxlat + 5.0, 5.), labels=[1, 0, 0, 0], color='grey') if (show_lon == 1): if (maxlon - minlon >= 180): m.drawmeridians(arange(minlon, maxlon + 60, 60.), labels=[0, 0, 0, 1], color='grey') else: m.drawmeridians(arange(minlon, maxlon + 10, 10.), labels=[0, 0, 0, 1], color='grey') title(stitle) show_colorbar = 1 if ('cb' in keywords): show_colorbar = keywords['cb'] if (show_colorbar == 1): colorbar(orientation=orientation, extend='both') if (do_bdr == 1): lvl = arange(max(vals.flat)) print shape(x), shape(y), shape(vals) # cs2=m.contour(x[0:-1,:], y[0:-1,:], transpose(vals), lvl, colors='k', linewidth=0.5) return m
lats[-1] = latsin[-1]+0.5*delon lons, lats = meshgrid(lons, lats) # setup of basemap ('lcc' = lambert conformal conic). # use major and minor sphere radii from WGS84 ellipsoid. m = Basemap(llcrnrlon=-145.5,llcrnrlat=1.,urcrnrlon=-2.566,urcrnrlat=46.352,\ resolution='l',area_thresh=1000.,projection='lcc',\ lat_1=50.,lon_0=-107.) fig=m.createfigure() ax = fig.add_axes([0.1,0.1,0.7,0.7],axisbg='aqua') # make topodat a masked array, masking values lower than sea level. topodat = where(topoin < 0.,1.e10,topoin) topodat = ma.masked_values(topodat, 1.e10) # make a pcolor plot. x, y = m(lons, lats) p = m.pcolor(x,y,topodat,shading='flat',cmap=cm.jet) clim(-4000.,3000.) # adjust colormap. cax = axes([0.875, 0.1, 0.05, 0.7]) # setup colorbar axes colorbar(tickfmt='%d', cax=cax) # draw colorbar axes(ax) # make the original axes current again # plot blue dot on boulder, colorado and label it as such. xpt,ypt = m(-104.237,40.125) m.plot([xpt],[ypt],'bo') text(xpt+100000,ypt+100000,'Boulder') # draw coastlines and political boundaries. m.drawcoastlines() #m.drawcountries() #m.drawstates() # draw parallels and meridians. # label on left, right and bottom of map. parallels = arange(0.,80,20.)
tlon = N.concatenate((tlon,tlon+360),1) tlat = N.concatenate((tlat,tlat),1) temp = MA.concatenate((temp,temp),1) tlon = tlon-360. pl.figure(figsize=(8.5,11)) pl.subplot(2,1,1) # subplot 1 just shows POP grid cells. map = Basemap(projection='merc', lat_ts=20, llcrnrlon=-180, \ urcrnrlon=180, llcrnrlat=-84, urcrnrlat=84, resolution='c') map.drawcoastlines() map.fillcontinents(color='white') x, y = map(tlon,tlat) im = map.pcolor(x,y,MA.masked_array(N.zeros(temp.shape,'f'), temp.mask),\ shading='faceted',cmap=pl.cm.cool,vmin=0,vmax=0) # disclaimer: these are not really the grid cells because of the # way pcolor interprets the x and y args. pl.title('(A) CCSM POP Grid Cells') # subplot 2 is a contour plot of surface temperature from the # CCSM ocean model. pl.subplot(2,1,2) map.drawcoastlines() map.fillcontinents(color='white') CS1 = map.contourf(x,y,temp,15) CS2 = map.contour(x,y,temp,15,colors='black',linewidths=0.5) pl.title('(B) Surface Temp contours on POP Grid') pl.show()
tlon = N.concatenate((tlon, tlon + 360), 1) tlat = N.concatenate((tlat, tlat), 1) temp = MA.concatenate((temp, temp), 1) tlon = tlon - 360. pl.figure(figsize=(8.5, 11)) pl.subplot(2, 1, 1) # subplot 1 just shows POP grid cells. map = Basemap(projection='merc', lat_ts=20, llcrnrlon=-180, \ urcrnrlon=180, llcrnrlat=-84, urcrnrlat=84, resolution='c') map.drawcoastlines() map.fillcontinents(color='white') x, y = map(tlon, tlat) im = map.pcolor(x,y,MA.masked_array(N.zeros(temp.shape,'f'), temp.mask),\ shading='faceted',cmap=pl.cm.cool,vmin=0,vmax=0) # disclaimer: these are not really the grid cells because of the # way pcolor interprets the x and y args. pl.title('(A) CCSM POP Grid Cells') # subplot 2 is a contour plot of surface temperature from the # CCSM ocean model. pl.subplot(2, 1, 2) map.drawcoastlines() map.fillcontinents(color='white') CS1 = map.contourf(x, y, temp, 15) CS2 = map.contour(x, y, temp, 15, colors='black', linewidths=0.5) pl.title('(B) Surface Temp contours on POP Grid') pl.show()
m = Basemap(llcrnrlon=llcrnrlon,llcrnrlat=llcrnrlat, urcrnrlon=urcrnrlon,urcrnrlat=urcrnrlat, rsphere=6371200., resolution='l',area_thresh=5000.,projection='lcc', lat_1=standardpar,lon_0=centerlon) x, y = m(lons, lats) # setup figure size so each panel will have same aspect ratio as map. xsize = rcParams['figure.figsize'][0] ysize = (3./2.)*m.aspect*xsize fig=figure(figsize=(xsize,ysize)) yoffset = (m.urcrnry-m.llcrnry)/30. for npanel,fcsthr in enumerate(arange(0,72,12)): nt = fcsthrs.index(fcsthr) ax = fig.add_subplot(320+npanel+1) # make a pcolor plot. p = m.pcolor(x,y,t2m[nt,:,:],shading='flat',cmap=cm.jet) clim(t2min,t2max) # fix color range to be the same for all panels m.drawcoastlines() m.drawstates() m.drawcountries() m.drawparallels(arange(25,75,20),labels=[1,0,0,0],fontsize=8,fontstyle='oblique') m.drawmeridians(arange(-140,0,20),labels=[0,0,0,1],fontsize=8,yoffset=yoffset,fontstyle='oblique') # panel title title(repr(fcsthr)+'-h forecast valid '+verifdates[nt],fontsize=12) # figure title figtext(0.5,0.95,u"2-m temp (\N{DEGREE SIGN}K) forecasts from %s"%verifdates[0],horizontalalignment='center',fontsize=14) # a single colorbar. cax = axes([0.25, 0.03, 0.5, 0.025]) colorbar(tickfmt='%d', cax=cax, orientation='horizontal') show()
hgt,x,y = m.transform_scalar(hgt,lons,lats,nx,ny,returnxy=True) fig = figure(figsize=(8,8)) plots = ['contour','pcolor'] #plots = ['contour','imshow'] for np,plot in enumerate(plots): fig.add_subplot(1,2,np+1) ax = gca() # plot data. print plot+' plot ...' if plot == 'pcolor': m.pcolor(x,y,hgt,shading='flat') elif plot == 'imshow': im = m.imshow(hgt) elif plot == 'contour': levels, colls = m.contour(x,y,hgt,15,linewidths=0.5,colors='k') levels, colls = m.contourf(x,y,hgt,15,cmap=cm.jet,colors=None) # set size of plot to match aspect ratio of map. l,b,w,h = ax.get_position() b = 0.5 - 0.5*w*m.aspect; h = w*m.aspect ax.set_position([l,b,w,h]) # draw map. m.drawcoastlines() # draw parallels
hgt, x, y = m.transform_scalar(hgt, lons, lats, nx, ny, returnxy=True) fig = figure(figsize=(8, 8)) plots = ['contour', 'pcolor'] #plots = ['contour','imshow'] for np, plot in enumerate(plots): fig.add_subplot(1, 2, np + 1) ax = gca() # plot data. print plot + ' plot ...' if plot == 'pcolor': m.pcolor(x, y, hgt, shading='flat') elif plot == 'imshow': im = m.imshow(hgt) elif plot == 'contour': levels, colls = m.contour(x, y, hgt, 15, linewidths=0.5, colors='k') levels, colls = m.contourf(x, y, hgt, 15, cmap=cm.jet, colors=None) # set size of plot to match aspect ratio of map. l, b, w, h = ax.get_position() b = 0.5 - 0.5 * w * m.aspect h = w * m.aspect ax.set_position([l, b, w, h]) # draw map. m.drawcoastlines()
resolution='l', area_thresh=5000., projection='lcc', lat_1=standardpar, lon_0=centerlon) x, y = m(lons, lats) # setup figure size so each panel will have same aspect ratio as map. xsize = rcParams['figure.figsize'][0] ysize = (3. / 2.) * m.aspect * xsize fig = figure(figsize=(xsize, ysize)) yoffset = (m.urcrnry - m.llcrnry) / 30. for npanel, fcsthr in enumerate(arange(0, 72, 12)): nt = fcsthrs.index(fcsthr) ax = fig.add_subplot(320 + npanel + 1) # make a pcolor plot. p = m.pcolor(x, y, t2m[nt, :, :], shading='flat', cmap=cm.jet) clim(t2min, t2max) # fix color range to be the same for all panels m.drawcoastlines() m.drawstates() m.drawcountries() m.drawparallels(arange(25, 75, 20), labels=[1, 0, 0, 0], fontsize=8, fontstyle='oblique') m.drawmeridians(arange(-140, 0, 20), labels=[0, 0, 0, 1], fontsize=8, yoffset=yoffset, fontstyle='oblique') # panel title title(repr(fcsthr) + '-h forecast valid ' + verifdates[nt], fontsize=12)