Changeset 17778
- Timestamp:
- 04/18/14 17:10:05 (11 years ago)
- Location:
- issm/trunk-jpl/src/m/plot
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/plot/applyoptions.py
r17756 r17778 8 8 import matplotlib as mpl 9 9 import pylab as p 10 import matplotlib.pyplot as plt 10 11 except ImportError: 11 12 print "could not import pylab, matplotlib has not been installed, no plotting capabilities enabled" 12 13 13 def applyoptions(md,data,options ):14 def applyoptions(md,data,options,fig,ax): 14 15 ''' 15 16 APPLYOPTIONS - apply options to current plot 17 18 'plotobj' is the object returned by the specific plot call used to 19 render the data. This object is used for adding a colorbar. 16 20 17 21 Usage: … … 26 30 27 31 # get handle to current figure and axes instance 28 fig = p.gcf()29 ax=p.gca()32 #fig = p.gcf() 33 #ax=p.gca() 30 34 31 35 #font {{{ 32 fontsize=options.getfieldvalue('fontsize', 14)36 fontsize=options.getfieldvalue('fontsize',8) 33 37 fontweight=options.getfieldvalue('fontweight','normal') 34 38 fontfamily=options.getfieldvalue('fontfamily','sans-serif') … … 55 59 titlefont['size']=titlefontsize 56 60 titlefont['weight']=titlefontweight 57 p.title(title,**titlefont)61 fig.set_title(title,**titlefont) 58 62 #}}} 59 63 … … 74 78 75 79 if options.exist('xlabel'): 76 p.xlabel(options.getfieldvalue('xlabel'),**labelfont)80 ax.set_xlabel(options.getfieldvalue('xlabel'),**labelfont) 77 81 if options.exist('ylabel'): 78 p.ylabel(options.getfieldvalue('ylabel'),**labelfont)82 ax.set_ylabel(options.getfieldvalue('ylabel'),**labelfont) 79 83 if options.exist('zlabel'): 80 p.zlabel(options.getfieldvalue('zlabel'),**labelfont)84 ax.set_zlabel(options.getfieldvalue('zlabel'),**labelfont) 81 85 #}}} 82 86 … … 85 89 if options.exist('xticklabels'): 86 90 xticklabels=options.getfieldvalue('xticklabels') 87 p.xticks(options.getfieldvalue('xticks'),xticklabels)88 else: 89 p.xticks(options.getfieldvalue('xticks'))91 ax.set_xticks(options.getfieldvalue('xticks'),xticklabels) 92 else: 93 ax.set_xticks(options.getfieldvalue('xticks')) 90 94 if options.exist('yticks'): 91 95 if options.exist('yticklabels'): 92 96 yticklabels=options.getfieldvalue('yticklabels') 93 p.yticks(options.getfieldvalue('yticks'),yticklabels)94 else: 95 p.yticks(options.getfieldvalue('yticks'))97 ax.set_yticks(options.getfieldvalue('yticks'),yticklabels) 98 else: 99 ax.set_yticks(options.getfieldvalue('yticks')) 96 100 if options.exist('zticks'): 97 101 if options.exist('zticklabels'): 98 102 zticklabels=options.getfieldvalue('zticklabels') 99 p.zticks(options.getfieldvalue('zticks'),zticklabels)100 else: 101 p.zticks(options.getfieldvalue('zticks'))103 ax.set_zticks(options.getfieldvalue('zticks'),zticklabels) 104 else: 105 ax.set_zticks(options.getfieldvalue('zticks')) 102 106 #}}} 103 107 … … 106 110 xticklabels=options.getfieldvalue('xticklabels') 107 111 xtickloc=p.xticks()[0] 108 p.xticks(xtickloc,xticklabels)112 ax.set_xticks(xtickloc,xticklabels) 109 113 if options.exist('yticklabels'): 110 114 yticklabels=options.getfieldvalue('yticklabels') 111 115 ytickloc=p.yticks()[0] 112 p.yticks(ytickloc,yticklabels)116 ax.set_yticks(ytickloc,yticklabels) 113 117 if options.exist('zticklabels'): 114 118 zticklabels=options.getfieldvalue('zticklabels') 115 119 ztickloc=p.zticks()[0] 116 p.zticks(ztickloc,zticklabels)120 ax.set_zticks(ztickloc,zticklabels) 117 121 #}}} 118 122 … … 144 148 #xlim, ylim, zlim {{{ 145 149 if options.exist('xlim'): 146 p.xlim(options.getfieldvalue('xlim'))150 ax.set_xlim(options.getfieldvalue('xlim')) 147 151 if options.exist('ylim'): 148 p.ylim(options.getfieldvalue('ylim'))152 ax.set_ylim(options.getfieldvalue('ylim')) 149 153 if options.exist('zlim'): 150 p.zlim(options.getfieldvalue('zlim'))154 ax.set_zlim(options.getfieldvalue('zlim')) 151 155 #}}} 152 156 … … 174 178 if options.exist('grid'): 175 179 if 'on' in options.getfieldvalue('grid','on'): 176 p.grid()180 ax.grid() 177 181 #}}} 178 182 … … 195 199 #}}} 196 200 201 #contours {{{ 202 if options.exist('contourlevels'): 203 plot_contour(md,data,options,ax) 204 #}}} 205 197 206 #wrapping 198 207 199 208 #colorbar {{{ 200 209 if options.getfieldvalue('colorbar',1)==1: 201 divider = make_axes_locatable(ax)202 cax = divider.new_horizontal("5%", pad=0.05, axes_class=mpl.axes.Axes)203 fig.add_axes(cax)204 210 if cbar_extend==0: 205 211 extend='neither' … … 210 216 elif cbar_extend==3: 211 217 extend='both' 212 cb = mpl.colorbar.ColorbarBase( cax, cmap=cmap, norm=norm, extend=extend)218 cb = mpl.colorbar.ColorbarBase(ax.cax, cmap=cmap, norm=norm, extend=extend) 213 219 cb.locator=MaxNLocator(nbins=5) # default 5 ticks 214 cb.update_ticks() 220 if options.exist('alpha'): 221 cb.set_alpha(options.getfieldvalue('alpha')) 215 222 if options.exist('colorbarnumticks'): 216 223 cb.locator=MaxNLocator(nbins=options.getfieldvalue('colorbarnumticks',5)) 217 cb.update_ticks() 224 if options.exist('colorbartickspacing'): 225 locs=npy.arange(lims[0],lims[1]+1,options.getfieldvalue('colorbartickspacing')) 226 cb.set_ticks(locs) 227 if options.exist('colorbarlines'): 228 locs=npy.arange(lims[0],lims[1]+1,options.getfieldvalue('colorbarlines')) 229 cb.add_lines(locs,['k' for i in range(len(locs))],npy.ones_like(locs)) 230 if options.exist('colorbartitle'): 231 cb.set_label(options.getfieldvalue('colorbartitle'),fontsize=fontsize) 232 cb.ax.tick_params(labelsize=fontsize) 218 233 #}}} 219 234 … … 230 245 #streamlines 231 246 232 #contours {{{233 if options.exist('contourlevels'):234 plot_contour(md,data,options)235 #}}}236 247 237 248 #axis positions -
issm/trunk-jpl/src/m/plot/plot_contour.py
r17756 r17778 4 4 from processdata import processdata 5 5 6 def plot_contour(md,datain,options ):6 def plot_contour(md,datain,options,ax): 7 7 ''' 8 8 plot contours of a given field (called within plotmodel) … … 16 16 x,y,z,elements,is2d,isplanet=processmesh(md,datain,options) 17 17 data,datatype=processdata(md,datain,options) 18 ax=plt.gca()19 18 20 19 # process data: must be on nodes -
issm/trunk-jpl/src/m/plot/plot_manager.py
r17756 r17778 1 1 try: 2 2 import pylab as p 3 import matplotlib.pyplot as plt 3 4 except ImportError: 4 5 print "could not import pylab, matplotlib has not been installed, no plotting capabilities enabled" … … 12 13 from plot_overlay import plot_overlay 13 14 14 def plot_manager(md,options, subplotwidth,nlines,ncols,i):15 def plot_manager(md,options,fig,ax): 15 16 ''' 16 17 PLOT_MANAGER - distribute the plots called by plotmodel 17 18 18 Usage: 19 plot_manager(md,options,subplotwidth,i); 19 'fig' is a handle to the figure instance created by plotmodel. 20 20 21 See also: PLOTMODEL, PLOT_UNIT 21 'ax' is a handle to the axes instance created by plotmodel. This is 22 currently generated using matplotlib's AxesGrid toolki. 23 24 Usage: 25 plot_manager(md,options,fig,ax); 26 27 See also: PLOTMODEL, PLOT_UNIT 22 28 ''' 23 29 … … 29 35 30 36 #initialize plot handle variable 31 handle=None37 #handle=None 32 38 33 39 # initialize subplot 34 p.subplot(nlines,ncols,i,aspect='equal')40 #p.subplot(nrows,ncols,i,aspect='equal') 35 41 36 42 ##basemap plot 37 43 #if options.exist('basemap'): 38 # plot_basemap(md,data,options,n lines,ncols,i)44 # plot_basemap(md,data,options,nrows,ncols,i) 39 45 40 46 #overlay plot 41 47 if options.exist('overlay'): 42 handle=plot_overlay(md,data,options,nlines,ncols,i)48 plot_overlay(md,data,options,ax) 43 49 options.addfielddefault('alpha',0.5) 44 50 options.addfielddefault('xlim',[min(md.mesh.x),max(md.mesh.x)]) … … 50 56 # convert string to lower case for a case-insensitive comparison 51 57 if data.lower()=='mesh': 52 plot_mesh(md,options, nlines,ncols,i)58 plot_mesh(md,options,ax) 53 59 return 54 60 elif data.lower()=='none': … … 74 80 #standard plot 75 81 #if not handle: 76 # p.subplot(n lines,ncols,i,aspect='equal')82 # p.subplot(nrows,ncols,i,aspect='equal') 77 83 78 84 #plot unit 79 plot_unit(x,y,z,elements,data2,is2d,isplanet,datatype,options )85 plot_unit(x,y,z,elements,data2,is2d,isplanet,datatype,options,ax) 80 86 81 87 #apply all options 82 applyoptions(md,data2,options )88 applyoptions(md,data2,options,fig,ax) 83 89 84 90 #ground overlay on kml plot_unit -
issm/trunk-jpl/src/m/plot/plot_overlay.py
r17756 r17778 7 7 import os 8 8 9 def plot_overlay(md,data,options, rows,cols,i):9 def plot_overlay(md,data,options,ax): 10 10 ''' 11 11 Function for plotting a georeferenced image. This function is called … … 76 76 plt.sca(ax) # return to original axes/figure 77 77 78 79 78 # get parameters from cropped geotiff 80 79 trans=gtif.GetGeoTransform() … … 90 89 xg,yg=npy.meshgrid(xarr,yarr) 91 90 if options.exist('basemap'): 92 # TODO get handle to basemap instance 93 # handle= functiontogethandle() 91 # TODO get handle to or create basemap instance 94 92 # create coordinate grid in map projection units (for plotting) 95 93 lats,lons=xy2ll(xg,yg,-1,0,71) … … 98 96 xgmap=xg 99 97 ygmap=yg 100 handle=plt.gca()101 98 102 99 overlaylims=options.getfieldvalue('overlaylims',[min(arr.ravel()),max(arr.ravel())]) … … 104 101 norm=mpl.colors.Normalize(vmin=overlaylims[0],vmax=overlaylims[1]) 105 102 106 handle.pcolormesh(xgmap, ygmap, npy.flipud(arr), cmap=mpl.cm.Greys, norm=norm) 107 handle.set_aspect('equal','box') 108 return handle 103 ax.pcolormesh(xgmap, ygmap, npy.flipud(arr), cmap=mpl.cm.Greys, norm=norm) -
issm/trunk-jpl/src/m/plot/plot_unit.py
r17716 r17778 3 3 import pylab as p 4 4 import matplotlib as mpl 5 import matplotlib.pyplot as plt 5 6 except ImportError: 6 7 print "could not import pylab, matplotlib has not been installed, no plotting capabilities enabled" 7 8 8 def plot_unit(x,y,z,elements,data,is2d,isplanet,datatype,options ):9 def plot_unit(x,y,z,elements,data,is2d,isplanet,datatype,options,ax): 9 10 """ 10 11 PLOT_UNIT - unit plot, display data … … 46 47 #element plot 47 48 if is2d: 48 p.tripcolor(x,y,elements,data,colorlevels,cmap=cmap,edgecolors=edgecolor)49 tri=ax.tripcolor(x,y,elements,data,colorlevels,cmap=cmap,edgecolors=edgecolor) 49 50 else: 50 51 raise ValueError('plot_unit error: 3D element plot not supported yet') 51 return 52 return 52 53 53 54 elif datatype==2: 54 55 #node plot 55 56 if is2d: 56 p.tricontourf(x,y,elements,data,colorlevels,cmap=cmap,norm=norm,alpha=alpha)57 tri=ax.tricontourf(x,y,elements,data,colorlevels,cmap=cmap,norm=norm,alpha=alpha) 57 58 if edgecolor != 'None': 58 p.triplot(x,y,elements,color=edgecolor)59 ax.triplot(x,y,elements,color=edgecolor) 59 60 else: 60 61 raise ValueError('plot_unit error: 3D node plot not supported yet') … … 76 77 else: 77 78 raise ValueError('datatype=%d not supported' % datatype) 79 -
issm/trunk-jpl/src/m/plot/plotmodel.py
r17756 r17778 4 4 try: 5 5 import pylab as p 6 import matplotlib.pyplot as plt 7 from mpl_toolkits.axes_grid1 import ImageGrid 6 8 except ImportError: 7 9 print "could not import pylab, matplotlib has not been installed, no plotting capabilities enabled" … … 28 30 hold=options.list[0].getfieldvalue('hold',False) 29 31 30 #if n lines and ncols specified, then bypass31 if options.list[0].exist('n lines'):32 n lines=options.list[0].getfieldvalue('nlines')33 n l=True32 #if nrows and ncols specified, then bypass 33 if options.list[0].exist('nrows'): 34 nrows=options.list[0].getfieldvalue('nrows') 35 nr=True 34 36 else: 35 n lines=npy.ceil(numberofplots/subplotwidth)36 n l=False37 nrows=npy.ceil(numberofplots/subplotwidth) 38 nr=False 37 39 38 40 if options.list[0].exist('ncols'): … … 43 45 nc=False 44 46 45 #check that n lines and ncols were given at the same time!46 if not n l==nc:47 raise StandardError('error: n lines and ncols need to be specified together, or not at all')47 #check that nrows and ncols were given at the same time! 48 if not nr==nc: 49 raise StandardError('error: nrows and ncols need to be specified together, or not at all') 48 50 49 51 #Go through plots 50 52 if numberofplots: 51 53 52 #Create figure 53 #plots will be visible by default if ipython is run in interactive mode (invoked by ipython --pylab) 54 #UNLESS plotmodel is called within a script (e.g. a runme.py file) 55 #handling the 'visible' option will need some check on whether ipython is currently in interactive or non-interactive mode 54 if not hold: # TODO need to also check whether figurenumber is a new plot so that old plots are not mistakenly cleared 55 plt.cla() 56 56 57 if not hold: # TODO need to also check whether figurenumber is a new plot so that old plots are not mistakenly cleared58 p.cla()59 60 #TODO fig, axarray = plt.subplots(nrows,ncols), then pass fix and axarr to plot_manager61 57 #if figsize specified 62 58 if options.list[0].exist('figsize'): 63 59 figsize=options.list[0].getfieldvalue('figsize') 64 p.figure(figurenumber,figsize=figsize)60 fig=plt.figure(figurenumber,figsize=figsize,tight_layout=True) 65 61 else: 66 p.figure(figurenumber) 67 68 #try: 62 fig=plt.figure(figurenumber,tight_layout=True) 63 fig.clf() 64 65 # options to pass: axes_pad, share_all, label_mode ("1","L","all"), cbar_mode (none,single,each), cbar_location(right,top) 66 axgrid=ImageGrid(fig, 111, 67 nrows_ncols=(nrows,ncols), 68 direction='row', 69 axes_pad=0.05, 70 add_all=True, 71 share_all=True, 72 label_mode='all', 73 cbar_mode='single', 74 cbar_location='right', 75 cbar_size='5%', 76 cbar_pad=0.05 77 ) 78 69 79 for i in xrange(numberofplots): 70 plot_manager(options.list[i].getfieldvalue('model',md),options.list[i],subplotwidth,nlines,ncols,i+1) 71 #except StandardError: 72 # print 'error in plot_manager' 73 p.show() 80 plot_manager(options.list[i].getfieldvalue('model',md),options.list[i],fig,axgrid[i]) 81 82 plt.show() 74 83 else: 75 84 raise StandardError('plotmodel error message: no output data found.')
Note:
See TracChangeset
for help on using the changeset viewer.