From 621882a489b4ac0e9f42fe1da0234e1628f1a10c Mon Sep 17 00:00:00 2001 From: Jeff Whitaker Date: Thu, 2 Aug 2012 09:38:59 -0600 Subject: [PATCH 1/2] fix bug triggered when drawlsmask called more than once. wiki_example.py modified to test for this bug. --- Changelog | 1 + examples/wiki_example.py | 90 +++++++++++++++++----------- lib/mpl_toolkits/basemap/__init__.py | 6 +- 3 files changed, 58 insertions(+), 39 deletions(-) diff --git a/Changelog b/Changelog index b39c2e672..cc952713c 100644 --- a/Changelog +++ b/Changelog @@ -1,5 +1,6 @@ version 1.0.5 (git tag v1.0.5rel) --------------------------------- +* fix bug triggered when drawlsmask method called more than once. * fix error in contour method that caused a bogus mask to be applied to the data (issue 58). * fix further corner cases with splitting of parallels that cross diff --git a/examples/wiki_example.py b/examples/wiki_example.py index 87bccc96d..e1d00359f 100644 --- a/examples/wiki_example.py +++ b/examples/wiki_example.py @@ -1,28 +1,28 @@ from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt import numpy as np -# set up orthographic map projection with +# set up orthographic bmap projection with # perspective of satellite looking down at 50N, 100W. # use low resolution coastlines. -map = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l') +bmap = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l') # draw coastlines, country boundaries, fill continents. -map.drawcoastlines(linewidth=0.25) -map.drawcountries(linewidth=0.25) -map.fillcontinents(color='coral',lake_color='aqua') +bmap.drawcoastlines(linewidth=0.25) +bmap.drawcountries(linewidth=0.25) +bmap.fillcontinents(color='coral',lake_color='aqua') # draw the edge of the map projection region (the projection limb) -map.drawmapboundary(fill_color='aqua') +bmap.drawmapboundary(fill_color='aqua') # draw lat/lon grid lines every 30 degrees. -map.drawmeridians(np.arange(0,360,30)) -map.drawparallels(np.arange(-90,90,30)) +bmap.drawmeridians(np.arange(0,360,30)) +bmap.drawparallels(np.arange(-90,90,30)) # lat/lon coordinates of five cities. lats=[40.02,32.73,38.55,48.25,17.29] lons=[-105.16,-117.16,-77.00,-114.21,-88.10] cities=['Boulder, CO','San Diego, CA', 'Washington, DC','Whitefish, MT','Belize City, Belize'] # compute the native map projection coordinates for cities. -xc,yc = map(lons,lats) +xc,yc = bmap(lons,lats) # plot filled circles at the locations of the cities. -map.plot(xc,yc,'bo') +bmap.plot(xc,yc,'bo') # plot the names of those five cities. for name,xpt,ypt in zip(cities,xc,yc): plt.text(xpt+50000,ypt+50000,name,fontsize=9) @@ -33,69 +33,87 @@ wave = 0.75*(np.sin(2.*lats)**8*np.cos(4.*lons)) mean = 0.5*np.cos(2.*lats)*((np.sin(2.*lats))**2 + 2.) # compute native map projection coordinates of lat/lon grid. -x, y = map(lons*180./np.pi, lats*180./np.pi) +x, y = bmap(lons*180./np.pi, lats*180./np.pi) # contour data over the map. -cs = map.contour(x,y,wave+mean,15,linewidths=1.5) +cs = bmap.contour(x,y,wave+mean,15,linewidths=1.5) plt.title('filled continent background') # as above, but use land-sea mask image as map background. fig = plt.figure() -map.drawmapboundary() -map.drawmeridians(np.arange(0,360,30)) -map.drawparallels(np.arange(-90,90,30)) +bmap.drawmapboundary() +bmap.drawmeridians(np.arange(0,360,30)) +bmap.drawparallels(np.arange(-90,90,30)) # plot filled circles at the locations of the cities. -map.plot(xc,yc,'wo') +bmap.plot(xc,yc,'wo') # plot the names of five cities. for name,xpt,ypt in zip(cities,xc,yc): plt.text(xpt+50000,ypt+50000,name,fontsize=9,color='w') # contour data over the map. -cs = map.contour(x,y,wave+mean,15,linewidths=1.5) +cs = bmap.contour(x,y,wave+mean,15,linewidths=1.5) plt.title('land-sea mask background') -map.drawlsmask(ocean_color='aqua',land_color='coral') +bmap.drawlsmask(ocean_color='aqua',land_color='coral') # as above, but use blue marble image as map background. fig = plt.figure() -map.drawmapboundary() -map.drawmeridians(np.arange(0,360,30)) -map.drawparallels(np.arange(-90,90,30)) +bmap.drawmapboundary() +bmap.drawmeridians(np.arange(0,360,30)) +bmap.drawparallels(np.arange(-90,90,30)) # plot filled circles at the locations of the cities. -map.plot(xc,yc,'wo') +bmap.plot(xc,yc,'wo') # plot the names of five cities. for name,xpt,ypt in zip(cities,xc,yc): plt.text(xpt+50000,ypt+50000,name,fontsize=9,color='w') # contour data over the map. -cs = map.contour(x,y,wave+mean,15,linewidths=1.5) +cs = bmap.contour(x,y,wave+mean,15,linewidths=1.5) plt.title('blue marble background') -map.bluemarble() +bmap.bluemarble() # as above, but use shaded relief image as map background. fig = plt.figure() -map.drawmapboundary() -map.drawmeridians(np.arange(0,360,30)) -map.drawparallels(np.arange(-90,90,30)) +bmap.drawmapboundary() +bmap.drawmeridians(np.arange(0,360,30)) +bmap.drawparallels(np.arange(-90,90,30)) # plot filled circles at the locations of the cities. -map.plot(xc,yc,'wo') +bmap.plot(xc,yc,'wo') # plot the names of five cities. for name,xpt,ypt in zip(cities,xc,yc): plt.text(xpt+50000,ypt+50000,name,fontsize=9,color='w') # contour data over the map. -cs = map.contour(x,y,wave+mean,15,linewidths=1.5) +cs = bmap.contour(x,y,wave+mean,15,linewidths=1.5) plt.title('shaded relief background') -map.shadedrelief() +bmap.shadedrelief() # as above, but use etopo image as map background. fig = plt.figure() -map.drawmapboundary() -map.drawmeridians(np.arange(0,360,30)) -map.drawparallels(np.arange(-90,90,30)) +bmap.drawmapboundary() +bmap.drawmeridians(np.arange(0,360,30)) +bmap.drawparallels(np.arange(-90,90,30)) # plot filled circles at the locations of the cities. -map.plot(xc,yc,'wo') +bmap.plot(xc,yc,'wo') # plot the names of five cities. for name,xpt,ypt in zip(cities,xc,yc): plt.text(xpt+50000,ypt+50000,name,fontsize=9,color='w') # contour data over the map. -cs = map.contour(x,y,wave+mean,15,linewidths=1.5) +cs = bmap.contour(x,y,wave+mean,15,linewidths=1.5) plt.title('etopo background') -map.etopo() +bmap.etopo() + +# as above, but use etopo image as map background overlaid with +# land-sea mask image where land areas are transparent (so etopo +# image shows through over land). +fig = plt.figure() +bmap.drawmapboundary() +bmap.drawmeridians(np.arange(0,360,30)) +bmap.drawparallels(np.arange(-90,90,30)) +# plot filled circles at the locations of the cities. +bmap.plot(xc,yc,'wo') +# plot the names of five cities. +for name,xpt,ypt in zip(cities,xc,yc): + plt.text(xpt+50000,ypt+50000,name,fontsize=9,color='w') +# contour data over the map. +cs = bmap.contour(x,y,wave+mean,15,linewidths=1.5) +plt.title('etopo background with oceans masked') +bmap.etopo() +bmap.drawlsmask(ocean_color='DarkBlue',land_color=(255,255,255,1)) plt.show() diff --git a/lib/mpl_toolkits/basemap/__init__.py b/lib/mpl_toolkits/basemap/__init__.py index 62b881c53..70e9b4693 100644 --- a/lib/mpl_toolkits/basemap/__init__.py +++ b/lib/mpl_toolkits/basemap/__init__.py @@ -3679,14 +3679,14 @@ def drawlsmask(self,land_color="0.8",ocean_color="w",lsmask=None, mask[j,:]=np.where(np.logical_or(xxxmax[j]),\ 255,mask[j,:]) self.lsmask = mask - ny, nx = mask.shape + ny, nx = self.lsmask.shape rgba = np.ones((ny,nx,4),np.uint8) rgba_land = np.array(rgba_land,np.uint8) rgba_ocean = np.array(rgba_ocean,np.uint8) for k in range(4): - rgba[:,:,k] = np.where(mask,rgba_land[k],rgba_ocean[k]) + rgba[:,:,k] = np.where(self.lsmask,rgba_land[k],rgba_ocean[k]) # make points outside projection limb transparent. - rgba[:,:,3] = np.where(mask==255,0,rgba[:,:,3]) + rgba[:,:,3] = np.where(self.lsmask==255,0,rgba[:,:,3]) # plot mask as rgba image. im = self.imshow(rgba,interpolation='nearest',ax=ax,**kwargs) # clip for round polar plots. From 249fd70053f7b723621e6efcc590297ecdcfdbc6 Mon Sep 17 00:00:00 2001 From: Jeff Whitaker Date: Thu, 2 Aug 2012 09:42:14 -0600 Subject: [PATCH 2/2] fix typo --- examples/wiki_example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/wiki_example.py b/examples/wiki_example.py index e1d00359f..4d735a2eb 100644 --- a/examples/wiki_example.py +++ b/examples/wiki_example.py @@ -1,7 +1,7 @@ from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt import numpy as np -# set up orthographic bmap projection with +# set up orthographic map projection with # perspective of satellite looking down at 50N, 100W. # use low resolution coastlines. bmap = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l')