decimals=3) #rmse_son = "%2f" % np.sqrt(rmse_son2) #calculate corr pattern sc_son, pv = np.round(stats.spearmanr(np.ravel(ecorrson), np.ravel(acorrson), axis=0), decimals=3) sc_son = "%.3f" % sc_son #plot #define global attributes lons, lats = np.meshgrid(lon, lat) v = np.linspace(-0.6, 0.6, 13) norm = colors.BoundaryNorm(boundaries=v, ncolors=256) #plot DJF comp plt.figure(figsize=(12, 5)) ax = plt.subplot(2, 3, 1) m = Basemap(projection='cyl', llcrnrlat=-45.0, llcrnrlon=110.0, urcrnrlat=-5.0, urcrnrlon=160.0) #define basemap as around Australia m.drawcoastlines() m.drawparallels(np.array([-45, -35, -25, -15, -5]), labels=[1, 0, 0, 0], fontsize=6) m.drawmeridians(np.array([110, 120, 130, 140, 150, 160]), labels=[0, 0, 0, 1],
def transectview(var, tindex, istart, iend, jstart, jend, gridid, \ filename=None, spval=1e37, cmin=None, cmax=None, clev=None, \ fill=False, contour=False, c=None, jrange=None, hrange=None,\ fts=None, title=None, map=False, \ pal=None, clb=True, xaxis='lon', outfile=None): """ transectview(var, tindex, istart, iend, jstart, jend, gridid, {optional switch}) optional switch: - filename if defined, load the variable from file - spval specify spval - cmin set color minimum limit - cmax set color maximum limit - clev set the number of color step - fill use contourf instead of pcolor - contour overlay contour - c desired contour level. If not specified, plot every 4 contour level. - jrange j range - hrange h range - fts set font size (default: 12) - title add title to the plot - map if True, draw a map showing transect location - pal set color map (default: cm.jet) - clb add colorbar (defaul: True) - xaxis use lon or lat for x axis - outfile if defined, write figure to file plot vertical transect between the points P1=(istart, jstart) and P2=(iend, jend) from 3D variable var. If filename is provided, var must be a string and the variable will be load from the file. grid can be a grid object or a gridid. In the later case, the grid object correponding to the provided gridid will be loaded. """ # get grid if type(gridid).__name__ == 'ROMS_Grid': grd = gridid else: grd = pyroms.grid.get_ROMS_grid(gridid) # get variable if filename == None: var = var else: data = pyroms.io.Dataset(filename) var = data.variables[var] Np, Mp, Lp = grd.vgrid.z_r[0, :].shape if tindex is not -1: assert len(var.shape) == 4, 'var must be 4D (time plus space).' K, N, M, L = var.shape else: assert len(var.shape) == 3, 'var must be 3D (no time dependency).' N, M, L = var.shape # determine where on the C-grid these variable lies if N == Np and M == Mp and L == Lp: Cpos = 'rho' lon = grd.hgrid.lon_vert lat = grd.hgrid.lat_vert mask = grd.hgrid.mask_rho if N == Np and M == Mp and L == Lp - 1: Cpos = 'u' lon = 0.5 * (grd.hgrid.lon_vert[:, :-1] + grd.hgrid.lon_vert[:, 1:]) lat = 0.5 * (grd.hgrid.lat_vert[:, :-1] + grd.hgrid.lat_vert[:, 1:]) mask = grd.hgrid.mask_u if N == Np and M == Mp - 1 and L == Lp: Cpos = 'v' lon = 0.5 * (grd.hgrid.lon_vert[:-1, :] + grd.hgrid.lon_vert[1:, :]) lat = 0.5 * (grd.hgrid.lat_vert[:-1, :] + grd.hgrid.lat_vert[1:, :]) mask = grd.hgrid.mask_v # get transect if tindex == -1: var = var[:, :, :] else: var = var[tindex, :, :, :] if fill == True: transect, zt, lont, latt, = pyroms.tools.transect(var, istart, iend, \ jstart, jend, grd, Cpos, spval=spval) else: transect, zt, lont, latt, = pyroms.tools.transect(var, istart, iend, \ jstart, jend, grd, Cpos, vert=True, spval=spval) if xaxis == 'lon': xt = lont elif xaxis == 'lat': xt = latt # plot if cmin is None: cmin = transect.min() else: cmin = float(cmin) if cmax is None: cmax = transect.max() else: cmax = float(cmax) if clev is None: clev = 100. else: clev = float(clev) dc = (cmax - cmin) / clev vc = np.arange(cmin, cmax + dc, dc) if pal is None: pal = cm.jet else: pal = pal if fts is None: fts = 12 else: fts = fts #pal.set_over('w', 1.0) #pal.set_under('w', 1.0) #pal.set_bad('w', 1.0) pal_norm = colors.BoundaryNorm(vc, ncolors=256, clip=False) # clear figure #plt.clf() if map is True: # set axes for the main plot in order to keep space for the map if fts < 12: ax = None else: ax = plt.axes([0.15, 0.08, 0.8, 0.65]) else: if fts < 12: ax = None else: ax = plt.axes([0.15, 0.1, 0.8, 0.8]) if fill is True: cf = plt.contourf(xt, zt, transect, vc, cmap=pal, norm=pal_norm, axes=ax) else: cf = plt.pcolor(xt, zt, transect, cmap=pal, norm=pal_norm, axes=ax) if clb is True: clb = plt.colorbar(cf, fraction=0.075, format='%.2f') for t in clb.ax.get_yticklabels(): t.set_fontsize(fts) if contour is True: if c is None: c = vc[::10] if fill is True: plt.contour(xt, zt, transect, c, colors='k', linewidths=0.5, linestyles='solid', axes=ax) else: xc = 0.5 * (xt[1:, :] + xt[:-1, :]) xc = 0.5 * (xc[:, 1:] + xc[:, :-1]) zc = 0.5 * (zt[1:, :] + zt[:-1, :]) zc = 0.5 * (zc[:, 1:] + zc[:, :-1]) plt.contour(xc, zc, transect, c, colors='k', linewidths=0.5, linestyles='solid', axes=ax) if jrange is not None: plt.xlim(jrange) if hrange is not None: plt.ylim(hrange) if title is not None: if map is True: # move the title on the right xmin, xmax = ax.get_xlim() ymin, ymax = ax.get_ylim() xt = xmin - (xmax - xmin) / 9. yt = ymax + (ymax - ymin) / 7. plt.text(xt, yt, title, fontsize=fts + 4) else: plt.title(title, fontsize=fts + 4) plt.xlabel('Latitude', fontsize=fts) plt.ylabel('Depth', fontsize=fts) if map is True: # draw a map with constant-i slice location ax_map = plt.axes([0.4, 0.76, 0.2, 0.23]) varm = np.ma.masked_where(mask[:, :] == 0, var[var.shape[0] - 1, :, :]) lon_min = lon.min() lon_max = lon.max() lon_0 = (lon_min + lon_max) / 2. lat_min = lat.min() lat_max = lat.max() lat_0 = (lat_min + lat_max) / 2. map = Basemap(projection='merc', llcrnrlon=lon_min, llcrnrlat=lat_min, \ urcrnrlon=lon_max, urcrnrlat=lat_max, lat_0=lat_0, lon_0=lon_0, \ resolution='i', area_thresh=10.) x, y = list(map(lon, lat)) xt, yt = list(map(lont[0, :], latt[0, :])) # fill land and draw coastlines map.drawcoastlines() map.fillcontinents(color='grey') #map.drawmapboundary() Basemap.pcolor(map, x, y, varm, axes=ax_map) Basemap.plot(map, xt, yt, 'k-', linewidth=3, axes=ax_map) if outfile is not None: if outfile.find('.png') != -1 or outfile.find( '.svg') != -1 or outfile.find('.eps') != -1: print('Write figure to file', outfile) plt.savefig(outfile, dpi=200, facecolor='w', edgecolor='w', orientation='portrait') else: print( 'Unrecognized file extension. Please use .png, .svg or .eps file extension.' ) return
def _rand_cmap(nlabels, type='bright', first_color_black=True, last_color_black=False, verbose=False): """ Creates a random colormap to be used together with matplotlib. Useful for segmentation tasks :param nlabels: Number of labels (size of colormap) :param type: 'bright' for strong colors, 'soft' for pastel colors :param first_color_black: Option to use first color as black, True or False :param last_color_black: Option to use last color as black, True or False :param verbose: Prints the number of labels and shows the colormap. True or False :return: colormap for matplotlib """ from matplotlib.colors import LinearSegmentedColormap import colorsys import numpy as np if type not in ('bright', 'soft'): print('Please choose "bright" or "soft" for type') return if verbose: print('Number of labels: ' + str(nlabels)) # Generate color map for bright colors, based on hsv if type == 'bright': randHSVcolors = [(np.random.uniform(low=0.0, high=1), np.random.uniform(low=0.2, high=1), np.random.uniform(low=0.9, high=1)) for i in range(nlabels)] # Convert HSV list to RGB randRGBcolors = [] for HSVcolor in randHSVcolors: randRGBcolors.append( colorsys.hsv_to_rgb(HSVcolor[0], HSVcolor[1], HSVcolor[2])) if first_color_black: randRGBcolors[0] = [0, 0, 0] if last_color_black: randRGBcolors[-1] = [0, 0, 0] random_colormap = LinearSegmentedColormap.from_list('new_map', randRGBcolors, N=nlabels) # Generate soft pastel colors, by limiting the RGB spectrum if type == 'soft': low = 0.6 high = 0.95 randRGBcolors = [(np.random.uniform(low=low, high=high), np.random.uniform(low=low, high=high), np.random.uniform(low=low, high=high)) for i in range(nlabels)] if first_color_black: randRGBcolors[0] = [0, 0, 0] if last_color_black: randRGBcolors[-1] = [0, 0, 0] random_colormap = LinearSegmentedColormap.from_list('new_map', randRGBcolors, N=nlabels) # Display colorbar if verbose: from matplotlib import colors, colorbar from matplotlib import pyplot as plt fig, ax = plt.subplots(1, 1, figsize=(15, 0.5)) bounds = np.linspace(0, nlabels, nlabels + 1) norm = colors.BoundaryNorm(bounds, nlabels) cb = colorbar.ColorbarBase(ax, cmap=random_colormap, norm=norm, spacing='proportional', ticks=None, boundaries=bounds, format='%1i', orientation=u'horizontal') return random_colormap
def plotZM(data, x, y, fig, ax1, colorMap, dataMin, dataMax, plotOpt=None): """Create a zonal mean contour plot of one variable plotOpt is a dictionary with plotting options: 'scale_factor': multiply values with this factor before plotting 'units': a units label for the colorbar 'levels': use list of values as contour intervals 'title': a title for the plot """ if plotOpt is None: plotOpt = {} # scale data if requested scale_factor = plotOpt.get('scale_factor', 1.0) pdata = data * scale_factor # determine contour levels to be used; default: linear spacing, 20 levels clevs = plotOpt.get('levels', numpy.linspace(dataMin, dataMax, 20)) # map contour values to colors norm = colors.BoundaryNorm(clevs, ncolors=256, clip=False) #print "data min/ max: ", dataMin, " / " , dataMax # draw the (filled) contours contour = ax1.contourf(x, y, pdata, levels=clevs, norm=norm, cmap=colorMap, \ vmin = dataMin, vmax = dataMax) # add a title title = plotOpt.get('title', 'Zonal Mean') ax1.set_title(title) # optional keyword: fontsize="small" # add colorbar # Note: use of the ticks keyword forces colorbar to draw all labels fmt = ticker.FormatStrFormatter("%.2g") cbar = fig.colorbar(contour, ax=ax1, orientation='horizontal', shrink=0.8, format=fmt) cbar.set_label(plotOpt.get('units', '')) for t in cbar.ax.get_xticklabels(): t.set_fontsize("x-small") # change font size of x labels xlabels = ax1.get_xticklabels() for t in xlabels: t.set_fontsize("x-small") # set up y axes: log pressure labels on the left y axis ax1.set_ylabel("levels") ax1.set_yscale('log') ax1.set_ylim(y.max(), y.min()) #ax1.set_ylim(y.min(), y.max()) subs = [1, 2, 5] print "y_max, y_min = ", y.max(), y.min() if y.max() / y.min() < 30.: subs = [1, 2, 3, 4, 5, 6, 7, 8, 9] loc = ticker.LogLocator(base=10., subs=subs) ax1.yaxis.set_major_locator(loc) fmt = ticker.FormatStrFormatter("%g") ax1.yaxis.set_major_formatter(fmt) ylabels = ax1.get_yticklabels() for t in ylabels: t.set_fontsize("x-small")
def plot_panel(n, fig, proj, var, clevels, cmap, title, parameters, stats=None): var = add_cyclic(var) lon = var.getLongitude() lat = var.getLatitude() var = ma.squeeze(var.asma()) # Contour levels levels = None norm = None if len(clevels) > 0: levels = [-1.0e8] + clevels + [1.0e8] norm = colors.BoundaryNorm(boundaries=levels, ncolors=256) # Contour plot ax = fig.add_axes(panel[n], projection=proj) ax.set_global() cmap = get_colormap(cmap, parameters) p1 = ax.contourf( lon, lat, var, transform=ccrs.PlateCarree(), norm=norm, levels=levels, cmap=cmap, extend='both', ) ax.set_aspect('auto') ax.coastlines(lw=0.3) if title[0] is not None: ax.set_title(title[0], loc='left', fontdict=plotSideTitle) if title[1] is not None: ax.set_title(title[1], fontdict=plotTitle) if title[2] is not None: ax.set_title(title[2], loc='right', fontdict=plotSideTitle) ax.set_xticks([0, 60, 120, 180, 240, 300, 359.99], crs=ccrs.PlateCarree()) # ax.set_xticks([-180, -120, -60, 0, 60, 120, 180], crs=ccrs.PlateCarree()) ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree()) lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format='.0f') lat_formatter = LatitudeFormatter() ax.xaxis.set_major_formatter(lon_formatter) ax.yaxis.set_major_formatter(lat_formatter) ax.tick_params(labelsize=8.0, direction='out', width=1) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # Color bar cbax = fig.add_axes( (panel[n][0] + 0.6635, panel[n][1] + 0.0215, 0.0326, 0.1792)) cbar = fig.colorbar(p1, cax=cbax) w, h = get_ax_size(fig, cbax) if levels is None: cbar.ax.tick_params(labelsize=9.0, length=0) else: maxval = np.amax(np.absolute(levels[1:-1])) if maxval < 10.0: fmt = "%5.2f" pad = 25 elif maxval < 100.0: fmt = "%5.1f" pad = 25 else: fmt = "%6.1f" pad = 30 cbar.set_ticks(levels[1:-1]) labels = [fmt % l for l in levels[1:-1]] cbar.ax.set_yticklabels(labels, ha='right') cbar.ax.tick_params(labelsize=9.0, pad=pad, length=0) # Min, Mean, Max fig.text(panel[n][0] + 0.6635, panel[n][1] + 0.2107, "Max\nMean\nMin", ha='left', fontdict=plotSideTitle) fig.text(panel[n][0] + 0.7635, panel[n][1] + 0.2107, "%.2f\n%.2f\n%.2f" % stats[0:3], ha='right', fontdict=plotSideTitle) # RMSE, CORR if len(stats) == 5: fig.text(panel[n][0] + 0.6635, panel[n][1] - 0.0105, "RMSE\nCORR", ha='left', fontdict=plotSideTitle) fig.text(panel[n][0] + 0.7635, panel[n][1] - 0.0105, "%.2f\n%.2f" % stats[3:5], ha='right', fontdict=plotSideTitle)
def AnalyzeMergedZones(diretorio, ImgRefRoot='/Binarized', XYField=[319.45, 319.45], RawImageDefinition=[1024, 1024], ZStep=1, ImportstackRootName='/Binarized', importFormat='.png', MergedRegion=1, FirstStack=1, LastStack=2, FirstSlice=1, LastSlice=2, initialTimePoint=30, timeinterval=30, BACVol_ylim=800, BACVel_ylim=800, EPSVol_ylim=800, EPSVel_ylim=800): StackList = list(range(FirstStack, LastStack + 1)) SliceRange = list(range(FirstSlice, LastSlice + 1)) print('\n Encontrando as regioes para calcular \n') LengthPixelRatio = XYField[0] / RawImageDefinition[0] VoxelVal = ZStep * (XYField[0] / RawImageDefinition[0]) * ( XYField[1] / RawImageDefinition[1]) ImgZProj1 = io.imread(diretorio + ImgRefRoot + '/bac' + '/ZProjection_t18' + importFormat, dtype='i4') ImgZProj2 = io.imread(diretorio + ImgRefRoot + '/bac' + '/ZProjection_t15' + importFormat, dtype='i4') ImgZProj3 = io.imread(diretorio + ImgRefRoot + '/bac' + '/ZProjection_t10' + importFormat, dtype='i4') ImgZProj4 = io.imread(diretorio + ImgRefRoot + '/bac' + '/ZProjection_t5' + importFormat, dtype='i4') ImgZProj5 = io.imread(diretorio + ImgRefRoot + '/bac' + '/ZProjection_t2' + importFormat, dtype='i4') ImgZProjF1 = ImgZProj1 + ImgZProj2 + ImgZProj3 + ImgZProj4 + ImgZProj5 ImgZProjF2 = (ImgZProjF1 / ImgZProjF1.max()) * 255 # Plot Imagens #Figura 1 - Grid fig1 = plt.figure(figsize=(12, 7), facecolor='w', edgecolor='k') plt.subplots_adjust(wspace=1.5, hspace=0.6) #Mapa 1 map1 = plt.subplot2grid((2, 3), (0, 0), rowspan=1, colspan=1) map1axi = map1.imshow(ImgZProjF2, alpha=0.8, cmap='jet') map1.set_title('Colony merging', fontsize=11) map1.tick_params(which='both', bottom='off', labelbottom='on', top='off', labeltop='off', left='off', labelleft='on', right='off', labelright='off', labelsize=11) map1.set_xlabel('$\\mu$m', fontsize=11) map1.set_xticks([0, int(ImgZProjF2.shape[1] / 2), ImgZProjF2.shape[1]]) map1.set_xticklabels([ str(0), str(int((ImgZProjF2.shape[1] / 2) * LengthPixelRatio)), str(int(ImgZProjF2.shape[1] * LengthPixelRatio)) ]) map1.set_yticks([0, int(ImgZProjF2.shape[0] / 2), ImgZProjF2.shape[0]]) map1.set_yticklabels([ str(int(ImgZProjF2.shape[0] * LengthPixelRatio)), str(int((ImgZProjF2.shape[0] / 2) * LengthPixelRatio)), str(0) ]) Colbar1 = fig1.colorbar(map1axi, fraction=0.04, pad=0.08, orientation='vertical', ticks=list( np.arange(0, ImgZProjF2.max() + 1, int(ImgZProjF2.max() / 5)))) Colbar1.set_label('time (min)', fontsize=11) Colbar1.set_ticklabels(['B'] + [str(i) for i in [510, 420, 270, 120, 30]]) Colbar1.ax.tick_params(labelsize=11) #Mapa 2 ImgRef1a = io.imread(diretorio + ImgRefRoot + '/bac' + '/ConvexHull1' + importFormat) ImgRef1b = morphology.label(ImgRef1a, connectivity=1) ImgRef1Props = measure.regionprops(ImgRef1b) CentroidList = [] for elementN in list(range(ImgRef1b.max())): CentroidList.append([ ImgRef1Props[elementN].centroid[0], ImgRef1Props[elementN].centroid[1] ]) ImgConvex1a = io.imread(diretorio + ImgRefRoot + '/bac' + '/ConvexHull2' + importFormat) ImgConvex1b = morphology.convex_hull_object(ImgConvex1a) ImgConvex2 = np.where(ImgConvex1b == True, 1, 0) ImgConvex3 = morphology.label(ImgConvex2, connectivity=1) HexColors = [] openHexColors = open(diretorio + '/HexColors.txt', 'r') for line in openHexColors: HexColors.append(str(line)[:-1]) colorlist = [] for labels in list(range(0, ImgConvex3.max() + 1)): colorlist.append(HexColors[labels + 47]) ColorBounds = list(range(0, ImgConvex3.max() + 1)) Colormap2 = colors.ListedColormap(colorlist) ColorNormalization = colors.BoundaryNorm(ColorBounds, Colormap2.N) map2 = plt.subplot2grid((2, 3), (1, 0), rowspan=1, colspan=1) map2final = map2.imshow(ImgConvex3, alpha=0.9, cmap=Colormap2) map2.set_title('Convex hull regions', fontsize=11) map2.tick_params(which='both', bottom='off', labelbottom='on', top='off', labeltop='off', left='off', labelleft='on', right='off', labelright='off', labelsize=11) map2.set_xlabel('$\\mu$m', fontsize=11) map2.set_xticks([0, int(ImgConvex3.shape[1] / 2), ImgConvex3.shape[1] - 1]) map2.set_xticklabels([ str(0), str(int((ImgConvex3.shape[1] / 2) * LengthPixelRatio)), str(int(ImgConvex3.shape[1] * LengthPixelRatio)) ]) map2.set_yticks([0, int(ImgConvex3.shape[0] / 2), ImgConvex3.shape[0] - 1]) map2.set_yticklabels([ str(int(ImgConvex3.shape[0] * LengthPixelRatio)), str(int((ImgConvex3.shape[0] / 2) * LengthPixelRatio)), str(0) ]) Colbar2 = fig1.colorbar(map2final, cmap=Colormap2, norm=ColorNormalization, ticks=list( np.arange(0, ColorBounds[-1], ColorBounds[-1] / len(ColorBounds))), fraction=0.04, pad=0.08, orientation='vertical') Colbar2.set_label('Regions', fontsize=11) Colbar2.set_ticklabels(['B'] + [str(i) for i in ColorBounds[1:]]) Colbar2.ax.tick_params(labelsize=11) #Adicionando os centroides e img referencia map2.imshow(ImgRef1a, alpha=0.5, cmap='Greens') map2.scatter(np.array(CentroidList)[:, 1], np.array(CentroidList)[:, 0], color='k', s=5) # Grafico 1. Growth BAC g1 = plt.subplot2grid((2, 3), (0, 1), rowspan=1, colspan=1) g1.set_title('Bacteria growth', fontsize=11) g1.set_ylabel('Growth ($\\mu$$m^3$)', labelpad=5, fontsize=11) g1.set_xlabel('Time (min)', labelpad=5, fontsize=11) g1.tick_params(axis="y", labelleft='on', left='on', labelright='off', right='off', colors='k', width=1.5, length=3.5, labelsize=11) g1.tick_params(axis="x", labelbottom='on', bottom='on', labeltop='off', top='off', colors='k', width=1.5, length=3.5, labelsize=11) g1.grid(True, color='k', linestyle='-', linewidth=0.4, alpha=0.1) # Grafico 2. Growth EPS g2 = plt.subplot2grid((2, 3), (1, 1), rowspan=1, colspan=1) g2.set_title('EPS growth', fontsize=11) g2.set_ylabel('Growth ($\\mu$$m^3$)', labelpad=5, fontsize=11) g2.set_xlabel('Time (min)', labelpad=5, fontsize=11) g2.tick_params(axis="y", labelleft='on', left='on', labelright='off', right='off', colors='k', width=1.5, length=3.5, labelsize=11) g2.tick_params(axis="x", labelbottom='on', bottom='on', labeltop='off', top='off', colors='k', width=1.5, length=3.5, labelsize=11) g2.grid(True, color='k', linestyle='-', linewidth=0.4, alpha=0.1) # Grafico 3. Growth rate BAC g3 = plt.subplot2grid((2, 3), (0, 2), rowspan=1, colspan=1) g3.set_title('Bacteria growth rate', fontsize=11) g3.set_ylabel('Growth rate ($\\mu$$m^3$/min)', labelpad=5, fontsize=11) g3.set_xlabel('Time (min)', labelpad=5, fontsize=11) g3.tick_params(axis="y", labelleft='on', left='on', labelright='off', right='off', colors='k', width=1.5, length=3.5, labelsize=11) g3.tick_params(axis="x", labelbottom='on', bottom='on', labeltop='off', top='off', colors='k', width=1.5, length=3.5, labelsize=11) g3.grid(True, color='k', linestyle='-', linewidth=0.4, alpha=0.1) # Grafico 4. Growth rate EPS g4 = plt.subplot2grid((2, 3), (1, 2), rowspan=1, colspan=1) g4.set_title('EPS growth rate', fontsize=11) g4.set_ylabel('Growth rate ($\\mu$$m^3$/min)', labelpad=5, fontsize=11) g4.set_xlabel('Time (min)', labelpad=5, fontsize=11) g4.tick_params(axis="y", labelleft='on', left='on', labelright='off', right='off', colors='k', width=1.5, length=3.5, labelsize=11) g4.tick_params(axis="x", labelbottom='on', bottom='on', labeltop='off', top='off', colors='k', width=1.5, length=3.5, labelsize=11) g4.grid(True, color='k', linestyle='-', linewidth=0.4, alpha=0.1) #Figura 2 - Campo escalar #fig2=plt.figure(figsize=(6, 6), facecolor='w', edgecolor='k') #plotPositions=plt.subplot2grid((1,1),(0,0),rowspan=1,colspan=1) g1_YboundMAX = 0 g2_YboundMAX = 0 g3_YboundMAX = 0 g4_YboundMAX = 0 for elementN in list(range(1, ImgConvex3.max() + 1)): YXPosList = [] for Ypos in list(range(ImgConvex3.shape[0])): for Xpos in list(range(ImgConvex3.shape[1])): if ImgConvex3[Ypos, Xpos] == elementN: YXPosList.append([Ypos, Xpos]) ElementPlotListBAC = [] ElementPlotListEPS = [] time = initialTimePoint for stackNumber in StackList: print('\n Encontrando os volume para o stack ', str(stackNumber), ' \n') ImgListBAC = [] ImgListEPS = [] for x in SliceRange: aa = io.imread(diretorio + ImportstackRootName + '/bac' + '/t' + str(stackNumber) + '/Slice' + str(x) + importFormat) bb = io.imread(diretorio + ImportstackRootName + '/EPS' + '/t' + str(stackNumber) + '/Slice' + str(x) + importFormat) ImgListBAC.append(aa) ImgListEPS.append(bb) Img3DarrayBAC = np.array(ImgListBAC) Img3DarrayBACF = np.where(Img3DarrayBAC > 0, 1, 0) Img3DarrayEPS = np.array(ImgListEPS) Img3DarrayEPSF = np.where(Img3DarrayEPS > 0, 1, 0) VolElemCountListBAC = [] VolElemCountListEPS = [] for sliceN in list(range(Img3DarrayBACF.shape[0])): for Position in YXPosList: VolElemCountListBAC.append(Img3DarrayBACF[sliceN, Position[0], Position[1]]) VolElemCountListEPS.append(Img3DarrayEPSF[sliceN, Position[0], Position[1]]) ElementPlotListBAC.append( [elementN, time, sum(VolElemCountListBAC) * VoxelVal]) ElementPlotListEPS.append( [elementN, time, sum(VolElemCountListEPS) * VoxelVal]) time += timeinterval with open(diretorio + '/Images/MergedRegion.txt', 'a') as textfile: for val in ElementPlotListBAC: textfile.write(str(val) + '\n') textfile.close() # Calculando as velocidades VelListBAC = AvgVelocity( np.array(ElementPlotListBAC)[:, 2], initialTimePoint, timeinterval, elementN) VelListEPS = AvgVelocity( np.array(ElementPlotListEPS)[:, 2], initialTimePoint, timeinterval, elementN) #Acertando os limites do gráfico if g1_YboundMAX < np.array(ElementPlotListBAC)[:, 2].max(): g1_YboundMAX = np.array(ElementPlotListBAC)[:, 2].max() if g2_YboundMAX < np.array(ElementPlotListEPS)[:, 2].max(): g2_YboundMAX = np.array(ElementPlotListEPS)[:, 2].max() if g3_YboundMAX < np.array(VelListBAC)[:, 2].max(): g3_YboundMAX = np.array(VelListBAC)[:, 2].max() if g4_YboundMAX < np.array(VelListEPS)[:, 2].max(): g4_YboundMAX = np.array(VelListEPS)[:, 2].max() #Plot graficos Growth g1.plot(np.array(ElementPlotListBAC)[:, 1], np.array(ElementPlotListBAC)[:, 2], color=colorlist[elementN], alpha=0.5) g1.scatter(np.array(ElementPlotListBAC)[:, 1], np.array(ElementPlotListBAC)[:, 2], color=colorlist[elementN], label='Element' + str(elementN), alpha=0.5, s=20) g1.axvline(x=(10 * 30) - 30, color="r", linewidth=1.5, alpha=0.1) g1.axvline(x=(11 * 30) - 30, color="b", linewidth=1.5, alpha=0.1) g1.axvline(x=(15 * 30) - 30, color="y", linewidth=1.5, alpha=0.1) g2.plot(np.array(ElementPlotListEPS)[:, 1], np.array(ElementPlotListEPS)[:, 2], color=colorlist[elementN], alpha=0.5) g2.scatter(np.array(ElementPlotListEPS)[:, 1], np.array(ElementPlotListEPS)[:, 2], color=colorlist[elementN], label='Element' + str(elementN), alpha=0.5, s=20) g2.axvline(x=(10 * 30) - 30, color="r", linewidth=1.5, alpha=0.1) g2.axvline(x=(11 * 30) - 30, color="b", linewidth=1.5, alpha=0.1) g2.axvline(x=(15 * 30) - 30, color="y", linewidth=1.5, alpha=0.1) #Plot graficos Growth rate g3.plot(np.array(VelListBAC)[:, 1], np.array(VelListBAC)[:, 2], color=colorlist[elementN], alpha=0.5) g3.scatter(np.array(VelListBAC)[:, 1], np.array(VelListBAC)[:, 2], color=colorlist[elementN], label='Element' + str(elementN), alpha=0.5, s=20) g3.axvline(x=(10 * 30) - 30, color="r", linewidth=1.5, alpha=0.1) g3.axvline(x=(11 * 30) - 30, color="b", linewidth=1.5, alpha=0.1) g3.axvline(x=(15 * 30) - 30, color="y", linewidth=1.5, alpha=0.1) g4.plot(np.array(VelListEPS)[:, 1], np.array(VelListEPS)[:, 2], color=colorlist[elementN], alpha=0.5) g4.scatter(np.array(VelListEPS)[:, 1], np.array(VelListEPS)[:, 2], color=colorlist[elementN], label='Element' + str(elementN), alpha=0.5, s=20) g4.axvline(x=(10 * 30) - 30, color="r", linewidth=1.5, alpha=0.1) g4.axvline(x=(11 * 30) - 30, color="b", linewidth=1.5, alpha=0.1) g4.axvline(x=(15 * 30) - 30, color="y", linewidth=1.5, alpha=0.1) #Plotar as posicoes usadas no campo escalar #plotPositions.scatter(np.array(YXPosList)[:,1], np.array(YXPosList)[:,0], color=colorlist[elementN]) # Definindo os eixos do gráfico g1.set_ylim(-40, BACVol_ylim) g1.spines["left"].set_visible(True) g1.spines["left"].set_linewidth(1.0) g1.spines["right"].set_visible(False) g1.spines["top"].set_visible(False) g1.spines["bottom"].set_visible(True) g1.spines["bottom"].set_linewidth(1.0) g1.set_aspect('auto') g2.set_ylim(-40, EPSVol_ylim) g2.spines["left"].set_visible(True) g2.spines["left"].set_linewidth(1.0) g2.spines["right"].set_visible(False) g2.spines["top"].set_visible(False) g2.spines["bottom"].set_visible(True) g2.spines["bottom"].set_linewidth(1.0) g2.set_aspect('auto') g3.set_ylim(-5, BACVel_ylim) g3.spines["left"].set_visible(True) g3.spines["left"].set_linewidth(1.0) g3.spines["right"].set_visible(False) g3.spines["top"].set_visible(False) g3.spines["bottom"].set_visible(True) g3.spines["bottom"].set_linewidth(1.0) g3.set_aspect('auto') g4.set_ylim(-5, EPSVel_ylim) g4.spines["left"].set_visible(True) g4.spines["left"].set_linewidth(1.0) g4.spines["right"].set_visible(False) g4.spines["top"].set_visible(False) g4.spines["bottom"].set_visible(True) g4.spines["bottom"].set_linewidth(1.0) g4.set_aspect('auto') plt.tight_layout() fig1.savefig(diretorio + '/Images/MergedRegion' + str(MergedRegion) + '.png', dpi=300) #fig2.savefig(diretorio + '/Images/MergedRegion' + str(MergedRegion) + '_PLOT.png') plt.show()
#get data from datasets light_prob = Pred.data_vars['lightning_prob'] actual_light = Actual_glm.data_vars['lightning_counts'] lons = Pred.data_vars['lon'] lats = Pred.data_vars['lat'] #clean up the actual dataset actual_light.values = actual_light.values.astype('float64') actual_light.values[actual_light.values <= 0.0] = np.nan #plot data and create animation fig = plt.figure(figsize=(18, 12)) ax = plt.axes(projection=ccrs.Miller()) states = cfeature.STATES.with_scale('10m') ax.add_feature(states, edgecolor='black') pred_bounds = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]) norm = colors.BoundaryNorm(boundaries=pred_bounds, ncolors=200, clip=True) Pred = ax.pcolormesh(lons, lats, light_prob[0, :, :], transform=ccrs.Miller(), norm=norm, cmap='Reds', vmin=0.1, vmax=1) Actual = ax.pcolormesh(lons, lats, actual_light[0, :, :], transform=ccrs.Miller(), cmap='Blues', vmin=0, vmax=100)
fontname="Times New Roman", fontsize=24, fontweight='bold') ax.set_ylabel("Latitude ", fontname="Times New Roman", fontsize=26, fontweight='bold', labelpad=70) ax.set_xlabel("Longitude", fontname="Times New Roman", fontsize=26, fontweight='bold', labelpad=30) cmap = c.ListedColormap(my_color) bounds = np.linspace(0, 30, 300, endpoint=True) norm = c.BoundaryNorm(bounds, ncolors=cmap.N) cs = plt.contourf(x, y, speed, bounds, cmap=cmap, extend='both', shading='interp', latlon=True) #map.quiver(x[points], y[points], uwnd[points], vwnd[points],angles='xy',scale=1000,color='k') #map.quiver(x, y, uwnd, vwnd, speed,cmap='rainbow',latlon=True) map.streamplot(x, y, uwnd, vwnd,
def get_distributions(x, n_clusters=6, idec_weights='', window=1, screen='', path='', tag=''): ae, decoder = autoencoder(dims=[x[0].shape[-1] - 2, 500, 500, 2000, 10]) n_stacks = len([x[0].shape[-1] - 2, 500, 500, 2000, 10]) - 1 hidden = ae.get_layer(name='encoder_%d' % (n_stacks - 1)).output clustering_layer = ClusteringLayer(n_clusters, name='clustering')(hidden) modele = Model(inputs=ae.input, outputs=[clustering_layer, ae.output]) modele.load_weights(idec_weights) analysis = [] duration_clustering = [[] for i in range(n_clusters)] Ts = 0.08 window_len = int(window / Ts) for j, larva in enumerate(x): t = larva[:, -1][window_len:-window_len] X = larva[:, :-2][window_len:-window_len] res = modele.predict(X) predictions = res[0].argmax(axis=1) if predictions[0] != predictions[1]: predictions[0] = predictions[1] if predictions[-1] != predictions[-2]: predictions[-1] = predictions[-2] for i in range(1, len(predictions) - 1): if (predictions[i] != predictions[i + 1]) & (predictions[i] != predictions[i - 1]): predictions[i] = predictions[i - 1] predictions = pd.DataFrame(predictions) for i in range(n_clusters): indices = list(predictions[predictions[0] == i].index) if len(indices) > 0: indices_change = [indices[i] + 1 for i in range(len(indices) - 1) if (indices[i] + 1 != indices[i + 1]) or (indices[i - 1] + 1 != indices[i])] + \ [indices[-1] + 1] times_change = [t[i - 1] for i in indices_change] times_change = [[times_change[i], times_change[i + 1]] for i in range(len(times_change)) if (i % 2 == 0)] len_behavior = np.array([ sublist[1] - sublist[0] for sublist in times_change ]).flatten().tolist() if len(len_behavior) > 0: duration_clustering[i] += len_behavior analysis.append([predictions.values, t]) save_dir = path + "/" + screen + tag + '/distributions' print(save_dir) if not os.path.exists(save_dir): os.makedirs(save_dir) np.savez_compressed(save_dir + '/distributions_' + str(n_clusters) + '_clusters.npz', x=np.array(analysis), t=np.array(duration_clustering)) # Save plot as png fig, ax = plt.subplots(1, 1, figsize=(6, 6)) bounds = [i for i in range(len(duration_clustering) + 1)] norm = colors.BoundaryNorm(boundaries=bounds, ncolors=len(duration_clustering)) for i in range(len(duration_clustering)): Y, X = np.histogram(duration_clustering[i], 100, density=True, range=(0, 4)) cm = plt.cm.get_cmap('Paired') h = plt.bar(X[:-1], Y, width=X[1] - X[0], color=cm(i)) plt.title('Time distributions ' + tag[1:] + ' ' + str(n_clusters) + ' clusters') ax2 = fig.add_axes([0.95, 0.12, 0.03, 0.7]) cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cm, norm=norm, boundaries=bounds, ticks=bounds, spacing='proportional') plt.savefig(save_dir + '/time_distributions' + str(tag[1:]) + '_' + str(n_clusters) + '_clusters' + '.png') print('Successfully computed distributions')
pcursor = POSTGIS.cursor() cmap = cm.get_cmap("Accent") cmap.set_under("#ffffff") cmap.set_over("black") m = MapPlot(sector='nws', axisbg='#EEEEEE', title='1+ TOR warn for 100 most active TOR warn days 1986-2015', subtitle=('A day is defined as 12 to12 UTC period, did the ' 'county get 1+ warning during those 100 events?'), cwas=True) bins = np.arange(0, 101, 10) bins[0] = 1 norm = mpcolors.BoundaryNorm(bins, cmap.N) pcursor.execute(""" WITH data as ( SELECT ugc, date(issue at time zone 'UTC' + '12 hours'::interval) from warnings where phenomena in ('TO') and significance = 'W' ), maxdays as ( SELECT date, count(*) from data GROUP by date ORDER by count DESC LIMIT 100 ), events as ( SELECT distinct ugc, d.date from data d JOIN maxdays m on (m.date = d.date) ) SELECT ugc, count(*) from events GROUP by ugc """)
Te = lev * 0 for k in range(len(lev)): Te[k] = max( Tstra, \ np.exp(np.log(Tsurf) + (dTdz*0.001*287/9.8) * np.log(lev[k]/1000.0)) ) #===================================================================== plt.figure(1, figsize=(8, 8)) plt.subplot(2, 1, 1) clev = np.linspace(-0.03, 0.02, 21) pic = (picdata['OMEGA'][0, :, nlat / 2 - 1, :] + picdata['OMEGA'][0, :, nlat / 2, :]) / 2 #plt.contourf(lon, lev, pic, 41, cmap=cm.jet) plt.contourf(lon, lev, pic, clev, norm=mc.BoundaryNorm(clev, 256), cmap=cm.RdYlBu_r) plt.colorbar() plt.contour(lon, lev, pic, [0], colors='m') pic = (picdata['mflx'][0, :, nlat / 2 - 1, :] + picdata['mflx'][0, :, nlat / 2, :]) / 2 clev = np.linspace(-1, 1, 41) * 5e4 plt.contour(lon, lev, pic, clev, colors='k', norm=mc.BoundaryNorm(clev, 256)) plt.xticks(range(0, 360, 30)) dpicx = 3
def hovplot(data, vname): import matplotlib.colors as mc LLJ_sm = data.start_month # starting the ananlysis from 6 LLJ_em = data.end_month # YB = data.yb YE = data.ye years = range(YB, YE + 1) xlat = np.arange(data.start_lat, data.end_lat, data.dlat) clevel = getattr(data, "%s_%s" % (vname.lower(), "clevel1")) cmp = plotres[vname]['cmp1'] norm = mc.BoundaryNorm(clevel, cmp.N) ncols = 2.0 import math gs1 = gridspec.GridSpec(int(ncols), int(math.ceil(len(data.cases) / ncols))) fig = plt.figure(figsize=(12, 8)) fig.suptitle(data.title[vname] + plotres[vname]['unit'], fontsize=12, fontweight='bold') gs1.update(wspace=0.07, hspace=0.15) for icase, case in enumerate(data.cases): print(data.plotdata[case][vname].shape, case, vname) nlat, nday = data.plotdata[case][vname].shape ax = plt.subplot(gs1[icase]) plt.xlim([0, nday - 1]) CS = plt.contourf(data.plotdata[case][vname], levels=clevel, norm=norm, cmap=cmp, extend='max') #if case ==data.cases[-1] or case ==data.cases[-2]: x_tickloc_major = [] labels_major = [] x_tickloc_major = [0] curdate = datetime.datetime(years[0], LLJ_sm, 1, 0, 0, 0) units_cur = data.time[case][vname].units calendar_cur = data.time[case][vname].calendar T0 = date2num(curdate, units=units_cur, calendar=calendar_cur) labels_major = [curdate.strftime("%m/%d")] for imonth, month in enumerate(range(LLJ_sm + 1, LLJ_em + 1)): curdate = datetime.datetime(years[0], month, 1, 0, 0, 0) T_loc = date2num(curdate, units=units_cur, calendar=calendar_cur) - T0 ax.plot([T_loc, T_loc], [0, len(xlat) - 1], lw=1, c="r", linestyle="--") x_tickloc_major.append(T_loc) labels_major.append(curdate.strftime("%m/%d")) ax.set_xticks(x_tickloc_major) ax.set_xticklabels(labels_major, color='brown') #else: # ax.set_xticks( [] ) # ax.set_xticklabels([],weight='bold',color='brown') ax.text(0.3, 0.9, sim_nicename[case], verticalalignment='bottom', horizontalalignment='center', transform=ax.transAxes, fontweight='bold') if icase == 0 or icase == 3: ax.set_yticks(range(0, len(xlat), 10)) ax.set_yticklabels(("%s$^\circ$ N" % int(x) for x in xlat[::10])) else: ax.set_yticks([]) ax.set_yticklabels([], rotation=90, weight='bold', color='brown') #ax.set_title(sim_nicename[case], fontsize=8, fontweight='bold') for axis in ['top', 'bottom', 'left', 'right']: ax.spines[axis].set_linewidth(0.01) ax2 = fig.add_axes([0.15, 0.01, 0.7, 0.1], aspect=0.02) cbar = fig.colorbar(CS, cax=ax2, orientation="horizontal", drawedges=False) cbar.outline.set_visible(False) cbar.ax.tick_params(labelsize=style.tickfs, length=0) cbar.set_ticks(clevel) fig.savefig(data.plotname + ".pdf")
def test_BoundaryNorm(): """ GitHub issue #1258: interpolation was failing with numpy 1.7 pre-release. """ boundaries = [0, 1.1, 2.2] vals = [-1, 0, 1, 2, 2.2, 4] # Without interpolation expected = [-1, 0, 0, 1, 2, 2] ncolors = len(boundaries) - 1 bn = mcolors.BoundaryNorm(boundaries, ncolors) assert_array_equal(bn(vals), expected) # ncolors != len(boundaries) - 1 triggers interpolation expected = [-1, 0, 0, 2, 3, 3] ncolors = len(boundaries) bn = mcolors.BoundaryNorm(boundaries, ncolors) assert_array_equal(bn(vals), expected) # with a single region and interpolation expected = [-1, 1, 1, 1, 3, 3] bn = mcolors.BoundaryNorm([0, 2.2], ncolors) assert_array_equal(bn(vals), expected) # more boundaries for a third color boundaries = [0, 1, 2, 3] vals = [-1, 0.1, 1.1, 2.2, 4] ncolors = 5 expected = [-1, 0, 2, 4, 5] bn = mcolors.BoundaryNorm(boundaries, ncolors) assert_array_equal(bn(vals), expected) # a scalar as input should not trigger an error and should return a scalar boundaries = [0, 1, 2] vals = [-1, 0.1, 1.1, 2.2] bn = mcolors.BoundaryNorm(boundaries, 2) expected = [-1, 0, 1, 2] for v, ex in zip(vals, expected): ret = bn(v) assert isinstance(ret, int) assert_array_equal(ret, ex) assert_array_equal(bn([v]), ex) # same with interp bn = mcolors.BoundaryNorm(boundaries, 3) expected = [-1, 0, 2, 3] for v, ex in zip(vals, expected): ret = bn(v) assert isinstance(ret, int) assert_array_equal(ret, ex) assert_array_equal(bn([v]), ex) # Clipping bn = mcolors.BoundaryNorm(boundaries, 3, clip=True) expected = [0, 0, 2, 2] for v, ex in zip(vals, expected): ret = bn(v) assert isinstance(ret, int) assert_array_equal(ret, ex) assert_array_equal(bn([v]), ex) # Masked arrays boundaries = [0, 1.1, 2.2] vals = np.ma.masked_invalid([-1., np.NaN, 0, 1.4, 9]) # Without interpolation ncolors = len(boundaries) - 1 bn = mcolors.BoundaryNorm(boundaries, ncolors) expected = np.ma.masked_array([-1, -99, 0, 1, 2], mask=[0, 1, 0, 0, 0]) assert_array_equal(bn(vals), expected) # With interpolation bn = mcolors.BoundaryNorm(boundaries, len(boundaries)) expected = np.ma.masked_array([-1, -99, 0, 2, 3], mask=[0, 1, 0, 0, 0]) assert_array_equal(bn(vals), expected) # Non-trivial masked arrays vals = np.ma.masked_invalid([np.Inf, np.NaN]) assert np.all(bn(vals).mask) vals = np.ma.masked_invalid([np.Inf]) assert np.all(bn(vals).mask) # Incompatible extend and clip with pytest.raises(ValueError, match="not compatible"): mcolors.BoundaryNorm(np.arange(4), 5, extend='both', clip=True) # Too small ncolors argument with pytest.raises(ValueError, match="ncolors must equal or exceed"): mcolors.BoundaryNorm(np.arange(4), 2) with pytest.raises(ValueError, match="ncolors must equal or exceed"): mcolors.BoundaryNorm(np.arange(4), 3, extend='min') with pytest.raises(ValueError, match="ncolors must equal or exceed"): mcolors.BoundaryNorm(np.arange(4), 4, extend='both') # Testing extend keyword, with interpolation (large cmap) bounds = [1, 2, 3] cmap = cm.get_cmap('viridis') mynorm = mcolors.BoundaryNorm(bounds, cmap.N, extend='both') refnorm = mcolors.BoundaryNorm([0] + bounds + [4], cmap.N) x = np.random.randn(100) * 10 + 2 ref = refnorm(x) ref[ref == 0] = -1 ref[ref == cmap.N - 1] = cmap.N assert_array_equal(mynorm(x), ref) # Without interpolation cmref = mcolors.ListedColormap(['blue', 'red']) cmref.set_over('black') cmref.set_under('white') cmshould = mcolors.ListedColormap(['white', 'blue', 'red', 'black']) assert mcolors.same_color(cmref.get_over(), 'black') assert mcolors.same_color(cmref.get_under(), 'white') refnorm = mcolors.BoundaryNorm(bounds, cmref.N) mynorm = mcolors.BoundaryNorm(bounds, cmshould.N, extend='both') assert mynorm.vmin == refnorm.vmin assert mynorm.vmax == refnorm.vmax assert mynorm(bounds[0] - 0.1) == -1 # under assert mynorm(bounds[0] + 0.1) == 1 # first bin -> second color assert mynorm(bounds[-1] - 0.1) == cmshould.N - 2 # next-to-last color assert mynorm(bounds[-1] + 0.1) == cmshould.N # over x = [-1, 1.2, 2.3, 9.6] assert_array_equal(cmshould(mynorm(x)), cmshould([0, 1, 2, 3])) x = np.random.randn(100) * 10 + 2 assert_array_equal(cmshould(mynorm(x)), cmref(refnorm(x))) # Just min cmref = mcolors.ListedColormap(['blue', 'red']) cmref.set_under('white') cmshould = mcolors.ListedColormap(['white', 'blue', 'red']) assert mcolors.same_color(cmref.get_under(), 'white') assert cmref.N == 2 assert cmshould.N == 3 refnorm = mcolors.BoundaryNorm(bounds, cmref.N) mynorm = mcolors.BoundaryNorm(bounds, cmshould.N, extend='min') assert mynorm.vmin == refnorm.vmin assert mynorm.vmax == refnorm.vmax x = [-1, 1.2, 2.3] assert_array_equal(cmshould(mynorm(x)), cmshould([0, 1, 2])) x = np.random.randn(100) * 10 + 2 assert_array_equal(cmshould(mynorm(x)), cmref(refnorm(x))) # Just max cmref = mcolors.ListedColormap(['blue', 'red']) cmref.set_over('black') cmshould = mcolors.ListedColormap(['blue', 'red', 'black']) assert mcolors.same_color(cmref.get_over(), 'black') assert cmref.N == 2 assert cmshould.N == 3 refnorm = mcolors.BoundaryNorm(bounds, cmref.N) mynorm = mcolors.BoundaryNorm(bounds, cmshould.N, extend='max') assert mynorm.vmin == refnorm.vmin assert mynorm.vmax == refnorm.vmax x = [1.2, 2.3, 4] assert_array_equal(cmshould(mynorm(x)), cmshould([0, 1, 2])) x = np.random.randn(100) * 10 + 2 assert_array_equal(cmshould(mynorm(x)), cmref(refnorm(x)))
def __init__(self, labels=None, seed=None, alpha=150, index_direct=True, min_val=0, max_val=255, min_any=0, background=None, dup_for_neg=False, symmetric_colors=False, cmap_labels=None): """Generate discrete colormap for labels using :func:``discrete_colormap``. Args: labels: Labels of integers for which a distinct color should be mapped to each unique label. Deafults to None, in which case no colormap will be generated. seed: Seed for randomizer to allow consistent colormap between runs; defaults to None. alpha: Transparency leve; defaults to 150 for semi-transparent. index_direct: True if the colormap will be indexed directly, which assumes that the labels will serve as indexes to the colormap and should span sequentially from 0, 1, 2, ...; defaults to True. If False, a colormap will be generated for the full range of integers between the lowest and highest label values, inclusive, with a :obj:`colors.BoundaryNorm`, which may incur performance cost. min_val (int): Minimum value for random numbers; defaults to 0. max_val (int): Maximum value for random numbers; defaults to 255. min_any (int, float): Minimum value above which at least one value must be in each set of RGB values; defaults to 0 background: Tuple of (backround_label, (R, G, B, A)), where background_label is the label value specifying the background, and RGBA value will replace the color corresponding to that label. Defaults to None. dup_for_neg: True to duplicate positive labels as negative labels to recreate the same set of labels as for a mirrored labels map. Defaults to False. symmetric_colors (bool): True to make symmetric colors, assuming symmetric labels centered on 0; defaults to False. cmap_labels (List[str]): Sequence of colors as Matplotlib color strings or RGB(A) hex (eg "#0fab24ff") strings. """ self.norm = None self.cmap_labels = None self.img_labels = None self.symmetric_colors = symmetric_colors if labels is None: return labels_unique = np.unique(labels) if dup_for_neg and np.sum(labels_unique < 0) == 0: # for labels that are only >= 0, duplicate the pos portion # as neg so that images with or without negs use the same colors labels_unique = np.append( -1 * labels_unique[labels_unique > 0][::-1], labels_unique) num_colors = len(labels_unique) labels_offset = 0 if index_direct: # assume label vals increase by 1 from 0 until num_colors; store # sorted labels sequence to translate labels based on index self.norm = colors.NoNorm() self.img_labels = labels_unique else: # use labels as bounds for each color, including wide bounds # for large gaps between successive labels; offset bounds to # encompass each label and avoid off-by-one errors that appear # when viewing images with additional extreme labels; float32 # gives unsymmetric colors for large values in mirrored atlases # despite remaining within range for unclear reasons, fixed by # using float64 instead labels_offset = 0.5 bounds = labels_unique.astype(np.float64) bounds -= labels_offset # number of boundaries should be one more than number of labels to # avoid need for interpolation of boundary bin numbers and # potential merging of 2 extreme labels bounds = np.append(bounds, [bounds[-1] + 1]) # TODO: may have occasional colormap inaccuracies from this bug: # https://github.com/matplotlib/matplotlib/issues/9937; self.norm = colors.BoundaryNorm(bounds, num_colors) if cmap_labels is None: # auto-generate colors for the number of labels self.cmap_labels = discrete_colormap(num_colors, alpha, False, seed, min_val, max_val, min_any, symmetric_colors, jitter=20, mode=DiscreteModes.RANDOMN) else: # generate RGBA colors from supplied color strings self.cmap_labels = colors.to_rgba_array(cmap_labels) * max_val if background is not None: # replace background label color with given color bkgdi = np.where(labels_unique == background[0] - labels_offset) if len(bkgdi) > 0 and bkgdi[0].size > 0: self.cmap_labels[bkgdi[0][0]] = background[1] #print(self.cmap_labels) self.make_cmap()
def draw_composite_map(date_obj, t850, u200, v200, u500, v500, mslp, gh500, u850, v850, pwat): """ Draw synoptic composite map. Args: map_subset (int, optional): [description]. Defaults to 1. map_region (list, optional): [description]. Defaults to [70, 140, 20, 60]. """ #Get lat and lon arrays for this dataset: lat = t850.lat.values lon = t850.lon.values #======================================================================================================== # Create a Basemap plotting figure and add geography #======================================================================================================== #Create a Plate Carree projection object proj_ccrs = ccrs.Miller(central_longitude=0.0) #Create figure and axes for main plot and colorbars fig = plt.figure(figsize=(18, 12), dpi=125) gs = gridspec.GridSpec(12, 36, figure=fig) #[ytop:ybot, xleft:xright] ax = plt.subplot(gs[:, :-1], projection=proj_ccrs) #main plot ax.set_xticklabels([]) ax.set_yticklabels([]) ax2 = plt.subplot(gs[:4, -1]) #top plot ax2.set_xticklabels([]) ax2.set_yticklabels([]) ax3 = plt.subplot(gs[4:8, -1]) #bottom plot ax3.set_xticklabels([]) ax3.set_yticklabels([]) ax4 = plt.subplot(gs[8:, -1]) #bottom plot ax4.set_xticklabels([]) ax4.set_yticklabels([]) #Add political boundaries and coastlines ax.add_feature(cfeature.COASTLINE.with_scale('50m'), linewidths=1.2) ax.add_feature(cfeature.BORDERS.with_scale('50m'), linewidths=1.2) ax.add_feature(cfeature.STATES.with_scale('50m'), linewidths=0.5) #Add land/lake/ocean masking land_mask = cfeature.NaturalEarthFeature('physical', 'land', '50m', edgecolor='face', facecolor='#e6e6e6') sea_mask = cfeature.NaturalEarthFeature('physical', 'ocean', '50m', edgecolor='face', facecolor='#ffffff') lake_mask = cfeature.NaturalEarthFeature('physical', 'lakes', '50m', edgecolor='face', facecolor='#ffffff') ax.add_feature(sea_mask, zorder=0) ax.add_feature(land_mask, zorder=0) ax.add_feature(lake_mask, zorder=0) #======================================================================================================== # Fill contours #======================================================================================================== #-------------------------------------------------------------------------------------------------------- # 850-hPa temperature #-------------------------------------------------------------------------------------------------------- #Specify contour settings clevs = np.arange(-40, 40, 1) cmap = plt.cm.jet extend = "both" #Contour fill this variable norm = col.BoundaryNorm(clevs, cmap.N) cs = ax.contourf(lon, lat, t850, clevs, cmap=cmap, norm=norm, extend=extend, transform=proj_ccrs, alpha=0.1) #-------------------------------------------------------------------------------------------------------- # PWAT #-------------------------------------------------------------------------------------------------------- #Specify contour settings clevs = np.arange(20, 71, 0.5) #Define a color gradient for PWAT pwat_colors = gradient([[(255, 255, 255), 0.0], [(255, 255, 255), 20.0]], [[(205, 255, 205), 20.0], [(0, 255, 0), 34.0]], [[(0, 255, 0), 34.0], [(0, 115, 0), 67.0]]) cmap = pwat_colors.get_cmap(clevs) extend = "max" #Contour fill this variable norm = col.BoundaryNorm(clevs, cmap.N) cs = ax.contourf(lon, lat, pwat, clevs, cmap=cmap, norm=norm, extend=extend, transform=proj_ccrs, alpha=0.9) #Add a color bar cbar = plt.colorbar(cs, cax=ax2, shrink=0.75, pad=0.01, ticks=[20, 30, 40, 50, 60, 70]) #-------------------------------------------------------------------------------------------------------- # 250-hPa wind #-------------------------------------------------------------------------------------------------------- #Get the data for this variable wind = calc.wind_speed(u200, v200) #Specify contour settings clevs = [40, 50, 60, 70, 80, 90, 100, 110] cmap = col.ListedColormap([ '#99E3FB', '#47B6FB', '#0F77F7', '#AC97F5', '#A267F4', '#9126F5', '#E118F3', '#E118F3' ]) extend = "max" #Contour fill this variable norm = col.BoundaryNorm(clevs, cmap.N) cs = ax.contourf(lon, lat, wind, clevs, cmap=cmap, norm=norm, extend=extend, transform=proj_ccrs) #Add a color bar cbar = plt.colorbar(cs, cax=ax3, shrink=0.75, pad=0.01, ticks=clevs) #-------------------------------------------------------------------------------------------------------- # 500-hPa smoothed vorticity #-------------------------------------------------------------------------------------------------------- #Get the data for this variable dx, dy = calc.lat_lon_grid_deltas(lon, lat) vort = calc.vorticity(u500, v500, dx, dy) smooth_vort = smooth(vort, 5.0) * 10**5 #Specify contour settings clevs = np.arange(2, 20, 1) cmap = plt.cm.autumn_r extend = "max" #Contour fill this variable norm = col.BoundaryNorm(clevs, cmap.N) cs = ax.contourf(lon, lat, smooth_vort, clevs, cmap=cmap, norm=norm, extend=extend, transform=proj_ccrs, alpha=0.3) #Add a color bar cbar = plt.colorbar(cs, cax=ax4, shrink=0.75, pad=0.01, ticks=clevs[::2]) #======================================================================================================== # Contours #======================================================================================================== #-------------------------------------------------------------------------------------------------------- # MSLP #-------------------------------------------------------------------------------------------------------- #Specify contour settings clevs = np.arange(960, 1040 + 4, 4) style = 'solid' #Plot solid lines color = 'red' #Plot lines as gray width = 0.8 #Width of contours 0.25 #Contour this variable cs = ax.contour(lon, lat, mslp, clevs, colors=color, linewidths=width, linestyles=style, transform=proj_ccrs, alpha=0.9) #Include value labels ax.clabel(cs, inline=1, fontsize=9, fmt='%d') #-------------------------------------------------------------------------------------------------------- # Geopotential heights #-------------------------------------------------------------------------------------------------------- #Get the data for this variable gh500 = gh500 / 10.0 #Specify contour settings clevs = np.arange(480, 612, 4) style = 'solid' #Plot solid lines color = 'black' #Plot lines as gray width = 2.0 #Width of contours #Contour this variable cs = ax.contour(lon, lat, gh500, clevs, colors=color, linewidths=width, linestyles=style, transform=proj_ccrs) #Include value labels ax.clabel(cs, inline=1, fontsize=12, fmt='%d') #-------------------------------------------------------------------------------------------------------- # Surface barbs #-------------------------------------------------------------------------------------------------------- #Plot wind barbs quivers = ax.quiver(lon, lat, u850.values, v850.values, transform=proj_ccrs, regrid_shape=(38, 30), scale=820, alpha=0.5) #-------------------------------------------------------------------------------------------------------- # Label highs & lows #-------------------------------------------------------------------------------------------------------- #Label highs and lows add_mslp_label(ax, proj_ccrs, mslp, lat, lon) #======================================================================================================== # Step 6. Add map boundary, legend, plot title, then save image and close #======================================================================================================== #Add china province boundary add_china_map_2cartopy(ax, name='province') #Add custom legend from matplotlib.lines import Line2D custom_lines = [ Line2D([0], [0], color='#00A123', lw=5), Line2D([0], [0], color='#0F77F7', lw=5), Line2D([0], [0], color='#FFC000', lw=5), Line2D([0], [0], color='k', lw=2), Line2D([0], [0], color='k', lw=0.1, marker=r'$\rightarrow$', ms=20), Line2D([0], [0], color='r', lw=0.8), ] ax.legend(custom_lines, [ 'PWAT (mm)', '200-hPa Wind (m/s)', '500-hPa Vorticity', '500-hPa Height (dam)', '850-hPa Wind (m/s)', 'MSLP (hPa)' ], loc=2, prop={'size': 12}) #Format plot title title = "Synoptic Composite \nValid: " + dt.datetime.strftime( date_obj, '%Y-%m-%d %H%M UTC') st = plt.suptitle(title, fontweight='bold', fontsize=16) st.set_y(0.92) #Return figuration return (fig)
def get_probas(path='', idec_weights='', n_clusters=6, window=1, screen='t15', tag=''): n_clusters = int(n_clusters) window = int(window) columns = [ 't', 'crawl', 'bend', 'stop', 'head retraction', 'back crawl', 'roll', 'straight_proba', 'straight_and_light_bend_proba', 'bend_proba', 'curl_proba', 'ball_proba', 'larva_length_smooth_5', 'larva_length_deriv_smooth_5', 'S_smooth_5', 'S_deriv_smooth_5', 'eig_smooth_5', 'eig_deriv_smooth_5', 'angle_upper_lower_smooth_5', 'angle_upper_lower_deriv_smooth_5', 'angle_downer_upper_smooth_5', 'angle_downer_upper_deriv_smooth_5', 'd_eff_head_norm_smooth_5', 'd_eff_head_norm_deriv_smooth_5', 'd_eff_tail_norm_smooth_5', 'd_eff_tail_norm_deriv_smooth_5', 'motion_velocity_norm_smooth_5', 'head_velocity_norm_smooth_5', 'tail_velocity_norm_smooth_5', 'As_smooth_5', 'prod_scal_1', 'prod_scal_2', 'motion_to_u_tail_head_smooth_5', 'motion_to_v_tail_head_smooth_5' ] feats = [ 'larva_length_smooth_5', 'S_smooth_5', 'eig_smooth_5', 'angle_upper_lower_smooth_5', 'angle_downer_upper_smooth_5', 'd_eff_head_norm_smooth_5', 'd_eff_tail_norm_smooth_5', 'As_smooth_5', 'prod_scal_1', 'prod_scal_2' ] if screen == 't15': start_time = 20 end_time = 60 elif screen == 't5': start_time = 40 end_time = 95 Ts = 0.08 window_len = int(np.floor(float(window) / Ts)) x_shape = (0, (2 * window_len + 1) * len(feats)) ae, decoder = autoencoder(dims=[x_shape[-1], 500, 500, 2000, 10]) n_stacks = len([x_shape[-1], 500, 500, 2000, 10]) - 1 hidden = ae.get_layer(name='encoder_%d' % (n_stacks - 1)).output clustering_layer = ClusteringLayer(n_clusters, name='clustering')(hidden) modele = Model(inputs=ae.input, outputs=[clustering_layer, ae.output]) modele.load_weights(idec_weights) files = [] proba = np.zeros((int((end_time - start_time) / Ts), n_clusters)) for dirs, _, _ in os.walk(path): files += glob.glob(dirs + r"/State_Amplitude_t*.txt") if files: for file in sorted(files): df = pd.read_csv(file, sep='\t', header=None, names=columns) # Reducing the length of df to the moments of interest. We shorten it to length (end_time - start_time)/Ts + 2*window_len b/c of sampling discrepancies df = df[(df['t'] > (start_time - window)) & (df['t'] < (end_time + window))].reset_index( drop=True)[:int(len(proba) + 2 * window_len)] for col in feats: if df[col].dtype == object: try: df[col] = (df[col].str.split('+')).str[0] df[col] = pd.to_numeric( (df[col].str.split('[0-9]-')).str[0]) except: break # Re-scale features maxs = df[feats].max() mins = df[feats].min() df[feats] = (df[feats] - mins) / (maxs - mins) if len(df) > 100: x = [] for i in range(window_len, len(df) - window_len): if not (np.isnan(df[feats][i - window_len:i + window_len + 1].T.values.flatten()).any()): x.append( np.array(df[feats][i - window_len:i + window_len + 1].T.values.flatten())[:, None].T) x = np.vstack(x) time = df[(df['t'] > start_time) & (df['t'] < end_time + 1)]['t'].values[:len(x)] if not (len(x) <= len(proba)) or not (len(x) == len(time)): continue res = modele.predict(x) predictions = res[0].argmax(axis=1) for i in range(len(x)): time_index = min( int((time[i] - start_time) / Ts) - 1, len(proba) - 1) proba[time_index, predictions[i]] += 1 proba = proba / np.sum(proba, axis=1)[:, None] times = np.arange(start_time, end_time, Ts)[:len(proba)] save_dir = path + "/" + screen + tag + '/probabilities' if not os.path.exists(save_dir): os.makedirs(save_dir) np.savez_compressed(save_dir + '/probas_' + str(n_clusters) + '_clusters.npz', x=proba, t=times) # Save plot as png fig, ax = plt.subplots(1, 1, figsize=(7, 5)) bounds = [i for i in range(proba.shape[-1] + 1)] norm = colors.BoundaryNorm(boundaries=bounds, ncolors=proba.shape[-1]) cm = plt.cm.get_cmap('Paired') for i in range(proba.shape[-1]): plt.plot(times, proba[:, i], color=cm(i)) plt.title('Probabilities ' + tag[1:] + ' ' + str(n_clusters) + ' clusters') ax2 = fig.add_axes([0.95, 0.12, 0.03, 0.7]) cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cm, norm=norm, boundaries=bounds, ticks=bounds, spacing='proportional') plt.savefig(save_dir + '/probabilities' + str(tag[1:]) + '_' + str(n_clusters) + '_clusters' '.png') print('Successfully computed probabilities')
# plt.title(r"Profile for 591", fontsize=16) plt.gca().invert_xaxis() # reverses x axis # # ax = fig # plt.savefig('/media/kiruba/New Volume/r/r_dir/stream_profile/new_code/591/linear_interpolation') plt.show() # raise SystemExit(0) # ## trace contours # Refer: Nikolai Shokhirev http://www.numericalexpert.com/blog/area_calculation/ check_dam_height = 0.66 #metre levels = [ 0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3, 1.4, 1.41, 1.5 ] #, 3.93] cmap = cm.hot norm = mc.BoundaryNorm(levels, cmap.N) plt.figure(figsize=(11.69, 8.27)) CS = plt.contourf(xi, yi, zi, len(levels), alpha=.75, norm=norm, levels=levels) C = plt.contour(xi, yi, zi, len(levels), colors='black', linewidth=.5, levels=levels) plt.clabel(C, inline=1, fontsize=10) plt.colorbar(CS, shrink=0.5, aspect=5) plt.yticks(np.arange(0, 30, 5)) plt.xticks(np.arange(-6, 6, 2)) plt.grid() plt.gca().invert_xaxis()
def twoDview(var, tindex, grid, filename=None, \ cmin=None, cmax=None, clev=None, fill=False, \ contour=False, d=4, range=None, fts=None, \ title=None, clb=True, pal=None, proj='merc', \ fill_land=False, outfile=None): """ map = twoDview(var, tindex, grid, {optional switch}) optional switch: - filename if defined, load the variable from file - cmin set color minimum limit - cmax set color maximum limit - clev set the number of color step - fill use contourf instead of pcolor - contour overlay contour (request fill=True) - d contour density (default d=4) - range set axis limit - fts set font size (default: 12) - title add title to the plot - clb add colorbar (defaul: True) - pal set color map (default: cm.jet) - proj set projection type (default: merc) - fill_land fill land masked area with gray (defaul: True) - outfile if defined, write figure to file plot 2-dimensions variable var. If filename is provided, var must be a string and the variable will be load from the file. grid can be a grid object or a gridid. In the later case, the grid object correponding to the provided gridid will be loaded. If proj is not None, return a Basemap object to be used with quiver for example. """ # get grid if type(grid).__name__ == 'ROMS_Grid': grd = grid else: grd = pyroms.grid.get_ROMS_grid(grid) # get variable if filename == None: var = var else: data = pyroms.io.Dataset(filename) var = data.variables[var] Np, Mp, Lp = grd.vgrid.z_r[0, :].shape if tindex is not -1: assert len(var.shape) == 3, 'var must be 3D (time plus space).' K, M, L = var.shape else: assert len(var.shape) == 2, 'var must be 2D (no time dependency).' M, L = var.shape # determine where on the C-grid these variable lies if M == Mp and L == Lp: Cpos = 'rho' if fill == True: lon = grd.hgrid.lon_rho lat = grd.hgrid.lat_rho else: lon = grd.hgrid.lon_vert lat = grd.hgrid.lat_vert mask = grd.hgrid.mask_rho if M == Mp and L == Lp - 1: Cpos = 'u' if fill == True: lon = grd.hgrid.lon_u lat = grd.hgrid.lat_u else: lon = 0.5 * (grd.hgrid.lon_vert[:, :-1] + grd.hgrid.lon_vert[:, 1:]) lat = 0.5 * (grd.hgrid.lat_vert[:, :-1] + grd.hgrid.lat_vert[:, 1:]) mask = grd.hgrid.mask_u if M == Mp - 1 and L == Lp: Cpos = 'v' if fill == True: lon = grd.hgrid.lon_v lat = grd.hgrid.lat_v else: lon = 0.5 * (grd.hgrid.lon_vert[:-1, :] + grd.hgrid.lon_vert[1:, :]) lat = 0.5 * (grd.hgrid.lat_vert[:-1, :] + grd.hgrid.lat_vert[1:, :]) mask = grd.hgrid.mask_v if M == Mp - 1 and L == Lp - 1: Cpos = 'psi' if fill == True: lon = grd.hgrid.lon_psi lat = grd.hgrid.lat_psi else: lon = grd.hgrid.lon_rho lat = grd.hgrid.lat_rho mask = grd.hgrid.mask_psi # get 2D var if tindex == -1: var = var[:, :] else: var = var[tindex, :, :] # mask var = np.ma.masked_where(mask == 0, var) # plot if cmin is None: cmin = var.min() else: cmin = float(cmin) if cmax is None: cmax = var.max() else: cmax = float(cmax) if clev is None: clev = 100. else: clev = float(clev) dc = (cmax - cmin) / clev vc = np.arange(cmin, cmax + dc, dc) if pal is None: pal = cm.jet else: pal = pal if fts is None: fts = 12 else: fts = fts #pal.set_over('w', 1.0) #pal.set_under('w', 1.0) #pal.set_bad('w', 1.0) pal_norm = colors.BoundaryNorm(vc, ncolors=256, clip=False) if range is None: lon_min = lon.min() lon_max = lon.max() lon_0 = (lon_min + lon_max) / 2. lat_min = lat.min() lat_max = lat.max() lat_0 = (lat_min + lat_max) / 2. else: lon_min = range[0] lon_max = range[1] lon_0 = (lon_min + lon_max) / 2. lat_min = range[2] lat_max = range[3] lat_0 = (lat_min + lat_max) / 2. # clear figure #plt.clf() if proj is not None: map = Basemap(projection=proj, llcrnrlon=lon_min, llcrnrlat=lat_min, \ urcrnrlon=lon_max, urcrnrlat=lat_max, lat_0=lat_0, lon_0=lon_0, \ resolution='h', area_thresh=5.) #map = pyroms.utility.get_grid_proj(grd, type=proj) x, y = list(map(lon, lat)) if fill_land is True and proj is not None: # fill land and draw coastlines map.drawcoastlines() map.fillcontinents(color='grey') else: if proj is not None: Basemap.pcolor(map, x, y, mask, vmin=-2, cmap=cm.gray, edgecolors='face') pyroms_toolbox.plot_coast_line(grd, map) else: plt.pcolor(lon, lat, mask, vmin=-2, cmap=cm.gray, edgecolors='face') pyroms_toolbox.plot_coast_line(grd) if fill is True: if proj is not None: cf = Basemap.contourf(map, x, y, var, vc, cmap = pal, \ norm = pal_norm) else: cf = plt.contourf(lon, lat, var, vc, cmap = pal, \ norm = pal_norm) else: if proj is not None: cf = Basemap.pcolor(map, x, y, var, cmap=pal, norm=pal_norm, edgecolors='face') else: cf = plt.pcolor(lon, lat, var, cmap=pal, norm=pal_norm, edgecolors='face') if clb is True: clb = plt.colorbar(cf, fraction=0.075, format='%.2f') for t in clb.ax.get_yticklabels(): t.set_fontsize(fts) if contour is True: if fill is not True: raise Warning( 'Please run again with fill=True to overlay contour.') else: if proj is not None: Basemap.contour(map, x, y, var, vc[::d], colors='k', linewidths=0.5, linestyles='solid') else: plt.contour(lon, lat, var, vc[::d], colors='k', linewidths=0.5, linestyles='solid') if proj is None and range is not None: plt.axis(range) if title is not None: plt.title(title, fontsize=fts + 4) if proj is not None: map.drawmeridians(np.arange(lon_min,lon_max, (lon_max-lon_min)/5.), \ labels=[0,0,0,1], fmt='%.1f') map.drawparallels(np.arange(lat_min,lat_max, (lat_max-lat_min)/5.), \ labels=[1,0,0,0], fmt='%.1f') if outfile is not None: if outfile.find('.png') != -1 or outfile.find('.svg') != -1 or \ outfile.find('.eps') != -1: print('Write figure to file', outfile) plt.savefig(outfile, dpi=200, facecolor='w', edgecolor='w', \ orientation='portrait') else: print( 'Unrecognized file extension. Please use .png, .svg or .eps file extension.' ) if proj is None: return else: return map
if c==13: if cou[i,j]==a1[c] or cou[i,j]==a2[c]: newisamy[i,j]= allyisam[c] newisamy1[i,j]= isamgrid[c] newisamp[i,j]= allproisam[c] else: if cou[i,j]>=a1[c] and cou[i,j]<=a2[c]: newisamy[i,j]= allyisam[c] newisamy1[i,j]= isamgrid[c] newisamp[i,j]= allproisam[c] cmap = plt.cm.terrain_r bounds=[0.0,1,10,100,1000,5000,10000,50000,100000,200000] #bounds=[-0.1,0.0,0.1,0.2] norm2 = colors.BoundaryNorm(bounds, cmap.N) bounds1=[-0.1,0.0,0.01,0.1,0.25,0.5,0.75,1,1.25,1.5,1.75,2] norm1 = colors.BoundaryNorm(bounds1, cmap.N) fig = plt.figure(figsize=(33,10)) n_groups = 14 #ax = fig.add_subplot(111) index = N.arange(n_groups) bar_width = 0.21 opacity = 0.8 rects1 = plt.bar(index+0.18, allproisam, bar_width, alpha=opacity,
def main(argv): """Go Main Go""" v = argv[1] agg = argv[2] ts = datetime.date(2008, 1, 1) ts2 = datetime.date(2016, 12, 31) scenario = 0 # suggested for runoff and precip if v in ['qc_precip', 'avg_runoff']: colors = ['#ffffa6', '#9cf26d', '#76cc94', '#6399ba', '#5558a1'] # suggested for detachment elif v in ['avg_loss']: colors = ['#cbe3bb', '#c4ff4d', '#ffff4d', '#ffc44d', '#ff4d4d', '#c34dee'] # suggested for delivery elif v in ['avg_delivery']: colors = ['#ffffd2', '#ffff4d', '#ffe0a5', '#eeb74d', '#ba7c57', '#96504d'] cmap = mpcolors.ListedColormap(colors, 'james') cmap.set_under('white') cmap.set_over('black') pgconn = psycopg2.connect(database='idep', host='localhost', port=5555, user='******') title = "for %s" % (ts.strftime("%-d %B %Y"),) if ts != ts2: title = "between %s and %s" % (ts.strftime("%-d %b %Y"), ts2.strftime("%-d %b %Y")) mp = MapPlot(axisbg='#EEEEEE', nologo=True, sector='iowa', nocaption=True, title=("DEP %s %s %s" ) % (V2NAME[v], "Yearly Average" if agg == 'avg' else 'Total', title), caption='Daily Erosion Project') df = read_postgis(""" WITH data as ( SELECT huc_12, extract(year from valid) as yr, sum("""+v+""") as d from results_by_huc12 WHERE scenario = %s and valid >= %s and valid <= %s GROUP by huc_12, yr), agg as ( SELECT huc_12, """ + agg + """(d) as d from data GROUP by huc_12) SELECT ST_Transform(simple_geom, 4326) as geo, coalesce(d.d, 0) as data from huc12 i LEFT JOIN agg d ON (i.huc_12 = d.huc_12) WHERE i.scenario = %s and i.states ~* 'IA' """, pgconn, params=(scenario, ts, ts2, scenario), geom_col='geo', index_col=None) df['data'] = df['data'] * V2MULTI[v] if df['data'].max() < 0.01: bins = [0.01, 0.02, 0.03, 0.04, 0.05] else: bins = np.array(V2RAMP[v]) * (10. if agg == 'sum' else 1.) norm = mpcolors.BoundaryNorm(bins, cmap.N) # m.ax.add_geometries(df['geo'], ccrs.PlateCarree()) for _, row in df.iterrows(): c = cmap(norm([row['data'], ]))[0] arr = np.asarray(row['geo'].exterior) points = mp.ax.projection.transform_points(ccrs.Geodetic(), arr[:, 0], arr[:, 1]) p = Polygon(points[:, :2], fc=c, ec='k', zorder=2, lw=0.1) mp.ax.add_patch(p) mp.drawcounties() mp.drawcities() lbl = [round(_, 2) for _ in bins] u = "%s, Avg: %.2f" % (V2UNITS[v], df['data'].mean()) mp.draw_colorbar(bins, cmap, norm, clevlabels=lbl, units=u, title="%s :: %s" % (V2NAME[v], V2UNITS[v])) plt.savefig('%s_%s_%s%s.eps' % (ts.year, ts2.year, v, "_sum" if agg == 'sum' else ''))
def plot_colormapped(self, u, v, c, bounds=None, colors=None, **kwargs): r"""Plot u, v data, with line colored based on a third set of data. Plots the wind data on the hodograph, but with a colormapped line. Takes a third variable besides the winds and either a colormap to color it with or a series of bounds and colors to create a colormap and norm to control colormapping. The bounds must always be in increasing order. For using custom bounds with height data, the function will automatically interpolate to the actual bounds from the height and wind data, as well as convert the input bounds from height AGL to height above MSL to work with the provided heights. Simple wrapper around plot so that pressure is the first (independent) input. This is essentially a wrapper around `semilogy`. It also sets some appropriate ticking and plot ranges. Parameters ---------- u : array_like u-component of wind v : array_like v-component of wind c : array_like data to use for colormapping bounds: array-like, optional Array of bounds for c to use in coloring the hodograph. colors: list, optional Array of strings representing colors for the hodograph segments. kwargs Other keyword arguments to pass to :class:`matplotlib.collections.LineCollection` Returns ------- matplotlib.collections.LineCollection instance created See Also -------- :meth:`Hodograph.plot` """ u, v, c = delete_masked_points(u, v, c) # Plotting a color segmented hodograph if colors: cmap = mcolors.ListedColormap(colors) # If we are segmenting by height (a length), interpolate the bounds if bounds.dimensionality == {'[length]': 1.0}: bounds = bounds + c[0] # Make all heights AGL interpolation_heights = concatenate((bounds, c)) interpolation_heights = (np.sort(interpolation_heights) * interpolation_heights.units) c, u, v = interp(interpolation_heights, c, c, u, v) c = c.to_base_units() # TODO: This shouldn't be required! # If segmenting by anything else, do not interpolate, just use the data else: bounds = np.asarray(bounds) * bounds.units norm = mcolors.BoundaryNorm(bounds.magnitude, cmap.N) cmap.set_over('none') cmap.set_under('none') kwargs['cmap'] = cmap kwargs['norm'] = norm line_args = self._form_line_args(kwargs) # Plotting a continuously colored line else: line_args = self._form_line_args(kwargs) # Do the plotting lc = colored_line(u, v, c, **line_args) self.ax.add_collection(lc) return lc
def __init__(self, maze_generator, pob_size=1, action_type='VonNeumann', obs_type='full', live_display=False, render_trace=False, dynamic=False): """Initialize the maze. DType: list""" # Maze: 0: free space, 1: wall self.maze_generator = maze_generator self.maze = np.array(self.maze_generator.get_maze()) self.maze_size = self.maze.shape self.init_state, self.goal_states = self.maze_generator.init_end_states() self.valid_states = [state for state, _ in np.ndenumerate(self.maze)] self.render_trace = render_trace self.traces = [] self.action_type = action_type self.obs_type = obs_type # If True, show the updated display each time render is called rather # than storing the frames and creating an animation at the end self.live_display = live_display self.state = None # Action space: 0: Up, 1: Down, 2: Left, 3: Right if self.action_type == 'VonNeumann': # Von Neumann neighborhood self.num_actions = 4 elif action_type == 'Moore': # Moore neighborhood self.num_actions = 8 else: raise TypeError('Action type must be either \'VonNeumann\' or \'Moore\'') self.action_space = spaces.Discrete(self.num_actions) self.all_actions = list(range(self.action_space.n)) # Size of the partial observable window self.pob_size = pob_size # Observation space low_obs = 0 # Lowest integer in observation high_obs = 10 # Highest integer in observation if self.obs_type == 'full': self.observation_space = spaces.Box(low=low_obs, high=high_obs, shape=self.maze_size) elif self.obs_type == 'partial': self.observation_space = spaces.Box(low=low_obs, high=high_obs, shape=(self.pob_size*2+1, self.pob_size*2+1)) else: raise TypeError('Observation type must be either \'full\' or \'partial\'') # Colormap: order of color is, free space, wall, agent, food, poison self.cmap = colors.ListedColormap(['white', 'black', 'blue', 'green', 'red', 'gray', 'lightblue','steelblue','powderblue', 'lightgray','yellow']) self.bounds = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # values for each color self.norm = colors.BoundaryNorm(self.bounds, self.cmap.N) self.ax_imgs = [] # For generating videos self.policy = None # Set a policy for this environment self.dynamic = dynamic # Activate dynamic obstacles
def classificaGELO(caminhonc, caminhosaidatsmmsg, caminhosaidaantartica): # >> Gerando a lista -------------------------------------------- dirList = os.listdir(caminhonc) print dirList controleNome = dirList[0] #Buscando variaveis nos elementos da lista posicA = controleNome.find( "CON") #Buscando variaveis nos elementos da lista anoi = posicA + 4 #Buscando variaveis nos elementos da lista anof = posicA + 8 #Buscando variaveis nos elementos da lista mesi = posicA + 8 #Buscando variaveis nos elementos da lista mesf = posicA + 10 #Buscando variaveis nos elementos da lista diai = posicA + 10 #Buscando variaveis nos elementos da lista diaf = posicA + 12 #Buscando variaveis nos elementos da lista horai = posicA + 12 #Buscando variaveis nos elementos da lista horaf = posicA + 14 #Buscando variaveis nos elementos da lista ano = controleNome[anoi:anof] #Buscando variaveis nos elementos da lista mes = controleNome[mesi:mesf] #Buscando variaveis nos elementos da lista dia = controleNome[diai:diaf] #Buscando variaveis nos elementos da lista horaTIT = controleNome[horai: horaf] #Buscando variaveis nos elementos da lista #imLista = len(dirList) #Definindo o tamnho da lista imLista = 01 #Redefinindo para considerar com base de 24 horas print ano, mes, dia, horaTIT print "################### TRATANDO OS DADOS *composição* ###################" # >> Lendo os arquivos e compondo a matriz ---------------------- entradas = '' for hora in range(0, imLista): entradas = caminhonc + nomeMSG + ano + mes + dia + str(horaTIT).zfill( 2) + FMT_nc #Criando caminho print entradas fileCon = Dataset(entradas, 'r') #------------------------------------------------------------------ IC0 = fileCon.variables['icec'][:] IC0 = numpy.copy(IC0) dims = IC0.shape ny = dims[1] nx = dims[2] IC0 = IC0[0, :, :] IC02 = np.zeros((ny, nx)) #print min(IC0) #print max(IC0) #exit() for j in range(0, nx): for i in range(0, ny): if IC0[i, j] > 0. and IC0[i, j] <= 9.9: IC02[i, j] = 3 elif IC0[i, j] > 9.9 and IC0[i, j] <= 39.9: IC02[i, j] = 2 elif IC0[i, j] > 39.9 and IC0[i, j] <= 100.0: IC02[i, j] = 1 elif IC0[i, j] == 0.: IC02[i, j] = 3 else: IC02[i, j] = 0 #fileCon.close IC02 = IC02 #my_cmap = colores.ListedColormap([(.3, .3, .3), (1., 0., 0.), (1., 0.4901960784, 0.0274509804), (1., 1., 0), (0.5490196078, 1., 0.6274509804), (0.5882352941, 0.7843137255, 1.), (0., 0.3921568627, 1.)]) my_cmap = colors.ListedColormap(['gray', 'red', 'yellow', 'green']) filename2 = '/home/geonetcast/gelo/d-eumetcast/LonLatGelo_OSI-SAF.nc' print filename2 file1 = Dataset(filename2, 'r') lon0 = file1.variables['longitude'][:] lat0 = file1.variables['latitude'][:] fig = plt.figure(figsize=(9.5, 9)) #print "to aqui" Dmap = Basemap( projection='spstere', lat_0=-90, lon_0=0., boundinglat=-55, lat_ts=-70, resolution='h', rsphere=(6378273., 6356889.44891)) #modificar a projeção entre os polos #Dmap.fillcontinents(color='gray') cor = 'black' x0, y0 = Dmap(lon0, lat0) cmap = cm.Blues IC0 = np.flipud(IC0[:, :]) col = Dmap.pcolor(x0, y0, IC0, shading='interp', vmin=0, vmax=3, cmap=my_cmap) # define parallels and meridians to draw. parallels = np.arange(-90., -55, 10.) meridians = np.arange(0., 360., 20.) Dmap.drawcoastlines(linewidth=.5, color=cor) # Linha de costa Dmap.drawcountries(linewidth=0.8, color='k', antialiased=1, ax=None, zorder=None) Dmap.drawstates(linewidth=0.5, color='k', antialiased=1, ax=None, zorder=None) Dmap.drawparallels(parallels, labels=[1, 0, 0, 0], color=cor, dashes=[1, 0], linewidth=0.2) Dmap.drawmeridians(meridians, labels=[0, 0, 0, 1], color=cor, dashes=[1, 0], linewidth=0.2) #Paleta de Cor #bounds=[421.5,422.5,423.5,424.5,425.5,426.5,427.5] bounds = [0.5, 1.5, 2.5, 3.5] norm = colores.BoundaryNorm(bounds, my_cmap.N) #l_label = '0-Mascara/1-Sem Gelo/2-Gelo Aberto/3-Gelo Fechado' #cbar = fig.colorbar(col, cmap=cm.Blues, norm=norm, boundaries=bounds, ticks=[422,423,424,425,426,427], orientation='horizontal', shrink=0.8,pad=0.045) #cbar.set_ticklabels(['9-10 Thents', '7-8 Thents', '4-6 Thents', '1-3 Thents', '< 1 Thents', 'Ice Free']) cbar = fig.colorbar(col, cmap=cmap, norm=norm, boundaries=bounds, ticks=[0, 1, 2, 3], orientation='horizontal', shrink=0.8, pad=0.045) cbar.set_ticklabels(['Gelo Fechado', 'Gelo Aberto', 'Sem Gelo']) #------------------------------------------------------------------ #Salvando e apresentado a imagem. d1 = datetime.date(int(ano), int(mes), int(dia)) #DIAS DA SEMANA diasem = d1.strftime("%A") if diasem == 'Monday': diasem = 'SEG' elif diasem == 'Tuesday': diasem = 'TER' elif diasem == 'Wednesday': diasem = 'QUA' elif diasem == 'Thursday': diasem = 'QUI' elif diasem == 'Friday': diasem = 'SEX' elif diasem == 'Saturday': diasem = 'SAB' elif diasem == 'Sunday': diasem = 'DOM' #MES EM NOME nomemes = d1.strftime("%B") if nomemes == 'January': nomemes = 'JAN' elif nomemes == 'February': nomemes = 'FEV' elif nomemes == 'March': nomemes = 'MAR' elif nomemes == 'April': nomemes = 'ABR' elif nomemes == 'May': nomemes = 'MAI' elif nomemes == 'June': nomemes = 'JUN' elif nomemes == 'July': nomemes = 'JUL' elif nomemes == 'August': nomemes = 'AGO' elif nomemes == 'September': nomemes = 'SET' elif nomemes == 'October': nomemes = 'OUT' elif nomemes == 'November': nomemes = 'NOV' elif nomemes == 'December': nomemes = 'DEZ' title('CHM-REMO Limite de Gelo Marinho ' + str(dia).zfill(2) + nomemes + ano + '(' + diasem + ') - EUMETCAST/O&SI-SAF', fontsize=8., fontweight='bold') dirsaida = caminhosaidaantartica FMT_png = ".png" nome1 = "LIM_GEL_" fname = dirsaida + nome1 + str(ano) + str(mes).zfill(2) + str(dia).zfill( 2) + 'analise' + FMT_png #savefig(fname, bbox_inches='tight') savefig(fname, dpi=700, bbox_inches='tight')
def rand_cmap(nlabels, type='bright', first_color_black=True, last_color_black=False, verbose=True): if type not in ('bright', 'soft'): print('Please choose "bright" or "soft" for type') return if verbose: print('Number of labels: ' + str(nlabels)) # Generate color map for bright colors, based on hsv if type == 'bright': randHSVcolors = [(np.random.uniform(low=0.0, high=1), np.random.uniform(low=0.2, high=1), np.random.uniform(low=0.9, high=1)) for i in range(nlabels)] # Convert HSV list to RGB randRGBcolors = [] for HSVcolor in randHSVcolors: randRGBcolors.append( colorsys.hsv_to_rgb(HSVcolor[0], HSVcolor[1], HSVcolor[2])) if first_color_black: randRGBcolors[0] = [0, 0, 0] if last_color_black: randRGBcolors[-1] = [0, 0, 0] random_colormap = LinearSegmentedColormap.from_list('new_map', randRGBcolors, N=nlabels) # Generate soft pastel colors, by limiting the RGB spectrum if type == 'soft': low = 0.6 high = 0.95 randRGBcolors = [(np.random.uniform(low=low, high=high), np.random.uniform(low=low, high=high), np.random.uniform(low=low, high=high)) for i in range(nlabels)] if first_color_black: randRGBcolors[0] = [0, 0, 0] if last_color_black: randRGBcolors[-1] = [0, 0, 0] random_colormap = LinearSegmentedColormap.from_list('new_map', randRGBcolors, N=nlabels) # Display colorbar if verbose: from matplotlib import colors, colorbar from matplotlib import pyplot as plt fig, ax = plt.subplots(1, 1, figsize=(15, 0.5)) bounds = np.linspace(0, nlabels, nlabels + 1) norm = colors.BoundaryNorm(bounds, nlabels) cb = colorbar.ColorbarBase(ax, cmap=random_colormap, norm=norm, spacing='proportional', ticks=None, boundaries=bounds, format='%1i', orientation=u'horizontal') return random_colormap
matchCtr += 1 print "There are", mismatchCtr, "mismatches!" print "There are", matchCtr, "matches!" print "------------------------" cmap = colors.ListedColormap(['white', 'blue', 'red']) cmap1 = colors.ListedColormap(['white', 'red', 'green']) if np.array_equal(outputPattern_cumulative, expected_outputPattern_cumulative): print "Everything Works!!", "Total Number of Hits =", int( np.sum(outputPattern_cumulative)) bounds = [0, 1, 2, 3] norm = colors.BoundaryNorm(bounds, cmap.N) # plt.figure() # plt.imshow(outputPattern,cmap=cmap1, norm = norm, interpolation = 'Nearest',aspect='auto') # plt.grid("on") # plt.xticks([i for i in range(0,32)]) # yr= [4*i for i in range(-1,32)] # yr.append(127) # plt.yticks(yr) # plt.title("protoVIPRAM 2D \n Test: Real Hits = Expected Hits, number of hits :" + str(int(np.sum(outputPattern)))) # plt.xlabel("Columns") # plt.ylabel("Rows") else: print "Expected number of hits :", int( np.sum(expected_outputPattern_cumulative))
def main(): N = 5 #number of iterations of prisoner's dilemma prob_betray_A = 1.0 prob_betray_B = 0.0 if random() > prob_betray_A: A = COOP else: A = BETRAY if random() > prob_betray_B: B = COOP else: B = BETRAY prisoner = np.empty(shape=(N, 2)) for i in range(0, N): for j in range(0, 2): prisoner[i, j] = NOTHING prisoner[0,0] = A prisoner[0,1] = B sumA = years_served(prisoner[0,0],prisoner[0,1]) sumB = years_served(prisoner[0, 1], prisoner[0, 0]) years_served_A = [sumA] years_served_B = [sumB] for i in range(1,N): newA = prisoner[i-1,1] #classic solution - tit for tat reciprocate other persons response from last time newB = prisoner[i-1,0] prisoner[i,0]=newA prisoner[i, 1] = newB sumA += years_served(prisoner[i, 0], prisoner[i, 1]) sumB += years_served(prisoner[i, 1], prisoner[i, 0]) years_served_A.append(sumA) years_served_B.append(sumB) #print grids[1] #rectangle = plt.Rectangle((0.5, 0.5), n - 2, n - 2, fc='none') #plt.gca().add_patch(rectangle) axes = AxesSequence() cmap1 = colors.ListedColormap(['red', 'green', 'black']) bounds=[0,0.99,1.99,2.99] norm=colors.BoundaryNorm(bounds, cmap1.N) plt.imshow(prisoner, cmap=cmap1, interpolation='nearest', norm=norm) plt.show() title_base = "Number of Years Served" title = title_base filename = "mod112.png" xlabel = "Number of Iterations" ylabel = "Years Served" y1label = "Years Served A" y2label = "Years Served B" TwoLineColorsPlot111(range(N),years_served_A,y1label,years_served_B,y2label,xlabel,ylabel,title,filename) """
def plotter(fdict): """ Go """ pgconn = get_dbconn('coop') ccursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor) ctx = get_autoplot_context(fdict, get_description()) station = ctx['station'] year = ctx['year'] gdd1 = ctx['gdd1'] gdd2 = ctx['gdd2'] table = "alldata_%s" % (station[:2],) nt = network.Table("%sCLIMATE" % (station[:2],)) ccursor.execute(""" SELECT day, gddxx(%s, %s, high, low) as gdd from """+table+""" WHERE year = %s and station = %s ORDER by day ASC """, (ctx['gddbase'], ctx['gddceil'], year, station)) days = [] gdds = [] for row in ccursor: gdds.append(float(row['gdd'])) days.append(row['day']) yticks = [] yticklabels = [] jan1 = datetime.datetime(year, 1, 1) for i in range(110, 330): ts = jan1 + datetime.timedelta(days=i) if ts.day == 1 or ts.day % 12 == 1: yticks.append(i) yticklabels.append(ts.strftime("%-d %b")) gdds = np.array(gdds) sts = datetime.datetime(year, 4, 1) ets = datetime.datetime(year, 6, 1) now = sts sz = len(gdds) days2 = [] starts = [] heights = [] success = [] rows = [] while now < ets: idx = int(now.strftime("%j")) - 1 running = 0 while idx < sz and running < gdd1: running += gdds[idx] idx += 1 idx0 = idx while idx < sz and running < gdd2: running += gdds[idx] idx += 1 success.append(running >= gdd2) idx1 = idx days2.append(now) starts.append(idx0) heights.append(idx1 - idx0) rows.append(dict(plant_date=now, start_doy=idx0, end_doy=idx1, success=success[-1])) now += datetime.timedelta(days=1) if True not in success: raise ValueError("No data, pick lower GDD values") df = pd.DataFrame(rows) heights = np.array(heights) success = np.array(success) starts = np.array(starts) cmap = plt.get_cmap(ctx['cmap']) bmin = min(heights[success]) - 1 bmax = max(heights[success]) + 1 bins = np.arange(bmin, bmax+1.1) norm = mpcolors.BoundaryNorm(bins, cmap.N) ax = plt.axes([0.125, 0.125, 0.75, 0.75]) bars = ax.bar(days2, heights, bottom=starts, fc='#EEEEEE') for i, mybar in enumerate(bars): if success[i]: mybar.set_facecolor(cmap(norm([heights[i]])[0])) ax.grid(True) ax.set_yticks(yticks) ax.set_yticklabels(yticklabels) ax.set_ylim(min(starts)-7, max(starts+heights)+7) ax.xaxis.set_major_formatter(mdates.DateFormatter('%-d\n%b')) ax.set_xlabel("Planting Date") ax.set_title(("%s [%s] %s GDD [base=%s,ceil=%s]\n" "Period between GDD %s and %s, gray bars incomplete" ) % (nt.sts[station]['name'], station, year, ctx['gddbase'], ctx['gddceil'], gdd1, gdd2)) ax2 = plt.axes([0.92, 0.1, 0.07, 0.8], frameon=False, yticks=[], xticks=[]) ax2.set_xlabel("Days") for i, mybin in enumerate(bins): ax2.text(0.52, i, "%g" % (mybin,), ha='left', va='center', color='k') # txt.set_path_effects([PathEffects.withStroke(linewidth=2, # foreground="k")]) ax2.barh(np.arange(len(bins[:-1])), [0.5]*len(bins[:-1]), height=1, color=cmap(norm(bins[:-1])), ec='None') ax2.set_xlim(0, 1) return plt.gcf(), df
def create_plot(self): fig = plt.figure(figsize=self.figsize) #data_projection=ccrs.LambertConformal(central_longitude=self.central_longitude) #data_projection=ccrs.Alb(central_longitude=self.central_longitude) data_projection = ccrs.Robinson( central_longitude=self.central_longitude) ax = plt.axes(projection=data_projection) if self.extent is not None: ax.set_extent(self.extent) else: left, right, top, bottom = np.min(self.x), np.max(self.x), np.max( self.y), np.min(self.y) ax.set_extent([left, right, bottom, top]) if self.display_counties: counties = create_feature(COUNTY_SHAPEFILE) ax.add_feature( counties, facecolor='none', edgecolor='gray', ) border_scale = '50m' states = NaturalEarthFeature(category="cultural", scale=border_scale, facecolor="none", edgecolor='black', name="admin_1_states_provinces_shp") lakes = NaturalEarthFeature('physical', 'lakes', border_scale, edgecolor='blue', facecolor='none') ax.add_feature(lakes, facecolor='none', edgecolor='blue', linewidth=0.5) ax.add_feature(states, facecolor='none', edgecolor='black') ax.coastlines(border_scale, linewidth=0.8) if (self.colormap is not None) and (self.color_levels is not None): cmap = mcolors.ListedColormap(self.colormap) norm = mcolors.BoundaryNorm(self.color_levels, cmap.N) cs = ax.contourf(self.x, self.y, self.z, self.color_levels, cmap=cmap, norm=norm, transform=ccrs.PlateCarree()) #cs = ax.pcolormesh(self.x, self.y, self.z, cmap=cmap, norm=norm, transform=ccrs.PlateCarree()) else: cs = ax.contourf(self.x, self.y, self.z, self.num_colors, transform=ccrs.PlateCarree()) #cs = ax.pcolormesh(self.x, self.y, self.z, transform=ccrs.PlateCarree()) for label in self.labels: text, coords = label lon, lat = coords transform = ccrs.PlateCarree()._as_mpl_transform(ax) #ax.annotate(text, (lon,lat), xycoords=transform) ax.text(lon, lat, text, horizontalalignment='left', transform=transform) ax.plot(lon, lat, markersize=2, marker='o', color='k', transform=ccrs.PlateCarree()) if isinstance(self.color_levels, list): cbar = plt.colorbar(cs, orientation='vertical', ticks=self.color_levels) else: cbar = plt.colorbar(cs, orientation='vertical') if self.units: cbar.set_label(self.units) if self.title is not None: plt.title(self.title) self.plot = fig self.ax = ax
def predict_draw(data, label, m1, m2, threshold1, threshold2, name): final_result = [] a = max(0, (101 - len(data)) // 2) b = max(0, 101 - len(data) - a) c = max(0, (101 - len(data[0])) // 2) d = max(0, 101 - len(data[0]) - c) data = extend_function(data) label = extend_label(label, a, b, c, d) cmap = colors.ListedColormap(['white', 'white']) bounds = [0, 0.5, 1] norm = colors.BoundaryNorm(bounds, cmap.N) fig, ax = plt.subplots() ax.imshow(label, cmap=cmap, norm=norm) for xo in range(len(label)): for yo in range(len(label[0])): if label[xo][yo] == 1: plt.plot(yo, xo, 'k.', markersize=1) ind_1 = 0 flag1 = 0 while ind_1 <= len(data): if ind_1 == len(data) or flag1 == 1: break if ind_1 + 101 > len(data): ind_1 = len(data) - 101 flag1 = 1 ind_2 = 0 flag2 = 0 while ind_2 <= len(data[0]): if ind_2 == len(data[0]) or flag2 == 1: break if ind_2 + 101 > len(data[0]): ind_2 = len(data[0]) - 101 flag2 = 1 ################################################################### test_data = normalize(data[ind_1:ind_1 + 101, ind_2:ind_2 + 101]) test_data = test_data.reshape(1, 101, 101, 1) re = model.predict(test_data)[0] loc = np.where(re > threshold1)[0] for k in loc: y0 = -0.5 + ind_1 + k // 10 * 10 x0 = -0.5 + ind_2 + k % 10 * 10 rect = patches.Rectangle((x0, y0), 10, 10, linewidth=0.5, edgecolor='b', facecolor='none') ax.add_patch(rect) image = test_data[0, k // 10 * 10:k // 10 * 10 + 11, k % 10 * 10:k % 10 * 10 + 11, :] max_m = 3 temp = 0 reconstruct_MML_matrix = [] for i in range(nx - 1): row = [] for j in range(ny - 1): center_right = center_points_right[temp] center_left = center_points_left[temp] temp += 1 mml_center = MMLmf(max_m, x, y, image, center_right, center_left) row.append(mml_center) reconstruct_MML_matrix.append(row) reconstruct_MML_matrix = abs( np.array(reconstruct_MML_matrix)) > threshold2 B = (reconstruct_MML_matrix != 0) loc_0 = np.where(B > 0) loc_t = list(zip(loc_0[0], loc_0[1])) for q in loc_t: y1 = q[0] x1 = q[1] rect = patches.Rectangle((x0 + x1, y0 + y1), 1, 1, linewidth=1, edgecolor='r', facecolor='none') final_result.append((y0 + y1 + 0.5, x0 + x1 + 0.5)) ax.add_patch(rect) ############################################################### ind_2 += m2 ind_1 += m1 plt.show() plt.title("length of each side=" + str(name)) plt.savefig('/2d/2d_general_prediction/plot/' + str(name) + '.pdf') plt.close() print(final_result)
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) station = ctx['station'] gddbase = ctx['gddbase'] base = ctx['base'] ceil = ctx['ceil'] nt = NetworkTable(ctx['network']) today = ctx['date'] byear = nt.sts[station]['archive_begin'].year eyear = today.year + 1 pgconn = get_dbconn('coop') cursor = pgconn.cursor() table = "alldata_%s" % (station[:2], ) cursor.execute( """SELECT year, extract(doy from day), gddxx(%s, %s, high,low), low from """ + table + """ where station = %s and year > %s and day < %s """, (base, ceil, station, byear, today)) gdd = np.zeros((eyear - byear, 366), 'f') freezes = np.zeros((eyear - byear), 'f') freezes[:] = 400.0 for row in cursor: gdd[int(row[0]) - byear, int(row[1]) - 1] = row[2] if row[1] > 180 and row[3] < 32 and row[1] < freezes[row[0] - byear]: freezes[int(row[0]) - byear] = row[1] for i, freeze in enumerate(freezes): gdd[i, int(freeze):] = 0.0 idx = int(today.strftime("%j")) - 1 apr1 = int(datetime.datetime(2000, 4, 1).strftime("%j")) - 1 jun30 = int(datetime.datetime(2000, 6, 30).strftime("%j")) - 1 sep1 = int(datetime.datetime(2000, 9, 1).strftime("%j")) - 1 oct31 = int(datetime.datetime(2000, 10, 31).strftime("%j")) - 1 # Replace all years with the last year's data scenario_gdd = gdd * 1 scenario_gdd[:-1, :idx] = gdd[-1, :idx] # store our probs probs = np.zeros((oct31 - sep1, jun30 - apr1), 'f') scenario_probs = np.zeros((oct31 - sep1, jun30 - apr1), 'f') rows = [] for x in range(apr1, jun30): for y in range(sep1, oct31): sums = np.where(np.sum(gdd[:-1, x:y], 1) >= gddbase, 1, 0) probs[y - sep1, x - apr1] = sum(sums) / float(len(sums)) * 100.0 sums = np.where(np.sum(scenario_gdd[:-1, x:y], 1) >= gddbase, 1, 0) scenario_probs[y - sep1, x - apr1] = (sum(sums) / float(len(sums)) * 100.0) rows.append( dict(x=x, y=y, prob=probs[y - sep1, x - apr1], scenario_probs=scenario_probs[y - sep1, x - apr1])) df = pd.DataFrame(rows) probs = np.where(probs < 0.1, -1, probs) scenario_probs = np.where(scenario_probs < 0.1, -1, scenario_probs) (fig, ax) = plt.subplots(1, 2, sharey=True, figsize=(8, 6)) cmap = plt.get_cmap(ctx['cmap']) cmap.set_under('white') norm = mpcolors.BoundaryNorm(np.arange(0, 101, 5), cmap.N) ax[0].imshow(np.flipud(probs), aspect='auto', extent=[apr1, jun30, sep1, oct31], interpolation='nearest', vmin=0, vmax=100, cmap=cmap, norm=norm) ax[0].grid(True) ax[0].set_title("Overall Frequencies") ax[0].set_xticks((91, 106, 121, 136, 152, 167)) ax[0].set_ylabel("Growing Season End Date") ax[0].set_xlabel("Growing Season Begin Date") ax[0].set_xticklabels(('Apr 1', '15', 'May 1', '15', 'Jun 1', '15')) ax[0].set_yticks((244, 251, 258, 265, 274, 281, 288, 295, 305)) ax[0].set_yticklabels(('Sep 1', 'Sep 8', 'Sep 15', 'Sep 22', 'Oct 1', 'Oct 8', 'Oct 15', 'Oct 22', 'Nov')) res = ax[1].imshow(np.flipud(scenario_probs), aspect='auto', extent=[apr1, jun30, sep1, oct31], interpolation='nearest', vmin=0, vmax=100, cmap=cmap, norm=norm) ax[1].grid(True) ax[1].set_title("Scenario after %s" % (today.strftime("%-d %B %Y"), )) ax[1].set_xticks((91, 106, 121, 136, 152, 167)) ax[1].set_xticklabels(('Apr 1', '15', 'May 1', '15', 'Jun 1', '15')) ax[1].set_xlabel("Growing Season Begin Date") fig.subplots_adjust(bottom=0.20, top=0.85) cbar_ax = fig.add_axes([0.05, 0.06, 0.85, 0.05]) fig.colorbar(res, cax=cbar_ax, orientation='horizontal') fig.text(0.5, 0.90, ("%s-%s %s GDDs\n" "Frequency [%%] of reaching %.0f GDDs (%.0f/%.0f) " "prior to first freeze") % (byear, eyear - 1, nt.sts[station]['name'], gddbase, base, ceil), fontsize=14, ha='center') return fig, df