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

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

merged trunk-jpl and trunk for revision 27033

File size: 10.4 KB
RevLine 
[24313]1from cmaptools import getcolormap
[13771]2try:
[24313]3 import matplotlib as mpl
4 import matplotlib.pyplot as plt
[25836]5 from matplotlib.ticker import MaxNLocator
[13771]6except ImportError:
[25836]7 print("could not import pyplot, matplotlib has not been installed, no plotting capabilities enabled")
8import numpy as np
[13771]9
[25836]10from expdisp import expdisp
11from plot_contour import plot_contour
12from plot_streamlines import plot_streamlines
[13417]13
[25836]14
[24313]15def applyoptions(md, data, options, fig, axgrid, gridindex):
16 '''
17 APPLYOPTIONS - apply options to current plot
[17806]18
[24313]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.
[13417]21
[25836]22 Usage:
23 applyoptions(md, data, options)
[13417]24
[25836]25 See also: PLOTMODEL, PARSE_OPTIONS
[24313]26 '''
[17806]27
[24313]28 # get handle to current figure and axes instance
29 #fig = p.gcf()
30 ax = axgrid[gridindex]
[13417]31
[24313]32 # {{{ font
33 fontsize = options.getfieldvalue('fontsize', 8)
34 fontweight = options.getfieldvalue('fontweight', 'normal')
35 fontfamily = options.getfieldvalue('fontfamily', 'sans - serif')
[25836]36 font = {
37 'fontsize': fontsize,
38 'fontweight': fontweight,
39 'family': fontfamily
40 }
[24313]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()
[27035]55 titlefont['fontsize'] = titlefontsize
56 titlefont['fontweight'] = titlefontweight
[24313]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
[13417]68
[24313]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 = getcolormap(options)
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
208 cb = mpl.colorbar.ColorbarBase(ax.cax, cmap=cmap, norm=norm, extend=extend)
209 if options.exist('alpha'):
210 cb.set_alpha(options.getfieldvalue('alpha'))
211 if options.exist('colorbarnumticks'):
212 cb.locator = MaxNLocator(nbins=options.getfieldvalue('colorbarnumticks', 5))
213 else:
214 cb.locator = MaxNLocator(nbins=5) # default 5 ticks
215 if options.exist('colorbartickspacing'):
216 locs = np.arange(lims[0], lims[1] + 1, options.getfieldvalue('colorbartickspacing'))
217 cb.set_ticks(locs)
218 if options.exist('colorbarlines'):
219 locs = np.arange(lims[0], lims[1] + 1, options.getfieldvalue('colorbarlines'))
220 cb.add_lines(locs, ['k' for i in range(len(locs))], np.ones_like(locs))
221 if options.exist('colorbarlineatvalue'):
222 locs = options.getfieldvalue('colorbarlineatvalue')
223 colors = options.getfieldvalue('colorbarlineatvaluecolor', ['k' for i in range(len(locs))])
224 widths = options.getfieldvalue('colorbarlineatvaluewidth', np.ones_like(locs))
225 cb.add_lines(locs, colors, widths)
226 if options.exist('colorbartitle'):
227 if options.exist('colorbartitlepad'):
228 cb.set_label(options.getfieldvalue('colorbartitle'),
229 labelpad=options.getfieldvalue('colorbartitlepad'),
230 fontsize=fontsize)
231 else:
232 cb.set_label(options.getfieldvalue('colorbartitle'), fontsize=fontsize)
233 cb.ax.tick_params(labelsize=fontsize)
234 cb.solids.set_rasterized(True)
235 cb.update_ticks()
236 cb.draw_all()
237 if options.exist('colorbarfontsize'):
238 colorbarfontsize = options.getfieldvalue('colorbarfontsize')
239 cb.ax.tick_params(labelsize=colorbarfontsize)
240 if options.exist('colorbarticks'):
241 colorbarticks = options.getfieldvalue('colorbarticks')
242 cb.set_ticks(colorbarticks)
243 plt.sca(ax) # return to original axes control
244 # }}}
245 # {{{ expdisp
246 if options.exist('expdisp'):
247 expdisp(ax, options)
248 # }}}
249 # {{{ area TODO
250 # }}}
251 # {{{ text
252 if options.exist('text'):
253 text = options.getfieldvalue('text')
254 textx = options.getfieldvalue('textx')
255 texty = options.getfieldvalue('texty')
256 textcolor = options.getfieldvalue('textcolor')
257 textweight = options.getfieldvalue('textweight')
258 textrotation = options.getfieldvalue('textrotation')
259 textfontsize = options.getfieldvalue('textfontsize')
260 for label, x, y, size, color, weight, rotation in zip(text, textx, texty, textfontsize, textcolor, textweight, textrotation):
261 ax.text(x, y, label, transform=ax.transAxes, fontsize=size, color=color, weight=weight, rotation=rotation)
262 # }}}
263 # {{{ north arrow TODO
264 # }}}
265 # {{{ scale ruler TODO
266 # }}}
267 # {{{ streamlines TOFIX
268 if options.exist('streamlines'):
269 plot_streamlines(md, options, ax)
270 # }}}
271 # {{{ axis positions TODO
272 # }}}
273 # {{{ figure position TODO
274 # }}}
275 # {{{ axes position TODO
276 # }}}
277 # {{{ showregion TODO
278 # }}}
279 # {{{ flat edges of a partition TODO
280 # }}}
281 # {{{ scatter TODO
282 # }}}
283 # {{{ backgroundcolor TODO
284 # }}}
285 # {{{ figurebackgroundcolor TODO
286 # }}}
287 # {{{ lighting TODO
288 # }}}
289 # {{{ point cloud TODO
290 # }}}
291 # {{{ inset TODO
292 # }}}
Note: See TracBrowser for help on using the repository browser.