def cn_line_thickness(plot, val=0., factor=2.): ''' Increases the thickness of contour lines corresponding to a list of values cn_line_thickness(contour, val=0., factor=2.) plot : plot Id of the plot to modify val : scalar or list of values, which contour lines should be modified. factor : scaling factor for line thickness ''' cnLevels = ngl.get_float_array(plot, "cnLevels") cnLineThicknesses = ngl.get_float_array(plot, "cnLineThicknesses") try: (v for v in val) except TypeError: val = [val] for i, thickness in enumerate(cnLineThicknesses[:]): if cnLevels[i] in val: cnLineThicknesses[i] = thickness * factor rlist = {"cnMonoLineThickness": False, "cnLineThicknesses": cnLineThicknesses} _set_values(plot, rlist)
def tm_lat_lon_labels(plot, ax="XB", direction=None, fmt="%d"): '''Add degree symbols to the tickmark labels of one axis of a plot. This function has to be called for each axis, that should get degree tickmark labels tm_lat_lon_labels(plot, ax="XB", direction="zonal") plot : plot Id of the plot to modify ax : one of "XB", "XT", "YL", "YR". Specifies the axis to work on. direction : String that starts with either "z" or "m", to indicate the direction of the axis (zonal or meridional). If omitted, a x-axis will be assumed to be zonal. If the string starts with any other character, there will be no hemisperic lables added, i.e N, S, W or E. fmt : format string of the number ''' def do_nothing(x): return x func_code = ngl.get_string(plot, "tm" + ax + "LabelFuncCode") fmt_s = func_code.join([fmt, "S", "o", "N", ""]) if not direction: if ax[0].lower() == "x": direction = "zonal" else: direction = "meridional" if direction[0].lower() == "z": hem_label = ("W", "E") func = abs elif direction[0].lower() == "m": hem_label = ("S", "N") func = abs else: hem_label = ("", "") func = do_nothing tm_values = ngl.get_float_array(plot, "tm" + ax + "Values") mtm_values = ngl.get_float_array(plot, "tm" + ax + "MinorValues") labels = [fmt_s % func(val) + hem_label[0] if val < 0. else fmt_s % func(val) + hem_label[1] for val in tm_values] rlist = {"tm" + ax + "Mode": "Explicit", "tm" + ax + "Values": tm_values, "tm" + ax + "MinorValues": mtm_values, "tm" + ax + "Labels": labels} _set_values(plot, rlist)
def add_axis(wks, plot, ax="XB", offset=0., res=None): val_ax = ("XT", "XB", "YL", "YR") if not res: res = {} else: res = _resource2dict(res) resp = {} keys = ["vpXF", "vpYF", "vpWidthF", "vpHeightF", "trXMinF", "trXMaxF", "trYMinF", "trYMaxF"] for a in val_ax: for k in ("LabelFontHeight", "MajorOutwardLength", "MinorOutwardLength"): keys.append("tm{}{}F".format(a, k)) for k in keys: resp[k] = ngl.get_float(plot, k) for k in ("tm" + a + k for a in val_ax for k in ("Values", "MinorValues")): resp[k] = ngl.get_float_array(plot, k) for k in ("tm{}MinorPerMajor".format(a) for a in val_ax): resp[k] = ngl.get_integer(plot, k) for k in ("tm{}MinorOn".format(a) for a in val_ax): resp[k] = (ngl.get_integer(plot, k) == 1) for k in ("tm" + a + "Labels".format(a) for a in val_ax): resp[k] = ngl.get_string_array(plot, k) resp.update(res) for a in ("XT", "XB", "YL", "YR"): resp["tm{}Mode".format(a)] = "Explicit" resp["tm{}On".format(a)] = (a == ax) resp["tm{}BorderOn".format(a)] = (a == ax) resp["nglDraw"] = False resp["nglFrame"] = False blank_plot = ngl.blank_plot(wks, _dict2Resource(resp)) amres = {"amJust": "CenterCenter"} if ax[1].lower() in "lt": ampos_sig = -1. else: ampos_sig = 1. if ax[0].lower() == "x": amres["amOrthogonalPosF"] = ampos_sig * offset else: amres["amParallelPosF"] = ampos_sig * offset return ngl.add_annotation(plot, blank_plot, _dict2Resource(amres))
def cn_neg_dash_contours(plot, pattern=2): '''Changes the line dash pattern of the negative contour lines. cn_neg_dash_contours(plot, pattern=2) plot : plot Id of the plot to modify pattern : Pattern index. See http://www.ncl.ucar.edu/Document/Graphics/Images/dashpatterns.png for a list of available patterns. ''' levels = ngl.get_float_array(plot, "cnLevels") patterns = np.zeros((len(levels)), 'i') patterns[:] = 0 # solid line for i in xrange(len(levels)): if (levels[i] < 0.): patterns[i] = pattern rlist = {"cnLineDashPatterns": patterns, "cnMonoLineDashPattern": False} _set_values(plot, rlist)
def neg_dash_contours(contour): # # Retrieve the contour levels used. # levels = Ngl.get_float_array(contour,"cnLevels") # # Create an array to hold the line dash patterns. # patterns = numpy.zeros((len(levels)),'i') patterns[:] = 0 # solid line # # Make contour lines above 0 dashed. # for i in xrange(len(levels)): if (levels[i] > 0.): patterns[i] = 2 rlist = Ngl.Resources() rlist.cnLineDashPatterns = patterns rlist.cnMonoLineDashPattern = False Ngl.set_values(contour,rlist)
def neg_dash_contours(contour): # # Retrieve the contour levels used. # levels = Ngl.get_float_array(contour,"cnLevels") # # Create an array to hold the line dash patterns. # patterns = numpy.zeros((len(levels)),'i') patterns[:] = 0 # solid line # # Make contour lines above 0 dashed. # for i in xrange(len(levels)): if (levels[i] > 0.): patterns[i] = 2 rlist = Ngl.Resources() rlist.cnLineDashPatterns = patterns rlist.cnMonoLineDashPattern = False Ngl.set_values(contour,rlist)
res.sfYCellBounds = vlat #-- needed if set cnFillMode = "CellFill" res.tiMainString = "ICON grid - raster fill" res.tiMainOffsetYF = 0.03 #-- move main title towards plot #-- create the contour plot to retrieve levels and colors plot = Ngl.contour_map(wks,var,res) Ngl.draw(plot) Ngl.frame(wks) del(levels) #========== second plot ============ #-- retrieve the colors and levels from contour plot levels = list(Ngl.get_float_array(plot.contour,"cnLevels")) labels = list(Ngl.get_string_array(plot.contour,"cnLevels")) nlevels = len(levels) #-- define colormap (RGB value range 0-255) colormap255 = np.array([[ 0, 0, 0], [255, 255, 255], [ 80, 255, 255], \ [ 27, 44, 98], [ 45, 102, 175], [ 66, 133, 196], \ [ 90, 166, 217], [119, 189, 230], [148, 211, 243], \ [215, 239, 249], [237, 248, 252], [255, 255, 255], \ [254, 248, 218], [253, 208, 107], [253, 167, 49], \ [247, 124, 43], [231, 75, 41], [203, 30, 38], \ [146, 21, 25]], int) ncol = colormap255.shape[0] #-- number of colors colormap = np.array(colormap255)/255. #-- convert to RGB value range 0.0-1.0
print("\n-----------printing some colormap values------------\n") print("color index 5 (NavyBlue) = ", cmap[5, :]) print("color index 10 (Purple) = ", cmap[10, :]) print("color index 15 (Gold) = ", cmap[15, :]) # # Retrieve some resource values of various types. # # Note that in order to retrieve some XY resources, like line colors, # dash patterns, markers, etc, you need to use the "xydspec" # attribute. This represents the DataSpec object which is created # internally whenever an XY plot is created. # cmap_len = Ngl.get_integer(wks, "wkColorMapLen") markers = Ngl.get_integer_array(xy.xydspec, "xyMarkers") thicknesses = Ngl.get_float_array(xy.xydspec, "xyLineThicknesses") line_mode = Ngl.get_string(xy.xydspec, "xyMarkLineMode") style = Ngl.get_integer(xy, "xyYStyle") print("\n-----------printing some resource values------------\n") print("color map len = ", cmap_len, " (should be 23)") print("markers = ", markers, " (should be [11 12 15 16])") print("thicknesses = ", thicknesses, " should be ([3. 4. 5. 6.])") print("mark/line mode = ", line_mode, " (should be 'MarkLines')") print("style = ", style, " (should be 1)") # # Here's another way to retrieve the color map. # del cmap cmap = Ngl.get_MDfloat_array(wks, "wkColorMap")
resources.cnFillOn = True # Turn on contour level fill. resources.cnMonoFillColor = True # Use one fill color. resources.cnMonoFillPattern = False # Use multiple fill patterns. resources.cnLineLabelAngleF = 0. # Draw contour line labels right-side up. resources.cnLevelSpacingF = 1.0 resources.nglDraw = False # Don't draw the plot or advance the resources.nglFrame = False # frame in the call to Ngl.contour. resources.nglMaximize = False resources.pmLabelBarDisplayMode = "Never" # Turn off label bar. contour = Ngl.contour(wks, unew, resources) # Create a contour plot. levels = Ngl.get_float_array(contour,"cnLevels") patterns = numpy.zeros((len(levels)+1),'i') patterns[:] = -1 for i in xrange(len(levels)): if (levels[i] <= -6.): patterns[i] = 5 else: if (levels[i] > 0.): patterns[i] = 17. patterns[-1] = 17 # last pattern rlist = Ngl.Resources() rlist.cnFillPatterns = patterns rlist.cnFillScaleF = 0.8
resources.cnFillOn = True # Turn on contour level fill. resources.cnMonoFillColor = True # Use one fill color. resources.cnMonoFillPattern = False # Use multiple fill patterns. resources.cnLineLabelAngleF = 0. # Draw contour line labels right-side up. resources.cnLevelSpacingF = 1.0 resources.nglDraw = False # Don't draw the plot or advance the resources.nglFrame = False # frame in the call to Ngl.contour. resources.nglMaximize = False resources.pmLabelBarDisplayMode = "Never" # Turn off label bar. contour = Ngl.contour(wks, unew, resources) # Create a contour plot. levels = Ngl.get_float_array(contour,"cnLevels") patterns = numpy.zeros((len(levels)+1),'i') patterns[:] = -1 for i in xrange(len(levels)): if (levels[i] <= -6.): patterns[i] = 5 else: if (levels[i] > 0.): patterns[i] = 17. patterns[-1] = 17 # last pattern rlist = Ngl.Resources() rlist.cnFillPatterns = patterns rlist.cnFillScaleF = 0.8
print "\n-----------printing some colormap values------------\n" print "color index 5 (NavyBlue) = ",cmap[5,:] print "color index 10 (Purple) = ",cmap[10,:] print "color index 15 (Gold) = ",cmap[15,:] # # Retrieve some resource values of various types. # # Note that in order to retrieve some XY resources, like line colors, # dash patterns, markers, etc, you need to use the "xydspec" # attribute. This represents the DataSpec object which is created # internally whenever an XY plot is created. # cmap_len = Ngl.get_integer(wks,"wkColorMapLen") markers = Ngl.get_integer_array(xy.xydspec,"xyMarkers") thicknesses = Ngl.get_float_array(xy.xydspec,"xyLineThicknesses") line_mode = Ngl.get_string(xy.xydspec,"xyMarkLineMode") style = Ngl.get_integer(xy,"xyYStyle") print "\n-----------printing some resource values------------\n" print "color map len = ",cmap_len," (should be 23)" print "markers = ",markers," (should be [11 12 15 16])" print "thicknesses = ",thicknesses," should be ([3. 4. 5. 6.])" print "mark/line mode = ",line_mode," (should be 'MarkLines')" print "style = ",style," (should be 1)" # # Here's another way to retrieve the color map. # del cmap cmap = Ngl.get_MDfloat_array(wks,"wkColorMap")