Changeset 1539 in lmdz_wrf
- Timestamp:
- Apr 25, 2017, 9:27:35 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/drawing_tools.py
r1534 r1539 84 84 # plot_2lines: Function to plot two lines in different axes (x/x2 or y/y2) 85 85 # plot_2lines_time: Function to plot two time-lines in different axes (x/x2 or y/y2) 86 # plot_bars: Function to plot a bar plot with multiple columns 86 87 # plot_lines: Function to plot a collection of lines 87 88 # plot_Taylor: Function to draw a Taylor diagram (Taylor 2001) … … 1896 1897 elif u == 'mmh-3': lu='$mmh^{-3}$' 1897 1898 elif u == 'mm/d': lu='$mmd^{-1}$' 1899 elif u == 'mmd-1': lu='$mmd^{-1}$' 1898 1900 elif u == 'ms-1': lu='$ms^{-1}$' 1899 1901 elif u == 'm s-1': lu='$ms^{-1}$' … … 2787 2789 xs: style of x-axis ('auto' for 'pretty') 2788 2790 'Nfix', values computed at even 'Ndx' 2791 'Stringlist', a given list of strings 2789 2792 'Vfix', values computed at even 'Ndx' increments 2790 2793 'pretty', values computed following aprox. 'Ndx' at 'pretty' intervals (2.,2.5,4,5,10) 2791 2794 xf: format of the ticks labels at the x-axis ('auto' for '5g') 2792 Nx: number of ticks labels at the x-axis ('auto' for 5 )2795 Nx: number of ticks labels at the x-axis ('auto' for 5, '@' list of values for 'Stringlist' '!' for spaces) 2793 2796 orx: angle of orientation of ticks at the x-axis ('auto' for horizontal) 2794 2797 ys: style of y-axis ('auto' for 'pretty') … … 2822 2825 else: 2823 2826 if Sv[0] == 'pretty' or Sv[0] == 'Nfix': Nx = int(Sv[2]) 2827 elif Sv[0] == 'Stringlist': 2828 Nx0 = Sv[2].replace('!', ' ').split('@') 2829 Nx = [] 2830 for Nxn in Nx0: Nx.append(gen.latex_text(Nxn)) 2824 2831 else: Nx = np.float(Sv[2]) 2825 2832 if Sv[3] == 'auto': orx = 0. … … 2832 2839 else: 2833 2840 if Sv[4] == 'pretty' or Sv[4] == 'Nfix': Ny = int(Sv[6]) 2841 elif Sv[4] == 'Stringlist': 2842 Ny0 = Sv[6].replace('!', ' ').split('@') 2843 Ny = [] 2844 for Nyn in Ny0: Ny.append(gen.latex_text(Nyn)) 2834 2845 else: Ny = np.float(Sv[6]) 2835 2846 if Sv[7] == 'auto': ory = 0. … … 9121 9132 9122 9133 output_kind(kfig, figname, ifclose) 9123 9134 9124 9135 return 9125 9136 9137 def plot_bars(xvalues, yvalues, xaxv, yaxv, dimxu, dimyu, valaxis, xlabel, ylabel, \ 9138 labels, cols, width, gtit, lloc, lsiz, kfig, figname, ifclose): 9139 """ Function to plot a bar plot with multiple columns 9140 xvalues= list with the values along the x-ais 9141 yvalues= list with the values along the y-ais 9142 [x/y]axv= values at the axes of x and y 9143 dim[x/y]u= units at the axes of x and y 9144 valaxis= which axis holds the values 'x' or 'y' 9145 xlabel= label at the x-axis 9146 ylabel= label at the y-axis 9147 labels= list of labels for legend (None for no legend) 9148 cols= list of colors for the bars (single value for the same, 'auto' for automatic) 9149 width= size of the column 9150 gtit= title of the figure 9151 lloc= location of the legend (0, autmoatic) 9152 1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right', 9153 5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center', 9154 9: 'upper center', 10: 'center' 9155 lsiz= font size of the legend 9156 kfig= kind of output: pdf, png, ps 9157 figname= name of the figure 9158 ifclose= whether figure should be closed 9159 """ 9160 fname = 'plot_bars' 9161 9162 Nvalues = len(xvalues) 9163 9164 # Maximum number of values: 9165 Lmaxvals = -10 9166 xticks = xvalues[0] 9167 for iv in range(Nvalues): 9168 if len(xvalues[iv]) > Lmaxvals: Lmaxvals = len(xvalues[iv]) 9169 NOTcoinc = list(set(xvalues[iv]).difference(set(xticks))) 9170 if len(NOTcoinc) != 0: 9171 xticks = xticks + NOTcoinc 9172 xticks.sort 9173 9174 dxn = np.min(xvalues) 9175 dxx = np.max(xvalues) 9176 dyn = np.min(yvalues) 9177 dyx = np.max(yvalues) 9178 9179 axstyles = ['pretty', 'Nfix', 'Stringlist', 'Vfix'] 9180 if xaxv[0] == 'pretty': 9181 dimxt = np.array(gen.pretty_int(dxn,dxx,xaxv[2])) 9182 dimxl = np.array(dimxt, dtype=str) 9183 elif xaxv[0] == 'Nfix': 9184 dimxl = np.arange(0.,1.,1./(xaxv[2]+1)) 9185 dimxt = np.arange(0.,1.,1./varsv.shape[1]) 9186 elif xaxv[0] == 'Stringlist': 9187 if len(xaxv[2]) != len(xticks): 9188 print warnmsg 9189 print ' ' + fname + ': number of ticks to plot=', len(xticks), \ 9190 'and number of labels provided', len(xaxv[2]), 'differ !!' 9191 print ' ' + fname + ': ticks to plot=', xticks, 'and provided labels', \ 9192 xaxv[2], 'differ !!' 9193 9194 dimxl = xaxv[2] 9195 dimxt = xticks 9196 elif xaxv[0] == 'Vfix': 9197 dxnvfix = int(dxn/xaxv[2]) 9198 dimxt = np.arange(dxnvfix*xaxv[2],dxx,xaxv[2]) 9199 else: 9200 print errormsg 9201 print ' ' + fname + ": x-xais style '" + xaxv[0] + "' not ready !!" 9202 print ' available ones: ', axstyles 9203 quit(-1) 9204 9205 if yaxv[0] == 'pretty': 9206 dimyt = np.array(gen.pretty_int(dyn,dyx,yaxv[2])) 9207 dimyl = np.array(dimyt, dtype=str) 9208 elif yaxv[0] == 'Nfix': 9209 dimyt = np.arange(0.,1.,1./(yaxv[2]+1)) 9210 dimyl = np.arange(0.,1.,1./varsv.shape[0]) 9211 axytype='fixpixel' 9212 elif yaxv[0] == 'Vfix': 9213 dynvfix = int(dyn/yaxv[2]) 9214 dimyt = np.arange(dynvfix*xaxv[2],dyx,yaxv[2]) 9215 axytype='data' 9216 else: 9217 print errormsg 9218 print ' ' + fname + ": x-xais style '" + xaxv[0] + "' not ready !!" 9219 print ' available ones: ', axstyles 9220 quit(-1) 9221 9222 if valaxis == 'x': 9223 orient = 'horizontal' 9224 else: 9225 orient = 'vertical' 9226 9227 colvs = [] 9228 if len(cols) == 1: 9229 if cols[0] == 'auto': 9230 colvs = colorsauto[0:Nvalues+1] 9231 else: 9232 for ic in range(Nvalues): colvs.append(cols[0]) 9233 else: 9234 colvs = cols 9235 9236 for iv in range(Nvalues): 9237 xvls = xvalues[iv] 9238 Lvalues = len(xvls) 9239 dxmean = np.mean(np.array(xvls[1:Lvalues])-np.array(xvls[0:Lvalues-1])) 9240 if dxmean < width*Nvalues: 9241 print warnmsg 9242 print ' ' + fname + ': mean distance among x-values:', dxmean, \ 9243 'is smaller than the total number of bars with the given witdh (', \ 9244 width, '):', width*Nvalues 9245 print ' reduce widht of the column, should not be larger than:', \ 9246 dxmean/Nvalues 9247 if width*Nvalues < 0.01*dxmean: 9248 print warnmsg 9249 print ' ' + fname + ': mean distance among x-values:', dxmean, \ 9250 'is 100-times larger than the total number of bars with the given ' + \ 9251 'witdh (', width, '):', width*Nvalues 9252 print ' increase widht of the column, should not be smaller than:', \ 9253 0.01*dxmean 9254 9255 # Relative position of the column respect the xtick 9256 if np.mod(Nvalues,2) == 0: 9257 xrel = width*(iv - Nvalues/2.) 9258 else: 9259 xrel = width*(iv - (Nvalues-1)/2.) 9260 9261 xrels = xrel*np.ones((Lvalues),dtype=np.float) 9262 9263 if valaxis == 'x': 9264 if labels is not None: 9265 plt.barh(xvls+xrels, yvalues[iv], height=width, color=colvs[iv], \ 9266 label=labels[iv]) 9267 else: 9268 plt.barh(xvls+xrels, yvalues[iv], height=width, color=colvs[iv]) 9269 else: 9270 if labels is not None: 9271 plt.bar(xvls+xrels, yvalues[iv], width=width, color=colvs[iv], \ 9272 label=labels[iv]) 9273 else: 9274 plt.bar(xvls+xrels, yvalues[iv], width=width, color=colvs[iv]) 9275 9276 if valaxis == 'x': 9277 plt.xticks(dimyt, dimyl, rotation=yaxv[3]) 9278 plt.yticks(dimxt, dimxl, rotation=xaxv[3]) 9279 plt.xlabel(ylabel + ' (' + units_lunits(dimyu) + ')') 9280 if dimxu is not None: 9281 plt.ylabel(xlabel + ' (' + units_lunits(dimxu) + ')') 9282 else: 9283 plt.ylabel(xlabel) 9284 else: 9285 plt.xticks(dimxt, list(dimxl), rotation=xaxv[3]) 9286 plt.yticks(dimyt, list(dimyl), rotation=yaxv[3]) 9287 if dimxu is not None: 9288 plt.xlabel(xlabel + ' (' + units_lunits(dimxu) + ')') 9289 else: 9290 plt.xlabel(xlabel) 9291 plt.ylabel(ylabel + ' (' + units_lunits(dimyu) + ')') 9292 9293 if labels is not None: 9294 plt.legend(loc=lloc, prop={'size':lsiz}) 9295 9296 plt.title(gen.latex_text(gtit)) 9297 9298 output_kind(kfig, figname, ifclose) 9299 9300 return 9301
Note: See TracChangeset
for help on using the changeset viewer.