1 | import numpy as npy
|
---|
2 | from cmaptools import truncate_colormap
|
---|
3 | from plot_contour import plot_contour
|
---|
4 |
|
---|
5 | try:
|
---|
6 | from matplotlib.ticker import MaxNLocator
|
---|
7 | from mpl_toolkits.axes_grid1 import make_axes_locatable
|
---|
8 | import matplotlib as mpl
|
---|
9 | import pylab as p
|
---|
10 | import matplotlib.pyplot as plt
|
---|
11 | except ImportError:
|
---|
12 | print "could not import pylab, matplotlib has not been installed, no plotting capabilities enabled"
|
---|
13 |
|
---|
14 | def applyoptions(md,data,options,fig,ax):
|
---|
15 | '''
|
---|
16 | APPLYOPTIONS - apply options to current plot
|
---|
17 |
|
---|
18 | 'plotobj' is the object returned by the specific plot call used to
|
---|
19 | render the data. This object is used for adding a colorbar.
|
---|
20 |
|
---|
21 | Usage:
|
---|
22 | applyoptions(md,data,options)
|
---|
23 |
|
---|
24 | See also: PLOTMODEL, PARSE_OPTIONS
|
---|
25 | '''
|
---|
26 |
|
---|
27 | #some defaults (this seems to be adding a field that already exists...)
|
---|
28 | #if not isnan(md.mesh.hemisphere):
|
---|
29 | # options.addfielddefault('hemisphere',md.mesh.hemisphere)
|
---|
30 |
|
---|
31 | # get handle to current figure and axes instance
|
---|
32 | #fig = p.gcf()
|
---|
33 | #ax=p.gca()
|
---|
34 |
|
---|
35 | #font {{{
|
---|
36 | fontsize=options.getfieldvalue('fontsize',8)
|
---|
37 | fontweight=options.getfieldvalue('fontweight','normal')
|
---|
38 | fontfamily=options.getfieldvalue('fontfamily','sans-serif')
|
---|
39 | font={
|
---|
40 | 'fontsize' :fontsize,
|
---|
41 | 'fontweight' :fontweight,
|
---|
42 | 'family' :fontfamily
|
---|
43 | }
|
---|
44 | #}}}
|
---|
45 |
|
---|
46 | #title {{{
|
---|
47 | if options.exist('title'):
|
---|
48 | title=options.getfieldvalue('title')
|
---|
49 | if options.exist('titlefontsize'):
|
---|
50 | titlefontsize=options.getfieldvalue('titlefontsize')
|
---|
51 | else:
|
---|
52 | titlefontsize=fontsize
|
---|
53 | if options.exist('titlefontweight'):
|
---|
54 | titlefontweight=options.getfieldvalue('titlefontweight')
|
---|
55 | else:
|
---|
56 | titlefontweight=fontweight
|
---|
57 | #title font
|
---|
58 | titlefont=font.copy()
|
---|
59 | titlefont['size']=titlefontsize
|
---|
60 | titlefont['weight']=titlefontweight
|
---|
61 | ax.set_title(title,**titlefont)
|
---|
62 | #}}}
|
---|
63 |
|
---|
64 | #xlabel, ylabel, zlabel {{{
|
---|
65 | if options.exist('labelfontsize'):
|
---|
66 | labelfontsize=options.getfieldvalue('labelfontsize')
|
---|
67 | else:
|
---|
68 | labelfontsize=fontsize
|
---|
69 | if options.exist('labelfontweight'):
|
---|
70 | labelfontweight=options.getfieldvalue('labelfontweight')
|
---|
71 | else:
|
---|
72 | labelfontweight=fontweight
|
---|
73 |
|
---|
74 | #font dict for labels
|
---|
75 | labelfont=font.copy()
|
---|
76 | labelfont['fontsize']=labelfontsize
|
---|
77 | labelfont['fontweight']=labelfontweight
|
---|
78 |
|
---|
79 | if options.exist('xlabel'):
|
---|
80 | ax.set_xlabel(options.getfieldvalue('xlabel'),**labelfont)
|
---|
81 | if options.exist('ylabel'):
|
---|
82 | ax.set_ylabel(options.getfieldvalue('ylabel'),**labelfont)
|
---|
83 | if options.exist('zlabel'):
|
---|
84 | ax.set_zlabel(options.getfieldvalue('zlabel'),**labelfont)
|
---|
85 | #}}}
|
---|
86 |
|
---|
87 | #xticks, yticks, zticks (tick locations) {{{
|
---|
88 | if options.exist('xticks'):
|
---|
89 | if options.exist('xticklabels'):
|
---|
90 | xticklabels=options.getfieldvalue('xticklabels')
|
---|
91 | ax.set_xticks(options.getfieldvalue('xticks'),xticklabels)
|
---|
92 | else:
|
---|
93 | ax.set_xticks(options.getfieldvalue('xticks'))
|
---|
94 | if options.exist('yticks'):
|
---|
95 | if options.exist('yticklabels'):
|
---|
96 | yticklabels=options.getfieldvalue('yticklabels')
|
---|
97 | ax.set_yticks(options.getfieldvalue('yticks'),yticklabels)
|
---|
98 | else:
|
---|
99 | ax.set_yticks(options.getfieldvalue('yticks'))
|
---|
100 | if options.exist('zticks'):
|
---|
101 | if options.exist('zticklabels'):
|
---|
102 | zticklabels=options.getfieldvalue('zticklabels')
|
---|
103 | ax.set_zticks(options.getfieldvalue('zticks'),zticklabels)
|
---|
104 | else:
|
---|
105 | ax.set_zticks(options.getfieldvalue('zticks'))
|
---|
106 | #}}}
|
---|
107 |
|
---|
108 | #xticklabels,yticklabels,zticklabels {{{
|
---|
109 | if options.getfieldvalue('ticklabels','off')=='off' or options.getfieldvalue('ticklabels',0)==0:
|
---|
110 | options.addfielddefault('xticklabels',[])
|
---|
111 | options.addfielddefault('yticklabels',[])
|
---|
112 | # TODO check if ax has a z-axis (e.g. is 3D)
|
---|
113 | if options.exist('xticklabels'):
|
---|
114 | xticklabels=options.getfieldvalue('xticklabels')
|
---|
115 | ax.set_xticklabels(xticklabels)
|
---|
116 | if options.exist('yticklabels'):
|
---|
117 | yticklabels=options.getfieldvalue('yticklabels')
|
---|
118 | ax.set_yticklabels(yticklabels)
|
---|
119 | if options.exist('zticklabels'):
|
---|
120 | zticklabels=options.getfieldvalue('zticklabels')
|
---|
121 | ax.set_zticklabels(zticklabels)
|
---|
122 | #}}}
|
---|
123 |
|
---|
124 | #ticklabel notation {{{
|
---|
125 | #ax.ticklabel_format(style='sci',scilimits=(0,0))
|
---|
126 | #}}}
|
---|
127 |
|
---|
128 | #ticklabelfontsize {{{
|
---|
129 | if options.exist('ticklabelfontsize'):
|
---|
130 | for label in ax.get_xticklabels() + ax.get_yticklabels():
|
---|
131 | label.set_fontsize(options.getfieldvalue('ticklabelfontsize'))
|
---|
132 | if int(md.mesh.dimension)==3:
|
---|
133 | for label in ax.get_zticklabels():
|
---|
134 | label.set_fontsize(options.getfieldvalue('ticklabelfontsize'))
|
---|
135 | #}}}
|
---|
136 |
|
---|
137 | #view
|
---|
138 |
|
---|
139 | #axis {{{
|
---|
140 | if options.exist('axis'):
|
---|
141 | if options.getfieldvalue('axis',True)=='off':
|
---|
142 | ax.ticklabel_format(style='plain')
|
---|
143 | p.setp(ax.get_xticklabels(), visible=False)
|
---|
144 | p.setp(ax.get_yticklabels(), visible=False)
|
---|
145 | # }}}
|
---|
146 |
|
---|
147 | #box
|
---|
148 |
|
---|
149 | #xlim, ylim, zlim {{{
|
---|
150 | if options.exist('xlim'):
|
---|
151 | ax.set_xlim(options.getfieldvalue('xlim'))
|
---|
152 | if options.exist('ylim'):
|
---|
153 | ax.set_ylim(options.getfieldvalue('ylim'))
|
---|
154 | if options.exist('zlim'):
|
---|
155 | ax.set_zlim(options.getfieldvalue('zlim'))
|
---|
156 | #}}}
|
---|
157 |
|
---|
158 | #latlon
|
---|
159 |
|
---|
160 | #Basinzoom
|
---|
161 |
|
---|
162 | #ShowBasins
|
---|
163 |
|
---|
164 | #clim {{{
|
---|
165 | if options.exist('clim'):
|
---|
166 | lims=options.getfieldvalue('clim')
|
---|
167 | assert len(lims)==2, 'error, clim should be passed as a list of length 2'
|
---|
168 | elif options.exist('caxis'):
|
---|
169 | lims=options.getfieldvalue('caxis')
|
---|
170 | assert len(lims)==2, 'error, caxis should be passed as a list of length 2'
|
---|
171 | options.addfielddefault('clim',lims)
|
---|
172 | else:
|
---|
173 | if len(data)>0: lims=[data.min(),data.max()]
|
---|
174 | else: lims=[0,1]
|
---|
175 | #}}}
|
---|
176 |
|
---|
177 | #shading
|
---|
178 |
|
---|
179 | #grid {{{
|
---|
180 | if options.exist('grid'):
|
---|
181 | if 'on' in options.getfieldvalue('grid','on'):
|
---|
182 | ax.grid()
|
---|
183 | #}}}
|
---|
184 |
|
---|
185 | #colormap {{{
|
---|
186 | # default sequential colormap
|
---|
187 | defaultmap=truncate_colormap(mpl.cm.gnuplot2,0.1,0.9,128)
|
---|
188 | cmap=options.getfieldvalue('colormap',defaultmap)
|
---|
189 | norm = mpl.colors.Normalize(vmin=lims[0], vmax=lims[1])
|
---|
190 | options.addfield('colornorm',norm)
|
---|
191 | cbar_extend=0
|
---|
192 | if options.exist('cmap_set_over'):
|
---|
193 | over=options.getfieldvalue('cmap_set_over','0.5')
|
---|
194 | cmap.set_over(over)
|
---|
195 | cbar_extend+=1
|
---|
196 | if options.exist('cmap_set_under'):
|
---|
197 | under=options.getfieldvalue('cmap_set_under','0.5')
|
---|
198 | cmap.set_under(under)
|
---|
199 | cbar_extend+=2
|
---|
200 | options.addfield('colormap',cmap)
|
---|
201 | #}}}
|
---|
202 |
|
---|
203 | #contours {{{
|
---|
204 | if options.exist('contourlevels'):
|
---|
205 | plot_contour(md,data,options,ax)
|
---|
206 | #}}}
|
---|
207 |
|
---|
208 | #wrapping
|
---|
209 |
|
---|
210 | #colorbar {{{
|
---|
211 | if options.getfieldvalue('colorbar',1)==1:
|
---|
212 | if cbar_extend==0:
|
---|
213 | extend='neither'
|
---|
214 | elif cbar_extend==1:
|
---|
215 | extend='max'
|
---|
216 | elif cbar_extend==2:
|
---|
217 | extend='min'
|
---|
218 | elif cbar_extend==3:
|
---|
219 | extend='both'
|
---|
220 | cb = mpl.colorbar.ColorbarBase(ax.cax, cmap=cmap, norm=norm, extend=extend)
|
---|
221 | if options.exist('alpha'):
|
---|
222 | cb.set_alpha(options.getfieldvalue('alpha'))
|
---|
223 | if options.exist('colorbarnumticks'):
|
---|
224 | cb.locator=MaxNLocator(nbins=options.getfieldvalue('colorbarnumticks',5))
|
---|
225 | else:
|
---|
226 | cb.locator=MaxNLocator(nbins=5) # default 5 ticks
|
---|
227 | if options.exist('colorbartickspacing'):
|
---|
228 | locs=npy.arange(lims[0],lims[1]+1,options.getfieldvalue('colorbartickspacing'))
|
---|
229 | cb.set_ticks(locs)
|
---|
230 | if options.exist('colorbarlines'):
|
---|
231 | locs=npy.arange(lims[0],lims[1]+1,options.getfieldvalue('colorbarlines'))
|
---|
232 | cb.add_lines(locs,['k' for i in range(len(locs))],npy.ones_like(locs))
|
---|
233 | if options.exist('colorbartitle'):
|
---|
234 | if options.exist('colorbartitlepad'):
|
---|
235 | cb.set_label(options.getfieldvalue('colorbartitle'),\
|
---|
236 | labelpad=options.getfieldvalue('colorbartitlepad'),fontsize=fontsize)
|
---|
237 | else:
|
---|
238 | cb.set_label(options.getfieldvalue('colorbartitle'),fontsize=fontsize)
|
---|
239 | cb.ax.tick_params(labelsize=fontsize)
|
---|
240 | cb.update_ticks()
|
---|
241 | plt.sca(ax) # return to original axes control
|
---|
242 | #}}}
|
---|
243 |
|
---|
244 | #area
|
---|
245 |
|
---|
246 | #expdisp
|
---|
247 |
|
---|
248 | #text {{{
|
---|
249 | if options.exist('text'):
|
---|
250 | text=options.getfieldvalue('text')
|
---|
251 | textx=options.getfieldvalue('textx')
|
---|
252 | texty=options.getfieldvalue('texty')
|
---|
253 | textsize=options.getfieldvalue('textsize')
|
---|
254 | textcolor=options.getfieldvalue('textcolor')
|
---|
255 | textweight=options.getfieldvalue('textweight')
|
---|
256 | textrotation=options.getfieldvalue('textrotation')
|
---|
257 | for label,x,y,size,color,weight,rotation in zip(text,textx,texty,textsize,textcolor,textweight,textrotation):
|
---|
258 | ax.text(x,y,label,transform=ax.transAxes,fontsize=size,color=color,weight=weight,rotation=rotation)
|
---|
259 | #}}}
|
---|
260 |
|
---|
261 | #north arrow
|
---|
262 |
|
---|
263 | #scale ruler
|
---|
264 |
|
---|
265 | #streamlines
|
---|
266 |
|
---|
267 |
|
---|
268 | #axis positions
|
---|
269 |
|
---|
270 | #figure position
|
---|
271 |
|
---|
272 | #axes position
|
---|
273 |
|
---|
274 | #showregion
|
---|
275 |
|
---|
276 | #flat edges of a partition
|
---|
277 |
|
---|
278 | #scatter
|
---|
279 |
|
---|
280 | #backgroundcolor
|
---|
281 |
|
---|
282 | #figurebackgroundcolor
|
---|
283 |
|
---|
284 | #lighting
|
---|
285 |
|
---|
286 | #point cloud
|
---|
287 |
|
---|
288 | #inset
|
---|