def draw_graph_on_map(g, node2weight, node2pos, pic_title, pic_area=[-130,10,140,70], output_fname_prefix=None, need_draw_edge=0): """ 2007-09-13 identity_pair_ls is a list of pairs of strains (ecotype id as in table ecotype) 2007-10-08 correct a bug in 4*diameter_ls, diameter_ls has to be converted to array first. sqrt the node weight, 8 times the original weight """ import os, sys sys.stderr.write("Drawing graph on a map ...\n") import pylab, math 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()) sys.stderr.write("\tDrawing nodes ...") euc_coord1_ls = [] euc_coord2_ls = [] diameter_ls = [] for n in g.nodes(): lat, lon = node2pos[n] euc_coord1, euc_coord2 = m(lon, lat) #longitude first, latitude 2nd euc_coord1_ls.append(euc_coord1) euc_coord2_ls.append(euc_coord2) diameter_ls.append(math.sqrt(node2weight[n])) import numpy diameter_ls = numpy.array(diameter_ls) m.scatter(euc_coord1_ls, euc_coord2_ls, 8*diameter_ls, marker='o', color='r', alpha=0.4, zorder=12, faceted=False) sys.stderr.write("Done.\n") if need_draw_edge: sys.stderr.write("\tDrawing edges ...") ax=pylab.gca() for popid1, popid2, no_of_connections in g.edges(): lat1, lon1 = node2pos[popid1] lat2, lon2 = node2pos[popid2] x1, y1 = m(lon1, lat1) x2, y2 = m(lon2, lat2) ax.plot([x1,x2],[y1,y2], 'g', linewidth=math.log(no_of_connections+1)/2, alpha=0.2, zorder=10) sys.stderr.write("Done.\n") #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(pic_title) if output_fname_prefix: pylab.savefig('%s.eps'%output_fname_prefix, dpi=600) pylab.savefig('%s.svg'%output_fname_prefix, dpi=600) pylab.savefig('%s.png'%output_fname_prefix, dpi=600) del fig, m, pylab sys.stderr.write("Done.\n")
def draw_clustered_strain_location(self, label_ls, weighted_pos_ls, diameter_ls, label_type, label_type2label_name, pic_area=[-180,-90,180,90], output_fname_prefix=None, label_name=None): """ 2007-07-11 draw populations derived from connected_components of the strain network #each pie denotes a population, with diameter proportional to the size of the population #each pie labeled with the number of strains in that population 2007-07-13 use popid as label 2007-07-17 no parallels, no meridians 2007-08-29 copied from CreatePopulation.py 2007-09-11 add label name 2007-10-14 correct a bug in 5*diameter_ls. diameter_ls has to an array """ sys.stderr.write("Drawing population map...") 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') euc_coord1_ls = [] euc_coord2_ls = [] ax=pylab.gca() for i in range(len(weighted_pos_ls)): lat, lon = weighted_pos_ls[i] euc_coord1, euc_coord2 = m(lon, lat) #longitude first, latitude 2nd euc_coord1_ls.append(euc_coord1) euc_coord2_ls.append(euc_coord2) ax.text(euc_coord1, euc_coord2, str(label_ls[i]), size=5, alpha=0.5, horizontalalignment='center', verticalalignment='center', zorder=12) import numpy diameter_ls = numpy.array(diameter_ls) m.scatter(euc_coord1_ls, euc_coord2_ls, 5*diameter_ls, marker='o', color='r', alpha=0.3, zorder=10, faceted=False) #m.drawcoastlines() m.drawparallels(pylab.arange(-90,90,30), labels=[1,1,0,1]) #labels intersect the left, right, top bottom of the plot m.drawmeridians(pylab.arange(-180,180,30), labels=[1,1,0,1]) m.fillcontinents() m.drawcountries() m.drawstates() pylab.title("worldwide distribution of %s populations, labeled by %s"%(len(weighted_pos_ls), label_type2label_name[label_type])) if output_fname_prefix: pylab.savefig('%s_pop_map.eps'%output_fname_prefix, dpi=300) pylab.savefig('%s_pop_map.svg'%output_fname_prefix, dpi=300) pylab.savefig('%s_pop_map.png'%output_fname_prefix, dpi=300) del m, pylab, Basemap sys.stderr.write("Done.\n")
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")
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")
# setup mercator map projection (-80 to +80). m = Basemap(-180.,-80.,180.,80.,\ resolution='c',area_thresh=10000.,projection='merc',\ lon_0=0.5*(lons[0]+lons[-1]),lat_ts=20.) # transform to nx x ny regularly spaced native projection grid nx = len(lons); ny = int(80.*len(lats)/90.) topodat = m.transform_scalar(topoin,lons,lats,nx,ny) # setup 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.75,0.75]) # plot image over map. im = m.imshow(topodat,cm.jet) m.drawcoastlines() m.drawcountries() m.drawstates() m.fillcontinents() # draw parallels m.drawparallels(circles,labels=[1,1,1,1]) # draw meridians m.drawmeridians(meridians,labels=[1,1,1,1]) title('Mercator',y=1.1) print 'plotting Mercator example, close plot window to proceed ...' show() # setup transverse mercator basemap. m = Basemap(170.,-45,10.,45.,\ resolution='c',area_thresh=10000.,projection='tmerc',\ lat_0=0.,lon_0=-90.) xsize = rcParams['figure.figsize'][0] fig=figure(figsize=(xsize/m.aspect,xsize))
m = Basemap(-180.,-80.,180.,80.,\ resolution='c',area_thresh=10000.,projection='merc',\ lon_0=0.5*(lons[0]+lons[-1]),lat_ts=20.) # transform to nx x ny regularly spaced native projection grid nx = len(lons) ny = int(80. * len(lats) / 90.) topodat = m.transform_scalar(topoin, lons, lats, nx, ny) # setup 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.75, 0.75]) # plot image over map. im = m.imshow(topodat, cm.jet) m.drawcoastlines() m.drawcountries() m.drawstates() m.fillcontinents() # draw parallels m.drawparallels(circles, labels=[1, 1, 1, 1]) # draw meridians m.drawmeridians(meridians, labels=[1, 1, 1, 1]) title('Mercator', y=1.1) print 'plotting Mercator example, close plot window to proceed ...' show() # setup transverse mercator basemap. m = Basemap(170.,-45,10.,45.,\ resolution='c',area_thresh=10000.,projection='tmerc',\ lat_0=0.,lon_0=-90.) xsize = rcParams['figure.figsize'][0] fig = figure(figsize=(xsize / m.aspect, xsize))
lon, lat = self.baseMap(0.0, y, inverse=True) return "%g" % lat m = Basemap(-180.,-80.,180.,80.,\ 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.drawcountries(ax) m.drawstates(ax) m.fillcontinents(ax) # draw parallels delat = 30. circles = arange(0.,90.,delat).tolist()+\ arange(-delat,-90,-delat).tolist() m.drawparallels(ax,circles) # convert parallels to native map projection coordinates nativeCoordCircles = m([0]*len(circles), circles)[1] ax.set_yticks(nativeCoordCircles) ax.yaxis.set_major_formatter(MercYAxisFormatter(m)) # draw meridians delon = 60. lon1 = int(m.llcrnrlon/delon)*delon lon2 = (int(m.urcrnrlon/delon)+1)*delon meridians = arange(lon1,lon2,delon)
# define grid (nx x ny regularly spaced native projection grid) nx = len(lons) ny = int(80. * len(lats) / 90.) 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)) fig.add_axes([0.1, 0.1, 0.8, 0.8]) im = imshow(topodat, cm.jet, extent=(m.llcrnrx, m.urcrnrx, m.llcrnry, m.urcrnry), origin='lower') ax = gca() # get current axis instance m.drawcoastlines(ax) m.drawcountries(ax) m.drawstates(ax) m.fillcontinents(ax) # draw parallels m.drawparallels(ax, circles) # draw meridians m.drawmeridians(ax, meridians) ax.set_xticks([]) # no ticks ax.set_yticks([]) title('Mercator') print 'plotting Mercator example, close plot window to proceed ...' show() 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)
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. circles = arange(0.,90.+delat,delat).tolist()+\ arange(-delat,-90.-delat,-delat).tolist() delon = 10. meridians = arange(10., 360., delon) lat_1 = 40 lat_2 = 55 lon_1 = -120 lon_2 = -140
fontsize = 14 #Do the necessary processing for each analysis contours = N.arange(5300., 6000., 60.0) parallels = N.arange(25., 60.0, 10.0) meridians = N.arange(-120., -60.0, 10.0) fields = (cress1, cress2, cress3, cress_187, barnes_analyses[0], barnes_analyses[1], barnes_analyses[2], barnes_3pass) names = ('Cressman 1st Pass', 'Cressman 2nd Pass', 'Cressman 3rd Pass', 'Cressman R=1.87d', r'$\rm{Barnes} \gamma=1.0$',r'$\rm{Barnes} \gamma=0.4$', r'$\rm{Barnes} \gamma=0.2$', r'$\rm{Barnes 3-pass} \gamma=1.0$') filenames = ('cress1', 'cress2', 'cress3', 'cress187', 'barnes1', 'barnes2', 'barnes3', 'barnes3pass') for field,name,filename in zip(fields, names, filenames): f = P.figure() bm_ps.drawstates() bm_ps.drawcountries() bm_ps.drawcoastlines() bm_ps.drawparallels(parallels, labels = [1,1,0,0]) bm_ps.drawmeridians(meridians, labels = [0,0,1,1]) cp = bm_ps.contour(x_bm, y_bm, field, contours, cmap=colormap) P.clabel(cp, fontsize=fontsize, fmt='%.1f') f.text(0.5,0.95,name,horizontalalignment='center',fontsize=16) if save_work: P.savefig(filename + '.png', dpi = 300) else: P.show() filename = 'cress12diff' f = P.figure() bm_ps.drawstates()
def draw_clustered_strain_location(self, label_ls, weighted_pos_ls, diameter_ls, label_type, label_type2label_name, pic_area=[-180, -90, 180, 90], output_fname_prefix=None, label_name=None): """ 2007-07-11 draw populations derived from connected_components of the strain network #each pie denotes a population, with diameter proportional to the size of the population #each pie labeled with the number of strains in that population 2007-07-13 use popid as label 2007-07-17 no parallels, no meridians 2007-08-29 copied from CreatePopulation.py 2007-09-11 add label name 2007-10-14 correct a bug in 5*diameter_ls. diameter_ls has to an array """ sys.stderr.write("Drawing population map...") 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') euc_coord1_ls = [] euc_coord2_ls = [] ax = pylab.gca() for i in range(len(weighted_pos_ls)): lat, lon = weighted_pos_ls[i] euc_coord1, euc_coord2 = m(lon, lat) #longitude first, latitude 2nd euc_coord1_ls.append(euc_coord1) euc_coord2_ls.append(euc_coord2) ax.text(euc_coord1, euc_coord2, str(label_ls[i]), size=5, alpha=0.5, horizontalalignment='center', verticalalignment='center', zorder=12) import numpy diameter_ls = numpy.array(diameter_ls) m.scatter(euc_coord1_ls, euc_coord2_ls, 5 * diameter_ls, marker='o', color='r', alpha=0.3, zorder=10, faceted=False) #m.drawcoastlines() m.drawparallels(pylab.arange(-90, 90, 30), labels=[ 1, 1, 0, 1 ]) #labels intersect the left, right, top bottom of the plot m.drawmeridians(pylab.arange(-180, 180, 30), labels=[1, 1, 0, 1]) m.fillcontinents() m.drawcountries() m.drawstates() pylab.title("worldwide distribution of %s populations, labeled by %s" % (len(weighted_pos_ls), label_type2label_name[label_type])) if output_fname_prefix: pylab.savefig('%s_pop_map.eps' % output_fname_prefix, dpi=300) pylab.savefig('%s_pop_map.svg' % output_fname_prefix, dpi=300) pylab.savefig('%s_pop_map.png' % output_fname_prefix, dpi=300) del m, pylab, Basemap sys.stderr.write("Done.\n")
def draw_graph_on_map(g, node2weight, node2pos, pic_title, pic_area=[-130, 10, 140, 70], output_fname_prefix=None, need_draw_edge=0): """ 2007-09-13 identity_pair_ls is a list of pairs of strains (ecotype id as in table ecotype) 2007-10-08 correct a bug in 4*diameter_ls, diameter_ls has to be converted to array first. sqrt the node weight, 8 times the original weight """ import os, sys sys.stderr.write("Drawing graph on a map ...\n") import pylab, math 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()) sys.stderr.write("\tDrawing nodes ...") euc_coord1_ls = [] euc_coord2_ls = [] diameter_ls = [] for n in g.nodes(): lat, lon = node2pos[n] euc_coord1, euc_coord2 = m(lon, lat) #longitude first, latitude 2nd euc_coord1_ls.append(euc_coord1) euc_coord2_ls.append(euc_coord2) diameter_ls.append(math.sqrt(node2weight[n])) import numpy diameter_ls = numpy.array(diameter_ls) m.scatter(euc_coord1_ls, euc_coord2_ls, 8 * diameter_ls, marker='o', color='r', alpha=0.4, zorder=12, faceted=False) sys.stderr.write("Done.\n") if need_draw_edge: sys.stderr.write("\tDrawing edges ...") ax = pylab.gca() for popid1, popid2, no_of_connections in g.edges(): lat1, lon1 = node2pos[popid1] lat2, lon2 = node2pos[popid2] x1, y1 = m(lon1, lat1) x2, y2 = m(lon2, lat2) ax.plot([x1, x2], [y1, y2], 'g', linewidth=math.log(no_of_connections + 1) / 2, alpha=0.2, zorder=10) sys.stderr.write("Done.\n") #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(pic_title) if output_fname_prefix: pylab.savefig('%s.eps' % output_fname_prefix, dpi=600) pylab.savefig('%s.svg' % output_fname_prefix, dpi=600) pylab.savefig('%s.png' % output_fname_prefix, dpi=600) del fig, m, pylab sys.stderr.write("Done.\n")
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 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) show() proj = 'omerc' delat = 10. circles = arange(0.,90.+delat,delat).tolist()+\ arange(-delat,-90.-delat,-delat).tolist() delon = 10. meridians = arange(10.,360.,delon) lat_1 = 40; lat_2 = 55 lon_1 = -120; lon_2 = -140 lat_0 = 47.5 ; lon_0 = -130 fig = figure()
x_bm,y_bm = ps2.to_basemap_xy(m2, x_grid / cm_per_m, y_grid / cm_per_m) #Radius factor used in all of the analyses R = 4.32 heights_cress = oban.analyze_grid(data.heights, x_grid, y_grid, data.x, data.y, oban.cressman_weights, R) contours = N.arange(5300., 6000., 60.0) parallels = N.arange(25., 60.0, 10.0) meridians = N.arange(-120., -60.0, 10.0) ##m2.drawstates() ##m2.drawcountries() ##m2.drawcoastlines() ##m2.drawparallels(parallels, labels = [1,1,0,0]) ##m2.drawmeridians(meridians, labels = [0,0,1,1]) ob_x, ob_y = ps2.to_basemap_xy(m2, data.x / cm_per_m, data.y / cm_per_m) #m2.plot(ob_x, ob_y, 'bx') #m2.plot(x_bm, y_bm, 'g.') for name in M.cm.cmapnames: f = P.figure() m2.drawstates() m2.drawcountries() m2.drawcoastlines() m2.drawparallels(parallels, labels = [1,1,0,0]) m2.drawmeridians(meridians, labels = [0,0,1,1]) c = m2.contour(x_bm, y_bm, heights_cress, contours, cmap=M.cm.get_cmap(name)) P.title(name) #P.clabel(c) P.show()