- Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathcustomticks.py
84 lines (75 loc) · 2.72 KB
/
customticks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from __future__ import (absolute_import, division, print_function)
from __future__ importunicode_literals
frommpl_toolkits.basemapimportBasemap
importnumpyasnp
importmatplotlib.pyplotasplt
frommatplotlib.tickerimportFuncFormatter
# example showing how to create custom tick labels for a cylindrical
# projection.
deflat2str(deg):
min=60* (deg-np.floor(deg))
deg=np.floor(deg)
dir='N'
ifdeg<0:
ifmin!=0.0:
deg+=1.0
min-=60.0
dir='S'
return ("%d\N{DEGREE SIGN} %g' %s") % (np.abs(deg),np.abs(min),dir)
deflon2str(deg):
min=60* (deg-np.floor(deg))
deg=np.floor(deg)
dir='E'
ifdeg<0:
ifmin!=0.0:
deg+=1.0
min-=60.0
dir='W'
return ("%d\N{DEGREE SIGN} %g' %s") % (np.abs(deg),np.abs(min),dir)
# (1) use matplotlib custom tick formatter
# instead of Basemap labelling methods.
# create figure.
fig=plt.figure()
# create Basemap instance (regular lat/lon projection).
# suppress_ticks=False allows custom axes ticks to be used
# Ticks are suppressed by default, so Basemap methods
# drawparallels and drawmeridians used to draw labelled lat/lon grid.
m=Basemap(llcrnrlon=-156.5,llcrnrlat=18.75,urcrnrlon=-154.5,urcrnrlat=20.5,
resolution='h',projection='cyl',suppress_ticks=False)
# 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')
# get axes instance.
ax=plt.gca()
# add custom ticks.
# This only works for projection='cyl'.
defxformat(x, pos=None): returnlon2str(x)
xformatter=FuncFormatter(xformat)
ax.xaxis.set_major_formatter(xformatter)
defyformat(y, pos=None): returnlat2str(y)
yformatter=FuncFormatter(yformat)
ax.yaxis.set_major_formatter(yformatter)
ax.fmt_xdata=lambdax: lon2str(x)
ax.fmt_ydata=lambday: 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=plt.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(np.linspace(18,21,7),labels=[1,0,0,0],fmt=lat2str,dashes=[2,2])
m.drawmeridians(np.linspace(-157,-154,7),labels=[0,0,0,1],fmt=lon2str,dashes=[2,2])
plt.title('Hawaii')
plt.show()