Last active
August 29, 2015 14:06
-
-
Save jklymak/84f807d2f4ffb2b1ad19 to your computer and use it in GitHub Desktop.
matplotlib lat and lon formatters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Format axes for lat and lon | |
ax.xaxis.set_major_locator(ticker.MultipleLocator(base=2./60.)) | |
ax.yaxis.set_major_locator(ticker.MultipleLocator(base=2./60.)) | |
def latformatter(x, pos): | |
'The two args are the value and tick position' | |
if x<0: | |
x=-x | |
minus=True | |
else: | |
minus=False | |
degrees = np.floor(x) | |
minutes = (x-degrees)*60. | |
if minus: | |
hem = 'S' | |
else: | |
hem='N' | |
if minutes==0.: | |
print '%d %s %d' % (degrees,hem,pos) | |
return '%d$^o$ %s' % (degrees,hem) | |
else: | |
if pos>1: | |
return "%1.1f'" % (minutes) | |
else: | |
return r'%d$^o$ %s %1.1f' % (degrees,hem,minutes) | |
def lonformatter(x, pos): | |
'The two args are the value and tick position' | |
if x<0: | |
x=-x | |
minus=True | |
else: | |
minus=False | |
degrees = np.floor(x) | |
minutes = (x-degrees)*60. | |
if minus: | |
hem = 'W' | |
else: | |
hem='E' | |
if minutes==0.: | |
print '%d %s %d' % (degrees,hem,pos) | |
return '%d$^o$ %s' % (degrees,hem) | |
else: | |
if pos>1: | |
return "%1.1f'" % (minutes) | |
else: | |
return r'%d$^o$ %s %1.1f' % (degrees,hem,minutes) | |
ax.xaxis.set_major_formatter(ticker.FuncFormatter(lonformatter)) | |
ax.yaxis.set_major_formatter(ticker.FuncFormatter(latformatter)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment