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

Last change on this file since 27232 was 27232, checked in by Mathieu Morlighem, 3 years ago

merged trunk-jpl and trunk for revision 27230

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