From: <js...@us...> - 2007-11-27 16:21:14
|
Revision: 4464 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4464&view=rev Author: jswhit Date: 2007-11-27 08:21:11 -0800 (Tue, 27 Nov 2007) Log Message: ----------- fmt kwarg to drawparallels and drawmeridians can now be a string formatting function. Modified Paths: -------------- trunk/toolkits/basemap/Changelog trunk/toolkits/basemap/examples/customticks.py trunk/toolkits/basemap/lib/matplotlib/toolkits/basemap/basemap.py Modified: trunk/toolkits/basemap/Changelog =================================================================== --- trunk/toolkits/basemap/Changelog 2007-11-27 13:12:10 UTC (rev 4463) +++ trunk/toolkits/basemap/Changelog 2007-11-27 16:21:11 UTC (rev 4464) @@ -1,4 +1,7 @@ version 0.9.8 (not yet released) + * 'fmt' kwarg to drawparallels and drawmeridians can + now be a custom string formatting function (example + customticks.py demonstrates usage). * remove 'linestyle' kwarg from drawparallels and drawmeridians (it never did anything anyway since it was overridden by the 'dashes' kwarg). Modified: trunk/toolkits/basemap/examples/customticks.py =================================================================== --- trunk/toolkits/basemap/examples/customticks.py 2007-11-27 13:12:10 UTC (rev 4463) +++ trunk/toolkits/basemap/examples/customticks.py 2007-11-27 16:21:11 UTC (rev 4464) @@ -5,19 +5,32 @@ # example showing how to create custom tick labels for a cylindrical # projection. -def deg2str(deg, dir='E', fmt="%3.1f"): +def lat2str(deg): + fmt = "%d" min = 60 * (deg - numpy.floor(deg)) deg = numpy.floor(deg) + dir = 'N' if deg < 0: if min != 0.0: deg += 1.0 min -= 60.0 - if dir=='E': - dir='W' - if dir=='N': - dir='S' - return (u"%d\N{DEGREE SIGN}" + fmt + "' %s") % (numpy.abs(deg), numpy.abs(min), dir) + dir='S' + return (u"%d\N{DEGREE SIGN} %g' %s") % (numpy.abs(deg), numpy.abs(min), dir) +def lon2str(deg): + min = 60 * (deg - numpy.floor(deg)) + deg = numpy.floor(deg) + dir = 'E' + if deg < 0: + if min != 0.0: + deg += 1.0 + min -= 60.0 + dir='W' + return (u"%d\N{DEGREE SIGN} %g' %s") % (numpy.abs(deg), numpy.abs(min), dir) + +# (1) use matplotlib custom tick formatter +# instead of Basemap labelling methods. + # create figure. fig=pylab.figure() # create Basemap instance (regular lat/lon projection). @@ -35,14 +48,34 @@ ax = pylab.gca() # add custom ticks. # This only works for projection='cyl'. -def xformat(x, pos=None): return deg2str(x, 'E', fmt="%2.0f") +def xformat(x, pos=None): return lon2str(x) xformatter = FuncFormatter(xformat) ax.xaxis.set_major_formatter(xformatter) -def yformat(y, pos=None): return deg2str(y, 'N', fmt="%2.0f") +def yformat(y, pos=None): return lat2str(y) yformatter = FuncFormatter(yformat) ax.yaxis.set_major_formatter(yformatter) -ax.fmt_xdata = lambda x: deg2str(x, 'E', fmt="%5.3f") -ax.fmt_ydata = lambda y: deg2str(y, 'N', fmt="%5.3f") +ax.fmt_xdata = lambda x: lon2str(x) +ax.fmt_ydata = lambda y: lat2str(y) ax.grid() ax.set_title('Hawaii') + +# (2) use Basemap labelling methods, but pass a +# custom formatting function with the 'fmt' keyword. + +# create figure. +fig = pylab.figure() +# create Basemap instance. +m = Basemap(llcrnrlon=-156.5,llcrnrlat=18.75,urcrnrlon=-154.5,urcrnrlat=20.5, + resolution='h',projection='cyl') +# draw coastlines, fill land and lake areas. +m.drawcoastlines() +m.fillcontinents(color='coral',lake_color='aqua') +# background color will be used for oceans. +m.drawmapboundary(fill_color='aqua') +# label meridians and parallels, passing string formatting function +# with 'fmt' keyword. +m.drawparallels(numpy.linspace(18,21,7),labels=[1,0,0,0],fmt=lat2str) +m.drawmeridians(numpy.linspace(-157,-154,7),labels=[0,0,0,1],fmt=lon2str) +pylab.title('Hawaii') + pylab.show() Modified: trunk/toolkits/basemap/lib/matplotlib/toolkits/basemap/basemap.py =================================================================== --- trunk/toolkits/basemap/lib/matplotlib/toolkits/basemap/basemap.py 2007-11-27 13:12:10 UTC (rev 4463) +++ trunk/toolkits/basemap/lib/matplotlib/toolkits/basemap/basemap.py 2007-11-27 16:21:11 UTC (rev 4464) @@ -1375,7 +1375,9 @@ but not the right and top. labelstyle - if set to "+/-", north and south latitudes are labelled with "+" and "-", otherwise they are labelled with "N" and "S". - fmt is a format string to format the parallel labels (default '%g'). + fmt can be is a format string to format the parallel labels + (default '%g') or a function that takes a latitude value + in degrees as it's only argument and returns a formatted string. xoffset - label offset from edge of map in x-direction (default is 0.01 times width of map in map projection coordinates). yoffset - label offset from edge of map in y-direction @@ -1517,36 +1519,40 @@ nl = _searchlist(lats,lat) nr = _searchlist(lats[::-1],lat) if nr != -1: nr = len(lons)-nr-1 - if lat<0: - if rcParams['text.usetex']: - if labelstyle=='+/-': - latlabstr = r'${\/-%s\/^{\circ}}$'%fmt + latlab = fmt(lat) + try: # fmt is a function that returns a formatted string + latlab = fmt(lat) + except: # fmt is a format string. + if lat<0: + if rcParams['text.usetex']: + if labelstyle=='+/-': + latlabstr = r'${\/-%s\/^{\circ}}$'%fmt + else: + latlabstr = r'${%s\/^{\circ}\/S}$'%fmt else: - latlabstr = r'${%s\/^{\circ}\/S}$'%fmt - else: - if labelstyle=='+/-': - latlabstr = u'-%s\N{DEGREE SIGN}'%fmt + if labelstyle=='+/-': + latlabstr = u'-%s\N{DEGREE SIGN}'%fmt + else: + latlabstr = u'%s\N{DEGREE SIGN}S'%fmt + latlab = latlabstr%npy.fabs(lat) + elif lat>0: + if rcParams['text.usetex']: + if labelstyle=='+/-': + latlabstr = r'${\/+%s\/^{\circ}}$'%fmt + else: + latlabstr = r'${%s\/^{\circ}\/N}$'%fmt else: - latlabstr = u'%s\N{DEGREE SIGN}S'%fmt - latlab = latlabstr%npy.fabs(lat) - elif lat>0: - if rcParams['text.usetex']: - if labelstyle=='+/-': - latlabstr = r'${\/+%s\/^{\circ}}$'%fmt - else: - latlabstr = r'${%s\/^{\circ}\/N}$'%fmt + if labelstyle=='+/-': + latlabstr = u'+%s\N{DEGREE SIGN}'%fmt + else: + latlabstr = u'%s\N{DEGREE SIGN}N'%fmt + latlab = latlabstr%lat else: - if labelstyle=='+/-': - latlabstr = u'+%s\N{DEGREE SIGN}'%fmt + if rcParams['text.usetex']: + latlabstr = r'${%s\/^{\circ}}$'%fmt else: - latlabstr = u'%s\N{DEGREE SIGN}N'%fmt - latlab = latlabstr%lat - else: - if rcParams['text.usetex']: - latlabstr = r'${%s\/^{\circ}}$'%fmt - else: - latlabstr = u'%s\N{DEGREE SIGN}'%fmt - latlab = latlabstr%lat + latlabstr = u'%s\N{DEGREE SIGN}'%fmt + latlab = latlabstr%lat # parallels can intersect each map edge twice. for i,n in enumerate([nl,nr]): # don't bother if close to the first label. @@ -1594,7 +1600,9 @@ but not the right and top. labelstyle - if set to "+/-", east and west longitudes are labelled with "+" and "-", otherwise they are labelled with "E" and "W". - fmt is a format string to format the meridian labels (default '%g'). + fmt can be is a format string to format the meridian labels + (default '%g') or a function that takes a longitude value + in degrees as it's only argument and returns a formatted string. xoffset - label offset from edge of map in x-direction (default is 0.01 times width of map in map projection coordinates). yoffset - label offset from edge of map in y-direction @@ -1721,42 +1729,45 @@ lons = [(lon+360) % 360 for lon in lons] for lon in meridians: # adjust so 0 <= lon < 360 - lon = (lon+360) % 360 + lon2 = (lon+360) % 360 # find index of meridian (there may be two, so # search from left and right). - nl = _searchlist(lons,lon) - nr = _searchlist(lons[::-1],lon) + nl = _searchlist(lons,lon2) + nr = _searchlist(lons[::-1],lon2) if nr != -1: nr = len(lons)-nr-1 - if lon>180: - if rcParams['text.usetex']: - if labelstyle=='+/-': - lonlabstr = r'${\/-%s\/^{\circ}}$'%fmt + try: # fmt is a function that returns a formatted string + lonlab = fmt(lon) + except: # fmt is a format string. + if lon2>180: + if rcParams['text.usetex']: + if labelstyle=='+/-': + lonlabstr = r'${\/-%s\/^{\circ}}$'%fmt + else: + lonlabstr = r'${%s\/^{\circ}\/W}$'%fmt else: - lonlabstr = r'${%s\/^{\circ}\/W}$'%fmt - else: - if labelstyle=='+/-': - lonlabstr = u'-%s\N{DEGREE SIGN}'%fmt + if labelstyle=='+/-': + lonlabstr = u'-%s\N{DEGREE SIGN}'%fmt + else: + lonlabstr = u'%s\N{DEGREE SIGN}W'%fmt + lonlab = lonlabstr%npy.fabs(lon2-360) + elif lon2<180 and lon2 != 0: + if rcParams['text.usetex']: + if labelstyle=='+/-': + lonlabstr = r'${\/+%s\/^{\circ}}$'%fmt + else: + lonlabstr = r'${%s\/^{\circ}\/E}$'%fmt else: - lonlabstr = u'%s\N{DEGREE SIGN}W'%fmt - lonlab = lonlabstr%npy.fabs(lon-360) - elif lon<180 and lon != 0: - if rcParams['text.usetex']: - if labelstyle=='+/-': - lonlabstr = r'${\/+%s\/^{\circ}}$'%fmt - else: - lonlabstr = r'${%s\/^{\circ}\/E}$'%fmt + if labelstyle=='+/-': + lonlabstr = u'+%s\N{DEGREE SIGN}'%fmt + else: + lonlabstr = u'%s\N{DEGREE SIGN}E'%fmt + lonlab = lonlabstr%lon2 else: - if labelstyle=='+/-': - lonlabstr = u'+%s\N{DEGREE SIGN}'%fmt + if rcParams['text.usetex']: + lonlabstr = r'${%s\/^{\circ}}$'%fmt else: - lonlabstr = u'%s\N{DEGREE SIGN}E'%fmt - lonlab = lonlabstr%lon - else: - if rcParams['text.usetex']: - lonlabstr = r'${%s\/^{\circ}}$'%fmt - else: - lonlabstr = u'%s\N{DEGREE SIGN}'%fmt - lonlab = lonlabstr%lon + lonlabstr = u'%s\N{DEGREE SIGN}'%fmt + lonlab = lonlabstr%lon2 # meridians can intersect each map edge twice. for i,n in enumerate([nl,nr]): lat = lats[n]/100. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |