def cn_sym_min_max(x, res, ncontours=15, outside=True, aboutZero=True): '''Adjust the contour level configuration of a plot resource to give nice contour levels symmetric about zero. The contour levels are computed with NGL.nice_cntr_levels(). cn_sym_min_max(x, res, ncontours=15, outside=True, aboutZero=True) x : data used in the contour plot. res : Resource object that will be updated accordingly. outside : Whether the maxima is outside or inside the contour range. Will be passed to NGL.nice_cntr_levels(). aboutZero : Whether the contour levels will be centered about Zero. Will be passed to NGL.nice_cntr_levels(). ''' min_x = np.min(x) max_x = np.max(x) if min_x == max_x: min_x, max_x = -1., 1. min_out, max_out, step_size = ngl.nice_cntr_levels(min_x, max_x, outside=outside, max_steps=ncontours, aboutZero=aboutZero) newres = {"cnLevelSelectionMode": "ManualLevels", "cnMinLevelValF": min_out, "cnMaxLevelValF": max_out, "cnLevelSpacingF": (max_out - min_out) / step_size} res.__dict__.update(newres)
data = Ngl.generate_2d_array([nlat,nlon], 10, 19, 0., 100.) lat = Ngl.fspan(minlat,maxlat,nlat) lon = Ngl.fspan(minlon,maxlon,nlon) return data,lat,lon #---------------------------------------------------------------------- # Main code #---------------------------------------------------------------------- #---Generate dummy data data,lat1d,lon1d = gen_dummy_data() #---Get nice contour levels cmin,cmax,cint = Ngl.nice_cntr_levels(numpy.min(data),numpy.max(data)) #---Start the graphics wks_type = "png" wks = Ngl.open_wks (wks_type,"shapefile3") #---Define some graphical resources res = Ngl.Resources() res.nglFrame = False res.nglDraw = False #---Scalar field resources res.sfXArray = lon1d res.sfYArray = lat1d
# Add some named colors. This is no longer needed in PyNGL 1.5.0 #forest_green = numpy.array([ 34, 139, 34])/255. #navy_blue = numpy.array([ 0, 0, 128])/255. #brown = numpy.array([165, 42, 42])/255. #ig = Ngl.new_color(wks,forest_green[0],forest_green[1],forest_green[2]) #ib = Ngl.new_color(wks,navy_blue[0], navy_blue[1], navy_blue[2]) #ir = Ngl.new_color(wks,brown[0], brown[1], brown[2]) # Generate some dummy data. cmin = -19.23 cmax = 16.81 data = Ngl.generate_2d_array([100, 100], 10, 10, cmin, cmax) nice_min, nice_max, nice_spc = Ngl.nice_cntr_levels(cmin, cmax, cint=3) # Read in color map so we can subset it cmap = Ngl.read_colormap_file("nrl_sirkes") # Set up resources for a contour plot. cnres = Ngl.Resources() cnres.nglMaximize = True # Maximize plot in frame cnres.nglDraw = False # Don't draw plot cnres.nglFrame = False # Don't advance the frame cnres.cnFillOn = True # Turn on contour fill cnres.cnFillPalette = cmap[:-3, :]
import numpy, Ngl, sys print "Testing Ngl.nice_cntr_levels code....." print "--------------------------------------------------" print "Testing CINT = 0.0, outside = False / returnLevels = False" print "Ngl.nice_cntr_levels(-23.1, 70.7, outside=False)" answer = (-20.0, 70.0, 9.0) answer_is = Ngl.nice_cntr_levels(-23.1, 70.7, outside=False) if (answer_is == answer): print "...SUCCESSFUL..." else: print "...UNSUCCESSFUL..." print "answer_is", answer_is print "answer", answer print "--------------------------------------------------" print "Testing CINT = 0.0, outside = True / returnLevels = False" print "Ngl.nice_cntr_levels(-23.1, 70.7, outside=True)" answer = (-30.0, 80.0, 11.0) answer_is = Ngl.nice_cntr_levels(-23.1, 70.7, outside=True) if (answer_is == answer): print "...SUCCESSFUL..." else: print "...UNSUCCESSFUL..." print "answer_is", answer_is print "answer", answer print "--------------------------------------------------" print "Testing cint = 0.0, outside = False / returnLevels = True" print "Ngl.nice_cntr_levels(-23.1, 70.7, outside=False, returnLevels=True)" answer1 = (-20.0, 70.0, 9.0)