def contour_map(self): params_dict = self.params_dict color_labels = self.color_labels color_levels = self.color_levels cn_fill_colors = np.arange(0,color_levels,1) nboxes = np.shape(cn_fill_colors)[0] # labels = [""] * nboxes #3.图例解释数据 # if (self.output_img_name.contains("RH") or self.output_img_name.contains("TK")): # labels[0] = "NoData" # labels[1] = "<" + str(cn_levels[1]) # labels[nboxes - 1] = ">" + str(cn_levels[nboxes - 2]) # for i in range(2, nboxes - 1): # labels[i] = str(cn_levels[i - 1]) + "~" + str(cn_levels[i]) # else: # labels[0] = "<" + str(cn_levels[0]) # labels[nboxes - 1] = ">" + str(cn_levels[nboxes - 2]) # for i in range(1, nboxes - 1): # labels[i] = str(cn_levels[i - 1]) + "~" + str(cn_levels[i]) #4.其他配置 params_dict['vpHeightF'] = 0.022 * 0.868 * (nboxes + 3 - (nboxes - 5) / 2.8) params_dict['lbFillColors'] = cn_fill_colors if (self.output_img_name.contains("TK")): params_dict["lbLabelFontHeightF"] = 0.009 else: params_dict["lbLabelFontHeightF"] = 0.009677 lbres = create_or_update_resource(params_dict=params_dict) lbid = Ngl.labelbar_ndc(self.workstation, nboxes, color_labels, 0, 0, lbres) #5.色标位置配置 lblres = create_or_update_resource(params_dict=self.location_params_dict) Ngl.add_annotation(self.plot, lbid, lblres)
def subtitles(wks, plot, left_string, center_string, right_string, tres): ttres = tres # Copy resources ttres.nglDraw = False # Make sure string is just created, not drawn. # # Retrieve font height of left axis string and use this to calculate # size of subtitles. # if not hasattr(ttres, "txFontHeightF"): font_height = Ngl.get_float(plot.base, "tiXAxisFontHeightF") ttres.txFontHeightF = font_height * 0.8 # Slightly smaller # # Set some some annotation resources to describe how close text # is to be attached to plot. # amres = Ngl.Resources() if not hasattr(ttres, "amOrthogonalPosF"): amres.amOrthogonalPosF = -0.51 # Top of plot plus a little extra # to stay off the border. else: amres.amOrthogonalPosF = ttres.amOrthogonalPosF # # Create three strings to put at the top, using a slightly # smaller font height than the axis titles. # if left_string != "": txidl = Ngl.text(wks, plot, left_string, 0., 0., ttres) amres.amJust = "BottomLeft" amres.amParallelPosF = -0.5 # Left-justified annoidl = Ngl.add_annotation(plot, txidl, amres) if center_string != "": txidc = Ngl.text(wks, plot, center_string, 0., 0., ttres) amres.amJust = "BottomCenter" amres.amParallelPosF = 0.0 # Centered annoidc = Ngl.add_annotation(plot, txidc, amres) if right_string != "": txidr = Ngl.text(wks, plot, right_string, 0., 0., ttres) amres.amJust = "BottomRight" amres.amParallelPosF = 0.5 # Right-justifed annoidr = Ngl.add_annotation(plot, txidr, amres) return
def right_axis(wks, plot, yaxis_string, tres): ttres = tres # Copy resources ttres.nglDraw = False # Make sure string is just created, not drawn. ttres.txAngleF = -90. # Use 90 to rotate other direction. # # Retrieve font height of left axis string and use to calculate size of # right axis string # if not hasattr(ttres, "txFontHeightF"): ttres.txFontHeightF = Ngl.get_float(plot.base, "tiYAxisFontHeightF") # # Set up variable to hold annotation resources. # amres = Ngl.Resources() # # Create string to put at right of plot, like a Y axis title. # if yaxis_string != "": txid = Ngl.text(wks, plot, yaxis_string, 0., 0., ttres) amres.amJust = "CenterCenter" amres.amParallelPosF = 0.55 # Move towards plot. annoid = Ngl.add_annotation(plot, txid, amres) return
def contour_map(self): params_dict = self.params_dict color_levels = self.color_levels cn_fill_colors = np.arange(0, len(color_levels), 1) # 1.绘制南海底板 south_sea_baseboard = params_dict.pop('south_sea_baseboard') south_sea_baseboard['cnLevels'] = color_levels south_sea_baseboard['cnFillColors'] = cn_fill_colors resource = create_or_update_resource(params_dict=south_sea_baseboard) south_sea_plot = Ngl.contour_map(self.workstation, self.input_data, resource) # 2.绘制南海相关地理线 south_sea_geoline = params_dict.pop('south_sea_geoline') # 多级 for key, params_dict in south_sea_geoline.items(): file_name = params_dict.pop('file_name') type = params_dict.pop('type') shape = Nio.open_file(shape_file_path + file_name, "r") lon = np.ravel(shape.variables["x"][:]) lat = np.ravel(shape.variables["y"][:]) params_dict['gsSegments'] = shape.variables["segments"][:, 0] resource = create_or_update_resource(params_dict=params_dict) # 2.绘制曲线图 if type == 'polyline': Ngl.add_polyline(self.workstation, south_sea_plot, lon, lat, resource) else: pass #polygon/point 处理待定 # 3. 南海加比例尺 south_sea_scale = params_dict.pop('south_sea_scale') scala_figure = south_sea_scale.pop('scala_figure') x, y = south_sea_scale.pop('location').split('x') resource = create_or_update_resource(params_dict=south_sea_scale) Ngl.add_text(self.workstation, south_sea_plot, scala_figure, x, y, resource) # 4.南海放在中国的位置 south_sea_location = params_dict.pop('south_sea_location') resource = create_or_update_resource(params_dict=south_sea_location) Ngl.add_annotation(self.plot, south_sea_plot, resource)
def postplot(self, wks, plot): """Annotate the plot with title strings.""" for position in ('left', 'right', 'center'): for string_spec in self.string_specs[position]: # Create text and annotation resource variables based on the # current string specification. txres = self._text_resources(string_spec, position) anres = self._annotation_resources(string_spec, position) # Create the actual text object. text_object = Ngl.text_ndc(wks, string_spec['string'], 0., 0., txres) # Add the text object to the plot as an annotation. The plot # is a mutable object so doing this attaches the annotation to # the input plot. anno = Ngl.add_annotation(plot, text_object, anres)
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))
sres = Ngl.Resources() sres.tiMainOn = False Ngl.set_values(plot2, sres) Ngl.draw(plot1) Ngl.draw(plot2) Ngl.frame(wks) # # Just to show it can be done, add the second plot as an annotation # of the first plot. # sres = Ngl.Resources() sres.amZone = 0 # '0' means centered over base plot. sres.amResizeNotify = True anno = Ngl.add_annotation(plot1, plot2, sres) # # Change the main title to reflect this is an added annotation. # sres = Ngl.Resources() sres.tiMainString = "One plot added as annotation of another" Ngl.set_values(plot1, sres) # # Now if you draw plot1, and plot2 will be drawn automatically. Also, # if you resize plot1, the attached plot2 will resize automatically. # Ngl.draw(plot1) Ngl.frame(wks)
lbres.lbTitleFontHeightF = 0.02 lbres.lbTitleOffsetF = 0.00 #---Create the labelbar labels = ["0","1","2","3"] lbid = Ngl.labelbar_ndc (wks,len(labels),labels,0.0,0.,lbres) # # Create some annotation resources indicating how we want to # attach the labelbar to the plot. The default is the center # of the plot. Below amOrthogonalPosF is set to move the # labelbar down and outside the plot. # amres = Ngl.Resources() amres.amOrthogonalPosF = 0.7 annoid = Ngl.add_annotation(plot,lbid,amres) txres = Ngl.Resources() txres.txFontHeightF = 0.01 txres.txJust = "TopRight" txres.txPerimOn = True txid = Ngl.add_text(wks,plot,flnm,res.mpMaxLonF,res.mpMaxLatF,txres) #---This will resize plot so it and the labelbar fit in the frame. Ngl.maximize_plot(wks, plot) #---Drawing the original map also draws the attached labelbar. Ngl.draw(plot) Ngl.frame(wks) Ngl.end()
# Make sure map plot is same size as contour/map plot. mpres.nglMaximize = False # this must be turned off otherwise plot will be resized! mpres.vpXF = bres.vpx mpres.vpYF = bres.vpy mpres.vpWidthF = bres.vpw mpres.vpHeightF = bres.vph # Turn off since they are already drawn in contour/map plot. mpres.pmTickMarkDisplayMode = "Never" # Make the ocean white and land transparent. This will mask out the contour fill over ocean. mpres.mpOceanFillColor = "white" mpres.mpInlandWaterFillColor = "white" mpres.mpLandFillColor = "transparent" # Create a map plot with the white ocean and transparent land. map_plot = Ngl.map(wks, mpres) # # If you need to resize the plots later---for example, to use in # a panel---then it's easier to make one plot an annotation of # the other. # annoid = Ngl.add_annotation(contour_map_plot, map_plot) Ngl.draw(contour_map_plot) # This draws both plots Ngl.frame(wks) Ngl.end()
sres = Ngl.Resources() sres.tiMainOn = False Ngl.set_values(plot2,sres) Ngl.draw(plot1) Ngl.draw(plot2) Ngl.frame(wks) # # Just to show it can be done, add the second plot as an annotation # of the first plot. # sres = Ngl.Resources() sres.amZone = 0 # '0' means centered over base plot. sres.amResizeNotify = True anno = Ngl.add_annotation(plot1,plot2,sres) # # Change the main title to reflect this is an added annotation. # sres = Ngl.Resources() sres.tiMainString = "One plot added as annotation of another" Ngl.set_values(plot1,sres) # # Now if you draw plot1, and plot2 will be drawn automatically. Also, # if you resize plot1, the attached plot2 will resize automatically. # Ngl.draw(plot1) Ngl.frame(wks)
lbres.lbFillColors = colors lbres.lbMonoFillPattern = True # Fill them all solid. lbres.lbLabelFontHeightF = 0.013 # label font height lbres.nglDraw = False # The labelbar will be created, but not drawn at this point. lbid = Ngl.labelbar_ndc(wks,nboxes,labels,0.5,0.2,lbres) # # Now create some annotation resources indicating how we want to # attach the labelbar to the plot. Here, we are using the top center # of the labelbar as the point which we are going to position # it, and then we use amOrthogonalPosF to move it down. # # amParallelPosF/amOrthogonalPosF # 0.0/ 0.0 - annotation in dead center of plot # 0.5/ 0.5 - annotation at bottom right of plot # 0.5/-0.5 - annotation at top right of plot # -0.5/-0.5 - annotation at top left of plot # -0.5/ 0.5 - annotation at bottom left of plot # amres = Ngl.Resources() amres.amJust = "TopCenter" amres.amOrthogonalPosF = 0.4 annoid = Ngl.add_annotation(map,lbid,amres) Ngl.draw(map) Ngl.frame(wks) Ngl.end ()
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)
nhres.mpMinLonF = 105.0 nhres.mpMaxLonF = 123.0 nhres.lbLabelBarOn = False nhres.tmXBOn = False nhres.tmXTOn = False nhres.tmYLOn = False nhres.tmYROn = False map_nanhai = Ngl.contour_map(wks, pres, nhres) adres = Ngl.Resources() adres.amParallelPosF = 0.495 # -0.5 is the left edge of the plot. adres.amOrthogonalPosF = 0.49 # -0.5 is the top edge of the plot. adres.amJust = "BottomRight" plotnh = Ngl.add_annotation(map, map_nanhai, adres) #--- add Yangtze and Yellow rivers --- plotrv = add_shapefile_outlines(wks, map, "./cnmap_NetCDF/rivers.nc", "blue", 3.0) #--- add City --- plotct = add_shapefile_outlines(wks, map, "./cnmap_NetCDF/diquJie_polyline.nc", "grey") Ngl.draw(map) Ngl.frame(wks) Ngl.end()
lbres.lbFillColors = colors lbres.lbMonoFillPattern = True # Fill them all solid. lbres.lbLabelFontHeightF = 0.013 # label font height lbres.nglDraw = False # The labelbar will be created, but not drawn at this point. lbid = Ngl.labelbar_ndc(wks, nboxes, labels, 0.5, 0.2, lbres) # # Now create some annotation resources indicating how we want to # attach the labelbar to the plot. Here, we are using the top center # of the labelbar as the point which we are going to position # it, and then we use amOrthogonalPosF to move it down. # # amParallelPosF/amOrthogonalPosF # 0.0/ 0.0 - annotation in dead center of plot # 0.5/ 0.5 - annotation at bottom right of plot # 0.5/-0.5 - annotation at top right of plot # -0.5/-0.5 - annotation at top left of plot # -0.5/ 0.5 - annotation at bottom left of plot # amres = Ngl.Resources() amres.amJust = "TopCenter" amres.amOrthogonalPosF = 0.4 annoid = Ngl.add_annotation(map, lbid, amres) Ngl.draw(map) Ngl.frame(wks) Ngl.end()