Changeset 2773 in lmdz_wrf for trunk/tools
- Timestamp:
- Nov 19, 2019, 12:38:33 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/drawing_tools.py
r2760 r2773 15101 15101 return 15102 15102 15103 def shad_simple(vv, vn, vu, xv, yv, vmin, vmax, parameters): 15104 """ Function to plot using pcolormesh 15105 vv: variable values 15106 xv, yv: x,y coordinates 15107 vmin, vmax: minimum and maximum rang of the plot 15108 parameters = [colorbarn, pos, orientation, fmt] 15109 colorbarn: ('auto', 'Reds') 15110 pos: position color bar ('auto', 0.85) 15111 orientation: position color bar ('auto', 'vertical') 15112 fmt: format of cbticks ('auto', '%6g') 15113 """ 15114 fname = 'shad_simple' 15115 15116 if type(parameters[0]) == type('S'): 15117 colorbarn = gen.auto_val(parameters[0], 'Reds') 15118 cmapv = plt.get_cmap(colorbarn) 15119 else: 15120 cmapv = parameters[0] 15121 15122 splot = plt.pcolormesh(xv, yv, vv, cmap=cmapv, vmin=vmin, \ 15123 vmax=vmax) 15124 orien = gen.auto_val(parameters[2], 'vertical') 15125 cbtkf = gen.auto_val(parameters[3], '%6g') 15126 15127 if orien == 'horizontal': 15128 cbar = plt.colorbar(format=cbtkf, orientation=orien) 15129 # From: http://stackoverflow.com/questions/32050030/rotation-of-colorbar-tick-labels-in-matplotlib 15130 ticklabels= cbar.ax.get_xticklabels() 15131 Nticks = len(ticklabels) 15132 ticklabs = [] 15133 for itick in range(Nticks): ticklabs.append(ticklabels[itick].get_text()) 15134 cbar.ax.set_xticklabels(ticklabs,rotation=90) 15135 else: 15136 cbar = plt.colorbar(format=cbtkf, orientation=orien) 15137 15138 cbar.set_label(gen.latex_text(extrapar[0]) +'('+ gen.units_lunits(extrapar[6]) + \ 15139 ')') 15140 15141 return splot 15142 15143 def cnt_simple(vv, xv, yv, vmin, vmax, contkind, parameters): 15144 """ Function to plot using contour 15145 vv: variables 15146 xv, yv: x,y coordinates 15147 vmin, vmax: minimum and maximum rang of the plot 15148 cntkind: type of contour plot 15149 'cmap': as it gets from colorbar 15150 'fixc': fixed color [colname], all stright lines 15151 'fixsigc': fixed color [colname], >0 stright, <0 dashed line 15152 parameters = [colorbarn/colname, freqv, fmtlabs] 15153 colorbarn: ('auto', 'Reds') 15154 colname: name of the color ('auto', '#FF0000') 15155 freqv: frequency of the lines ('auto', pretty(minxv, maxv, 10)) 15156 fmtlabs: format of the labels in the contour (None, also possible) ('auto', 15157 '%d') 15158 """ 15159 fname = 'cnt_simple' 15160 15161 availcontkind = ['cmap', 'fixc', 'fixsigc'] 15162 15163 clabfmt = gen.auto_val(parameters[2], '%d') 15164 if clabfmt == 'None': clabmt = None 15165 15166 if freqv == 'auto': 15167 vc = pretty(vmin, vmax, 10) 15168 else: 15169 vc = np.arange(vmin, vmax+freqv, freqv) 15170 15171 contkind = ckind.split(',')[0] 15172 if contkind == 'cmap': 15173 colorval = gen.auto_val(parameters[0], 'Reds') 15174 cplot = plt.contour(xv, yv, vv, levels=vc, cmap=plt.get_cmap(colorval)) 15175 elif contkind == 'fixc': 15176 colorval = gen.auto_val(parameters[0], '#FF0000') 15177 plt.rcParams['contour.negative_linestyle'] = 'solid' 15178 coln = parameters[0] 15179 cplot = plt.contour(xv, yv, vv, levels=vc, colors=colorval) 15180 elif contkind == 'fixsigc': 15181 colorval = gen.auto_val(parameters[0], '#FF0000') 15182 cplot = plt.contour(xv, yv, vv, levels=vc, colors=colorval) 15183 else: 15184 print errormsg 15185 print ' ' + fname + ': contour kind "' + contkind + '" not defined !!!!!' 15186 print ' available ones'. availcontkind 15187 quit(-1) 15188 15189 if clabfmt is not None: 15190 plt.clabel(cplot, fmt=clabfmt) 15191 mincntS = format(vc[0], clabfmt[1:len(clabfmt)]) 15192 maxcntS = format(vc[len(vc)-1], clabfmt[1:len(clabfmt)]) 15193 else: 15194 mincntS = '{:g}'.format(vc[0]) 15195 maxcntS = '{:g}'.format(vc[len(vc)-1]) 15196 15197 return cplot 15198 15199 def plot_multivar_multikind(infilen, figv, figtitle, mapv=None, legvals=[0,8,1], \ 15200 mervals=[8,'#AAAAAA',0,0.5], parvals=[8,'#AAAAAA',0,0.5], \ 15201 coastlvals=[0.25,'#AAAAAA'], countrylvals=[0.25,'#161616'], \ 15202 stslvals=[0.25,'#080808'], close=True): 15203 """ Function to plot multiple variables in one figure 15204 infilen: name of the file with data 15205 mapv= [m,meridians,parallels,x,y]: map environtment (from basemap) [None for 15206 no map] 15207 figv = {'lvars': ['vari', ...], 'vari': [valsi,drawtype,minv,maxv,dtypev]}: 15208 dictionary with the information of the variables 15209 'lvars': key with a sorted list of the variables to plot 15210 vari: a sort of name to be used in `variables_values' 15211 valsi: matrix values to plot 15212 drawtype: type of figure to draw with this variable 15213 'shad': shadow 15214 'cnt_fixsigc': contour single color changing line-type in pos/neg values 15215 minv, maxv: minimum and maximum range for the variable (outside masked) 15216 dtypev: values for drawtype 15217 'shad': [colorbarn, pos, orientation] 15218 colorbarn: ('auto', 'Reds') 15219 pos: position color bar ('auto', 0.85) 15220 orientation: position color bar ('auto', 'vertical') 15221 'cnt_cmap': as it gets from [colorbar, freqv, cfmt] 15222 colorbar: color bar ('auto', 'Reds') 15223 freqv: frequency of lines ('auto', pretty(minxv, maxv, 10)) 15224 'cnt_fixc': fixed color [colorline, freqv, cfmt], all stright lines 15225 colorline: color of the lines ('auto', 'k') 15226 freqv: frequency of lines ('auto', pretty(minxv, maxv, 10)) 15227 'cnt_fixsigc': [colorline, freqv, cfmt], fixed color [colname], >0 stright, 15228 <0 dashed line 15229 colorline: color of the lines ('auto', 'k') 15230 freqv: frequency of lines ('auto', pretty(minxv, maxv, 10)) 15231 figtitle: title of the figure 15232 mervals = values for the meridians [fontsize, color line, labels rotation, 15233 line width] ([8,'#AAAAAA', 0, 0.5], default) 15234 parvals = values for the parallels [fontsize, color line, labels rotation, 15235 line width] ([8,'#AAAAAA', 0, 0.5], default) 15236 coastlvals = values for the coastline [line width, color line] None for any 15237 ([0.25,'#161616'], default) 15238 countrylvals = values for the countries [line width, color line] None for any 15239 ([0.25, '#161616'], default) 15240 ststlvals = values for the states [line width, color line] None for any (None, 15241 default) 15242 """ 15243 fname = 'plot_multivar_multikind' 15244 15245 availfigt = ['shad', 'cnt_cmap', 'cnt_fixc', 'cnt_fixsigc'] 15246 15247 # Preparing environtment 15248 lvars = figv['lvars'] 15249 var1 = lvars[0] 15250 dvar1 = figv[var1] 15251 var1v = dvar1[0] 15252 dx = var1v.shape[1] 15253 dy = var1v.shape[0] 15254 15255 if mapv is not None: 15256 m = mapv[0] 15257 meridians = mapv[1] 15258 parallels = mapv[2] 15259 x = mapv[3] 15260 y = mapv[4] 15261 else: 15262 xv = np.arange(dx) 15263 yv = np.arange(dy) 15264 x, y = np.meshgrid(xv, yv) 15265 15266 for varn in lvars: 15267 dvari = figv[varn] 15268 valsi = dvari[0] 15269 figt = dvari[1] 15270 vn = dvari[2] 15271 vx = dvari[3] 15272 parfigt = dvari[4] 15273 15274 if figt == 'cnt_cmap': 15275 figk = 'cmap' 15276 ifp = cont_simple(valsi, x, y, vn, vx, figk, parfigt) 15277 elif figt == 'cnt_fixc': 15278 figk = 'fixc' 15279 ifp = cont_simple(valsi, x, y, vn, vx, figk, parfigt) 15280 elif figt == 'cnt_fixsigc': 15281 figk = 'fixsigc' 15282 ifp = cont_simple(valsi, x, y, vn, vx, figk, parfigt) 15283 elif figt == 'shad': 15284 ifp = shad_simple(valsi, x, y, vn, vx, parfigt) 15285 else: 15286 print errormsg 15287 print ' ' + fname + ": type of figure '" + figt + "' not ready !!" 15288 print ' available ones:', availfigt 15289 quit(-1) 15290 15291 if mapv is not None: 15292 15293 if coastlvals is not None: 15294 m.drawcoastlines(linewidth=coastlvals[0], color=coastlvals[1]) 15295 if countrylvals is not None: 15296 m.drawcountries(linewidth=countrylvals[0], color=countrylvals[1]) 15297 if stslvals is not None: 15298 m.drawstates(linewidth=stslvals[0], color=stslvals[1]) 15299 15300 m.drawmeridians(meridians, labels=[True,False,False,True], \ 15301 fontsize=mervals[0], color=mervals[1], rotation=mervals[2], \ 15302 linewidth=mervals[3]) 15303 m.drawparallels(parallels, labels=[True,False,False,True], \ 15304 fontsize=parvals[0], color=parvals[1], rotation=parvals[2], \ 15305 linewidth=parvals[3]) 15306 15307 plt.title(figtitle) 15308 15309 output_kind(kfig, fign, close) 15310
Note: See TracChangeset
for help on using the changeset viewer.