def _getData(self): ret = {} if (self.dm == 2): # Loop over all components for cmpstr in self.comps: tmpdat = numpy.array([[0.0] * self.ny] * self.nx) tmpdat = [[0.0] * self.nx, [0.0] * self.ny] (tmpdat, ierr) = fsnapshot.fplotfile_get_data_2d( self.name, cmpstr, tmpdat) if (ierr != 0): sys.exit(2) ret[cmpstr] = tmpdat return ret elif (self.dm == 3): # Loop over all components for cmpstr in self.comps: tmpdat = numpy.array([[[0.0] * self.nz] * self.ny] * self.nx) (tmpdat, ierr) = fsnapshot.fplotfile_get_data_3d( self.name, cmpstr, tmpdat) if (ierr != 0): sys.exit(2) ret[cmpstr] = tmpdat return ret else: print('ERROR! Invalid dimension in BLPlotfile._getData()') sys.exit(2)
def _getData(self): ret = {} if(self.dm == 2): #Loop over all components for cmpstr in self.comps: tmpdat = numpy.array([[0.0]*self.ny]*self.nx) tmpdat = [[0.0]*self.nx, [0.0]*self.ny] (tmpdat, ierr) = fsnapshot.fplotfile_get_data_2d(self.name, cmpstr, tmpdat) if(ierr != 0): sys.exit(2) ret[cmpstr] = tmpdat return ret elif(self.dm == 3): #Loop over all components for cmpstr in self.comps: tmpdat = numpy.array([[[0.0]*self.nz]*self.ny]*self.nx) (tmpdat, ierr) = fsnapshot.fplotfile_get_data_3d(self.name, cmpstr, tmpdat) if(ierr != 0): sys.exit(2) ret[cmpstr] = tmpdat return ret else: print('ERROR! Invalid dimension in BLPlotfile._getData()') sys.exit(2)
def do_plot(plotfile, component, component2, outFile, log, minval, maxval, minval2, maxval2, eps, dpi, origin, annotation, particles, time_ind): # plotfile plotfile to read data from # component first variable to plot # component2 optional second variable to plot # outFile save plot as "outFile" # log (= 1,0) plot log (base 10) of the data # minval (float) specify minimum data range for plot # maxval (float) specify maximum data range for plot # eps (= 1,0) make an EPS plot instead of PNG # dpi (int) (PNG only) make the plot with dpi value # origin (= 1,0) (3-d only) slice through origin (0,0,0) # annotation (2-d only) add annotation string under time # particles (2-d only) particle data # time_ind array index of time cooresponding to plotfile #---------------------- # check incoming values: #----------------------- if (plotfile == ""): print "\n---ERROR: plotfile not specified---" print " (plotsinglevar_parts.py)\n" usage() sys.exit(2) if (component == ""): print "\n---ERROR: no component specified---" print " (plotsinglevar_parts.py)\n" usage() sys.exit(2) if ((log != 1) and (log != 0)): print "\n---ERROR: invalid value for log (= 1,0)---" print " (plotsinglevar_parts.py)\n" usage() sys.exit(2) if ((eps != 1) and (eps != 0)): print "\n---ERROR: invalid value for eps (= 1,0)---" print " (plotsinglevar_parts.py)\n" usage() sys.exit(2) if (minval != None): try: minval = float(minval) except ValueError: print "\n---ERROR: invalid value for minval (= float)---" print " (plotsinglevar_parts.py)\n" usage() sys.exit(2) if (maxval != None): try: maxval = float(maxval) except ValueError: print "\n---ERROR: invalid value for maxval (= float)---" print " (plotsinglevar_parts.py)\n" usage() sys.exit(2) if (minval2 != None): try: minval2 = float(minval2) except ValueError: print "\n---ERROR: invalid value for minval2 (= float)---" print " (plotsinglevar_parts.py)\n" usage() sys.exit(2) if (maxval2 != None): try: maxval2 = float(maxval2) except ValueError: print "\n---ERROR: invalid value for maxval2 (= float)---" print " (plotsinglevar_parts.py)\n" usage() sys.exit(2) if ((origin != 1) and (origin != 0)): print "\n---ERROR: invalid value for origin (= 1,0)---" print " (plotsinglevar_parts.py)\n" usage() sys.exit(2) if (dpi != None): try: dpi = int(dpi) except ValueError: print "\n---ERROR: invalid value for dpi (= int)---" print " (plotsinglevar_parts.py)\n" usage() sys.exit(2) #-------------------------------------------------------------------------- # construct the output file name #-------------------------------------------------------------------------- if (outFile == ""): outFile = os.path.normpath(plotfile) + "_" + component if (not component2 == ""): outFile += "_" + component2 if (not eps): outFile += ".png" else: outFile += ".eps" else: # make sure the proper extension is used if (not eps): if (not string.rfind(outFile, ".png") > 0): outFile = outFile + ".png" else: if (not string.rfind(outFile, ".eps") > 0): outFile = outFile + ".eps" #-------------------------------------------------------------------------- # read in the meta-data from the plotfile #-------------------------------------------------------------------------- (nx, ny, nz) = fsnapshot.fplotfile_get_size(plotfile) time = fsnapshot.fplotfile_get_time(plotfile) (xmin, xmax, ymin, ymax, zmin, zmax) = \ fsnapshot.fplotfile_get_limits(plotfile) x = xmin + numpy.arange( (nx), dtype=numpy.float64 )*(xmax - xmin)/nx y = ymin + numpy.arange( (ny), dtype=numpy.float64 )*(ymax - ymin)/ny if (nz > 0): z = zmin + numpy.arange( (nz), dtype=numpy.float64 )*(zmax - zmin)/nz if (nz == -1): #---------------------------------------------------------------------- # 2-d plots #---------------------------------------------------------------------- extent = xmin, xmax, ymin, ymax # read in the main component data = numpy.zeros( (nx, ny), dtype=numpy.float64) (data, err) = fsnapshot.fplotfile_get_data_2d(plotfile, component, data) if (not err == 0): sys.exit(2) data = numpy.transpose(data) if (minval == None): minval = numpy.min(data) if (maxval == None): maxval = numpy.max(data) # read in the component #2, if present if (not component2 == ""): data2 = numpy.zeros( (nx, ny), dtype=numpy.float64) (data2, err) = fsnapshot.fplotfile_get_data_2d(plotfile, component2, data2) if (not err == 0): sys.exit(2) data2 = numpy.transpose(data2) if (minval2 == None): minval2 = numpy.min(data2) if (maxval2 == None): maxval2 = numpy.max(data2) if log: data = numpy.log10(data) if (not component2 == ""): data2 = numpy.log10(data2) minval2 = math.log10(minval2) maxval2 = math.log10(maxval2) minval = math.log10(minval) maxval = math.log10(maxval) #---------------------------------------------------------------------- # plot main component #---------------------------------------------------------------------- if (not component2 == ""): ax = pylab.subplot(1,2,1) pylab.subplots_adjust(wspace=0.5) else: ax = pylab.subplot(1,1,1) divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax) im=pylab.imshow(data,origin='lower', extent=extent, vmin=minval, vmax=maxval) #---------------------------------------------------------------------- # plot the particle data #---------------------------------------------------------------------- n=0 while(n < len(particles)): # sometimes the length of particle history is larger than index if (time_ind < len(particles[n].history)): pylab.scatter(particles[n].history[time_ind].xyz[0], particles[n].history[time_ind].xyz[1], s=0.5,c='black',marker='o') n += 1 pylab.title(component) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) # make space for a colorbar -- getting it the same size as the # vertical extent of the plot is surprisingly tricky. See # http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#colorbar-whose-height-or-width-in-sync-with-the-master-axes ax_cb = divider.new_horizontal(size="10%", pad=0.1) fig1 = ax.get_figure() fig1.add_axes(ax_cb) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cb = pylab.colorbar(im, format=formatter, cax=ax_cb) # make the font size for the colorbar axis labels small. Note #---------------------------------------------------------------------- # plot component #2 #---------------------------------------------------------------------- if (not component2 == ""): ax = pylab.subplot(1,2,2) divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax) im = pylab.imshow(data2, origin='lower', extent=extent, vmin=minval2, vmax=maxval2) #---------------------------------------------------------------- # plot the particle data #---------------------------------------------------------------- n=0 while(n < len(particles)): # sometimes the length of particle history is larger than index if (time_ind < len(particles[n].history)): pylab.scatter(particles[n].history[time_ind].xyz[0], particles[n].history[time_ind].xyz[1], s=0.5,c='black',marker='o') n += 1 pylab.title(component2) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) # make space for a colorbar -- getting it the same size as # the vertical extent of the plot is surprisingly tricky. # See # http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#colorbar-whose-height-or-width-in-sync-with-the-master-axes ax_cb = divider.new_horizontal(size="10%", pad=0.1) fig1 = ax.get_figure() fig1.add_axes(ax_cb) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cb = pylab.colorbar(im, format=formatter, cax=ax_cb) # make the font size for the colorbar axis labels small. Note the # offsetText is the 10^N that appears at the top of the y-axis. cl = pylab.getp(cb.ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cb.ax.yaxis.offsetText.set_fontsize("small") #ax_cb.yaxis.tick_right() else: #---------------------------------------------------------------------- # 3-d plot #---------------------------------------------------------------------- ################################### # NOT SUPPORTED YET ################################### print "\n\n--- ERROR: 3-d not yet implemented ---" print " (plotsinglevar_parts.py)\n" sys.exit(2) # figure out the maximum width -- we will plot xy, xz, and yz w1 = xmax - xmin # also w2 w3 = ymax - ymin if (w3 > w1): scale = w3 else: scale = w1 # starting points for the figure positions # assume that the width of the plotting area is 0.05 to 0.95, # leaving 0.9 to be split amongst the 3 plots. So each has a # width of 0.3 # for the height, we will assume that the colorbar at the # bottom gets 0.15, and that we go until 0.95, leaving 0.8 of # height for the plots. pos1 = [0.05, 0.15, 0.3, 0.8] pos2 = [0.35, 0.15, 0.3, 0.8] pos3 = [0.65, 0.15, 0.3, 0.8] fig = pylab.figure() # x-y extent = xmin, xmax, ymin, ymax # read in the main component data = numpy.zeros( (nx, ny), dtype=numpy.float64) indir = 3 (data, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, indir, origin, data) if (not err == 0): sys.exit(2) data = numpy.transpose(data) if log: data = numpy.log10(data) if (not minval == None): minval = math.log10(minval) if (not maxval == None): maxval = math.log10(maxval) ax = pylab.subplot(1,3,1) pylab.subplots_adjust(wspace=0.4) #fig.add_axes(pos1) im=pylab.imshow(data,origin='lower', extent=extent, vmin=minval, vmax=maxval, axes=pos1) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # do a fixed offset in pixels from the (xmin,ymin) data point fig1 = ax.get_figure() trans=matplotlib.transforms.offset_copy(ax.transData, x=0, y=-0.5, fig=fig1, units='inches') pylab.text(xmin, ymin, "time = %7.3g s" % (time), verticalalignment="bottom", transform = trans, clip_on=False, fontsize=10) # x-z extent = xmin, xmax, zmin, zmax # read in the main component data = numpy.zeros( (nx, nz), dtype=numpy.float64) (data, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, 2, origin, data) if (not err == 0): sys.exit(2) data = numpy.transpose(data) if log: data = numpy.log10(data) if (not minval == None): minval = math.log10(minval) if (not maxval == None): maxval = math.log10(maxval) ax = pylab.subplot(1,3,2) #fig.add_axes(pos2) im=pylab.imshow(data,origin='lower', extent=extent, vmin=minval, vmax=maxval, axes=pos2) pylab.xlabel("x") pylab.ylabel("z") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # y-z extent = ymin, ymax, zmin, zmax # read in the main component data = numpy.zeros( (ny, nz), dtype=numpy.float64) (data, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, 1, origin, data) if (not err == 0): sys.exit(2) data = numpy.transpose(data) if log: data = numpy.log10(data) if (not minval == None): minval = math.log10(minval) if (not maxval == None): maxval = math.log10(maxval) ax = pylab.subplot(1,3,3) #fig.add_axes(pos3) im=pylab.imshow(data,origin='lower', extent=extent, vmin=minval, vmax=maxval, axes=pos3) pylab.xlabel("y") pylab.ylabel("z") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # colorbar pylab.subplots_adjust(bottom=0.1, left=0.05, right=0.95) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cax = pylab.axes([0.05, 0.06, 0.9, 0.04]) pylab.colorbar(orientation="horizontal", cax=cax, format=formatter) pylab.title(component) #-------------------------------------------------------------------------- # save the figure #-------------------------------------------------------------------------- if (not eps): pylab.savefig(outFile, bbox_inches='tight', dpi=dpi, pad_inches=0.33) else: pylab.savefig(outFile, bbox_inches='tight', pad_inches=0.33)
def do_plot(plotfile, component, component2, outFile, log, minval, maxval, minval2, maxval2, eps, dpi, origin, annotation, xmin_pass, ymin_pass, zmin_pass, xmax_pass, ymax_pass, zmax_pass): pylab.rc("font", size=9) #-------------------------------------------------------------------------- # construct the output file name #-------------------------------------------------------------------------- if (outFile == ""): outFile = os.path.normpath(plotfile) + "_" + component if (not component2 == ""): outFile += "_" + component2 if (not eps): outFile += ".png" else: outFile += ".eps" else: # make sure the proper extension is used if (not eps): if (not string.rfind(outFile, ".png") > 0): outFile = outFile + ".png" else: if (not string.rfind(outFile, ".eps") > 0): outFile = outFile + ".eps" #-------------------------------------------------------------------------- # read in the meta-data from the plotfile #-------------------------------------------------------------------------- (nx, ny, nz) = fsnapshot.fplotfile_get_size(plotfile) time = fsnapshot.fplotfile_get_time(plotfile) (xmin, xmax, ymin, ymax, zmin, zmax) = \ fsnapshot.fplotfile_get_limits(plotfile) dx = (xmax - xmin) / nx x = xmin + numpy.arange((nx), dtype=numpy.float64) * dx dy = (ymax - ymin) / ny y = ymin + numpy.arange((ny), dtype=numpy.float64) * dy if (nz > 0): dz = (zmax - zmin) / nz z = zmin + numpy.arange((nz), dtype=numpy.float64) * dz if (nz == -1): #---------------------------------------------------------------------- # 2-d plots #---------------------------------------------------------------------- extent = [xmin, xmax, ymin, ymax] ix0 = 0 ix = nx iy0 = 0 iy = ny if (not xmin_pass == None): extent[0] = xmin_pass ix0 = int((xmin_pass - xmin) / dx) if (not ymin_pass == None): extent[2] = ymin_pass iy0 = int((ymin_pass - ymin) / dy) if (not xmax_pass == None): extent[1] = xmax_pass ix = int((xmax_pass - xmin) / dx) if (not ymax_pass == None): extent[3] = ymax_pass iy = int((ymax_pass - ymin) / dy) sparseX = 0 if (ny >= 3 * nx): sparseX = 1 # read in the main component data = numpy.zeros((nx, ny), dtype=numpy.float64) (data, err) = fsnapshot.fplotfile_get_data_2d(plotfile, component, data) if (not err == 0): sys.exit(2) data = numpy.transpose(data) # read in the component #2, if present if (not component2 == ""): data2 = numpy.zeros((nx, ny), dtype=numpy.float64) (data2, err) = fsnapshot.fplotfile_get_data_2d(plotfile, component2, data2) if (not err == 0): sys.exit(2) data2 = numpy.transpose(data2) # log? if log: if (numpy.min(data) < 0): data = numpy.log10(numpy.abs(data)) else: data = numpy.log10(data) if (not component2 == ""): if (numpy.min(data2) < 0.0): data2 = numpy.log10(numpy.abs(data2)) else: data2 = numpy.log10(data2) if (not minval == None): minval = math.log10(minval) if (not maxval == None): maxval = math.log10(maxval) if (not minval2 == None): minval2 = math.log10(minval2) if (not maxval2 == None): maxval2 = math.log10(maxval2) #---------------------------------------------------------------------- # plot main component #---------------------------------------------------------------------- if (not component2 == ""): ax = pylab.subplot(1, 2, 1) pylab.subplots_adjust(wspace=0.5) else: ax = pylab.subplot(1, 1, 1) divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax) im = pylab.imshow(data[iy0:iy, ix0:ix], origin='lower', extent=extent, vmin=minval, vmax=maxval) pylab.title(component) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) if (sparseX): ax.xaxis.set_major_locator(pylab.MaxNLocator(3)) # make space for a colorbar -- getting it the same size as the # vertical extent of the plot is surprisingly tricky. See # http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#colorbar-whose-height-or-width-in-sync-with-the-master-axes ax_cb = divider.new_horizontal(size="10%", pad=0.1) fig1 = ax.get_figure() fig1.add_axes(ax_cb) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cb = pylab.colorbar(im, format=formatter, cax=ax_cb) # make the font size for the colorbar axis labels small. Note # the offsetText is the 10^N that appears at the top of the # y-axis. cl = pylab.getp(cb.ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cb.ax.yaxis.offsetText.set_fontsize("small") # do a fixed offset in pixels from the (xmin,ymin) data point trans = matplotlib.transforms.offset_copy(ax.transData, x=0, y=-0.5, fig=fig1, units='inches') pylab.text(xmin, ymin, "time = %7.3g s" % (time), verticalalignment="bottom", transform=trans, clip_on=False, fontsize=10) if (not annotation == ""): trans = matplotlib.transforms.offset_copy(ax.transData, x=0, y=-0.65, fig=fig1, units='inches') pylab.text(xmin, ymin, "%s" % (annotation), verticalalignment="bottom", transform=trans, clip_on=False, fontsize=10) #---------------------------------------------------------------------- # plot component #2 #---------------------------------------------------------------------- if (not component2 == ""): ax = pylab.subplot(1, 2, 2) divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax) im = pylab.imshow(data2[iy0:iy, ix0:ix], origin='lower', extent=extent, vmin=minval2, vmax=maxval2) pylab.title(component2) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax.xaxis.set_major_formatter( pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter( pylab.ScalarFormatter(useMathText=True)) if (sparseX): ax.xaxis.set_major_locator(pylab.MaxNLocator(3)) # make space for a colorbar -- getting it the same size as # the vertical extent of the plot is surprisingly tricky. # See # http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#colorbar-whose-height-or-width-in-sync-with-the-master-axes ax_cb = divider.new_horizontal(size="10%", pad=0.1) fig1 = ax.get_figure() fig1.add_axes(ax_cb) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cb = pylab.colorbar(im, format=formatter, cax=ax_cb) # make the font size for the colorbar axis labels small. Note the # offsetText is the 10^N that appears at the top of the y-axis. cl = pylab.getp(cb.ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cb.ax.yaxis.offsetText.set_fontsize("small") #ax_cb.yaxis.tick_right() else: #---------------------------------------------------------------------- # 3-d plot #---------------------------------------------------------------------- # starting points for the figure positions # assume that the width of the plotting area is 0.05 to 0.95, # leaving 0.9 to be split amongst the 3 plots. So each has a # width of 0.3 # for the height, we will assume that the colorbar at the # bottom gets 0.15, and that we go until 0.95, leaving 0.8 of # height for the plots. pos1 = [0.05, 0.15, 0.3, 0.8] pos2 = [0.35, 0.15, 0.3, 0.8] pos3 = [0.65, 0.15, 0.3, 0.8] fig = pylab.figure() # read in the slices # x-y data_xy = numpy.zeros((nx, ny), dtype=numpy.float64) indir = 3 (data_xy, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, indir, origin, data_xy) if (not err == 0): sys.exit(2) data_xy = numpy.transpose(data_xy) if log: if (numpy.min(data_xy) < 0): data_xy = numpy.log10(numpy.abs(data_xy)) else: data_xy = numpy.log10(data_xy) # x-z data_xz = numpy.zeros((nx, nz), dtype=numpy.float64) (data_xz, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, 2, origin, data_xz) if (not err == 0): sys.exit(2) data_xz = numpy.transpose(data_xz) if log: if (numpy.min(data_xz) < 0): data_xz = numpy.log10(numpy.abs(data_xz)) else: data_xz = numpy.log10(data_xz) # y-z data_yz = numpy.zeros((ny, nz), dtype=numpy.float64) (data_yz, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, 1, origin, data_yz) if (not err == 0): sys.exit(2) data_yz = numpy.transpose(data_yz) if log: if (numpy.min(data_yz) < 0): data_yz = numpy.log10(numpy.abs(data_yz)) else: data_yz = numpy.log10(data_yz) if (not minval == None): if (log): minval = math.log10(minval) else: minval = numpy.min(data_xy) minval = min(minval, numpy.min(data_xz)) minval = min(minval, numpy.min(data_yz)) if (not maxval == None): if (log): maxval = math.log10(maxval) else: maxval = numpy.max(data_xy) maxval = max(maxval, numpy.max(data_xz)) maxval = max(maxval, numpy.max(data_yz)) # x-y extent = [xmin, xmax, ymin, ymax] ix0 = 0 ix = nx iy0 = 0 iy = ny if (not xmin_pass == None): extent[0] = xmin_pass ix0 = int((xmin_pass - xmin) / dx) if (not ymin_pass == None): extent[2] = ymin_pass iy0 = int((ymin_pass - ymin) / dy) if (not xmax_pass == None): extent[1] = xmax_pass ix = int((xmax_pass - xmin) / dx) if (not ymax_pass == None): extent[3] = ymax_pass iy = int((ymax_pass - ymin) / dy) ax = pylab.subplot(1, 3, 1) pylab.subplots_adjust(wspace=0.4) #fig.add_axes(pos1) im = pylab.imshow(data_xy[iy0:iy, ix0:ix], origin='lower', extent=extent, vmin=minval, vmax=maxval, axes=pos1) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # do a fixed offset in pixels from the (xmin,ymin) data point fig1 = ax.get_figure() trans = matplotlib.transforms.offset_copy(ax.transData, x=0, y=-0.5, fig=fig1, units='inches') # pylab.text(xmin_pass, ymin_pass, "time = %7.3g s" % (time), # verticalalignment="bottom", transform = trans, # clip_on=False, fontsize=10) # x-z extent = [xmin, xmax, zmin, zmax] ix0 = 0 ix = nx iz0 = 0 iz = nz if (not xmin_pass == None): extent[0] = xmin_pass ix0 = int((xmin_pass - xmin) / dx) if (not zmin_pass == None): extent[2] = zmin_pass iz0 = int((zmin_pass - zmin) / dz) if (not xmax_pass == None): extent[1] = xmax_pass ix = int((xmax_pass - xmin) / dx) if (not zmax_pass == None): extent[3] = zmax_pass iz = int((zmax_pass - zmin) / dz) ax = pylab.subplot(1, 3, 2) #fig.add_axes(pos2) im = pylab.imshow(data_xz[iz0:iz, ix0:ix], origin='lower', extent=extent, vmin=minval, vmax=maxval, axes=pos2) pylab.xlabel("x") pylab.ylabel("z") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # y-z extent = [ymin, ymax, zmin, zmax] iy0 = 0 iy = ny iz0 = 0 iz = nz if (not ymin_pass == None): extent[0] = ymin_pass iy0 = int((ymin_pass - ymin) / dy) if (not zmin_pass == None): extent[2] = zmin_pass iz0 = int((zmin_pass - zmin) / dz) if (not ymax_pass == None): extent[1] = ymax_pass iy = int((ymax_pass - ymin) / dy) if (not zmax_pass == None): extent[3] = zmax_pass iz = int((zmax_pass - zmin) / dz) ax = pylab.subplot(1, 3, 3) #fig.add_axes(pos3) im = pylab.imshow(data_yz[iz0:iz, iy0:iy], origin='lower', extent=extent, vmin=minval, vmax=maxval, axes=pos3) pylab.xlabel("y") pylab.ylabel("z") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # colorbar pylab.subplots_adjust(bottom=0.1, left=0.05, right=0.95) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cax = pylab.axes([0.05, 0.06, 0.9, 0.04]) pylab.colorbar(orientation="horizontal", cax=cax, format=formatter) pylab.title(component) #-------------------------------------------------------------------------- # save the figure #-------------------------------------------------------------------------- # try: pylab.tight_layout() # requires matplotlib >= 1.1 # except: # pass if (not eps): pylab.savefig(outFile, bbox_inches='tight', dpi=dpi, pad_inches=0.33) else: pylab.savefig(outFile) #, bbox_inches='tight', pad_inches=0.33)
def do_plot(plotfile, component, component2, outFile, log, minval, maxval, minval2, maxval2, eps, dpi, origin, annotation, xmin_pass, ymin_pass, zmin_pass, xmax_pass, ymax_pass, zmax_pass): pylab.rc("font", size=9) #-------------------------------------------------------------------------- # construct the output file name #-------------------------------------------------------------------------- if (outFile == ""): outFile = os.path.normpath(plotfile) + "_" + component if (not component2 == ""): outFile += "_" + component2 if (not eps): outFile += ".png" else: outFile += ".eps" else: # make sure the proper extension is used if (not eps): if (not string.rfind(outFile, ".png") > 0): outFile = outFile + ".png" else: if (not string.rfind(outFile, ".eps") > 0): outFile = outFile + ".eps" #-------------------------------------------------------------------------- # read in the meta-data from the plotfile #-------------------------------------------------------------------------- (nx, ny, nz) = fsnapshot.fplotfile_get_size(plotfile) time = fsnapshot.fplotfile_get_time(plotfile) (xmin, xmax, ymin, ymax, zmin, zmax) = \ fsnapshot.fplotfile_get_limits(plotfile) dx = (xmax - xmin)/nx x = xmin + numpy.arange( (nx), dtype=numpy.float64 )*dx dy = (ymax - ymin)/ny y = ymin + numpy.arange( (ny), dtype=numpy.float64 )*dy if (nz > 0): dz = (zmax - zmin)/nz z = zmin + numpy.arange( (nz), dtype=numpy.float64 )*dz if (nz == -1): #---------------------------------------------------------------------- # 2-d plots #---------------------------------------------------------------------- extent = [xmin, xmax, ymin, ymax] ix0 = 0 ix = nx iy0 = 0 iy = ny if (not xmin_pass == None): extent[0] = xmin_pass ix0 = int((xmin_pass - xmin)/dx) if (not ymin_pass == None): extent[2] = ymin_pass iy0 = int((ymin_pass - ymin)/dy) if (not xmax_pass == None): extent[1] = xmax_pass ix = int((xmax_pass - xmin)/dx) if (not ymax_pass == None): extent[3] = ymax_pass iy = int((ymax_pass - ymin)/dy) sparseX = 0 if (ny >= 3*nx): sparseX = 1 # read in the main component data = numpy.zeros( (nx, ny), dtype=numpy.float64) (data, err) = fsnapshot.fplotfile_get_data_2d(plotfile, component, data) if (not err == 0): sys.exit(2) data = numpy.transpose(data) # read in the component #2, if present if (not component2 == ""): data2 = numpy.zeros( (nx, ny), dtype=numpy.float64) (data2, err) = fsnapshot.fplotfile_get_data_2d(plotfile, component2, data2) if (not err == 0): sys.exit(2) data2 = numpy.transpose(data2) # log? if log: if (numpy.min(data) < 0): data = numpy.log10(numpy.abs(data)) else: data = numpy.log10(data) if (not component2 == ""): if (numpy.min(data2) < 0.0): data2 = numpy.log10(numpy.abs(data2)) else: data2 = numpy.log10(data2) if (not minval == None): minval = math.log10(minval) if (not maxval == None): maxval = math.log10(maxval) if (not minval2 == None): minval2 = math.log10(minval2) if (not maxval2 == None): maxval2 = math.log10(maxval2) #---------------------------------------------------------------------- # plot main component #---------------------------------------------------------------------- if (not component2 == ""): ax = pylab.subplot(1,2,1) pylab.subplots_adjust(wspace=0.5) else: ax = pylab.subplot(1,1,1) divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax) im=pylab.imshow(data[iy0:iy,ix0:ix],origin='lower', extent=extent, vmin=minval, vmax=maxval) pylab.title(component) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) if (sparseX): ax.xaxis.set_major_locator(pylab.MaxNLocator(3)) # make space for a colorbar -- getting it the same size as the # vertical extent of the plot is surprisingly tricky. See # http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#colorbar-whose-height-or-width-in-sync-with-the-master-axes ax_cb = divider.new_horizontal(size="10%", pad=0.1) fig1 = ax.get_figure() fig1.add_axes(ax_cb) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cb = pylab.colorbar(im, format=formatter, cax=ax_cb) # make the font size for the colorbar axis labels small. Note # the offsetText is the 10^N that appears at the top of the # y-axis. cl = pylab.getp(cb.ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cb.ax.yaxis.offsetText.set_fontsize("small") # do a fixed offset in pixels from the (xmin,ymin) data point trans=matplotlib.transforms.offset_copy(ax.transData, x=0, y=-0.5, fig=fig1, units='inches') pylab.text(xmin, ymin, "time = %7.3g s" % (time), verticalalignment="bottom", transform = trans, clip_on=False, fontsize=10) if (not annotation == ""): trans=matplotlib.transforms.offset_copy(ax.transData, x=0, y=-0.65, fig=fig1, units='inches') pylab.text(xmin, ymin, "%s" % (annotation), verticalalignment="bottom", transform = trans, clip_on=False, fontsize=10) #---------------------------------------------------------------------- # plot component #2 #---------------------------------------------------------------------- if (not component2 == ""): ax = pylab.subplot(1,2,2) divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax) im = pylab.imshow(data2[iy0:iy,ix0:ix], origin='lower', extent=extent, vmin=minval2, vmax=maxval2) pylab.title(component2) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) if (sparseX): ax.xaxis.set_major_locator(pylab.MaxNLocator(3)) # make space for a colorbar -- getting it the same size as # the vertical extent of the plot is surprisingly tricky. # See # http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#colorbar-whose-height-or-width-in-sync-with-the-master-axes ax_cb = divider.new_horizontal(size="10%", pad=0.1) fig1 = ax.get_figure() fig1.add_axes(ax_cb) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cb = pylab.colorbar(im, format=formatter, cax=ax_cb) # make the font size for the colorbar axis labels small. Note the # offsetText is the 10^N that appears at the top of the y-axis. cl = pylab.getp(cb.ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cb.ax.yaxis.offsetText.set_fontsize("small") #ax_cb.yaxis.tick_right() else: #---------------------------------------------------------------------- # 3-d plot #---------------------------------------------------------------------- # starting points for the figure positions # assume that the width of the plotting area is 0.05 to 0.95, # leaving 0.9 to be split amongst the 3 plots. So each has a # width of 0.3 # for the height, we will assume that the colorbar at the # bottom gets 0.15, and that we go until 0.95, leaving 0.8 of # height for the plots. pos1 = [0.05, 0.15, 0.3, 0.8] pos2 = [0.35, 0.15, 0.3, 0.8] pos3 = [0.65, 0.15, 0.3, 0.8] fig = pylab.figure() # read in the slices # x-y data_xy = numpy.zeros( (nx, ny), dtype=numpy.float64) indir = 3 (data_xy, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, indir, origin, data_xy) if (not err == 0): sys.exit(2) data_xy = numpy.transpose(data_xy) if log: if (numpy.min(data_xy) < 0): data_xy = numpy.log10(numpy.abs(data_xy)) else: data_xy = numpy.log10(data_xy) # x-z data_xz = numpy.zeros( (nx, nz), dtype=numpy.float64) (data_xz, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, 2, origin, data_xz) if (not err == 0): sys.exit(2) data_xz = numpy.transpose(data_xz) if log: if (numpy.min(data_xz) < 0): data_xz = numpy.log10(numpy.abs(data_xz)) else: data_xz = numpy.log10(data_xz) # y-z data_yz = numpy.zeros( (ny, nz), dtype=numpy.float64) (data_yz, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, 1, origin, data_yz) if (not err == 0): sys.exit(2) data_yz = numpy.transpose(data_yz) if log: if (numpy.min(data_yz) < 0): data_yz = numpy.log10(numpy.abs(data_yz)) else: data_yz = numpy.log10(data_yz) if (not minval == None): if (log): minval = math.log10(minval) else: minval = numpy.min(data_xy) minval = min(minval,numpy.min(data_xz)) minval = min(minval,numpy.min(data_yz)) if (not maxval == None): if (log): maxval = math.log10(maxval) else: maxval = numpy.max(data_xy) maxval = max(maxval,numpy.max(data_xz)) maxval = max(maxval,numpy.max(data_yz)) # x-y extent = [xmin, xmax, ymin, ymax] ix0 = 0 ix = nx iy0 = 0 iy = ny if (not xmin_pass == None): extent[0] = xmin_pass ix0 = int((xmin_pass - xmin)/dx) if (not ymin_pass == None): extent[2] = ymin_pass iy0 = int((ymin_pass - ymin)/dy) if (not xmax_pass == None): extent[1] = xmax_pass ix = int((xmax_pass - xmin)/dx) if (not ymax_pass == None): extent[3] = ymax_pass iy = int((ymax_pass - ymin)/dy) ax = pylab.subplot(1,3,1) pylab.subplots_adjust(wspace=0.4) #fig.add_axes(pos1) im=pylab.imshow(data_xy[iy0:iy,ix0:ix],origin='lower', extent=extent, vmin=minval, vmax=maxval, axes=pos1) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # do a fixed offset in pixels from the (xmin,ymin) data point fig1 = ax.get_figure() trans=matplotlib.transforms.offset_copy(ax.transData, x=0, y=-0.5, fig=fig1, units='inches') # pylab.text(xmin_pass, ymin_pass, "time = %7.3g s" % (time), # verticalalignment="bottom", transform = trans, # clip_on=False, fontsize=10) # x-z extent = [xmin, xmax, zmin, zmax] ix0 = 0 ix = nx iz0 = 0 iz = nz if (not xmin_pass == None): extent[0] = xmin_pass ix0 = int((xmin_pass - xmin)/dx) if (not zmin_pass == None): extent[2] = zmin_pass iz0 = int((zmin_pass - zmin)/dz) if (not xmax_pass == None): extent[1] = xmax_pass ix = int((xmax_pass - xmin)/dx) if (not zmax_pass == None): extent[3] = zmax_pass iz = int((zmax_pass - zmin)/dz) ax = pylab.subplot(1,3,2) #fig.add_axes(pos2) im=pylab.imshow(data_xz[iz0:iz,ix0:ix],origin='lower', extent=extent, vmin=minval, vmax=maxval, axes=pos2) pylab.xlabel("x") pylab.ylabel("z") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # y-z extent = [ymin, ymax, zmin, zmax] iy0 = 0 iy = ny iz0 = 0 iz = nz if (not ymin_pass == None): extent[0] = ymin_pass iy0 = int((ymin_pass - ymin)/dy) if (not zmin_pass == None): extent[2] = zmin_pass iz0 = int((zmin_pass - zmin)/dz) if (not ymax_pass == None): extent[1] = ymax_pass iy = int((ymax_pass - ymin)/dy) if (not zmax_pass == None): extent[3] = zmax_pass iz = int((zmax_pass - zmin)/dz) ax = pylab.subplot(1,3,3) #fig.add_axes(pos3) im=pylab.imshow(data_yz[iz0:iz,iy0:iy],origin='lower', extent=extent, vmin=minval, vmax=maxval, axes=pos3) pylab.xlabel("y") pylab.ylabel("z") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # colorbar pylab.subplots_adjust(bottom=0.1, left=0.05, right=0.95) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cax = pylab.axes([0.05, 0.06, 0.9, 0.04]) pylab.colorbar(orientation="horizontal", cax=cax, format=formatter) pylab.title(component) #-------------------------------------------------------------------------- # save the figure #-------------------------------------------------------------------------- # try: pylab.tight_layout() # requires matplotlib >= 1.1 # except: # pass if (not eps): pylab.savefig(outFile, bbox_inches='tight', dpi=dpi, pad_inches=0.33) else: pylab.savefig(outFile)#, bbox_inches='tight', pad_inches=0.33)