1 | import numpy as np
|
---|
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={'fontsize' :fontsize,
|
---|
39 | 'fontweight' :fontweight,
|
---|
40 | 'family' :fontfamily}
|
---|
41 | # }}}
|
---|
42 | # {{{ title
|
---|
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
|
---|
57 | ax.set_title(title,**titlefont)
|
---|
58 | # }}}
|
---|
59 | # {{{ xlabel, ylabel, zlabel
|
---|
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'):
|
---|
75 | ax.set_xlabel(options.getfieldvalue('xlabel'),**labelfont)
|
---|
76 | if options.exist('ylabel'):
|
---|
77 | ax.set_ylabel(options.getfieldvalue('ylabel'),**labelfont)
|
---|
78 | if options.exist('zlabel'):
|
---|
79 | ax.set_zlabel(options.getfieldvalue('zlabel'),**labelfont)
|
---|
80 | # }}}
|
---|
81 | # {{{ xticks, yticks, zticks (tick locations)
|
---|
82 | if options.exist('xticks'):
|
---|
83 | if options.exist('xticklabels'):
|
---|
84 | xticklabels=options.getfieldvalue('xticklabels')
|
---|
85 | ax.set_xticks(options.getfieldvalue('xticks'),xticklabels)
|
---|
86 | else:
|
---|
87 | ax.set_xticks(options.getfieldvalue('xticks'))
|
---|
88 | if options.exist('yticks'):
|
---|
89 | if options.exist('yticklabels'):
|
---|
90 | yticklabels=options.getfieldvalue('yticklabels')
|
---|
91 | ax.set_yticks(options.getfieldvalue('yticks'),yticklabels)
|
---|
92 | else:
|
---|
93 | ax.set_yticks(options.getfieldvalue('yticks'))
|
---|
94 | if options.exist('zticks'):
|
---|
95 | if options.exist('zticklabels'):
|
---|
96 | zticklabels=options.getfieldvalue('zticklabels')
|
---|
97 | ax.set_zticks(options.getfieldvalue('zticks'),zticklabels)
|
---|
98 | else:
|
---|
99 | ax.set_zticks(options.getfieldvalue('zticks'))
|
---|
100 | # }}}
|
---|
101 | # {{{ xticklabels,yticklabels,zticklabels
|
---|
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)
|
---|
106 | if options.exist('xticklabels'):
|
---|
107 | xticklabels=options.getfieldvalue('xticklabels')
|
---|
108 | ax.set_xticklabels(xticklabels)
|
---|
109 | if options.exist('yticklabels'):
|
---|
110 | yticklabels=options.getfieldvalue('yticklabels')
|
---|
111 | ax.set_yticklabels(yticklabels)
|
---|
112 | if options.exist('zticklabels'):
|
---|
113 | zticklabels=options.getfieldvalue('zticklabels')
|
---|
114 | ax.set_zticklabels(zticklabels)
|
---|
115 | # }}}
|
---|
116 | # {{{ ticklabel notation
|
---|
117 | #ax.ticklabel_format(style='sci',scilimits=(0,0))
|
---|
118 | # }}}
|
---|
119 | # {{{ ticklabelfontsize
|
---|
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'))
|
---|
126 | # }}}
|
---|
127 | # {{{ view TOFIX
|
---|
128 | #if int(md.mesh.dimension) == 3 and options.exist('layer'):
|
---|
129 | # #options.getfieldvalue('view') ?
|
---|
130 | # ax=fig.gca(projection='3d')
|
---|
131 | #plt.show()
|
---|
132 | # }}}
|
---|
133 | # {{{ axis
|
---|
134 | if options.exist('axis'):
|
---|
135 | if options.getfieldvalue('axis',True)=='off':
|
---|
136 | ax.ticklabel_format(style='plain')
|
---|
137 | p.setp(ax.get_xticklabels(), visible=False)
|
---|
138 | p.setp(ax.get_yticklabels(), visible=False)
|
---|
139 | # }}}
|
---|
140 | # {{{ box
|
---|
141 | if options.exist('box'):
|
---|
142 | eval(options.getfieldvalue('box'))
|
---|
143 | # }}}
|
---|
144 | # {{{ xlim, ylim, zlim
|
---|
145 | if options.exist('xlim'):
|
---|
146 | ax.set_xlim(options.getfieldvalue('xlim'))
|
---|
147 | if options.exist('ylim'):
|
---|
148 | ax.set_ylim(options.getfieldvalue('ylim'))
|
---|
149 | if options.exist('zlim'):
|
---|
150 | ax.set_zlim(options.getfieldvalue('zlim'))
|
---|
151 | # }}}
|
---|
152 | # {{{ latlon TODO
|
---|
153 | # }}}
|
---|
154 | # {{{ Basinzoom TODO
|
---|
155 | # }}}
|
---|
156 | # {{{ ShowBasins TODO
|
---|
157 | # }}}
|
---|
158 | # {{{ clim
|
---|
159 | if options.exist('clim'):
|
---|
160 | lims=options.getfieldvalue('clim')
|
---|
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:
|
---|
167 | if len(data)>0:
|
---|
168 | lims=[data.min(),data.max()]
|
---|
169 | else:
|
---|
170 | lims=[0,1]
|
---|
171 | # }}}
|
---|
172 | # {{{ shading TODO
|
---|
173 | #if options.exist('shading'):
|
---|
174 | # }}}
|
---|
175 | # {{{ grid
|
---|
176 | if options.exist('grid'):
|
---|
177 | if 'on' in options.getfieldvalue('grid','on'):
|
---|
178 | ax.grid()
|
---|
179 | # }}}
|
---|
180 | # {{{ colormap
|
---|
181 | if options.exist('colornorm'):
|
---|
182 | norm=options.getfieldvalue('colornorm')
|
---|
183 | if options.exist('colormap'):
|
---|
184 | cmap=options.getfieldvalue('colormap')
|
---|
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
|
---|
190 | # }}}
|
---|
191 | # {{{ contours
|
---|
192 | if options.exist('contourlevels'):
|
---|
193 | plot_contour(md,data,options,ax)
|
---|
194 | # }}}
|
---|
195 | # {{{ wrapping TODO
|
---|
196 | # }}}
|
---|
197 | # {{{ colorbar
|
---|
198 | if options.getfieldvalue('colorbar',1)==1:
|
---|
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
|
---|
245 | if options.exist('text'):
|
---|
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 |
|
---|