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