Changeset 430
- Timestamp:
- Nov 30, 2011, 5:41:30 PM (13 years ago)
- Location:
- trunk/UTIL/PYTHON
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UTIL/PYTHON/mymath.py
r427 r430 197 197 return idx 198 198 199 def fig2data ( fig ): 200 import numpy 201 """ 202 @brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels and return it 203 @param fig a matplotlib figure 204 @return a numpy 3D array of RGBA values 205 """ 206 # draw the renderer 207 fig.canvas.draw ( ) 208 209 # Get the RGBA buffer from the figure 210 w,h = fig.canvas.get_width_height() 211 buf = numpy.fromstring ( fig.canvas.tostring_argb(), dtype=numpy.uint8 ) 212 buf.shape = ( w, h,4 ) 213 214 # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode 215 buf = numpy.roll ( buf, 3, axis = 2 ) 216 return buf 217 218 def fig2img ( fig ): 219 import Image 220 import numpy 221 """ 222 @brief Convert a Matplotlib figure to a PIL Image in RGBA format and return it 223 @param fig a matplotlib figure 224 @return a Python Imaging Library ( PIL ) image 225 """ 226 # put the figure pixmap into a numpy array 227 buf = fig2data ( fig ) 228 w, h, d = buf.shape 229 return Image.fromstring( "RGBA", ( w ,h ), buf.tostring( ) ) -
trunk/UTIL/PYTHON/myplot.py
r429 r430 374 374 return 375 375 376 ## Author: AS 376 ## Author: AS + AC 377 377 def dumpbdy (field,n,stag=None): 378 378 nx = len(field[0,:])-1 … … 380 380 if stag == 'U': nx = nx-1 381 381 if stag == 'V': ny = ny-1 382 if stag == 'W': nx = nx+1 #special les case when we dump stag on W 382 383 return field[n:ny-n,n:nx-n] 383 384 -
trunk/UTIL/PYTHON/planetoplot.py
r429 r430 50 50 blat=None,\ 51 51 tsat=False,\ 52 flagnolow=False): 52 flagnolow=False,\ 53 movie_axis=0): 53 54 54 55 … … 63 64 zoomset,getcoorddef,getwinddef,whatkindfile,reducefield,bounds,getstralt,getfield,smooth,nolow,\ 64 65 getname,localtime,polarinterv,getsindex,define_axis,determineplot,readslices,bidimfind,getlschar,hole_bounds 65 from mymath import deg,max,min,mean,get_tsat,writeascii 66 from mymath import deg,max,min,mean,get_tsat,writeascii,fig2data,fig2img 66 67 import matplotlib as mpl 67 68 from matplotlib.pyplot import contour,contourf, subplot, figure, rcParams, savefig, colorbar, pcolor, show, plot, clabel, title … … 69 70 import numpy as np 70 71 from numpy.core.defchararray import find 72 from videosink import VideoSink 71 73 72 74 ################################ … … 281 283 if var2 == 'HGT': zelevels = np.arange(-10000.,30000.,2000.) 282 284 if mapmode == 0: 285 if typefile in ['mesoideal']: what_I_plot = dumpbdy(what_I_plot,0,stag='W') 286 itime=indextime 287 if len(what_I_plot.shape) is 3:itime=[0] 283 288 what_I_plot, x, y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\ 284 i ndextime,what_I_plot, len(all_var2[index_f].shape),vertmode)289 itime,what_I_plot, len(all_var2[index_f].shape),vertmode) 285 290 ### If we plot a 2-D field 286 291 if len(what_I_plot.shape) is 2: 292 zelevels=[-10.,-8.,-6.,-4.,-2.,0.,2.,4.,6.,8.,10.] 287 293 cs = contour(x,y,what_I_plot, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5) 288 # clabel(cs,#zelevels[::2],289 #inline=3,290 # fmt='%1.1e',291 #fontsize=7)294 clabel(cs,zelevels, 295 inline=3, 296 fmt='%1.1f', 297 fontsize=7) 292 298 ### If we plot a 1-D field 293 299 elif len(what_I_plot.shape) is 1: … … 308 314 if typefile in ['mesoapi','meso']: what_I_plot = dumpbdy(what_I_plot,6) 309 315 elif mapmode == 0: 316 itime=indextime 317 if len(what_I_plot.shape) is 3:itime=[0] 310 318 what_I_plot, x, y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\ 311 i ndextime,what_I_plot, len(all_var[index_f].shape),vertmode)319 itime,what_I_plot, len(all_var[index_f].shape),vertmode) 312 320 zxmin, zxmax = xaxis ; zymin, zymax = yaxis 313 321 if zxmin is not None: mpl.pyplot.xlim(xmin=zxmin) … … 322 330 elif (fileref is not None) and (index_f is numplot-1): palette = get_cmap(name="RdBu_r") 323 331 else: palette = get_cmap(name=colorb) 324 ##### 2D field 325 if len(what_I_plot.shape) is 2: 326 if hole: what_I_plot = hole_bounds(what_I_plot,zevmin,zevmax) 327 else: what_I_plot = bounds(what_I_plot,zevmin,zevmax) 328 if flagnolow: what_I_plot = nolow(what_I_plot) 329 if not tile: 330 #zelevels = np.linspace(zevmin*(1. + 1.e-7),zevmax*(1. - 1.e-7)) #,num=20) 331 zelevels = np.linspace(zevmin,zevmax,num=ticks) 332 print np.array(x).shape 333 print np.array(y).shape 334 print np.array(what_I_plot).shape 335 336 if mapmode == 1: m.contourf( x, y, what_I_plot, zelevels, cmap = palette) 337 elif mapmode == 0: contourf( x, y, what_I_plot, zelevels, cmap = palette) 338 else: 339 if mapmode == 1: m.pcolor( x, y, what_I_plot, cmap = palette, vmin=zevmin, vmax=zevmax ) 340 elif mapmode == 0: pcolor( x, y, what_I_plot, cmap = palette, vmin=zevmin, vmax=zevmax ) 341 if colorb != 'nobar': 342 if (fileref is not None) and (index_f is numplot-1): daformat = "%.3f" 343 else: daformat = fmtvar(fvar.upper()) 344 colorbar( fraction=0.05,pad=0.03,format=daformat,\ 345 ticks=np.linspace(zevmin,zevmax,num=min([ticks/2+1,20])),extend='neither',spacing='proportional' ) 332 ##### simple 2D field and movies of 2D fields 333 if len(what_I_plot.shape) >= 2: 334 istart=0 335 if indextime is None:iend=len(time)-1 336 else:iend=istart 337 imov=istart 338 339 while imov <= iend: 340 what_I_plot_frame=what_I_plot 341 if len(what_I_plot.shape) is 3: 342 what_I_plot_frame=what_I_plot[imov,:,:] 343 print "-> frame ",imov+1 344 if hole: what_I_plot_frame = hole_bounds(what_I_plot_frame,zevmin,zevmax) 345 else: what_I_plot_frame = bounds(what_I_plot_frame,zevmin,zevmax) 346 if flagnolow: what_I_plot_frame = nolow(what_I_plot_frame) 347 if not tile: 348 #zelevels = np.linspace(zevmin*(1. + 1.e-7),zevmax*(1. - 1.e-7)) #,num=20) 349 zelevels = np.linspace(zevmin,zevmax,num=ticks) 350 if imov is 0: 351 print np.array(x).shape 352 print np.array(y).shape 353 print np.array(what_I_plot_frame).shape 354 355 if mapmode == 1: m.contourf( x, y, what_I_plot_frame, zelevels, cmap = palette) 356 elif mapmode == 0: contourf( x, y, what_I_plot_frame, zelevels, cmap = palette) 357 else: 358 if mapmode == 1: m.pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax ) 359 elif mapmode == 0: pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax ) 360 if colorb != 'nobar': 361 if (fileref is not None) and (index_f is numplot-1): daformat = "%.3f" 362 else: daformat = fmtvar(fvar.upper()) 363 colorbar( fraction=0.05,pad=0.03,format=daformat,\ 364 ticks=np.linspace(zevmin,zevmax,num=min([ticks/2+1,20])),extend='neither',spacing='proportional' ) 365 #mframe=mpl.pyplot.gci().to_rgba(mpl.pyplot.gci().get_array()) 366 #mframe=mpl.pyplot.imshow() 367 #mframe=fig2data(mpl.pyplot.gcf()) 368 mframe=fig2img(mpl.pyplot.gcf()) 369 370 if ((len(what_I_plot.shape) is 3) and (imov is 0)): 371 W,H = mpl.pyplot.gcf().canvas.get_width_height() 372 rate = 10 373 video = VideoSink((H,W), "test", rate=rate, byteorder="rgba") 374 if len(what_I_plot.shape) is 3: 375 video.run(mframe) 376 mpl.pyplot.close() 377 378 imov=imov+1 379 if len(what_I_plot.shape) is 3: video.close 346 380 ##### 1D field 347 381 elif len(what_I_plot.shape) is 1:
Note: See TracChangeset
for help on using the changeset viewer.