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