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))
bres.tmXBMode = "Explicit" bres.tmXBValues = values bres.tmXBLabels = labels bres.tmXBLabelFontHeightF = 0.01 # Make these labels smaller. bres.tmXBMajorOutwardLengthF = 0.0 # Don't draw tickmarks b/c they bres.tmXBMajorLengthF = 0.0 # were drawn on previous plot. bres.tmXBLabelDeltaF = 0.6 # Move label away from tickmarks. bres.tmXBLabelFontColor = "Brown" bres.tmYROn = False # Turn off right tickmarks. bres.tmXTOn = False # Turn off top tickmarks. bres.tmYLOn = False # Turn off left tickmarks. #---Create the blank plot. blank = Ngl.blank_plot(wks, bres) #---Overlay on existing XY plot. Ngl.overlay(plot, blank) #---Drawing the original plot also draws the overlaid blank plot Ngl.draw(plot) Ngl.frame(wks) # # Second plot section. # #---Create dummy data for additional curve. npts2 = 40 y2 = numpy.zeros([npts2], 'f')
bres.tmXBMode = "Explicit" bres.tmXBValues = lon_values bres.tmXBLabels = lon_labels bres.tmXBLabelFontHeightF = 0.015 # Make labels smaller. This # will affect Y labels too. #---Set the values and labels for the Y axis of blank plot. bres.tmYLMode = "Explicit" bres.tmYLValues = lat_values bres.tmYLLabels = lat_labels #---Align four corners of both plots, that is, don't do data transformation bres.tfDoNDCOverlay = True #---Create the blank plot. blank = Ngl.blank_plot(wks,bres) #---Draw blank plot for debugging purposes. #Ngl.draw(blank) #Ngl.frame(wks) #---Overlay blank plot on existing map plot. Ngl.overlay(map.base,blank) #---Resize map so it fits within frame Ngl.maximize_plot(wks, map) #---Drawing the original map also draws the overlaid blank plot Ngl.draw(map) Ngl.frame(wks)
def add_map_tickmarks(wks,map,lat_spc,lon_spc): bres = Ngl.Resources() bres.nglMaximize = False #---Set some resources based on map plot already created. bres.vpXF = Ngl.get_float(map, "vpXF") bres.vpYF = Ngl.get_float(map, "vpYF") bres.vpWidthF = Ngl.get_float(map, "vpWidthF") bres.vpHeightF = Ngl.get_float(map, "vpHeightF") bres.trXMinF = Ngl.get_float(map, "trXMinF") bres.trXMaxF = Ngl.get_float(map, "trXMaxF") bres.trYMinF = Ngl.get_float(map, "trYMinF") bres.trYMaxF = Ngl.get_float(map, "trYMaxF") bres.tmEqualizeXYSizes = True # make sure labels same size #---Create longitude labels based on longitude spacing given. lon_values = np.arange(-180,181,lon_spc) lon_labels = [] for l in lon_values: if l < 0: lon_labels.append("{}~S~o~N~W".format(np.fabs(l))) elif l > 0: lon_labels.append("{}~S~o~N~E".format(l)) else: lon_labels.append("0") #---Create latitude labels based on latitude spacing given. lat_values = np.arange(-90,91,lat_spc) lat_labels = [] for l in lat_values: if l < 0: lat_labels.append("{}~S~o~N~S".format(np.fabs(l))) elif l > 0: lat_labels.append("{}~S~o~N~N".format(l)) else: lat_labels.append("EQ") #---Set tickmark resources bres.tmXBMode = "Explicit" bres.tmXBValues = lon_values bres.tmXBLabels = lon_labels bres.tmYLMode = "Explicit" bres.tmYLValues = lat_values bres.tmYLLabels = lat_labels bres.tmYLLabelFontHeightF = 0.009 # Make these labels smaller. bres.tmXBLabelFontHeightF = 0.009 # Ditto #---To urn on tickmark lines, change these values to something like 0.01 #---Turn off tickmark lines bres.tmXBMajorLengthF = 0. bres.tmYLMajorLengthF = 0. bres.tmXBMajorOutwardLengthF = 0. bres.tmYLMajorOutwardLengthF = 0. #---Create the blank plot with the special labels blank = Ngl.blank_plot(wks,bres) #---Attach blank plot with special labels to map plot, and return sres = Ngl.Resources() sres.amZone = 0 # '0' means centered over base plot. sres.amResizeNotify = True return Ngl.add_annotation(map,blank,sres)