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

Last change on this file since 24686 was 24686, checked in by Mathieu Morlighem, 5 years ago

merged trunk-jpl and trunk for revision 24684

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