def lb_format_labels(plot, fmt, minmax=None): '''Applies a formatting to the lables of a label bar lb_format_labels(plot, fmt, minmax=None) plot : plot id of object that controlls the lable bar fmt : formatting string minmax : values of the maximum and minimum. Required, if cnLabelBarEndStyle is set to IncludeMinMaxLabels. If not provided in this case, a ValueError will be raised. ''' levels = [float(s) for s in ngl.get_string_array(plot, "cnLevels")] labels = [fmt % lev for lev in levels] if ngl.get_string(plot, "cnLabelBarEndStyle") == "IncludeMinMaxLabels": if not minmax: raise ValueError("You need to provide minmax," + " since cnLabelBarEndStyle is set to " + "IncludeMinMaxLabels!") minlabel = [fmt % minmax[0]] minlabel.extend(labels) labels = minlabel labels.append(fmt % minmax[1]) rlist = {"cnExplicitLabelBarLabelsOn": True, "lbLabelStrings": 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 lb_set_phase_labels(plot): '''Changes the labels of a color bar to -pi, -pi/2, 0, pi/2 and pi. The label bar must already have only these lables. lb_set_phase_labels(plot) plot : plot id of object that controlls the lable bar ''' lstride = ngl.get_integer(plot, "lbLabelStride") label = ngl.get_string_array(plot, "lbLabelStrings") fun_code = ngl.get_string(plot, "lbLabelFuncCode") pi_str = fun_code.join(["", "F33", "p"]) pi_half_str = fun_code.join(["", "H2V15F33", "p", "H-15V-1", "_", "H-16V-30", "2"]) label[::lstride] = ["-" + pi_str, "-" + pi_half_str, "0", pi_half_str, pi_str] rlist = {"cnExplicitLabelBarLabelsOn": True, "lbLabelStrings": label} _set_values(plot, rlist)
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 #-- open a workstation for second plot: triangles plot
# # Open a workstation and draw a contour plot. # wks_type = "ps" wks = Ngl.open_wks(wks_type,"cns01p") res = Ngl.Resources() res.cnMonoLineDashPattern = False plot1 = Ngl.contour(wks,T,res) # # Retrieve the automatically set line labels. # These will be: ['-80', '-60', '-40', '-20', '0', '20', '40', '60', '80'] # line_labels1 = Ngl.get_string_array(plot1,"cnLineLabelStrings") # # Set explicit line labels. Notice that the dash line # setting carries over from the first plot. # res.cnExplicitLineLabelsOn = True res.cnLineLabelStrings = ["Lab1", "Lab2", "Lab3", "Lab4", "Lab5", "Lab6", "Lab7", "Lab8", "Lab9"] plot2 = Ngl.contour(wks,T,res) # # Retrieve the explicitly set line labels. # These will be: ['Lab1', 'Lab2', 'Lab3', 'Lab4', 'Lab5', # 'Lab6', 'Lab7', 'Lab8', 'Lab9'] #
# # Open a workstation and draw a contour plot. # wks_type = "png" wks = Ngl.open_wks(wks_type, "cns01p") res = Ngl.Resources() res.cnMonoLineDashPattern = False plot1 = Ngl.contour(wks, T, res) # # Retrieve the automatically set line labels. # These will be: ['-80', '-60', '-40', '-20', '0', '20', '40', '60', '80'] # line_labels1 = Ngl.get_string_array(plot1, "cnLineLabelStrings") # # Set explicit line labels. Notice that the dash line # setting carries over from the first plot. # res.cnExplicitLineLabelsOn = True res.cnLineLabelStrings = [ "Lab1", "Lab2", "Lab3", "Lab4", "Lab5", "Lab6", "Lab7", "Lab8", "Lab9" ] plot2 = Ngl.contour(wks, T, res) # # Retrieve the explicitly set line labels. # These will be: ['Lab1', 'Lab2', 'Lab3', 'Lab4', 'Lab5', # 'Lab6', 'Lab7', 'Lab8', 'Lab9']
# Arkansas : 01, group 7 # Arkansas : 02, group 8 # Arkansas : 03, group 7 # Arkansas : 04, group 6 # Arkansas : 05, group 5 # Arkansas : 06, group 6 # Arkansas : 07, group 4 # Arkansas : 08, group 7 # Arkansas : 09, group 4 # # From this, if you chose to use the default group numbers to color each # climate area, then climate areas (01,03,08) would be the same color, # (07,09) would be the same, and (04,06) would be the same, and # 02 and 05 would each be their own color. # anames = Ngl.get_string_array(map,"mpAreaNames") groups = Ngl.get_integer_array(map,"mpDynamicAreaGroups") # Areas 659 through 1116 are the states with their climates. # print(anames[659:1116] + ", group " + groups[659:1116]) states = ["Alabama", "Arizona", "Arkansas", "California", "Colorado",\ "Connecticut", "Delaware", "Florida", "Georgia", "Idaho",\ "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",\ "Louisiana", "Maine", "Maryland", "Massasachusetts", "Michigan",\ "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", \ "Nevada", "New Hampshire", "New Jersey", "New Mexico", \ "New York", "North Carolina", "North Dakota", "Ohio", \ "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", \ "South Carolina", "South Dakota", "Tennessee", "Texas", \ "Utah", "Vermont", "Virginia", "Washington", "West Virginia",\
# Arkansas : 01, group 7 # Arkansas : 02, group 8 # Arkansas : 03, group 7 # Arkansas : 04, group 6 # Arkansas : 05, group 5 # Arkansas : 06, group 6 # Arkansas : 07, group 4 # Arkansas : 08, group 7 # Arkansas : 09, group 4 # # From this, if you chose to use the default group numbers to color each # climate area, then climate areas (01,03,08) would be the same color, # (07,09) would be the same, and (04,06) would be the same, and # 02 and 05 would each be their own color. # anames = Ngl.get_string_array(map, "mpAreaNames") groups = Ngl.get_integer_array(map, "mpDynamicAreaGroups") # Areas 659 through 1116 are the states with their climates. # print(anames[659:1116] + ", group " + groups[659:1116]) states = ["Alabama", "Arizona", "Arkansas", "California", "Colorado",\ "Connecticut", "Delaware", "Florida", "Georgia", "Idaho",\ "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",\ "Louisiana", "Maine", "Maryland", "Massasachusetts", "Michigan",\ "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", \ "Nevada", "New Hampshire", "New Jersey", "New Mexico", \ "New York", "North Carolina", "North Dakota", "Ohio", \ "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", \ "South Carolina", "South Dakota", "Tennessee", "Texas", \ "Utah", "Vermont", "Virginia", "Washington", "West Virginia",\