source: issm/trunk/src/m/plot/applyoptions.py@ 21341

Last change on this file since 21341 was 21341, checked in by Mathieu Morlighem, 8 years ago

merged trunk-jpl and trunk for revision 21337

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