Skip to content

Instantly share code, notes, and snippets.

@jklymak
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jklymak/84f807d2f4ffb2b1ad19 to your computer and use it in GitHub Desktop.
Save jklymak/84f807d2f4ffb2b1ad19 to your computer and use it in GitHub Desktop.
matplotlib lat and lon formatters
# 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