def _RasterGGplotContinuous(self): ''' ''' #Set the colormap cmap = clr.LinearSegmentedColormap('custom', self.cdict) #Open the source raster layer - only to get the extent srcDS, rasterLayer = mjgis.RasterOpenGetFirstLayer(self.srcComp.FPN) #Read tbe first band, including the metadata rasterLayer.ReadBand() #Close the raster data srcDS.CloseDS() #Set the bouunds of the map bounds = (rasterLayer.bounds[0], rasterLayer.bounds[2], \ rasterLayer.bounds[1], rasterLayer.bounds[3]) #Convert the image array to a datarame #colourImg = Image.open("test.png") #colourPixels = colourImg.convert("RGB") #colourArray = np.array(colourPixels.getdata()).reshape(colourImg.size + (3,)) #Flip the array along the lines (this is how the data must be given to p9 mapArrF = np.flipud(self.mapArr) df = pd.DataFrame(mapArrF).reset_index().melt('index') df.columns = ['row', 'col', 'val'] mapplot = (ggplot(df) + geom_tile(aes(x='col', y='row', fill='val'))) print(mapplot) SNULEBULLE '''
def testImage03(): srcFPN = '/Volumes/africa/export/trmm/region/rainfall-ixc-n/africasubsahara/199801-201712@M/obs-lag-best_3b43_africasubsahara_199801-201712@M_v7-f-m-30km-nadd-a1.tif' cdict = { 'red': [(0.0, 0.0, 0.0), (0.5, 1.0, 1.0), (1.0, 1.0, 1.0)], 'green': [(0.0, 0.0, 0.0), (0.25, 0.0, 0.0), (0.75, 1.0, 1.0), (1.0, 1.0, 1.0)], 'blue': [(0.0, 0.0, 0.0), (0.5, 0.0, 0.0), (1.0, 1.0, 1.0)] } cmap = clr.LinearSegmentedColormap('custom', cdict) srcDS, rasterLayer = mjgis.RasterOpenGetFirstLayer(srcFPN) #source = rasterio.open('/Volumes/africa/export/trmm/region/rainfall-ixc-n/africasubsahara/199801-201712@M/obs-lag-best_3b43_africasubsahara_199801-201712@M_v7-f-m-30km-nadd-a1.tif', 'r') rasterLayer.ReadBand() source = rasterLayer.NPBAND srcDS.CloseDS() bounds = (rasterLayer.bounds[0], rasterLayer.bounds[2], \ rasterLayer.bounds[1], rasterLayer.bounds[3]) f = plt.figure(figsize=(6, 6)) ax = plt.imshow(source, extent=bounds, cmap=cmap, vmin=0, vmax=13) plt.colorbar(ticks=range(12), label='digit value') plt.clim(-0.5, 11.5) plt.show()
def testImage01(): srcFPN = '/Volumes/africa/export/trmm/region/rainfall-ixc-n/africasubsahara/199801-201712@M/obs-lag-best_3b43_africasubsahara_199801-201712@M_v7-f-m-30km-nadd-a1.tif' srcDS, rasterLayer = mjgis.RasterOpenGetFirstLayer(srcFPN) #source = rasterio.open('/Volumes/africa/export/trmm/region/rainfall-ixc-n/africasubsahara/199801-201712@M/obs-lag-best_3b43_africasubsahara_199801-201712@M_v7-f-m-30km-nadd-a1.tif', 'r') rasterLayer.ReadBand() bounds = (rasterLayer.bounds[0], rasterLayer.bounds[2], \ rasterLayer.bounds[1], rasterLayer.bounds[3]) srcDS.CloseDS() img = plt.imread(srcFPN) plt.imshow(img, extent=bounds, vmin=2, vmax=13) plt.colorbar(ticks=range(12), label='digit value') plt.clim(-0.5, 11.5) plt.show()
def testImage02(): srcFPN = '/Volumes/africa/export/trmm/region/rainfall-ixc-n/africasubsahara/199801-201712@M/obs-lag-best_3b43_africasubsahara_199801-201712@M_v7-f-m-30km-nadd-a1.tif' colors = [(0.75, 0.15, 0.15), (1, 0.75, 0.15), (0.15, 0.75, 0.15)] nThresholds = 12 cmap = clr.LinearSegmentedColormap.from_list(name='custom', colors=colors, N=nThresholds) srcDS, rasterLayer = mjgis.RasterOpenGetFirstLayer(srcFPN) #source = rasterio.open('/Volumes/africa/export/trmm/region/rainfall-ixc-n/africasubsahara/199801-201712@M/obs-lag-best_3b43_africasubsahara_199801-201712@M_v7-f-m-30km-nadd-a1.tif', 'r') rasterLayer.ReadBand() source = rasterLayer.NPBAND srcDS.CloseDS() bounds = (rasterLayer.bounds[0], rasterLayer.bounds[2], \ rasterLayer.bounds[1], rasterLayer.bounds[3]) f = plt.figure(figsize=(6, 6)) ax = plt.imshow(source, extent=bounds, cmap=cmap, vmin=1, vmax=13) plt.show()
def _RasterMatPlotLibContinuous(self): ''' ''' #Set the colormap cmap = clr.LinearSegmentedColormap('custom', self.cdict) #Open the source raster layer - only to get the extent srcDS, rasterLayer = mjgis.RasterOpenGetFirstLayer(self.srcComp.FPN) #Read tbe first band, including the metadata rasterLayer.ReadBand() #Close the raster data srcDS.CloseDS() #Set the bouunds of the map bounds = (rasterLayer.bounds[0], rasterLayer.bounds[2], \ rasterLayer.bounds[1], rasterLayer.bounds[3]) #Set the font ''' font = {'family' : 'DejaVu Sans', 'weight' : 'normal', 'size' : 16} plt.rc('font', **font) #Set the labelsizes for the x and y axis plt.rc('xtick', labelsize=10) plt.rc('ytick', labelsize=10) ''' fig = plt.figure(figsize=(self.params.plotwidth, self.params.plotheight)) #ax = fig.add_subplot(111) # I have no idea why I do like this ax = plt.subplot(1, 1, 1) #Set title if len(self.params.title) > 0: font = {} if len(self.params.titlefontcolor) > 0: font['color'] = self.params.titlefontcolor if len(self.params.titlefont) > 0: font['family'] = self.params.titlefont if self.params.titlefontsize: font['size'] = self.params.titlefontsize if 'bold' in self.params.titlefonteffect: font['weight'] = 'bold' ax.set_title(self.params.title, y=self.params.titleypos, fontdict=font) #Set the frame (spine) colors, style and width (colors can vary, style and width not) spinecolorL = self.params.spinecolors.split(',') spinelinestyle = self.params.spinelinestyle spinelinewidth = self.params.spinelinewidth ax.spines['top'].set_color(spinecolorL[0]) ax.spines['right'].set_color(spinecolorL[1]) ax.spines['bottom'].set_color(spinecolorL[2]) ax.spines['left'].set_color(spinecolorL[3]) ax.spines['top'].set_linestyle(spinelinestyle) ax.spines['right'].set_linestyle(spinelinestyle) ax.spines['bottom'].set_linestyle(spinelinestyle) ax.spines['left'].set_linestyle(spinelinestyle) if spinelinewidth: #if not it will be default ax.spines['top'].set_linewidth(spinelinewidth) ax.spines['right'].set_linewidth(spinelinewidth) ax.spines['bottom'].set_linewidth(spinelinewidth) ax.spines['left'].set_linewidth(spinelinewidth) #Set the axis major tick, these are always set but if given is set using locator if self.params.majorticks: majorLocator = MultipleLocator(self.params.majorticks) ax.xaxis.set_major_locator(majorLocator) ax.yaxis.set_major_locator(majorLocator) ax.tick_params(which='major', length=self.params.majorticklength, width=self.params.majortickwidth, direction=self.params.majortickdirection) if ',' in self.params.majortickcolors: majtickXcolor, majtickYcolor = self.params.majortickcolors.split( ',') ax.tick_params(axis='x', colors=majtickXcolor) ax.tick_params(axis='y', colors=majtickYcolor) #Major tick mark labels ax.tick_params(labelbottom=True, labeltop=True, labelleft=True, labelright=True, bottom=True, top=True, left=True, right=True) if self.params.axisfontsize: ax.tick_params(labelsize=self.params.axisfontsize) axisXfontcolors = axisYfontcolors = False if ',' in self.params.axisfontcolors: axisXfontcolors, axisYfontcolors = self.params.axisfontcolors.split( ',')[0:2] for tick in ax.get_xticklabels(): if len(self.params.axisfont) > 0: tick.set_fontname(self.params.axisfont) if axisXfontcolors: tick.set_color(axisXfontcolors) for tick in ax.get_yticklabels(): if len(self.params.axisfont) > 0: tick.set_fontname(self.params.axisfont) if axisYfontcolors: tick.set_color(axisYfontcolors) ''' THE ALTERNATIVE BELOW DOES NOT WORK #Get the labels labels = [item.get_text() for item in ax.get_xticklabels()] #Rest the labels using fontdict print ('labels',labels) print ('font',font) ax.set_xticklabels(labels, fontdict=font) ''' #minor ticks only set if defined if self.params.minorticks: if self.params.minorticks < 0: minorLocator = AutoMinorLocator() else: minorLocator = MultipleLocator(self.params.minorticks) mintickXcolor, mintickYcolor = self.params.minortickcolors.split( ',') ax.tick_params(which='minor', length=self.params.minorticklength, width=self.params.minortickwidth, direction=self.params.minortickdirection) ax.tick_params(which='minor', axis='x', colors=mintickXcolor) ax.tick_params(which='minor', axis='y', colors=mintickYcolor) ax.xaxis.set_minor_locator(minorLocator) ax.yaxis.set_minor_locator(minorLocator) ax.tick_params(which='minor', bottom=True, top=True, left=True, right=True) #then also turn on, otherwise some are missing?? ax.minorticks_on() #Never any labels for minor ticks #ax.xaxis.label.set_color('blue') #ax.tick_params(axis='x', colors='red') if self.params.gridlinewidth: gridXcolor, gridYcolor = self.params.gridcolors.split(',') ax.grid(linestyle=self.params.gridlinestyle, linewidth=self.params.gridlinewidth, alpha=self.params.gridalpha) ax.grid(axis='x', color=gridXcolor) ax.grid(axis='y', color=gridYcolor) ''' with plt.rc_context({'axes.edgecolor':'orange', 'xtick.color':'red', 'ytick.color':'green', 'figure.facecolor':'white'}): # Temporary rc parameters in effect fig, (ax1, ax2) = plt.subplots(1,2) ax1.plot(range(10)) ax2.plot(range(10)) ''' #Set north arrow if len(self.params.northarrow) > 6: im = img.imread(self.params.northarrow) imaspect = im.shape[0] / im.shape[1] xsize = self.params.northarrowsize * ( bounds[1] - bounds[0]) / self.params.plotwidth xmargin = self.params.northarrowmarginx * (bounds[1] - bounds[0]) / 100 ymargin = self.params.northarrowmarginy * (bounds[3] - bounds[2]) / 100 #fig, ax = plt.subplots() if self.params.northarrowanchor == 'coords': pass elif self.params.northarrowanchor == 'upperleft': left = bounds[0] + xmargin top = bounds[3] - ymargin right = left + xsize bottom = top - (xsize * imaspect) ax.imshow(im, aspect='auto', extent=(left, right, bottom, top), zorder=3) imgplot = plt.imshow(self.mapArr, extent=bounds, cmap=cmap, vmin=0, vmax=255) #extendfrac = 5/255 #cbar = plt.colorbar(ticks=[0, 63, 125, 188, 250], orientation='horizontal',extend='max', extendfrac=extendfrac) ticks = [] tickLabels = [] for item in self.legendText: ticks.append(item['pos']) tickLabels.append(item['txt']) legsize = '%(ls)d%%' % {'ls': self.params.legendrelativesize} legwidth = '%(ls)d%%' % {'ls': self.params.legendrelativewidth} legheight = '%(ls)d%%' % {'ls': self.params.legendrelativeheight} if self.params.legendfitaxis: divider = make_axes_locatable(ax) cax = divider.append_axes(self.params.legendposition, size=legsize, pad=self.params.legendpad) elif self.params.legendinsetaxis: cax = inset_axes(ax, width=legwidth, height=legheight, loc=self.params.legendinsetposition, borderpad=self.params.legendborderpad) #plt.colorbar(cax=cbaxes, ticks=[0.,1], orientation='horizontal') if self.params.legendposition in ['right', 'left']: cbar = plt.colorbar(ticks=ticks, orientation='vertical', cax=cax) #cbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation='horizontal') cbar.ax.set_yticklabels(tickLabels) # horizontal colorbar # clim compresees the orignal data to the new range, not wh else: #cax = divider.append_axes('bottom', size='5%', pad=0.5) cbar = plt.colorbar(ticks=ticks, orientation='horizontal', cax=cax) #cbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation='horizontal') cbar.ax.set_xticklabels(tickLabels) # horizontal colorbar # clim compresees the orignal data to the new range, not what I want cbar.set_label(self.legend.columnhead) #plt.clim(0, 250); if self.params.tightlayout: fig.tight_layout() plt.show() if self.process.dstpath.hdrfiletype == 'show': plt.show() else: if self.params.tightlayout: plt.savefig(self.dstComp.FPN, bbox_inches='tight') else: plt.savefig(self.dstComp.FPN)