From 020abb6d047f632a1a2dde3012e0205d28577352 Mon Sep 17 00:00:00 2001 From: Javier Vegas-Regidor Date: Thu, 30 Jul 2020 17:18:46 +0200 Subject: [PATCH 1/2] Add subplots support --- mapgenerator/plotting/definitions.py | 5 ++ mapgenerator/plotting/plotmap.py | 124 +++++++++++++-------------- 2 files changed, 67 insertions(+), 62 deletions(-) diff --git a/mapgenerator/plotting/definitions.py b/mapgenerator/plotting/definitions.py index d391cb3..1670148 100644 --- a/mapgenerator/plotting/definitions.py +++ b/mapgenerator/plotting/definitions.py @@ -617,6 +617,11 @@ class MapDrawOptions(object): self.coastsopts = ".5,grey" self.countropts = ".3,grey" + self.save = True + self.subplot = None + + self.boundinglat = None + ## FILLED COUNTOUR OPTIONS ## AIR QUALITY OPTIONS diff --git a/mapgenerator/plotting/plotmap.py b/mapgenerator/plotting/plotmap.py index d38128b..aa7e3b6 100644 --- a/mapgenerator/plotting/plotmap.py +++ b/mapgenerator/plotting/plotmap.py @@ -337,14 +337,15 @@ class PlotMap(MapCross, MapDrawOptions): # overwrite option if os.path.exists(figName + ".png") and not self.overwrite: print(figName, " already exists.") - plt.clf() + if self.subplot is None: + plt.clf() return figName map_data = data.getMapData() # FIXME scatter_data = data.getScatterData() or cur_scatter_data - - plt.clf() + if self.subplot is None: + plt.clf() # params = { # 'font.size': 14, # 'text.fontsize': 28, @@ -361,14 +362,26 @@ class PlotMap(MapCross, MapDrawOptions): #print("-----------", lon_0, lat_0, "-----------") else: lon_0, lat_0 = None, None - self.map = Basemap( - projection=self.projection, resolution=self.resolution, - llcrnrlon=self.lon[0], llcrnrlat=self.lat[0], - urcrnrlon=self.lon[-1], urcrnrlat=self.lat[-1], - lon_0=lon_0, lat_0=lat_0, - fix_aspect=self.keep_aspect, - area_thresh=self.area_thresh - ) + if self.subplot: + self.map = Basemap( + ax=self.mgaxis, + projection=self.projection, resolution=self.resolution, + llcrnrlon=self.lon[0], llcrnrlat=self.lat[0], + urcrnrlon=self.lon[-1], urcrnrlat=self.lat[-1], + lon_0=lon_0, lat_0=lat_0, + fix_aspect=self.keep_aspect, + area_thresh=self.area_thresh + ) + else: + self.map = Basemap( + projection=self.projection, resolution=self.resolution, + llcrnrlon=self.lon[0], llcrnrlat=self.lat[0], + urcrnrlon=self.lon[-1], urcrnrlat=self.lat[-1], + lon_0=lon_0, lat_0=lat_0, + fix_aspect=self.keep_aspect, + area_thresh=self.area_thresh, + boundinglat=self.boundinglat, + ) glon, glat = grid #log.info("GLON: %s, GLAT: %s" % (str(glon.shape), str(glat.shape))) @@ -704,51 +717,35 @@ f=image" % (self.lon[0], self.lat[0], self.lon[-1], self.lat[-1], size, self.dpi log.warning("printing {}".format(fullname)) if self.kml or self.kmz: #self.mgplot.axes(frameon=0) - self.mgplot.savefig(fullname, - bbox_inches='tight', - frameon=0, - pad_inches=0, dpi=self.dpi, transparent=True) + if self.save: + self.mgplot.savefig(fullname, + bbox_inches='tight', + frameon=0, + pad_inches=0, dpi=self.dpi, transparent=True) else: #a = plt.axes([0.0, 0.0, 1.0, 1.0]) #plt.axis('off') - plt.setp(plt.gca().spines.values(), linewidth=.1) + plt.setp(self.mgaxis.spines.values(), linewidth=.1) xpos = self.xsize/2 if not self.keep_aspect: ypos = ystart_norm + ysize_norm + 0.05 else: ypos = self.ysize*0.95 - self.mgplot.text( - xpos, - ypos, - imgTitle, - horizontalalignment='center', - verticalalignment='top', - fontsize=self.fontsize, - zorder=0, - ) - self.mgplot.savefig(fullname, bbox_inches='tight', pad_inches=.2, dpi=self.dpi) - #self.mgplot.show() - - #plt.tight_layout() - #self.printTime("saving") - -# if(self.first_image): -# #if first image of the batch, produce a image "color map" -# print "First image of the batch: creating image color map" -# self.mapName = "%s.map.png" % figName -# # Get the colorbar to generate the image color map -# self.runCommand("convert -crop 70x600+730+0 -colors 160 %s.png %s" % (figName, self.mapName)) -# self.first_image = False - - #convert from png to gif - #print "printing ", figName -# self.runCommand("convert -quality 80 +dither -remap %s %s.png %s.anim.png" % (self.mapName, figName, figName)) -# self.runCommand("convert +dither %s.png %s.gif" % (figName, figName)) -# self.runCommand("rm %s.png" % figName) - - #plt.clf() - #self.printTime("convert") + if self.subplot is None: + self.mgplot.text( + xpos, + ypos, + imgTitle, + horizontalalignment='center', + verticalalignment='top', + fontsize=self.fontsize, + zorder=0, + ) + else: + self.mgaxis.set_title(imgTitle, fontsize=self.fontsize, zorder=0,) + if self.save: + self.mgplot.savefig(fullname, bbox_inches='tight', pad_inches=.2, dpi=self.dpi) return figName @@ -956,9 +953,12 @@ f=image" % (self.lon[0], self.lat[0], self.lon[-1], self.lat[-1], size, self.dpi else: fName = 'mg_aplot' figName = "%s/%s" % (self.outdir, fName) - - plt.clf() - self.mgplot, self.mgaxis = plt.subplots() + if self.subplot is None: + plt.clf() + self.mgplot, self.mgaxis = plt.subplots() + else: + self.mgaxis = plt.subplot(self.subplot[0], self.subplot[1], self.subplot[2]) + self.mgplot = plt.gcf() fn = self.genImageMap( figName, @@ -1088,12 +1088,12 @@ f=image" % (self.lon[0], self.lat[0], self.lon[-1], self.lat[-1], size, self.dpi START = 0 for nTime in self.timesteps: #range(START,int(TOTAL),INTERVAL): + if self.subplot is None: + plt.clf() + self.mgplot, self.mgaxis = plt.subplots() + else: + self.mgplot, self.mgaxis = plt.subplot(self.subplot[0], self.subplot[1], self.subplot[3]) - plt.clf() - -# self.mgplot = plt.figure() -# self.mgaxis = plt.gca() - self.mgplot, self.mgaxis = plt.subplots() valid_tmp = dims[nTime] currDate = datetime.strptime("%s %s %s %s" % (valid_tmp[-4:], valid_tmp[-7:-4], valid_tmp[-9:-7], valid_tmp[0:2]), "%Y %b %d %H" ) @@ -1197,13 +1197,13 @@ f=image" % (self.lon[0], self.lat[0], self.lon[-1], self.lat[-1], size, self.dpi os.mkdir("%s-%s" % (runDate, self.srcvars[0])) except: pass - - fig.savefig( - "%s/%s-%s/%s-colorbar.png" % (self.outdir, runDate, self.srcvars[0], runDate), - bbox_inches='tight', - pad_inches=0, dpi=self.dpi, - transparent=True - ) + if self.save: + fig.savefig( + "%s/%s-%s/%s-colorbar.png" % (self.outdir, runDate, self.srcvars[0], runDate), + bbox_inches='tight', + pad_inches=0, dpi=self.dpi, + transparent=True + ) #generate KMZ - Offline if self.kmz: -- GitLab From 8e160f2097e09c229a0e3d994cbc70c7ad6b54ef Mon Sep 17 00:00:00 2001 From: Javier Vegas-Regidor Date: Fri, 21 Aug 2020 09:17:25 +0200 Subject: [PATCH 2/2] Add boundinglat option --- mapgenerator/plotting/plotmap.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mapgenerator/plotting/plotmap.py b/mapgenerator/plotting/plotmap.py index aa7e3b6..4532e18 100644 --- a/mapgenerator/plotting/plotmap.py +++ b/mapgenerator/plotting/plotmap.py @@ -370,7 +370,8 @@ class PlotMap(MapCross, MapDrawOptions): urcrnrlon=self.lon[-1], urcrnrlat=self.lat[-1], lon_0=lon_0, lat_0=lat_0, fix_aspect=self.keep_aspect, - area_thresh=self.area_thresh + area_thresh=self.area_thresh, + boundinglat=self.boundinglat, ) else: self.map = Basemap( -- GitLab