def set_default_basemap(lon, lat, frame_width=5.): test = lon < 0. if True in test: # matplotlib expects 0-360 while WRF for example uses -180-180 delta = n.ones(lon.shape) delta *= 360 delta = ma.masked_where(lon > 0., delta) lon += delta.filled(fill_value=0) llcrnrlon=lon.min() - frame_width urcrnrlon=lon.max() + frame_width llcrnrlat=lat.min() - frame_width urcrnrlat=lat.max() + frame_width lon_0 = llcrnrlon + (urcrnrlon - llcrnrlon) / 2. lat_0 = llcrnrlat + (urcrnrlat - llcrnrlat) / 2. map = Basemap( llcrnrlon=llcrnrlon, llcrnrlat=llcrnrlat, urcrnrlon=urcrnrlon, urcrnrlat=urcrnrlat, resolution='l', projection='cyl', lon_0=lon_0, lat_0=lat_0 ) return map
def draw_client_density(): m = Basemap(llcrnrlon=-180.,llcrnrlat=-90,urcrnrlon=180.,urcrnrlat=90.,\ resolution='c',projection='cyl') # plot them as filled circles on the map. # first, create a figure. dpi=100 dimx=800/dpi dimy=400/dpi fig=figure(figsize=(dimx,dimy), dpi=dpi, frameon=False, facecolor='blue') # ax=fig.add_axes([0.1,0.1,0.7,0.7],axisbg='g') ax=fig.add_axes([0.0,0.0,1.0,1.0],axisbg='g') canvas = FigureCanvas(fig) results = lookup_client_locations() X,Y,Z = find_client_density(m,results) # s = random.sample(results, 40000) # for t in s: # lat=t[2] # lon=t[3] # # draw a red dot at the center. # xpt, ypt = m(lon, lat) # m.plot([xpt],[ypt],'ro', zorder=10) # draw coasts and fill continents. m.drawcoastlines(linewidth=0.5) m.drawcountries(linewidth=0.5) m.drawlsmask([100,100,100,0],[0,0,255,255]) # m.fillcontinents(color='green') palette = cm.YlOrRd m.imshow(Z,palette,extent=(m.xmin,m.xmax,m.ymin,m.ymax),interpolation='gaussian',zorder=0) # l,b,w,h = ax.get_position() # cax = axes([l+w+0.075, b, 0.05, h]) # colorbar(cax=cax) # draw colorbar canvas.print_figure(outdir+'/clientmap.png', dpi=100)
def set_default_basemap(lon, lat): map = Basemap(llcrnrlon=lon.min() - 0.5, llcrnrlat=lat.min() - 0.5, urcrnrlon=lon.max() + 0.5, urcrnrlat=lat.max() + 0.5, resolution='l', projection='cyl', lon_0=lon.min() + (lon.max() - lon.min()) / 2., lat_0=lat.min() + (lat.max() - lat.min()) / 2.) return map
def DrawSiteNetwork(self, g, node_label2pos_counts, pic_area=[-180, -90, 180, 90], output_fname_prefix=None): """ 2007-07-17 put ax.plot() right after Basemap() but after m.xxx() so that it'll zoom in use 'g' in ax.plot(), otherwise, ax.plot() alternates all colors. no parallels, no meridians 2007-08-29 copied from CreatePopulation.py and renamed from DrawStrainNetwork """ sys.stderr.write("Drawing Site Network...") import pylab from matplotlib.toolkits.basemap import Basemap pylab.clf() fig = pylab.figure() fig.add_axes([0.05, 0.05, 0.9, 0.9]) #[left, bottom, width, height] m = Basemap(llcrnrlon=pic_area[0],llcrnrlat=pic_area[1],urcrnrlon=pic_area[2],urcrnrlat=pic_area[3],\ resolution='l',projection='mill') ax = pylab.gca() for e in g.edges(): lat1, lon1 = node_label2pos_counts[e[0]][0] lat2, lon2 = node_label2pos_counts[e[1]][0] x1, y1 = m(lon1, lat1) x2, y2 = m(lon2, lat2) ax.plot([x1, x2], [y1, y2], 'g', alpha=0.5, zorder=12) #m.drawcoastlines() m.drawparallels(pylab.arange(-90, 90, 30), labels=[1, 1, 0, 1]) m.drawmeridians(pylab.arange(-180, 180, 30), labels=[1, 1, 0, 1]) m.fillcontinents() m.drawcountries() m.drawstates() pylab.title("Network of strains") if output_fname_prefix: pylab.savefig('%s_site_network.eps' % output_fname_prefix, dpi=300) pylab.savefig('%s_site_network.svg' % output_fname_prefix, dpi=300) pylab.savefig('%s_site_network.png' % output_fname_prefix, dpi=300) del m, pylab, Basemap sys.stderr.write("Done.\n")
# example demonstrating how to draw a great circle on a map. from matplotlib.toolkits.basemap import Basemap from pylab import * # setup lambert azimuthal map projection. m = Basemap(llcrnrlon=-100.,llcrnrlat=20.,urcrnrlon=20.,urcrnrlat=60.,\ rsphere=(6378137.00,6356752.3142),\ resolution='c',area_thresh=10000.,projection='merc',\ lat_0=40.,lon_0=-20.,lat_ts=20.) # make figure with aspect ratio that matches map region. xsize = rcParams['figure.figsize'][0] fig = figure(figsize=(xsize, m.aspect * xsize)) fig.add_axes([0.1, 0.1, 0.8, 0.8]) # nylat, nylon are lat/lon of New York nylat = 40.78 nylon = -73.98 # lonlat, lonlon are lat/lon of London. lonlat = 51.53 lonlon = 0.08 # find 1000 points along the great circle. #x,y = m.gcpoints(nylon,nylat,lonlon,lonlat,1000) # draw the great circle. #m.plot(x,y,linewidth=2) # drawgreatcircle performs the previous 2 steps in one call. m.drawgreatcircle(nylon, nylat, lonlon, lonlat, linewidth=2, color='b') m.drawcoastlines() m.fillcontinents() # draw parallels circles = arange(10, 90, 20) m.drawparallels(circles, labels=[1, 1, 0, 1]) # draw meridians
x = property(get_xdata) def get_ydata(self): return self._line.get_ydata() y = property(get_ydata) if __name__ == '__main__': from matplotlib.toolkits.basemap import Basemap from matplotlib.ticker import FuncFormatter from pyroms.focus import Focus m = Basemap(projection='lcc', resolution='i', llcrnrlon=5.0, llcrnrlat= 52., urcrnrlon=35.5, urcrnrlat=68.0, lat_0=15.00, lon_0=0.0, suppress_ticks=False) fig=pl.figure() # background color will be used for 'wet' areas. fig.add_axes([0.1,0.1,0.8,0.8],axisbg='azure') m.drawcoastlines(linewidth=0.25) m.fillcontinents(color='beige') x, y, beta = zip(*[(241782.65384467551, 1019981.3539730886, 1.0), (263546.12512432877, 686274.79435173667, 0), (452162.87621465814, 207478.42619936226, 1.0), (1431519.0837990609, 432367.62942244718, 1.0),
def add_text(rlon, rlat, txt, m=None, **keywords): show_lat = 1 if ('show_lat' in keywords): show_lat = keywords['show_lat'] rlon = array(rlon) rlat = array(rlat) show_lon = 1 if ('show_lon' in keywords): show_lon = keywords['show_lon'] if (m == None): 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'] minlon = -180 maxlon = 180.0 if ('minlon' in keywords): minlon = keywords['minlon'] if ('maxlon' in keywords): maxlon = keywords['maxlon'] lon_0 = 0 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: m=Basemap(llcrnrlon=minlon, llcrnrlat=minlat, \ urcrnrlon=maxlon, urcrnrlat=maxlat,projection=map_proj, lon_0=lon_0, lat_0=lat_0) if (show_lat == 1): m.drawparallels(arange(minlat, maxlat + 30.0, 30.), labels=[1, 0, 0, 0]) if (show_lon == 1): m.drawmeridians(arange(minlon, maxlon + 60, 60.), labels=[0, 0, 0, 1]) x, y = m(rlon, rlat) for i in range(size(rlon)): text(x[i], y[i], txt[i], fontsize=10) if ('title' in keywords): stitle = keywords['title'] title(stitle) return m
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
# read in topo data from pickle (on a regular lat/lon grid) # longitudes go from 20 to 380. topodict = cPickle.load(open('etopo20.pickle', 'rb')) topoin = topodict['data'] lons = topodict['lons'] lats = topodict['lats'] # shift data so lons go from -180 to 180 instead of 20 to 380. topoin, lons = shiftgrid(180., topoin, lons, start=False) print 'min/max etopo20 data:' print min(ravel(topoin)), max(ravel(topoin)) # setup cylindrical equidistant map projection (global domain). m = Basemap(-180.,-90,180.,90.,\ resolution='c',area_thresh=10000.,projection='cyl') # setup figure with same aspect ratio as map. xsize = rcParams['figure.figsize'][0] fig = figure(figsize=(xsize, m.aspect * xsize)) ax = fig.add_axes([0.1, 0.1, 0.75, 0.75]) # plot image over map. im = m.imshow(topoin, cm.jet) cax = axes([0.875, 0.1, 0.05, 0.75]) # setup colorbar axes. colorbar(tickfmt='%d', cax=cax) # draw colorbar axes(ax) # make the original axes current again m.drawcoastlines() #m.drawcountries() #m.drawstates() #m.fillcontinents() # draw parallels delat = 30.
pylab.clf() b = 5. # Plot the region surrounding the coastline rounded to the nearest 5 degree lat/lon. lonmin = b*math.floor(min(lon)/b) lonmax = b*math.ceil(max(lon)/b) latmin = b*math.floor(min(lat)/b) latmax = b*math.ceil(max(lat)/b) meridian = (range(lonmin, lonmax+b, b)) parallel = (range(latmin, latmax+b, b)) prj = 'cyl' res = 'i' map = Basemap(projection=prj, llcrnrlon=lonmin, llcrnrlat=latmin, urcrnrlon=lonmax, urcrnrlat=latmax, resolution=res) map.plot(lon, lat, 'r-') map.plot(lon, lat, 'k+') fs = 10 pylab.text(115.9,-31.9, "Perth", fontsize=fs, ha='left', va='top') pylab.text(114.1,-21.9, "Exmouth", fontsize=fs, ha='left', va='top') pylab.text(118.5,-20.4, "Port Hedland", fontsize=fs, ha='left', va='top') pylab.text(130.8,-12.6, "Darwin", fontsize=fs, ha='left', va='top') pylab.text(145.75,-16.9, "Cairns", fontsize=fs, ha='right', va='top') pylab.text(149.2,-21.2, "Mackay", fontsize=fs, ha='right', va='top') pylab.text(153.0,-27.5, "Brisbane", fontsize=fs, ha='right', va='top')
bounding_lat = 20. # loop over projections, one for each panel of the figure. fig = figure(figsize=(8, 8)) npanel = 0 for proj, projname in zip(projs, projnames): npanel = npanel + 1 if hem == 'South': projection = 'sp' + proj elif hem == 'North': projection = 'np' + proj # setup map projection # centered on Australia (for SH) or US (for NH). if proj == 'ortho': m = Basemap(projection='ortho', resolution='c', area_thresh=10000., lat_0=lat_0, lon_0=lon_0_ortho) else: m = Basemap(boundinglat=bounding_lat,lon_0=lon_0,\ resolution='c',area_thresh=10000.,projection=projection) # compute native map projection coordinates for lat/lon grid. x, y = m(*meshgrid(lons, lats)) ax = fig.add_subplot(2, 2, npanel) # make filled contour plot. cs = m.contourf(x, y, etopo, 20, cmap=cm.jet) # draw coastlines. m.drawcoastlines() # draw parallels and meridians. m.drawparallels(arange(-80., 90, 20.)) m.drawmeridians(arange(0., 360., 60.))
# example demonstrating how to draw a great circle on a map. import matplotlib from matplotlib.toolkits.basemap import Basemap from pylab import * # setup a mercator projection. m = Basemap(-90.,30.,30.,60.,\ resolution='c',area_thresh=10000.,projection='merc',\ lat_ts=20.) xsize = rcParams['figure.figsize'][0] fig = figure(figsize=(xsize, m.aspect * xsize)) fig.add_axes([0.1, 0.1, 0.8, 0.8]) ax = gca() # get current axis instance ax.update_datalim(((m.llcrnrx, m.llcrnry), (m.urcrnrx, m.urcrnry))) ax.set_xlim((m.llcrnrx, m.urcrnrx)) ax.set_ylim((m.llcrnry, m.urcrnry)) m.drawcoastlines(ax) m.fillcontinents(ax) # draw parallels circles = [35, 45, 55] m.drawparallels(ax, circles, labels=[1, 1, 0, 1]) # draw meridians meridians = [-90, -60, -30, 0, 30] m.drawmeridians(ax, meridians, labels=[1, 1, 0, 1]) # nylat, nylon are lat/lon of New York nylat = 40.78 nylon = -73.98 # lonlat, lonlon are lat/lon of London. lonlat = 51.53 lonlon = 0.08 # draw the great circle.
from matplotlib.toolkits.basemap import Basemap, shiftgrid from pylab import * # read in topo data (on a regular lat/lon grid) # longitudes go from 20 to 380. topoin = array(load('etopo20data.gz'),'d') lons = array(load('etopo20lons.gz'),'d') lats = array(load('etopo20lats.gz'),'d') # shift data so lons go from -180 to 180 instead of 20 to 380. topoin,lons = shiftgrid(180.,topoin,lons,start=False) # 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,\ rsphere=(6378137.00,6356752.3142),\ resolution='l',area_thresh=1000.,projection='lcc',\ lat_1=50.,lon_0=-107.) # transform to nx x ny regularly spaced native projection grid nx = int((m.xmax-m.xmin)/40000.)+1; ny = int((m.ymax-m.ymin)/40000.)+1 topodat,x,y = m.transform_scalar(topoin,lons,lats,nx,ny,returnxy=True) # create the figure. fig=figure(figsize=(8,8)) # add an axes, leaving room for colorbar on the right. ax = fig.add_axes([0.1,0.1,0.7,0.7]) # plot image over map with imshow. im = m.imshow(topodat,cm.jet) # setup colorbar axes instance. l,b,w,h = ax.get_position() cax = axes([l+w+0.075, b, 0.05, h]) colorbar(tickfmt='%d', cax=cax) # draw colorbar axes(ax) # make the original axes current again
# country boundaries, and parallels/meridians. from matplotlib.toolkits.basemap import Basemap, interp from pylab import * import cPickle # read in topo data from pickle (on a regular lat/lon grid) # longitudes go from 20 to 380. topodict = cPickle.load(open('etopo20.pickle', 'rb')) topoin = topodict['data'] lons = topodict['lons'] lats = topodict['lats'] # setup of basemap ('lcc' = lambert conformal conic). m = Basemap(-145.5,1.,-2.566,46.352,\ resolution='c',area_thresh=10000.,projection='lcc',\ lat_1=50.,lon_0=-107.) # define grid (nx x ny regularly spaced native projection grid) nx = int((m.xmax - m.xmin) / 40000.) + 1 ny = int((m.ymax - m.ymin) / 40000.) + 1 lonsout, latsout = m.makegrid(nx, ny) topodat = interp(topoin, lons, lats, lonsout, latsout) xsize = rcParams['figure.figsize'][0] fig = figure(figsize=(xsize, m.aspect * xsize)) ax = fig.add_axes([0.1, 0.1, 0.75, 0.75]) im = imshow(topodat, cm.jet, extent=(m.xmin, m.xmax, m.ymin, m.ymax), origin='lower') cax = axes([0.875, 0.1, 0.05, 0.75])
from matplotlib.toolkits.basemap import Basemap from pylab import title, show, arange # create Basemap instance for Orthographic (satellite view) projection. lon_0 = float(raw_input('enter reference longitude (lon_0):')) lat_0 = float(raw_input('enter reference latitude (lat_0):')) m = Basemap(projection='ortho', lon_0=lon_0, lat_0=lat_0) # plot land-sea mask. rgba_land = (0, 255, 0, 255) # land green. rgba_ocean = (0, 0, 255, 255) # ocean blue. # lakes=True means plot inland lakes with ocean color. m.drawlsmask(rgba_land, rgba_ocean, lakes=True) # draw parallels and meridians. m.drawparallels(arange(-90., 120., 30.)) m.drawmeridians(arange(0., 420., 60.)) m.drawmapboundary() title('Orthographic Map Centered on Lon=%s, Lat=%s' % (lon_0, lat_0)) show()
from gdalconst import * import pylab as p # download from # http://edcftp.cr.usgs.gov/pub/data/DEM/250/D/denver-w.gz gd = gdal.Open('denver-w') # get data from DEM file array = gd.ReadAsArray() # get lat/lon coordinates from DEM file. coords = gd.GetGeoTransform() llcrnrlon = coords[0] urcrnrlon = llcrnrlon+(array.shape[1]-1)*coords[1] urcrnrlat = coords[3] llcrnrlat = urcrnrlat+(array.shape[0]-1)*coords[5] # create Basemap instance. m = Basemap(llcrnrlon=llcrnrlon,llcrnrlat=llcrnrlat,urcrnrlon=urcrnrlon,urcrnrlat=urcrnrlat,projection='cyl') # create a figure, add an axes # (leaving room for a colorbar). fig = p.figure() ax = fig.add_axes([0.1,0.1,0.75,0.75]) # plot image from DEM over map. im = m.imshow(array,origin='upper') # make a colorbar. cax = p.axes([0.875, 0.1, 0.05, 0.75]) # setup colorbar axes. p.colorbar(cax=cax) # draw colorbar p.axes(ax) # make the original axes current again # draw meridians and parallels. m.drawmeridians(p.linspace(llcrnrlon+0.1,urcrnrlon-0.1,5),labels=[0,0,0,1],fmt='%4.2f') m.drawparallels(p.linspace(llcrnrlat+0.1,urcrnrlat-0.1,5),labels=[1,0,0,0],fmt='%4.2f') # plot county boundaries from # http://edcftp.cr.usgs.gov/pub/data/nationalatlas/countyp020.tar.gz
# read in topo data from pickle (on a regular lat/lon grid) # longitudes go from 20 to 380. topodict = cPickle.load(open('etopo20.pickle', 'rb')) topoin = topodict['data'] lons = topodict['lons'] lats = topodict['lats'] # shift data so lons go from 0 to 360 instead of 20 to 380. topoin, lons = shiftgrid(360., topoin, lons, start=False) print 'min/max etopo20 data:' print min(ravel(topoin)), max(ravel(topoin)) m = Basemap(lons[0],lats[0],lons[-1],lats[-1],\ resolution='c',area_thresh=10000.,projection='cyl') xsize = rcParams['figure.figsize'][0] fig = figure(figsize=(xsize, m.aspect * xsize)) ax = fig.add_axes([0.1, 0.1, 0.75, 0.75]) im = imshow(topoin, cm.jet, extent=(m.llcrnrx, m.urcrnrx, m.llcrnry, m.urcrnry), origin='lower') cax = axes([0.875, 0.1, 0.05, 0.75]) colorbar(tickfmt='%d', cax=cax) # draw colorbar axes(ax) # make the original axes current again m.drawcoastlines(ax) #m.drawcountries(ax) #m.drawstates(ax) #m.fillcontinents(ax) # draw parallels
from matplotlib.toolkits.basemap import Basemap from pylab import * # create Basemap instance. Use 'crude' resolution coastlines. m = Basemap(-11., 51., -5., 56., resolution='c', area_thresh=1000., projection='cyl') # create figure with same aspect ratio as map. xsize = rcParams['figure.figsize'][0] fig = figure(figsize=(xsize, m.aspect * xsize)) fig.add_axes([0.1, 0.1, 0.8, 0.8]) # draw coastlines and fill continents. m.drawcoastlines() m.fillcontinents() # draw parallels circles = arange(50, 60, 1).tolist() m.drawparallels(circles, labels=[1, 1, 1, 1]) # draw meridians meridians = arange(-12, 0, 1) m.drawmeridians(meridians, labels=[1, 1, 1, 1]) title("Crude Res Coastlines ('c')", y=1.075) show() # create Basemap instance. Use 'low' resolution coastlines. m = Basemap(-11., 51., -5.,
from matplotlib.toolkits.basemap import Basemap from pylab import * # read in topo data (on a regular lat/lon grid) etopo = array(load('etopo20data.gz'), 'd') lons = array(load('etopo20lons.gz'), 'd') lats = array(load('etopo20lats.gz'), 'd') # create Basemap instance for Robinson projection. m = Basemap(projection='robin', lon_0=0.5 * (lons[0] + lons[-1])) # create figure with same aspect ratio as map. fig = m.createfigure() # make filled contour plot. x, y = m(*meshgrid(lons, lats)) cs = m.contourf(x, y, etopo, 30, cmap=cm.jet) # draw coastlines. m.drawcoastlines() # draw a line around the map region. m.drawmapboundary() # draw parallels and meridians. m.drawparallels(arange(-60., 90., 30.), labels=[1, 0, 0, 0]) m.drawmeridians(arange(0., 420., 60.), labels=[0, 0, 0, 1]) # add a title. title('Robinson Projection') show()
#lonfile = '/home/blew/programy/Mscs/WMAPstuff/SKregstat/sm1.0/3sigma_chisq_comb-K.lon' #latfile = '/home/blew/programy/Mscs/WMAPstuff/SKregstat/sm1.0/3sigma_chisq_comb-K.lat' mapfile = sys.argv[1]+'.lonlatT' lonfile = sys.argv[1]+'.lon' latfile = sys.argv[1]+'.lat' color_num = int(sys.argv[2]) # load data map=array(load(mapfile)) lons=array(load(lonfile)) lats=array(load(latfile)) map,lons = shiftgrid(180.,map,lons,start=False) # Mollweide projection m1 = Basemap(projection='moll',lon_0=0.5*(lons[0]+lons[-1])) lons, lats = meshgrid(lons, lats) fig1 = m1.createfigure() x, y = m1(lons, lats) cs = m1.contourf(x,y,map,color_num,cmap=cm.jet) m1.drawparallels(arange(-90.,90.,20.),labels=[1,0,0,0]) m1.drawmeridians(arange(0.,420.,30.),labels=[0,0,0,1]) #title('Moll. proj:'+ sys.argv[1]) title(r'$>3\sigma\/ detections\/ composite\/ map:\/ skewness$') #title(r'$>3\sigma\/ detections\/ composite\/ map:\/ kurtosis$') colorbar(); savefig(sys.argv[1]+'-moll.png'); #savefig(sys.argv[1]+'-moll.eps') savefig(sys.argv[1]+'-moll.jpg');
from matplotlib.toolkits.basemap import Basemap, basemap_datadir from pylab import show, title, arange, figure, draw, ion, ioff, clf import cPickle, time, sys, os # turn interactive mode on. ion() # create new figure fig = figure() # create Basemap instance. Use 'crude' resolution coastlines. m = Basemap(llcrnrlon=-11., llcrnrlat=50.5, urcrnrlon=-5., urcrnrlat=56., resolution='c', projection='tmerc', lon_0=-8., lat_0=0.) # draw coastlines and fill continents. m.drawcoastlines() m.fillcontinents() # draw political boundaries. m.drawcountries() # draw parallels circles = arange(50, 60, 1).tolist() m.drawparallels(circles, labels=[1, 1, 0, 0]) # draw meridians meridians = arange(-12, 0, 1) m.drawmeridians(meridians, labels=[0, 0, 1, 1]) print 'plotting with crude res boundaries ...' title("Crude Res Boundaries ('c')", y=1.05) draw()
lat_0 = 40. lon_0 = -100. width = 6000000. height = 2. * width / 3. delat = 25. circles = arange(0.,90.+delat,delat).tolist()+\ arange(-delat,-90.-delat,-delat).tolist() delon = 30. meridians = arange(10., 360., delon) npanel = 0 # plots of the US. projs = ['lcc', 'aeqd', 'aea', 'laea', 'eqdc', 'stere'] fig = figure(figsize=(8, 12)) for proj in projs: m = Basemap(width=width,height=height, resolution='c',projection=proj,\ lat_0=lat_0,lon_0=lon_0) npanel = npanel + 1 fig.add_subplot(3, 2, npanel) # setup figure with same aspect ratio as map. m.drawcoastlines() m.drawcountries() m.fillcontinents() m.drawstates() m.drawparallels(circles) m.drawmeridians(meridians) title('proj = ' + proj + ' centered on %sW, %sN' % (lon_0, lat_0), fontsize=10) proj = 'omerc' delat = 10.
def plot_track(rlon, rlat=None, m=None, **keywords): 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'] if (m == None): 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'] minlon = -180 maxlon = 180.0 if ('minlon' in keywords): minlon = keywords['minlon'] if ('maxlon' in keywords): maxlon = keywords['maxlon'] lon_0 = 0 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: m=Basemap(llcrnrlon=minlon, llcrnrlat=minlat, \ urcrnrlon=maxlon, urcrnrlat=maxlat,projection=map_proj, lon_0=lon_0, lat_0=lat_0) if (show_lat == 1): m.drawparallels(arange(minlat, maxlat + 30.0, 30.), labels=[1, 0, 0, 0]) if (show_lon == 1): m.drawmeridians(arange(minlon, maxlon + 60, 60.), labels=[0, 0, 0, 1]) sgn = '+' if ('sgn' in keywords): sgn = keywords['sgn'] if ('color' in keywords): lcolor = keywords['color'] if (len(rlon) < 100): m.plot(rlon, rlat, sgn, color=lcolor, markersize=11) else: m.plot(rlon, rlat, sgn, color=lcolor, markersize=6) else: m.plot(rlon, rlat, sgn) m.drawcoastlines(color='w', linewidth=0.5) m.drawmapboundary() # m.drawcountries(color='k', linewidth=0.5) if ('title' in keywords): stitle = keywords['title'] title(stitle) return m
if __version__ <= '0.70': print 'warning - contour example will not work with this version of matplotlib if axes.patch is not applied' # read in data on lat/lon grid. datadict = cPickle.load(open('500hgt.pickle', 'rb')) hgt = datadict['data'] lons = datadict['lons'] lats = datadict['lats'] # set up map projection (lambert azimuthal equal area). m = Basemap(-150., -20., 30., -20., resolution='c', area_thresh=10000., projection='laea', lat_0=90., lon_0=-105.) # interpolate to map projection grid. nx = 101 ny = 101 lonsout, latsout = m.makegrid(nx, ny) hgt = interp(hgt, lons, lats, lonsout, latsout) dx = (m.xmax - m.xmin) / (nx - 1) dy = (m.ymax - m.ymin) / (ny - 1) x = m.xmin + dx * indices((ny, nx))[1, :, :] y = m.ymin + dy * indices((ny, nx))[0, :, :] #m = Basemap(lons[0],lats[0],lons[-1],lats[-1],\
ul.append(float(l[0])) vl.append(float(l[1])) pl.append(float(l[2])) u = reshape(array(ul, Float32), (nlats, nlons)) v = reshape(array(vl, Float32), (nlats, nlons)) p = reshape(array(pl, Float32), (nlats, nlons)) lats1 = -90. + dellat * arange(nlats) lons1 = -180. + dellon * arange(nlons) lons, lats = meshgrid(lons1, lats1) # plot vectors in geographical (lat/lon) coordinates. # north polar projection. m = Basemap(lon_0=-135, boundinglat=25, resolution='c', area_thresh=10000., projection='npstere') # create a figure, add an axes. fig = figure(figsize=(8, 8)) ax = fig.add_axes([0.1, 0.1, 0.7, 0.7]) # rotate wind vectors to map projection coordinates. # (also compute native map projections coordinates of lat/lon grid) # only do Northern Hemisphere. urot, vrot, x, y = m.rotate_vector(u[36:, :], v[36:, :], lons[36:, :], lats[36:, :], returnxy=True) # plot filled contours over map. cs = m.contourf(x, y, p[36:, :], 15, cmap=cm.jet)
# in which direction to depart for other points on earth and how far # it will be to reach that destination. # The specified point shows up as a red dot in the center of the map. # This example shows how to use the width and height keywords # to specify the map projection region (instead of specifying # the lat/lon of the upper right and lower left corners). # user enters the lon/lat of the point, and it's name lon_0 = float(raw_input('input reference lon (degrees):')) lat_0 = float(raw_input('input reference lat (degrees):')) location = raw_input('name of location:') # use these values to setup Basemap instance. width = 28000000 m = Basemap(width=width,height=width,\ resolution='c',projection='aeqd',\ lat_0=lat_0,lon_0=lon_0) # draw coasts and fill continents. m.drawcoastlines(linewidth=0.5) m.fillcontinents() # 20 degree graticule. m.drawparallels(arange(-80, 81, 20)) m.drawmeridians(arange(-180, 180, 20)) # draw a red dot at the center. xpt, ypt = m(lon_0, lat_0) m.plot([xpt], [ypt], 'ro') # draw the title. title('The World According to Garp in ' + location) show()
from pylab import * import math, random from matplotlib.toolkits.basemap import Basemap from matplotlib.numerix.random_array import uniform # Plot a bunch of randomly distributed points on the earth. # set up stereographic map centered on N. Pole. m = Basemap(lon_0=-105,boundinglat=30., resolution='l',area_thresh=10000.,projection='npstere') # number of points to plot. npts = 750 # generate random points on a sphere, # so that every small area on the sphere is expected # to have the same number of points. # http://mathworld.wolfram.com/SpherePointPicking.html try: # this works for numpy u = uniform(0.,1.,size=npts) v = uniform(0.,1.,size=npts) z = uniform(0.,1.,size=npts) except: # this works for Numeric/numarray u = uniform(0.,1.,shape=npts) v = uniform(0.,1.,shape=npts) z = uniform(0.,1.,shape=npts) lons = 360.*u lats = (180./math.pi)*arccos(2*v-1) - 90. # transform lons and lats to map coordinates. x,y = m(lons,lats) # plot them as filled circles on the map. # first, create a figure. fig=figure()
def plot_track(rlon, rlat=None, m=None, **keywords): 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'] if (m is None): 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'] minlon = -180 maxlon = 180.0 if ('minlon' in keywords): minlon = keywords['minlon'] if ('maxlon' in keywords): maxlon = keywords['maxlon'] lon_0 = 0 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: m = Basemap(llcrnrlon=minlon, llcrnrlat=minlat, urcrnrlon=maxlon, urcrnrlat=maxlat, projection=map_proj, lon_0=lon_0, lat_0=lat_0) if (show_lat == 1): m.drawparallels(arange(minlat, maxlat + 30.0, 30.), labels=[1, 0, 0, 0]) if (show_lon == 1): m.drawmeridians(arange(minlon, maxlon + 60, 60.), labels=[0, 0, 0, 1]) sgn = 'o' if ('sgn' in keywords): sgn = keywords['sgn'] if ('color' in keywords): print('I am here', size(rlon)) lcolor = keywords['color'] print(max(lcolor), min(lcolor)) if ('minv' in keywords): vmin = keywords['minv'] else: vmin = min(lcolor) if ('maxv' in keywords): vmax = keywords['maxv'] else: vmax = max(lcolor) cx = cm.jet cx.set_over('r') cx.set_under('w') x, y = m(rlon, rlat) if (len(rlon) < 100): scatter(x, y, marker=sgn, c=lcolor, s=30.0, color='w', vmin=vmin, vmax=vmax, cmap=cx) # , markersize=11) else: scatter(x, y, marker=sgn, c=lcolor, s=26, edgecolor='w', vmin=vmin, vmax=vmax, cmap=cx) # , markersize=6) cbar_vert = 1 if ('cbar_vert' in keywords): cbar_vert = keywords['cbar_vert'] if (cbar_vert == 1): orientation = 'vertical' else: orientation = 'horizontal' show_colorbar = 0 if ('cb' in keywords): show_colorbar = keywords['cb'] if (show_colorbar == 1): colorbar(orientation=orientation) # colorbar(cmap=cx) m.drawcoastlines(color='k', linewidth=1.0) m.drawmapboundary() # m.drawcountries(color='k', linewidth=0.5) if ('title' in keywords): stitle = keywords['title'] title(stitle) return m
lats = 90. - delat * arange(nlats) lons, lats = meshgrid(lons, lats) lons = (d2r * lons.flat).tolist() lats = (d2r * lats.flat).tolist() # randomly shuffle locations. random.shuffle(lons) random.shuffle(lats) lons = array(lons, 'f') lats = array(lats, 'f') # minimum separation distance in km. rcrit = 500. # set up lambert azimuthal map centered on N. Pole. m = Basemap(llcrnrlon=-150.,llcrnrlat=0.,urcrnrlon=30.,urcrnrlat=0., resolution='l',area_thresh=10000.,projection='stere',\ lat_0=90.,lon_0=-105.,lat_ts=90.) print len(lons), ' obs before thinning' # calculate distance between each ob and all preceding obs in list. # throw out those that are closer than rcrit. nob = 0 lats_out = [] lons_out = [] for lon, lat in zip(lons, lats): if nob: r = (m.rmajor / 1000.) * get_dist(lon, lons[0:nob], lat, lats[0:nob]) if min(r) > rcrit: lats_out.append(lat) lons_out.append(lon)
lats = 90.-delat*arange(nlats) lons, lats = meshgrid(lons, lats) lons = (d2r*lons.flat).tolist() lats = (d2r*lats.flat).tolist() # randomly shuffle locations. random.shuffle(lons) random.shuffle(lats) lons = array(lons,'f') lats = array(lats,'f') # minimum separation distance in km. rcrit = 500. # set up lambert azimuthal map centered on N. Pole. m = Basemap(-150.,0.,30.,0., resolution='l',area_thresh=10000.,projection='stere',\ lat_0=90.,lon_0=-105.,lat_ts=90.) print len(lons), ' obs before thinning' # calculate distance between each ob and all preceding obs in list. # throw out those that are closer than rcrit. nob = 0 lats_out = [] lons_out = [] for lon,lat in zip(lons,lats): if nob: r = (m.rsphere/1000.)*get_dist(lon,lons[0:nob],lat,lats[0:nob]) if min(r) > rcrit: lats_out.append(lat) lons_out.append(lon)