def get_colormap(request, parameter=None, default=None): parameter = parameter or 'styles' default = default or 'cubehelix' try: from matplotlib.pyplot import colormaps requested_cm = split(request.GET.get(parameter).split(',')[0], '_', maxsplit=1)[1] assert requested_cm return next(x for x in colormaps() if x.lower() == requested_cm.lower()) except (AssertionError, IndexError, AttributeError, TypeError, StopIteration): return default
def random_forest_model(features, solutions, verbose=0): columns = solutions.columns clf = RandomForestRegressor(100, max_features='log2', n_jobs=-1, verbose=verbose) print('Training Model... ') clf.fit(features, solutions) print('Done Training') features_importance = clf.feature_importances_ features_importance = np.reshape(features_importance, (169, 8)) features_importance = np.max(features_importance, axis=1) features_importance = np.reshape(features_importance, (13, 13)) fig, ax = plt.subplots() ax.pcolor(features_importance) plt.colormaps() plt.show() return (clf, columns)
def decision_tree_regressor(features, solutions, verbose=0): columns = solutions.columns clf = DecisionTreeRegressor(max_depth=8) print('Training Model... ') clf.fit(features, solutions) print('Done Training') features_importance = clf.feature_importances_ features_importance = np.reshape(features_importance, (169, 8)) features_importance = np.sum(features_importance, axis=1) features_importance = np.reshape(features_importance, (13, 13)) fig, ax = plt.subplots() ax.pcolor(features_importance) plt.colormaps() plt.show() return (clf, columns)
def show_image(showme, axis_y=None, axis_x=None, colormap=cmap): """ Shows an image with the given X and Y axes. :param showme: the 2D array to be shown :param axis_y: a 1D array containing the y axis to be used in the plot. :param axis_x: a 1D array containing the x axis to be used in the plot. :param colormap: matplotlib colormap :return: """ if colormap not in plt.colormaps(): # use default cmap if default colormap is not found # (None falls back to matplotlib's default) colormap = None if axis_x is None: axis_x = [i for i in range(len(showme[0]))] if axis_y is None: axis_y = [i for i in range(len(showme))] (x_min, x_max) = (min(axis_x), max(axis_x)) (y_min, y_max) = (min(axis_y), max(axis_y)) fig = plt.figure() plt.imshow(showme, aspect='auto', extent=[x_min, x_max, y_min, y_max], cmap=colormap) plt.colorbar() return fig
def getColorMapChoices(): return plt.colormaps()
def movie_plot_2D(xy, BL, bs=None, fname='none', title='', NL=[], KL=[], BLNNN=[], NLNNN=[], KLNNN=[], PVx=[], PVy=[], PVxydict={}, nljnnn=None, kljnnn=None, klknnn=None, ax=None, fig=None, axcb='auto', cbar_ax=None, cbar_orientation='vertical', xlimv='auto', ylimv='auto', climv=0.1, colorz=True, ptcolor=None, figsize='auto', colorpoly=False, bondcolor=None, colormap='seismic', bgcolor=None, axis_off=False, axis_equal=True, text_topleft=None, lw=-1., ptsize=10, negative_NNN_arrows=False, show=False, arrow_alpha=1.0, fontsize=8, cax_label='Strain', zorder=0, rasterized=False): """Plots (and saves if fname is not 'none') a 2D image of the lattice with colored bonds and particles colored by coordination (both optional). Parameters ---------- xy : array of dimension nx2 2D lattice of points (positions x,y) BL : array of dimension #bonds x 2 Each row is a bond and contains indices of connected points bs : array of dimension #bonds x 1 or None Strain in each bond fname : string Full path including name of the file (.png, etc), if None, will not save figure title : string The title of the frame NL : NP x NN int array (optional, for speed) Specify to speed up computation, if colorz or colorpoly or if periodic boundary conditions KL : NP x NN int array (optional, for speed) Specify to speed up computation, if colorz or colorpoly or if periodic boundary conditions BLNNN : NLNNN : KLNNN : PVx : NP x NN float array (optional, for periodic lattices and speed) ijth element of PVx is the x-component of the vector taking NL[i,j] to its image as seen by particle i If PVx and PVy are specified, PVxydict need not be specified. PVy : NP x NN float array (optional, for periodic lattices and speed) ijth element of PVy is the y-component of the vector taking NL[i,j] to its image as seen by particle i If PVx and PVy are specified, PVxydict need not be specified. PVxydict : dict (optional, for periodic lattices) dictionary of periodic bonds (keys) to periodic vectors (values) nljnnn : #pts x max(#NNN) int array or None nearest neighbor array matching NLNNN and KLNNN. nljnnn[i, j] gives the neighbor of i such that NLNNN[i, j] is the next nearest neighbor of i through the particle nljnnn[i, j] kljnnn : #pts x max(#NNN) int array or None bond array describing periodicity of bonds matching NLNNN and KLNNN. kljnnn[i, j] describes the bond type (bulk -> +1, periodic --> -1) of bond connecting i to nljnnn[i, j] klknnn : #pts x max(#NNN) int array or None bond array describing periodicity of bonds matching NLNNN and KLNNN. klknnn[i, j] describes the bond type (bulk -> +1, periodic --> -1) of bond connecting nljnnn[i, j] to NLNNN[i, j] ax: matplotlib figure axis instance Axis on which to draw the network fig: matplotlib figure instance Figure on which to draw the network axcb: matplotlib colorbar instance Colorbar to use for strains in bonds cbar_ax : axis instance Axis to use for colorbar. If colorbar instance is not already defined, use axcb instead. cbar_orientation : str ('horizontal' or 'vertical') Orientation of the colorbar xlimv: float or tuple of floats ylimv: float or tuple of floats climv : float or tuple Color limit for coloring bonds by bs colorz: bool whether to color the particles by their coordination number ptcolor: string color spec or tuple color spec or None color specification for coloring the points, if colorz is False. Default is None (no coloring of points) figsize : tuple w,h tuple in inches colorpoly : bool Whether to color in polygons formed by bonds according to the number of sides bondcolor : color specification (hexadecimal or RGB) colormap : if bondcolor is None, uses bs array to color bonds bgcolor : hex format string, rgb color spec, or None If not None, sets the bgcolor. Often used is '#d9d9d9' axis_off : bool Turn off the axis border and canvas axis_equal : bool text_topleft : str or None lw: float line width for plotting bonds. If lw == -1, then uses automatic line width to adjust for bond density.. ptsize: float size of points passed to absolute_sizer negative_NNN_arrows : bool make positive and negative NNN hoppings different color show : bool whether to show the plot after creating it arrow_alpha : float opacity of the arrow fontsize : int (default=8) fontsize for all labels cax_label : int (default='Strain') Label for the colorbar zorder : int z placement on axis (higher means bringing network to the front, lower is to the back Returns ---------- [ax,axcb] : stuff to clear after plotting """ if fig is None or fig == 'none': fig = plt.gcf() if ax is None or ax == 'none': if figsize == 'auto': plt.clf() else: fig = plt.figure(figsize=figsize) ax = plt.axes() if bs is None: bs = np.zeros_like(BL[:, 0], dtype=float) if colormap not in plt.colormaps(): lecmaps.register_colormaps() NP = len(xy) if lw == -1: if NP < 10000: lw = 0.5 s = leplt.absolute_sizer() else: lw = (10 / np.sqrt(len(xy))) if NL == [] and KL == []: if colorz or colorpoly: NL, KL = le.BL2NLandKL(BL, NP=NP, NN='min') if (BL < 0).any(): if len(PVxydict) == 0: raise RuntimeError( 'PVxydict must be supplied to display_lattice_2D() when periodic BCs exist, ' + 'if NL and KL not supplied!') else: PVx, PVy = le.PVxydict2PVxPVy(PVxydict, NL, KL) if colorz: zvals = (KL != 0).sum(1) zmed = np.median(zvals) # print 'zmed = ', zmed under1 = np.logical_and(zvals < zmed - 0.5, zvals > zmed - 1.5) over1 = np.logical_and(zvals > zmed + 0.5, zvals < zmed + 1.5) Cz = np.zeros((len(xy), 3), dtype=int) # far under black // under blue // equal white // over red // far over green Cz[under1] = [0. / 255, 50. / 255, 255. / 255] Cz[zvals == zmed] = [100. / 255, 100. / 255, 100. / 255] Cz[over1] = [255. / 255, 0. / 255, 0. / 255] Cz[zvals > zmed + 1.5] = [0. / 255, 255. / 255, 50. / 255] # Cz[zvals<zmed-1.5] = [0./255,255./255,150./255] #leave these black s = leplt.absolute_sizer() sval = min([.005, .12 / np.sqrt(len(xy))]) sizes = np.zeros(NP, dtype=float) sizes[zvals > zmed + 0.5] = sval sizes[zvals == zmed] = sval * 0.5 sizes[zvals < zmed - 0.5] = sval # topinds = zvals!=zmed ax.scatter(xy[:, 0], xy[:, 1], s=s(sizes), c=Cz, edgecolor='none', zorder=10, rasterized=rasterized) ax.axis('equal') elif ptcolor is not None and ptcolor != 'none' and ptcolor != '': if NP < 10000: # if smallish #pts, plot them # print 'xy = ', xy # plt.plot(xy[:,0],xy[:,1],'k.') s = leplt.absolute_sizer() ax.scatter(xy[:, 0], xy[:, 1], s=ptsize, alpha=0.5, facecolor=ptcolor, edgecolor='none', rasterized=rasterized) if colorpoly: # Color the polygons based on # sides # First extract polygons. To do that, if there are periodic boundaries, we need to supply as dict if PVxydict == {} and len(PVx) > 0: PVxydict = le.PVxy2PVxydict(PVx, PVy, NL, KL=KL) polygons = le.extract_polygons_lattice(xy, BL, NL=NL, KL=KL, viewmethod=True, PVxydict=PVxydict) PolyPC = le.polygons2PPC(polygons) # number of polygon sides Pno = np.array([len(polyg) for polyg in polygons], dtype=int) print 'nvis: Pno = ', Pno print 'nvis: medPno = ', np.floor(np.median(Pno)) medPno = np.floor(np.median(Pno)) uIND = np.where(Pno == medPno - 1)[0] mIND = np.where(Pno == medPno)[0] oIND = np.where(Pno == medPno + 1)[0] loIND = np.where(Pno < medPno - 1.5)[0] hiIND = np.where(Pno > medPno + 1.5)[0] print ' uIND = ', uIND print ' oIND = ', oIND print ' loIND = ', loIND print ' hiIND = ', hiIND if len(uIND) > 0: PPCu = [PolyPC[i] for i in uIND] pu = PatchCollection(PPCu, color='b', alpha=0.5) ax.add_collection(pu) if len(mIND) > 0: PPCm = [PolyPC[i] for i in mIND] pm = PatchCollection(PPCm, color=[0.5, 0.5, 0.5], alpha=0.5) ax.add_collection(pm) if len(oIND) > 0: PPCo = [PolyPC[i] for i in oIND] po = PatchCollection(PPCo, color='r', alpha=0.5) ax.add_collection(po) if len(loIND) > 0: PPClo = [PolyPC[i] for i in loIND] plo = PatchCollection(PPClo, color='k', alpha=0.5) ax.add_collection(plo) if len(hiIND) > 0: PPChi = [PolyPC[i] for i in hiIND] phi = PatchCollection(PPChi, color='g', alpha=0.5) ax.add_collection(phi) # Efficiently plot many lines in a single set of axes using LineCollection # First check if there are periodic bonds if BL.size > 0: if (BL < 0).any(): if PVx == [] or PVy == [] or PVx is None or PVy is None: raise RuntimeError( 'PVx and PVy must be supplied to display_lattice_2D when periodic BCs exist!' ) else: # get indices of periodic bonds perINDS = np.unique(np.where(BL < 0)[0]) perBL = np.abs(BL[perINDS]) # # Check # print 'perBL = ', perBL # plt.plot(xy[:,0], xy[:,1],'b.') # for i in range(len(xy)): # plt.text(xy[i,0]+0.05, xy[i,1],str(i)) # plt.show() # define the normal bonds which are not periodic normINDS = np.setdiff1d(np.arange(len(BL)), perINDS) BLtmp = BL[normINDS] bstmp = bs[normINDS] lines = [ zip(xy[BLtmp[i, :], 0], xy[BLtmp[i, :], 1]) for i in range(len(BLtmp)) ] xy_add = np.zeros((4, 2)) # Build new strain list bs_out by storing bulk lines first, then recording the strain twice # for each periodic bond since the periodic bond is at least two lines in the plot, get bs_out # ready for appending # bs_out = np.zeros(len(normINDS) + 5 * len(perINDS), dtype=float) # bs_out[0:len(normINDS)] = bstmp bs_out = bstmp.tolist() # Add periodic bond lines to image # Note that we have to be careful that a single particle can be connected to another both in the bulk # and through a periodic boundary, and perhaps through more than one periodic boundary # kk indexes perINDS to determine what the strain of each periodic bond should be # draw_perbond_count counts the number of drawn linesegments that are periodic bonds kk, draw_perbond_count = 0, 0 for row in perBL: colA = np.argwhere(NL[row[0]] == row[1]).ravel() colB = np.argwhere(NL[row[1]] == row[0]).ravel() if len(colA) > 1 or len(colB) > 1: # Look for where KL < 0 to pick out just the periodic bond(s) -- ie there # were both bulk and periodic bonds connecting row[0] to row[1] a_klneg = np.argwhere(KL[row[0]] < 0) colA = np.intersect1d(colA, a_klneg) b_klneg = np.argwhere(KL[row[1]] < 0) colB = np.intersect1d(colB, b_klneg) # print 'colA = ', colA # print 'netvis here' # sys.exit() if len(colA) > 1 or len(colB) > 1: # there are multiple periodic bonds connecting one particle to another (in different # directions). Plot each of them. for ii in range(len(colA)): print 'netvis: colA = ', colA print 'netvis: colB = ', colB # columns a and b for this ii index caii, cbii = colA[ii], colB[ii] # add xy points to the network to plot to simulate image particles xy_add[0] = xy[row[0]] xy_add[1] = xy[row[1]] + np.array( [PVx[row[0], caii], PVy[row[0], caii]]) xy_add[2] = xy[row[1]] xy_add[3] = xy[row[0]] + np.array( [PVx[row[1], cbii], PVy[row[1], cbii]]) # Make the lines to draw (dashed lines for this periodic case) lines += zip(xy_add[0:2, 0], xy_add[0:2, 1]), zip( xy_add[2:4, 0], xy_add[2:4, 1]) bs_out.append(bs[perINDS[kk]]) bs_out.append(bs[perINDS[kk]]) draw_perbond_count += 1 kk += 1 else: # print 'row = ', row # print 'NL = ', NL # print 'KL = ', KL # print 'colA, colB = ', colA, colB colA, colB = colA[0], colB[0] xy_add[0] = xy[row[0]] xy_add[1] = xy[row[1]] + np.array( [PVx[row[0], colA], PVy[row[0], colA]]) xy_add[2] = xy[row[1]] xy_add[3] = xy[row[0]] + np.array( [PVx[row[1], colB], PVy[row[1], colB]]) lines += zip(xy_add[0:2, 0], xy_add[0:2, 1]), zip( xy_add[2:4, 0], xy_add[2:4, 1]) # bs_out[2 * kk + len(normINDS)] = bs[perINDS[kk]] # bs_out[2 * kk + 1 + len(normINDS)] = bs[perINDS[kk]] bs_out.append(bs[perINDS[kk]]) bs_out.append(bs[perINDS[kk]]) draw_perbond_count += 1 kk += 1 else: colA, colB = colA[0], colB[0] xy_add[0] = xy[row[0]] xy_add[1] = xy[row[1]] + np.array( [PVx[row[0], colA], PVy[row[0], colA]]) xy_add[2] = xy[row[1]] xy_add[3] = xy[row[0]] + np.array( [PVx[row[1], colB], PVy[row[1], colB]]) lines += zip(xy_add[0:2, 0], xy_add[0:2, 1]), zip(xy_add[2:4, 0], xy_add[2:4, 1]) # bs_out[2 * kk + len(normINDS)] = bs[perINDS[kk]] # bs_out[2 * kk + 1 + len(normINDS)] = bs[perINDS[kk]] bs_out.append(bs[perINDS[kk]]) bs_out.append(bs[perINDS[kk]]) draw_perbond_count += 1 kk += 1 # replace bs by the new bs (bs_out) bs = np.array(bs_out) # store number of bulk bonds nbulk_bonds = len(normINDS) else: if len(np.shape(BL)) > 1: lines = [ zip(xy[BL[i, :], 0], xy[BL[i, :], 1]) for i in range(np.shape(BL)[0]) ] # store number of bulk bonds nbulk_bonds = len(lines) else: lines = [ zip(xy[BL[i][0]], xy[BL[i][1]]) for i in range(np.shape(BL)[0]) ] # store number of bulk bonds nbulk_bonds = 1 if isinstance(climv, tuple): cmin = climv[0] cmax = climv[1] elif isinstance(climv, float): cmin = -climv cmax = climv elif climv is None: cmin = None cmax = None if bondcolor is None: # draw the periodic bonds as dashed, regular bulk bonds as solid line_segments = LineCollection( lines[0:nbulk_bonds], # Make a sequence of x,y pairs linewidths=lw, # could iterate over list linestyles='solid', cmap=colormap, norm=plt.Normalize(vmin=cmin, vmax=cmax), zorder=zorder, rasterized=rasterized) line_segments.set_array(bs[0:nbulk_bonds]) # draw the periodic bonds as dashed, if there are any periodic_lsegs = LineCollection( lines[nbulk_bonds:], # Make a sequence of x,y pairs linewidths=lw, # could iterate over list linestyles='dashed', cmap=colormap, norm=plt.Normalize(vmin=cmin, vmax=cmax), zorder=zorder, rasterized=rasterized) periodic_lsegs.set_array(bs[nbulk_bonds:]) else: line_segments = LineCollection(lines[0:nbulk_bonds], linewidths=lw, linestyles='solid', colors=bondcolor, zorder=zorder, rasterized=rasterized) # draw the periodic bonds as dashed, if there are any periodic_lsegs = LineCollection(lines[nbulk_bonds:], linewidths=lw, linestyles='dashed', colors=bondcolor, zorder=zorder, rasterized=rasterized) ax.add_collection(line_segments) if periodic_lsegs: ax.add_collection(periodic_lsegs) # If there is only a single bond color, ignore the colorbar specification if bondcolor is None or isinstance(bondcolor, np.ndarray): if axcb == 'auto': if cbar_ax is None: print 'nvis: Instantiating colorbar...' axcb = fig.colorbar(line_segments) else: print 'nvis: Using cbar_ax to instantiate colorbar' axcb = fig.colorbar(line_segments, cax=cbar_ax, orientation=cbar_orientation) if axcb != 'none' and axcb is not None: print 'nvis: Creating colorbar...' axcb.set_label(cax_label, fontsize=fontsize) axcb.set_clim(vmin=cmin, vmax=cmax) else: # Ignore colorbar axis specification axcb = 'none' else: axcb = 'none' if len(BLNNN) > 0: # todo: add functionality for periodic NNN connections # Efficiently plot many lines in a single set of axes using LineCollection lines = [ zip(xy[BLNNN[i, :], 0], xy[BLNNN[i, :], 1]) for i in range(len(BLNNN)) ] linesNNN = LineCollection( lines, # Make a sequence of x,y pairs linewidths=lw, # could iterate over list linestyles='dashed', color='blue', zorder=100) ax.add_collection(linesNNN, rasterized=rasterized) elif len(NLNNN) > 0 and len(KLNNN) > 0: factor = 0.8 if (BL < 0).any(): print 'nvis: plotting periodic NNN...' if nljnnn is None: raise RuntimeError( 'Must supply nljnnn to plot NNN hoppings/connections') for i in range(NP): todo = np.where(KLNNN[i, :] > 1e-12)[0] for index in todo: kk = NLNNN[i, index] # Ascribe the correct periodic vector based on both PVx[i, NNind] and PVx[NNind, ind] # Note : nljnnn is # nearest neighbor array matching NLNNN and KLNNN. nljnnn[i, j] gives the neighbor of i such that # NLNNN[i, j] is the next nearest neighbor of i through the particle nljnnn[i, j] jj = nljnnn[i, index] if kljnnn[i, index] < 0 or klknnn[i, index] < 0: jind = np.where(NL[i, :] == jj)[0][0] kind = np.where(NL[jj, :] == kk)[0][0] # print 'jj = ', jj # print 'kk = ', kk # print 'jind = ', jind # print 'kind = ', kind # print 'NL[i, :] =', NL[i, :] # print 'NL[jj, :] =', NL[jj, :] # print 'PVx[i, jind] = ', PVx[i, jind] # print 'PVy[i, jind] = ', PVy[i, jind] # print 'PVx[jj, kind] = ', PVx[jj, kind] # print 'PVy[jj, kind] = ', PVy[jj, kind] dx = (xy[kk, 0] + PVx[i, jind] + PVx[jj, kind] - xy[i, 0]) * factor dy = (xy[kk, 1] + PVy[i, jind] + PVy[jj, kind] - xy[i, 1]) * factor else: dx = (xy[kk, 0] - xy[i, 0]) * factor dy = (xy[kk, 1] - xy[i, 1]) * factor ax.arrow(xy[i, 0], xy[i, 1], dx, dy, head_width=0.1, head_length=0.2, fc='b', ec='b', linestyle='dashed') # Check # print 'dx = ', dx # print 'dy = ', dy # for ind in range(NP): # plt.text(xy[ind, 0]-0.2, xy[ind, 1]-0.2, str(ind)) # plt.show() # sys.exit() else: # amount to offset clockwise nnn arrows for i in range(NP): todo = np.where(KLNNN[i, :] > 1e-12)[0] # Allow for both blue and red arrows (forward/backward), or just blue. If just blue, use no offset and # full scale factor if negative_NNN_arrows: scalef = 0.3 else: scalef = 0.8 offset = np.array([0.0, 0.0]) for ind in NLNNN[i, todo]: if negative_NNN_arrows: offset = (xy[ind, :] - xy[i, :]) * 0.5 ax.arrow(xy[i, 0] + offset[0], xy[i, 1] + offset[1], (xy[ind, 0] - xy[i, 0]) * scalef, (xy[ind, 1] - xy[i, 1]) * scalef, head_width=0.1, head_length=0.2, fc='b', ec='b', alpha=arrow_alpha) if negative_NNN_arrows: todo = np.where(KLNNN[i, :] < -1e-12)[0] for ind in NLNNN[i, todo]: offset = (xy[ind, :] - xy[i, :]) * 0.5 ax.arrow(xy[i, 0] + offset[0], xy[i, 1] + offset[1], (xy[ind, 0] - xy[i, 0]) * 0.3, (xy[ind, 1] - xy[i, 1]) * 0.3, head_width=0.1, head_length=0.2, fc='r', ec='r', alpha=arrow_alpha) if bgcolor is not None: ax.set_axis_bgcolor(bgcolor) # set limits ax.axis('scaled') if xlimv != 'auto' and xlimv is not None: if isinstance(xlimv, tuple): ax.set_xlim(xlimv[0], xlimv[1]) else: print 'nvis: setting xlimv' ax.set_xlim(-xlimv, xlimv) else: ax.set_xlim(np.min(xy[:, 0]) - 2.5, np.max(xy[:, 0]) + 2.5) if ylimv != 'auto' and ylimv is not None: if isinstance(ylimv, tuple): print 'nvis: setting ylimv to tuple' ax.set_ylim(ylimv[0], ylimv[1]) else: ax.set_ylim(-ylimv, ylimv) else: print 'nvis: setting', min(xy[:, 1]), max(xy[:, 1]) ax.set_ylim(np.min(xy[:, 1]) - 2, np.max(xy[:, 1]) + 2) if title is not None: ax.set_title(title, fontsize=fontsize) if text_topleft is not None: ax.text(0.05, .98, text_topleft, horizontalalignment='right', verticalalignment='top', transform=ax.transAxes) if axis_off: ax.axis('off') if fname != 'none' and fname != '' and fname is not None: print 'nvis: saving figure: ', fname plt.savefig(fname) if show: plt.show() return [ax, axcb]
import numpy as np from matplotlib import pyplot, cm min_samples = 256 with open('matplotlib_color_maps.py', 'w') as file: file.write('import numpy as np\n\n') for name in sorted(pyplot.colormaps()): cmap = cm.get_cmap(name) name_c = name.capitalize() if name_c == name: file.write('{} = np.array([ \n'.format(name)) else: # compability with older matplotlib_color_maps.py where all names were capitalized file.write('{} = {} = np.array([ \n'.format(name, name_c)) # cmap.N is the actual number of datapoints the map is constructed with for x in np.linspace(0, 1, max(cmap.N, min_samples)): r, g, b = cmap(x)[:3] file.write(' {x}, {r}, {g}, {b},\n'.format(**locals())) file.write('], dtype=np.float32)\n\n')
import argparse def get_cmap(name, nVal): cmp = plt.get_cmap(name, nVal) return cmp(range(nVal)) def write_cmap(file, cmap): np.savetxt(file, cmap) if __name__ == '__main__': ap = argparse.ArgumentParser() ap.add_argument('cmName', type=str, choices=['listCMapNames'] + plt.colormaps()) ap.add_argument('-o', type=str, dest='outfile') ap.add_argument('-n', type=int, default=256, dest='nVal') args = ap.parse_args() if args.outfile is None: args.outfile = args.cmName + '.txt' if args.cmName in 'listCMapNames': with open(args.outfile, 'w') as f: f.writelines(c + '\n' for c in plt.colormaps()) else: cmp = get_cmap(args.cmName, args.nVal) write_cmap(args.outfile, cmp)
phi = np.radians(90 - elevation) return theta, phi def test_pandas_iterable(pd): # Using a list or series yields equivalent # color maps, i.e the series isn't seen as # a single color lst = ['red', 'blue', 'green'] s = pd.Series(lst) cm1 = mcolors.ListedColormap(lst, N=5) cm2 = mcolors.ListedColormap(s, N=5) assert_array_equal(cm1.colors, cm2.colors) @pytest.mark.parametrize('name', sorted(plt.colormaps())) def test_colormap_reversing(name): """ Check the generated _lut data of a colormap and corresponding reversed colormap if they are almost the same. """ cmap = plt.get_cmap(name) cmap_r = cmap.reversed() if not cmap_r._isinit: cmap._init() cmap_r._init() assert_array_almost_equal(cmap._lut[:-3], cmap_r._lut[-4::-1]) # Test the bad, over, under values too assert_array_almost_equal(cmap(-np.inf), cmap_r(np.inf)) assert_array_almost_equal(cmap(np.inf), cmap_r(-np.inf)) assert_array_almost_equal(cmap(np.nan), cmap_r(np.nan))
def iplot_dos_lines(path_evr = None, elements = [[0,],], orbs = [[0],], labels = ['s',], elim = [], colormap = 'gist_rainbow', tdos_color = (0.5,0.95,0), linewidth = 2, fill_area = True, vertical = False, E_Fermi = None, figsize = None, spin = 'both', interp_nk = {}, title = None, query_data = {} ): """ - Returns plotly's figure. If given,elements,orbs colors, and labels must have same length. If not given, zeroth ions is plotted with s-orbital. - **Parameters**) - path_evr : Path/to/vasprun.xml or output of `export_vasprun`. Auto picks in CWD. - elements : List [[0,],] of ions indices, by defualt plot first ion's projections. - orbs : List [[0,],] lists of indices of orbitals, could be empty. - labels : List [str,] of orbitals labels. len(labels) == len(orbs) must hold. - elim : [min,max] of energy range. - E_Fermi : If not given, automatically picked from `export_vasprun`. - colormap : Matplotlib's standard color maps. Default is 'gist_ranibow'. Use 'RGB' if want to compare with `iplot_rgb_lines` with 3 projection inputs (len(orbs)==3). - fill_area : Default is True and plots filled area for dos. If False, plots lines only. - vertical : False, If True, plots along y-axis. - interp_nk : Dictionary with keys 'n' and 'k' for interpolation. - figsize : Tuple(width,height) in pixels, e.g. (700,400). - query_data : Dictionary with keys as label and values as list of length 2. If given, used in place of elements, orbs and labels arguments. Example: {'s':([0,1],[0]),'p':([0,1],[1,2,3]),'d':([0,1],[4,5,6,7,8])} will pick up s,p,d orbitals of first two ions of system. - **Returns** - fig : Plotly's figure object. """ if query_data: elements,orbs,labels = sp._format_input(query_data,rgb=False) # prefer query_data over elements,orbs,labels en,tdos,pdos,vr=None,None,None,None # Place holders for defining cl_dos = sp._collect_dos(path_evr=path_evr,elim=elim, elements=elements, orbs=orbs,labels=labels, E_Fermi=E_Fermi, spin='both',interp_nk=interp_nk) try: en,tdos,pdos,labels,vr = cl_dos except TypeError: return print(gu.color.g("Try with large energy range.")) labels=[label.replace('$','').replace('^↑','<sup>↑</sup>').replace('^↓','<sup>↓</sup>') for label in labels] # Make additional colors for spin down. Inverted colors are better. if(elim): ylim=[min(elim),max(elim)] else: ylim=[-10,10] # Fix elements and colors length if colormap in plt.colormaps(): from matplotlib.pyplot import cm if len(tdos) == 2: c_map = cm.get_cmap(colormap) c_vals = np.linspace(0,1,2*len(orbs)) colors = c_map(c_vals) else: c_map = cm.get_cmap(colormap) c_vals = np.linspace(0,1,len(orbs)) colors = c_map(c_vals) # Fix for RGB comparison if len(tdos) == 2 and 'both' in spin and len(orbs)==3: colors[[-1,-2]]= colors[[-2,-1]] #Flip last two colors only else: return print("`colormap` expects one of the follwoing:\n{}".format(plt.colormaps())) # Total DOS colors t_color=mpl.colors.to_rgb(tdos_color) it_color=gu.transform_color(t_color,c = -1) #inverts for c = -1 #========Title Name======== SYSTEM=vr.sys_info.SYSTEM if(title==None): title="{}".format(SYSTEM) fig = go.Figure() fig.update_layout(title=title,margin=go.layout.Margin(l=60,r=50,b=40,t=75,pad=0),\ font=dict(family="stix, serif",size=14)) if(figsize!=None): fig.update_layout(width=figsize[0],height=figsize[1],autosize=False) if(vertical==False): if(fill_area==False): fill=None if(fill_area==True): fill='tozeroy' args_dic=dict(mode='lines',line_width=linewidth,fill=fill) fig.update_xaxes(range=ylim,title='Energy (eV)') if(len(tdos)==2): # Spin polarized. fig.add_scatter(x=en,y=tdos[0],line_color='rgb({},{},{})'.format(*[int(255*i) for i in t_color]),\ name='TDOS<sup>↑</sup>',**args_dic) fig.add_scatter(x=en,y=tdos[1],line_color='rgb({},{},{})'.format(*[int(255*i) for i in it_color]),\ name='TDOS<sup>↓</sup>',**args_dic) else: # unpolarized. fig.add_trace(go.Scatter(x=en,y=tdos,line_color='rgb({},{},{})'.format(*[int(255*i) for i in t_color]),\ name='TDOS',**args_dic)) for p,l,c in zip(pdos,labels,colors): fig.add_trace(go.Scatter(x=en,y=p,line_color='rgb({},{},{})'.format(*[int(255*i) for i in c]),\ name=l,**args_dic)) if(vertical==True): if(fill_area==False): fill=None if(fill_area==True): fill='tozerox' args_dic=dict(mode='lines',line_width=linewidth,fill=fill) fig.update_yaxes(range=ylim,title='Energy (eV)') if(len(tdos)==2): # Spin polarized. fig.add_scatter(y=en,x=tdos[0],line_color='rgb({},{},{})'.format(*[int(255*i) for i in t_color]),\ name='TDOS<sup>↑</sup>',**args_dic) fig.add_scatter(y=en,x=tdos[1],line_color='rgb({},{},{})'.format(*[int(255*i) for i in it_color]),\ name='TDOS<sup>↓</sup>',**args_dic) else: # unpolarized. fig.add_trace(go.Scatter(y=en,x=tdos,line_color='rgb({},{},{})'.format(*[int(255*i) for i in t_color]),\ name='TDOS',**args_dic)) for p,l,c in zip(pdos,labels,colors): fig.add_trace(go.Scatter(y=en,x=p,line_color='rgb({},{},{})'.format(*[int(255*i) for i in c]),\ name=l,**args_dic)) fig.update_xaxes(showgrid=True, zeroline=True,showline=True, linewidth=0.1, linecolor='rgba(222,222,222,0.1)', mirror=True) fig.update_yaxes(showgrid=True, zeroline=True,showline=True, linewidth=0.1, linecolor='rgba(222,222,222,0.1)', mirror=True) return fig
row2 = np.linspace(y_min, y_max, height) n = np.empty((width, height)) for i in range(width): for j in range(height): n[i, j] = mandelbrot(row1[i] + 1j * row2[j], max) return (n) x = man_set(-2.0, 0.5, -1.25, 1.25, 500, 500, 500) fig = pl.figure(figsize=(40, 40)) ims = [] for i in range(2): im = pl.imshow(x, animated=True) ims.append([im]) ani = animation.ArtistAnimation(fig, ims, interval=100, blit=True, repeat_delay=5) pl.colormaps() pl.hot() pl.savefig("mandelbrot.jpg") pl.show()
def plot_histogram(self, bins): self.p = figure(plot_width=self.plot_width, plot_height=self.plot_height, x_axis_type=self.x_axis_type, x_axis_label=self.x_axis_label, y_axis_label=self.y_axis_label, title=self.plot_title) if self.group is not None: for k in self.source.keys(): cuts = pd.Series(pd.cut( self.source[k].data["x"], bins)).str.replace("\(|\]", "").str.split(", ", expand=True).astype(float) cuts = cuts.groupby( [0, 1], as_index=False).size().reset_index(name="freq") self.hist_source = ColumnDataSource( data=dict(min=cuts[0].tolist(), max=cuts[1].tolist(), freq=cuts["freq"].tolist(), color=[ self.source[k].data["color"][0], ] * len(cuts.index))) self.p.quad(left="min", right="max", bottom=0, top="freq", source=self.hist_source, line_color="black", color="color") else: cuts = pd.cut(self.source.data["x"], bins).str.replace( "\(|\]", "").str.split(", ", expand=True).astype(float) cuts = cuts.groupby([0, 1], as_index=False).size().reset_index(name="freq") self.hist_source = ColumnDataSource( data=dict(min=cuts[0].tolist(), max=cuts[1].tolist(), freq=cuts["freq"].tolist(), color=[ self.source.data["color"][0], ] * len(cuts.index))) self.p.quad(left="min", right="max", bottom=0, top="freq", source=self.hist_source, line_color="black", color="color") select_pal = Select( options=[c for c in pyplot.colormaps() if c != "jet"]) alpha_slider = Slider(start=0, end=1, value=1, step=.01, title="Transparency") title_text = TextInput(placeholder="Figure Title") bins_slider = Slider(start=1, end=99, value=bins, step=1, title="Bins") line_check = CheckboxGroup(labels=["Outline"], active=[0]) y_axis_label = TextInput(placeholder="y-axis label") x_axis_label = TextInput(placeholder="x-axis label") select_pal.on_change("value", self.change_palette_hist) alpha_slider.on_change("value", self.change_glyph_alpha) title_text.on_change("value", self.change_figure_title) y_axis_label.on_change("value", self.change_figure_yaxis) x_axis_label.on_change("value", self.change_figure_xaxis) line_check.on_change("active", self.change_hist_line) bins_slider.on_change("value", self.change_bins) app_layout = layout([title_text], [select_pal], [y_axis_label, self.p], [Spacer(height=10, width=500), x_axis_label], [alpha_slider], [bins_slider], [line_check]) return app_layout # df = pd.read_csv("iris.csv") # # gp = GraphPlot(df["Sepal_Length"], df["Sepal_Width"], group=df["Species"])#, group=df["Tests Failed"])#, x_axis_type="datetime") # # app_layout = gp.plot_histogram(7) # app_layout = gp.plot_scatter() # doc = curdoc() # doc.add_root(app_layout)
key=lambda data: self.minvals[col] if math.isnan(data[col]) else data[col]) if order == Qt.DescendingOrder: #print(self.columnheaderdata[col] + " sort DescendingOrder") self._data = sorted(self._data, key=lambda data: self.minvals[col] if math.isnan(data[col]) else data[col], reverse=True) self.layoutChanged.emit() # Dialog box with MatPlotLib colour gradient charts from # http://matplotlib.org/examples/color/colormaps_reference.html # list all colour maps except their reverse which end with "_r" cmaps = [c for c in plt.colormaps() if not c.endswith("_r")] gradient = np.linspace(0, 1, 256) gradient = np.vstack((gradient, gradient)) dpi = 50 class MplCanvas(FigureCanvas): def __init__(self, parent=None, width=5, height=4, dpi=dpi): self.parent = parent self.fig = Figure(figsize=(10, 20), dpi=dpi, facecolor=(1, 1, 1), edgecolor=(0.5, 0, 0)) self.axes = self.fig.subplots(nrows=len(cmaps), ncols=1) # alignment of each subplot.
def plot_line(self): """ Plot line graph :return: """ self.p = figure(plot_width=self.plot_width, plot_height=self.plot_height, x_axis_type=self.x_axis_type, x_axis_label=self.x_axis_label, y_axis_label=self.y_axis_label, title=self.plot_title) select_pal = Select( options=[c for c in pyplot.colormaps() if c != "jet"]) line_thick_slider = Slider(start=1, end=10, value=1, step=1, title="Line Width") title_text = TextInput(placeholder="Figure Title") y_axis_label = TextInput(placeholder="y-axis label") x_axis_label = TextInput(placeholder="x-axis label") if self.group is not None: for k in self.source.keys(): self.p.line("x", "y", color=self.source[k].data["color"][0], source=self.source[k]) legend = Legend(items=[ *list( zip(list(self.source.keys()), [[r] for r in self.p.renderers if isinstance(r, GlyphRenderer)])) ], location=(0, -30)) self.p.yaxis[0].formatter.use_scientific = False self.p.add_layout(legend, 'left') else: self.p.line("x", "y", color=self.source.data["color"][0], source=self.source) select_pal.on_change("value", self.change_palette_lines) line_thick_slider.on_change("value", self.change_line_thick) title_text.on_change("value", self.change_figure_title) y_axis_label.on_change("value", self.change_figure_yaxis) x_axis_label.on_change("value", self.change_figure_xaxis) app_layout = layout([select_pal], [title_text], [y_axis_label, self.p], [Spacer(height=10, width=500), x_axis_label], [line_thick_slider]) return app_layout
def plot_bar(self): """ Plot bar chart :return: """ self.p = figure(plot_width=self.plot_width, plot_height=self.plot_height, x_axis_label=self.x_axis_label, y_axis_label=self.y_axis_label) y_axis_label = TextInput(placeholder="y-axis label") x_axis_label = TextInput(placeholder="x-axis label") if self.group is not None: for g in self.source.keys(): if list(self.source.keys()).index(g) % 2 == 0: self.source[g].data["x"] = [ x + .2 * list(self.source.keys()).index(g) for x in self.source[g].data["x"] ] else: self.source[g].data["x"] = [ x - .2 * list(self.source.keys()).index(g) for x in self.source[g].data["x"] ] self.p.vbar(x="x", top="y", width=.5, fill_color="color", source=self.source[g], line_color="black") else: self.p.vbar(x="x", top="y", width=.5, fill_color="color", source=self.source, line_color="black") select_pal = Select( options=[c for c in pyplot.colormaps() if c != "jet"]) title_text = TextInput(placeholder="Figure Title") alpha_slider = Slider(start=0, end=1, value=1, step=.01, title="Transparency") select_pal.on_change("value", self.change_palette_bar) y_axis_label.on_change("value", self.change_figure_yaxis) x_axis_label.on_change("value", self.change_figure_xaxis) title_text.on_change("value", self.change_figure_title) alpha_slider.on_change("value", self.change_glyph_alpha) app_layout = layout([[select_pal], [title_text], [y_axis_label, self.p], [Spacer(height=10, width=500), x_axis_label], [alpha_slider]]) return app_layout
def plot_scatter(self): """ Plot scatter plots :return: None """ # set figure self.p = figure(plot_width=self.plot_width, plot_height=self.plot_height, x_axis_label=self.x_axis_label, y_axis_label=self.y_axis_label) y_axis_label = TextInput(placeholder="y-axis label") x_axis_label = TextInput(placeholder="x-axis label") # handle groups if self.group is not None: # plot each line for k in self.source.keys(): self.p.scatter("x", "y", color="color", source=self.source[k]) reg_x, reg_y, pred_upper, pred_lower = get_regression_line( self.source[k].data["x"], self.source[k].data["y"]) self.p.line(reg_x, reg_y, name="reg_line", color=self.source[k].data["color"][0]) bands = sorted(list( zip(pred_upper.tolist(), pred_lower.tolist(), reg_x)), key=lambda z: z[2]) band_x = [t[2] for t in bands] + [t[2] for t in bands][::-1] bounds = [t[1] for t in bands] + [t[0] for t in bands][::-1] self.p.patch(band_x, bounds, color=self.source[k].data["color"][0], alpha=.2, name="error") rends = [r for r in self.p.renderers] for r in rends: if r.name in ["reg_line", "error"]: r.visible = False legend = Legend(items=[ *list( zip(list(self.source.keys()), [[r] for r in self.p.renderers if isinstance(r, GlyphRenderer) and r.name not in ["reg_line", "error"]])) ], location=(0, -30)) self.p.add_layout(legend, 'left') else: self.p.scatter('x', 'y', color="color", source=self.source) reg_x, reg_y, pred_upper, pred_lower = get_regression_line( self.source.data["x"], self.source.data["y"]) self.p.line(reg_x, reg_y, name="reg_line", color=self.source.data["color"][0]) bands = sorted(list( zip(pred_upper.tolist(), pred_lower.tolist(), reg_x)), key=lambda z: z[2]) band_x = [t[2] for t in bands] + [t[2] for t in bands][::-1] bounds = [t[1] for t in bands] + [t[0] for t in bands][::-1] self.p.patch(band_x, bounds, color=self.source.data["color"][0], alpha=.2, name="error") rends = [r for r in self.p.renderers] for r in rends: if r.name in ["reg_line", "error"]: r.visible = False select_pal = Select( options=[c for c in pyplot.colormaps() if c != "jet"]) title_text = TextInput(placeholder="Figure Title") dot_size_slider = Slider(start=1, end=100, value=1, step=1, title="Dot Size") alpha_slider = Slider(start=0, end=1, value=1, step=.01, title="Transparency") reg_check = CheckboxGroup(labels=["Regression Line"]) self.reg_err_check = CheckboxGroup(labels=["Error Region"], disabled=True) dot_size_slider.on_change("value", self.change_dot_size) select_pal.on_change("value", self.change_palette_scatter) title_text.on_change("value", self.change_figure_title) alpha_slider.on_change("value", self.change_glyph_alpha) reg_check.on_change("active", self.add_regression) self.reg_err_check.on_change("active", self.add_reg_error) y_axis_label.on_change("value", self.change_figure_yaxis) x_axis_label.on_change("value", self.change_figure_xaxis) app_layout = layout([[title_text], [y_axis_label, self.p], [Spacer(height=10, width=500), x_axis_label], [select_pal], [dot_size_slider], [alpha_slider], [reg_check, self.reg_err_check]]) return app_layout
def remove_random_noise(spectrogram, plot=False, outputPngName=None, filter_compensation='log10', passes=1): nxMx = spectrogram.data nxMx = nxMx / np.max(np.abs(nxMx)) means = np.mean(nxMx, axis=1) stdevs = np.std(nxMx, axis=1) fs = spectrogram.metadata.sampling_configuration.fs H = spectrogram.metadata.sampling_configuration.window_step N = spectrogram.metadata.sampling_configuration.dft_length freq_per_bin = fs / len(means) frmTime = H * np.arange(nxMx.shape[1])/float(fs) binFreq = freq_per_bin*np.arange(len(means)) * 0.5 if plot: plt.figure(1) plt.subplot(311) plt.pcolormesh(frmTime, binFreq, inDb(nxMx)) plt.colormaps() low = np.min(nxMx) nf_nxMx = np.copy(nxMx) nf_nxMx2 = np.copy(nxMx) maxlog = np.log10(len(means)) subs = [] if filter_compensation == 'log10': for i in range(len(means)): if i == 0: continue subs.append(1 - (np.log10(i) / maxlog)) for k in range(passes): nf_nxMx[i] -= abs(means[i] + ((1 - (np.log10(i + eps) / maxlog)) * stdevs[i])) if filter_compensation == 'linear': for i in range(len(means)): nf_nxMx[i] -= abs(means[i] + ( (1 - (i / len(means)-1)) * stdevs[i])) nf_nxMx = np.clip(nf_nxMx, a_min=low, a_max=1.0) nf_nxMx2 = np.copy(nf_nxMx) spectrogram.data = nf_nxMx if outputPngName is not None: output = np.copy(nf_nxMx) #plt.imsave(outputPngName, inDb(output)[::-1]) plt.imsave(outputPngName, inDb(nxMx)[::-1]) if plot: # plt.figure(0) # plt.plot(subs) # plt.figure(1) sbplt = plt.subplot(312) plt.pcolormesh(frmTime, binFreq, inDb(nf_nxMx)) sbplt = plt.subplot(313) plt.pcolormesh(frmTime, binFreq, inDb(nf_nxMx2)) if plot: plt.figure(1) plt.show(1) plt.close()
def show_colormaps(subset="all", filename=None): """ Displays the colormaps available to yt. Note, most functions can use both the matplotlib and the native yt colormaps; however, there are some special functions existing within image_writer.py (e.g. write_image() write_bitmap(), etc.), which cannot access the matplotlib colormaps. In addition to the colormaps listed, one can access the reverse of each colormap by appending a "_r" to any map. If you wish to only see certain colormaps, include them in the cmap_list attribute. Parameters ---------- subset : string, or list of strings, optional valid values : "all", "yt_native", or list of cmap names default : "all" As mentioned above, a few functions can only access yt_native colormaps. To display only the yt_native colormaps, set this to "yt_native". If you wish to only see a few colormaps side by side, you can include them as a list of colormap names. Example: ['cmyt.algae', 'gist_stern', 'cmyt.kamae', 'nipy_spectral'] filename : string, opt default: None If filename is set, then it will save the colormaps to an output file. If it is not set, it will "show" the result interactively. """ from matplotlib import pyplot as plt a = np.outer(np.arange(0, 1, 0.01), np.ones(10)) if subset == "all": maps = [ m for m in plt.colormaps() if (not m.startswith("idl")) & (not m.endswith("_r")) ] elif subset == "yt_native": maps = [ m for m in _cm.color_map_luts if (not m.startswith("idl")) & (not m.endswith("_r")) ] else: try: maps = [m for m in plt.colormaps() if m in subset] if len(maps) == 0: raise AttributeError except AttributeError as e: raise AttributeError("show_colormaps requires subset attribute " "to be 'all', 'yt_native', or a list of " "valid colormap names.") from e maps = sorted(set(maps)) # scale the image size by the number of cmaps plt.figure(figsize=(2.0 * len(maps) / 10.0, 6)) plt.subplots_adjust(top=0.7, bottom=0.05, left=0.01, right=0.99) l = len(maps) + 1 for i, m in enumerate(maps): plt.subplot(1, l, i + 1) plt.axis("off") plt.imshow(a, aspect="auto", cmap=plt.get_cmap(m), origin="lower") plt.title(m, rotation=90, fontsize=10, verticalalignment="bottom") if filename is not None: plt.savefig(filename, dpi=100, facecolor="gray") else: plt.show()
def _plot_image(x, y, z, xlim=None, ylim=None, vlim=None, norm=None, cmap='viridis', interpolation='none', title=None, xlabel=None, ylabel=None, fontsize=16, cblabel=None, cbticks=None, cborientation='horizontal', ax=None, fig=None, geometry=111): '''Create an image of z(y, x). Parameters: ----------- x - array_like, (`M`,) The x-axis of image. It is assumed that the x-values correspond to the midpoints of bins in the x-dimension (e.g. the midpoint of the ensembles in :py:class:`SpectralDensity <random_data.spectra.SpectralDensity>`) y - array_like, (`N`,) The y-axis of image. It is assumed that the y-values correspond to discrete samples of a function, such as the discrete frequencies of a discrete Fourier transform. z - array_like, (`N`, `M`) The array containing the image values. xlim - array_like, (2,) The minimum and maximum values of `x` to display. ylim - array_like, (2,) The minimum and maximum values of `y` to display. vlim - array_like, (2,) The minimum and maximum values of `z` to display. norm - string or None If `log`, display image on logarithmic scale; otherwise, display image on linear scale. cmap - string Colormap used for image. Default matplotlib colormaps are found in :py:module:`cm <matplotlib.cm>`. interpolation - string Interpolation method to be used by :py:function `imshow. <matplotlib.pyplot.imshow>`. Examples of each interpolation scheme are displayed here: http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html and the difference between 'none' and 'nearest' is demonstrated here: http://matplotlib.org/examples/images_contours_and_fields/interpolation_none_vs_nearest.html title, xlabel, ylabel, cblabel - string Titles of respective objects in image. cborientation - string Orientation of colorbar; may be in {'horizontal', 'vertical'}. fontsize - int Size of font in titles, labels, etc. ax - :py:class:`AxesSubplot <matplotlib.axes._subplots.AxesSubplot>` instance corresponding to the axis (i.e. "subplot") where the image will be drawn. `ax` will (obviously) be modified by this method. If an axis instance is not provided, an axis will automatically be created. fig - :py:class:`Figure <matplotlib.figure.Figure>` instance If an axis instance is *not* provided, one can provide a figure instance (and an axis `geometry`, describing the location of the axis instance in the figure) to control which window is plotted in. If a figure instance is not provided (and axis instance is also not provided), a figure instance will be created with the next available window number. geometry - int, or tuple If an axis instance is *not* provided, `geometry` determines the location of the axis instance in the provided or created figure. The standard matplotlib subplot geometry indexing is used (see `<matplotlib.pyplot.subplot>` for more information). Returns: -------- ax - :py:class:`AxesSubplot <matplotlib.axes._subplots.AxesSubplot>` instance corresponding to the axis (i.e. "subplot") where the spectrogram will be drawn. This is either identical to the axis instance used during the call or, if an axis instance was not provided, the axis instance created during the call. ''' # Determine (x, y) extent of image if xlim is not None: xlim = np.sort(xlim) xind = np.where(np.logical_and(x >= xlim[0], x <= xlim[1]))[0] else: xind = np.arange(len(x)) if ylim is not None: ylim = np.sort(ylim) yind = np.where(np.logical_and(y >= ylim[0], y <= ylim[1]))[0] else: yind = np.arange(len(y)) dx = x[1] - x[0] dy = y[1] - y[0] extent = (x[xind[0]] - (0.5 * dx), x[xind[-1]] + (0.5 * dx), y[yind[0]] - (0.5 * dy), y[yind[-1]] + (0.5 * dy)) # If an axis instance is not provided, create one if ax is None: # If, in addition, a figure instance is not provided, # create a new figure if fig is None: fig = plt.figure() # Create axis with desired subplot geometry ax = fig.add_subplot(geometry) if vlim is not None: vlim = np.sort(vlim) else: vlim = [np.nanmin(z[yind, :][:, xind]), np.nanmax(z[yind, :][:, xind])] if norm == 'log': norm = LogNorm() # Ensure that specified colormap is available if not isinstance(cmap, Colormap) and (cmap not in plt.colormaps()): cmap_backup = 'Purples' print ("\nThe '%s' colormap is not available; falling back to '%s'\n" % (cmap, cmap_backup)) cmap = cmap_backup # Create plot im = ax.imshow(np.flipud(z[yind, :][:, xind]), extent=extent, aspect='auto', vmin=vlim[0], vmax=vlim[1], norm=norm, cmap=cmap, interpolation=interpolation) # Colorbar if norm == 'log': format = LogFormatter(labelOnlyBase=True) else: format = None cb = plt.colorbar(im, format=format, ticks=cbticks, ax=ax, orientation=cborientation) # Labeling if title is not None: ax.set_title(title, fontsize=fontsize) if xlabel is not None: ax.set_xlabel(xlabel, fontsize=fontsize) if ylabel is not None: ax.set_ylabel(ylabel, fontsize=fontsize) if cblabel is not None: cb.set_label(cblabel, fontsize=fontsize) return ax
def heatmapperOptionalArgs(mode=['heatmap', 'profile'][0]): parser = argparse.ArgumentParser(add_help=False) cluster = parser.add_argument_group('Clustering arguments') cluster.add_argument( '--kmeans', help='Number of clusters to compute. When this ' 'option is set, the matrix is split into clusters ' 'using the k-means algorithm. Only works for data that ' 'is not grouped, otherwise only the first group will ' 'be clustered. If more specific clustering methods ' 'are required, then save the underlying matrix ' 'and run the clustering using other software. The plotting ' 'of the clustering may fail with an error if a ' 'cluster has very few members compared to the total number ' 'or regions.', type=int) cluster.add_argument( '--hclust', help='Number of clusters to compute. When this ' 'option is set, then the matrix is split into clusters ' 'using the hierarchical clustering algorithm, using "ward linkage". ' 'Only works for data that is not grouped, otherwise only the first ' 'group will be clustered. --hclust could be very slow if you have ' '>1000 regions. In those cases, you might prefer --kmeans or if more ' 'clustering methods are required you can save the underlying matrix and run ' 'the clustering using other software. The plotting of the clustering may ' 'fail with an error if a cluster has very few members compared to the ' 'total number of regions.', type=int) cluster.add_argument( '--silhouette', help='Compute the silhouette score for regions. This is only' ' applicable if clustering has been performed. The silhouette score' ' is a measure of how similar a region is to other regions in the' ' same cluster as opposed to those in other clusters. It will be reported' ' in the final column of the BED file with regions. The ' 'silhouette evaluation can be very slow when you have more' 'than 100 000 regions.', action='store_true' ) optional = parser.add_argument_group('Optional arguments') optional.add_argument("--help", "-h", action="help", help="show this help message and exit") optional.add_argument('--version', action='version', version='%(prog)s {}'.format(__version__)) if mode == 'profile': optional.add_argument( '--averageType', default='mean', choices=["mean", "median", "min", "max", "std", "sum"], help='The type of statistic that should be used for the ' 'profile. The options are: "mean", "median", "min", "max", ' '"sum" and "std".') optional.add_argument('--plotHeight', help='Plot height in cm.', type=float, default=7) optional.add_argument('--plotWidth', help='Plot width in cm. The minimum value is 1 cm.', type=float, default=11) optional.add_argument( '--plotType', help='"lines" will plot the profile line based ' 'on the average type selected. "fill" ' 'fills the region between zero and the profile ' 'curve. The fill in color is semi transparent to ' 'distinguish different profiles. "se" and "std" ' 'color the region between the profile and the ' 'standard error or standard deviation of the data. ' 'As in the case of ' 'fill, a semi-transparent color is used. ' '"overlapped_lines" plots each region\'s value, one on ' 'top of the other. "heatmap" plots a ' 'summary heatmap.', choices=['lines', 'fill', 'se', 'std', 'overlapped_lines', 'heatmap'], default='lines') optional.add_argument('--colors', help='List of colors to use ' 'for the plotted lines (N.B., not applicable to \'--plotType overlapped_lines\'). Color names ' 'and html hex strings (e.g., #eeff22) ' 'are accepted. The color names should ' 'be space separated. For example, ' '--colors red blue green ', nargs='+') optional.add_argument('--numPlotsPerRow', help='Number of plots per row', type=int, default=8) optional.add_argument('--clusterUsingSamples', help='List of sample numbers (order as in ' 'matrix), that are used for clustering by ' '--kmeans or --hclust if not given, all samples ' 'are taken into account for clustering. ' 'Example: --ClusterUsingSamples 1 3', type=int, nargs='+') elif mode == 'heatmap': optional.add_argument( '--plotType', help='"lines" will plot the profile line based ' 'on the average type selected. "fill" ' 'fills the region between zero and the profile ' 'curve. The fill in color is semi transparent to ' 'distinguish different profiles. "se" and "std" ' 'color the region between the profile and the ' 'standard error or standard deviation of the data.', choices=['lines', 'fill', 'se', 'std'], default='lines') optional.add_argument('--sortRegions', help='Whether the heatmap should present ' 'the regions sorted. The default is ' 'to sort in descending order based on ' 'the mean value per region. Note that "keep" and "no" are the same thing.', choices=["descend", "ascend", "no", "keep"], default='descend') optional.add_argument('--sortUsing', help='Indicate which method should be used for ' 'sorting. For each row the method is computed. ' 'For region_length, a dashed line is drawn at ' 'the end of the region (reference point TSS and ' 'center) or the beginning of the region ' '(reference point TES) as appropriate.', choices=["mean", "median", "max", "min", "sum", "region_length"], default='mean') optional.add_argument('--sortUsingSamples', help='List of sample numbers (order as in matrix), ' 'which are used by --sortUsing for sorting. ' 'If no value is set, it uses all samples. ' 'Example: --sortUsingSamples 1 3', type=int, nargs='+') optional.add_argument('--linesAtTickMarks', help='Draw dashed lines from all tick marks through the heatmap. ' 'This is then similar to the dashed line draw at region bounds ' 'when using a reference point and --sortUsing region_length', action='store_true') optional.add_argument('--clusterUsingSamples', help='List of sample numbers (order as in ' 'matrix), that are used for clustering by ' '--kmeans or --hclust if not given, all samples ' 'are taken into account for clustering. ' 'Example: --ClusterUsingSamples 1 3', type=int, nargs='+') optional.add_argument( '--averageTypeSummaryPlot', default='mean', choices=["mean", "median", "min", "max", "std", "sum"], help='Define the type of statistic that should be plotted in the ' 'summary image above the heatmap. The options are: "mean", ' '"median", "min", "max", "sum" and "std".') optional.add_argument( '--missingDataColor', default='black', help='If --missingDataAsZero was not set, such cases ' 'will be colored in black by default. Using this ' 'parameter, a different color can be set. A value ' 'between 0 and 1 will be used for a gray scale ' '(black is 0). For a list of possible color ' 'names see: http://packages.python.org/ete2/' 'reference/reference_svgcolors.html. ' 'Other colors can be specified using the #rrggbb ' 'notation.') import matplotlib.pyplot as plt color_options = "', '".join([x for x in plt.colormaps() if not x.endswith('_r')]) optional.add_argument( '--colorMap', help='Color map to use for the heatmap. If more than one heatmap is being plotted the color ' 'of each heatmap can be enter individually (e.g. `--colorMap Reds Blues`). Color maps ' 'are recycled if the number of color maps is smaller than the number of heatmaps being ' 'plotted. Available values can be seen here: http://matplotlib.org/users/colormaps.html ' 'The available options are: \'' + color_options + '\'', default=['RdYlBu'], nargs='+') optional.add_argument( '--alpha', default=1.0, type=check_float_0_1, help='The alpha channel (transparency) to use for the heatmaps. The default is 1.0 and values ' 'must be between 0 and 1.') optional.add_argument( '--colorList', help='List of colors to use to create a colormap. For example, if `--colorList black,yellow,blue` ' 'is set (colors separated by comas) then a color map that starts with black, continues to ' 'yellow and finishes in blue is created. If this option is selected, it overrides the --colorMap ' 'chosen. The list of valid color names can be seen here: ' 'http://matplotlib.org/examples/color/named_colors.html ' 'Hex colors are valid (e.g #34a2b1). If individual colors for different heatmaps ' 'need to be specified they need to be separated by space as for example: ' '`--colorList "white,#cccccc" "white,darkred"` ' 'As for --colorMap, the color lists are recycled if their number is smaller thatn the number of' 'plotted heatmaps. ' 'The number of transitions is defined by the --colorNumber option.', type=check_list_of_comma_values, nargs='+') optional.add_argument( '--colorNumber', help='N.B., --colorList is required for an effect. This controls the ' 'number of transitions from one color to the other. If --colorNumber is ' 'the number of colors in --colorList then there will be no transitions ' 'between the colors.', type=int, default=256) optional.add_argument('--zMin', '-min', default=None, help='Minimum value for the heatmap intensities. Multiple values, separated by ' 'spaces can be set for each heatmap. If the number of zMin values is smaller than' 'the number of heatmaps the values are recycled. If a value is set to "auto", it will be set ' ' to the first percentile of the matrix values.', type=str, nargs='+') optional.add_argument('--zMax', '-max', default=None, help='Maximum value for the heatmap intensities. Multiple values, separated by ' 'spaces can be set for each heatmap. If the number of zMax values is smaller than' 'the number of heatmaps the values are recycled. If a value is set to "auto", it will be set ' ' to the 98th percentile of the matrix values.', type=str, nargs='+') optional.add_argument('--heatmapHeight', help='Plot height in cm. The default for the heatmap ' 'height is 28. The minimum value is ' '3 and the maximum is 100.', type=float, default=28) optional.add_argument('--heatmapWidth', help='Plot width in cm. The default value is 4 ' 'The minimum value is 1 and the ' 'maximum is 100.', type=float, default=4) optional.add_argument( '--whatToShow', help='The default is to include a summary or profile plot on top ' 'of the heatmap and a heatmap colorbar. Other options are: ' '"plot and heatmap", "heatmap only", "heatmap and ' 'colorbar", and the default "plot, heatmap and ' 'colorbar".', choices=["plot, heatmap and colorbar", "plot and heatmap", "heatmap only", "heatmap and colorbar"], default='plot, heatmap and colorbar') optional.add_argument( '--boxAroundHeatmaps', help='By default black boxes are plot around heatmaps. This can be turned off ' 'by setting --boxAroundHeatmaps no', default='yes') optional.add_argument('--xAxisLabel', '-x', default='gene distance (bp)', help='Description for the x-axis label.') # end elif optional.add_argument('--startLabel', default='TSS', help='[only for scale-regions mode] Label shown ' 'in the plot for the start of ' 'the region. Default is TSS (transcription ' 'start site), but could be changed to anything, ' 'e.g. "peak start". ' 'Same for the --endLabel option. See below.') optional.add_argument('--endLabel', default='TES', help='[only for scale-regions mode] Label ' 'shown in the plot for the region ' 'end. Default is TES (transcription end site).') optional.add_argument('--refPointLabel', help='[only for reference-point mode] Label ' 'shown in the plot for the ' 'reference-point. Default ' 'is the same as the reference point selected ' '(e.g. TSS), but could be anything, e.g. ' '"peak start".', default=None) optional.add_argument('--labelRotation', dest='label_rotation', help='Rotation of the X-axis labels in degrees. The default is 0, positive values denote a counter-clockwise rotation.', type=float, default=0.0) optional.add_argument('--nanAfterEnd', help=argparse.SUPPRESS, default=False) optional.add_argument('--regionsLabel', '-z', help='Labels for the regions plotted in the ' 'heatmap. If more than one region is being ' 'plotted, a list of labels separated by spaces is required. ' 'If a label itself contains a space, then quotes are ' 'needed. For example, --regionsLabel label_1, "label 2". ', nargs='+') optional.add_argument('--samplesLabel', help='Labels for the samples plotted. The ' 'default is to use the file name of the ' 'sample. The sample labels should be separated ' 'by spaces and quoted if a label itself' 'contains a space E.g. --samplesLabel label-1 "label 2" ', nargs='+') optional.add_argument('--plotTitle', '-T', help='Title of the plot, to be printed on top of ' 'the generated image. Leave blank for no title.', default='') optional.add_argument('--yAxisLabel', '-y', default='', help='Y-axis label for the top panel.') optional.add_argument('--yMin', default=None, nargs='+', help='Minimum value for the Y-axis. Multiple values, separated by ' 'spaces can be set for each profile. If the number of yMin values is smaller than' 'the number of plots, the values are recycled.') optional.add_argument('--yMax', default=None, nargs='+', help='Maximum value for the Y-axis. Multiple values, separated by ' 'spaces can be set for each profile. If the number of yMin values is smaller than' 'the number of plots, the values are recycled.') optional.add_argument('--legendLocation', default='best', choices=['best', 'upper-right', 'upper-left', 'upper-center', 'lower-left', 'lower-right', 'lower-center', 'center', 'center-left', 'center-right', 'none' ], help='Location for the legend in the summary plot. ' 'Note that "none" does not work for the profiler.') optional.add_argument('--perGroup', help='The default is to plot all groups of regions by ' 'sample. Using this option instead plots all samples by ' 'group of regions. Note that this is only useful if you ' 'have multiple groups of regions. by sample rather than ' 'group.', action='store_true') optional.add_argument('--plotFileFormat', metavar='', help='Image format type. If given, this ' 'option overrides the ' 'image format based on the plotFile ending. ' 'The available options are: "png", ' '"eps", "pdf", "plotly" and "svg"', choices=['png', 'pdf', 'svg', 'eps', 'plotly']) optional.add_argument('--verbose', help='If set, warning messages and ' 'additional information are given.', action='store_true') return parser
def __do_layout(self): """ fill the dialog window . """ sizer_main = wx.BoxSizer(wx.VERTICAL) sizer_button = wx.BoxSizer(wx.HORIZONTAL) sizer_params = wx.GridBagSizer(5, 5) sizer_colormap = wx.BoxSizer(wx.VERTICAL) sizer_selection = wx.BoxSizer(wx.HORIZONTAL) iy = 0 sizer_params.Add(self.label_xnpts, (iy, 0), (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) sizer_params.Add(self.xnpts_ctl, (iy, 1), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) iy += 1 sizer_params.Add(self.label_ynpts, (iy, 0), (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) sizer_params.Add(self.ynpts_ctl, (iy, 1), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) iy += 1 sizer_params.Add(self.label_qmax, (iy, 0), (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) sizer_params.Add(self.qmax_ctl, (iy, 1), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) iy += 1 sizer_params.Add(self.label_beam, (iy, 0), (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) sizer_params.Add(self.beam_ctl, (iy, 1), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) iy += 1 sizer_params.Add(self.label_zmin, (iy, 0), (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) sizer_params.Add(self.zmin_ctl, (iy, 1), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) iy += 1 sizer_params.Add(self.label_zmax, (iy, 0), (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) sizer_params.Add(self.zmax_ctl, (iy, 1), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) iy += 1 self.fig = plt.figure(dpi=self.dpi, figsize=(4, 1)) self.ax1 = self.fig.add_axes([0.05, 0.65, 0.9, 0.15]) self.norm = mpl.colors.Normalize(vmin=0, vmax=100) self.cb1 = mpl.colorbar.ColorbarBase(self.ax1, cmap=self.cmap, norm=self.norm, orientation='horizontal') self.cb1.set_label('Detector Colors') self.canvas = Canvas(self, -1, self.fig) sizer_colormap.Add(self.canvas, 0, wx.LEFT | wx.EXPAND, 5) self.cmap_selector = wx.ComboBox(self, -1) self.cmap_selector.SetValue(str(self.cmap.name)) maps = sorted(m for m in plt.colormaps() if not m.endswith("_r")) for i, m in enumerate(maps): self.cmap_selector.Append(str(m), plt.cm.get_cmap(m)) wx.EVT_COMBOBOX(self.cmap_selector, -1, self._on_select_cmap) sizer_selection.Add(wx.StaticText(self, -1, "Select Cmap: "), 0, wx.LEFT | wx.ADJUST_MINSIZE, 5) sizer_selection.Add(self.cmap_selector, 0, wx.EXPAND | wx.ALL, 10) sizer_main.Add(sizer_params, 0, wx.EXPAND | wx.ALL, 5) sizer_main.Add(sizer_selection, 0, wx.EXPAND | wx.ALL, 5) note = " Note: This is one time option. " + \ "It will be reset on updating the image." note_txt = wx.StaticText(self, -1, note) sizer_main.Add(note_txt, 0, wx.EXPAND | wx.ALL, 5) sizer_main.Add(sizer_colormap, 1, wx.EXPAND | wx.ALL, 5) sizer_main.Add(self.static_line_3, 0, wx.EXPAND, 0) sizer_button.Add(self.button_reset, 0, wx.LEFT | wx.ADJUST_MINSIZE, 100) sizer_button.Add(self.button_ok, 0, wx.LEFT | wx.ADJUST_MINSIZE, 10) sizer_button.Add(self.button_cancel, 0, wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10) sizer_main.Add(sizer_button, 0, wx.EXPAND | wx.BOTTOM | wx.TOP, 10) self.SetAutoLayout(True) self.SetSizer(sizer_main) self.Layout() self.Centre()
def __init__(self, particlearray): self.array_name = particlearray.name self.scalar = widgets.Dropdown(options=['None'] + particlearray.output_property_arrays, value='rho', description="scalar", disabled=False, layout=widgets.Layout(width='240px', display='flex')) self.scalar.owner = self.array_name self.scalar_cmap = widgets.Dropdown( options=list(map(str, plt.colormaps())), value='viridis', description="Colormap", disabled=False, layout=widgets.Layout(width='240px', display='flex')) self.scalar_cmap.owner = self.array_name self.legend = widgets.Checkbox(value=False, description="legend", disabled=False, layout=widgets.Layout(width='200px', display='flex')) self.legend.owner = self.array_name self.vector = widgets.Text(value='', placeholder='variable1,variable2', description='vector', disabled=False, layout=widgets.Layout(width='240px', display='flex'), continuous_update=False) self.vector.owner = self.array_name self.vector_width = widgets.FloatSlider( min=1, max=100, step=1, value=25, description='vector width', layout=widgets.Layout(width='300px'), continuous_update=False, ) self.vector_width.owner = self.array_name self.vector_scale = widgets.FloatSlider( min=1, max=100, step=1, value=55, description='vector scale', layout=widgets.Layout(width='300px'), continuous_update=False, ) self.vector_scale.owner = self.array_name self.scalar_size = widgets.FloatSlider( min=0, max=50, step=1, value=10, description='scalar size', layout=widgets.Layout(width='300px'), continuous_update=False, ) self.scalar_size.owner = self.array_name
import numpy as np import matplotlib.pyplot as plt N = 200 X = np.linspace(-1, 1, N + 1) Y = np.linspace(-1, 1, N + 1) XX, YY = np.meshgrid(X, Y) x = np.linspace(-1, 1, N) y = np.linspace(-1, 1, N) xx, yy = np.meshgrid(x, y) r = np.sqrt((xx - .25)**2 + (yy - .5)**2) z = np.cos(8 * r) + xx # get a list of available colormaps cml = plt.colormaps() # remove reversed one cml = [item for item in cml if '_r' not in item] # plotting plt.close('all') fig = plt.figure(figsize=(20, 12)) ii = 1 for cm in cml: ax = fig.add_subplot(6, 14, ii) ax.pcolormesh(XX, YY, z, cmap=cm) ax.axis('square')
# -*- coding: utf-8 -*- """ Created on Wed May 13 19:28:11 2020 @author: santi """ import numpy as np from pathlib import Path import matplotlib.pyplot as plt ang = np.empty(0) mag = np.empty([0,806]) for file in list(Path('./Directividad').glob('*.txt')): t = np.loadtxt(str(file), skiprows = 2, usecols = [0,1]) print(t) ang = np.hstack([ang,(int((str(file)[25:-5])))]) mag = np.vstack([mag,t[:,1]]) freq = t[:,0] ang = np.hstack([-ang[1:][::-1],ang]) X, Y = np.meshgrid(freq,ang) mag = np.vstack([mag[1:][::-1],mag]) plt.figure(1) plt.contourf(X, Y, mag) plt.colormaps() plt.colorbar()
import matplotlib.pyplot as plt import numpy as np gradient = np.linspace(0, 1, 256) gradient = np.vstack((gradient, gradient)) fig, ax = plt.subplots(figsize=(6, 1)) ax.set_axis_off() for name in plt.colormaps(): ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name)) plt.savefig(f'{name}.png', dpi=300)
def overlay_data(ax, cb_ax, odata, var_label=None, **kw): """ overlay data onto image with vertex lat/lon points. inputs: ax: axis object where polygons will be drawn cb_ax: the colorbar axis object. Note this is only used if the cmap is a colormap. odata: oco2 overlay data dictionary extra kw: cmap (string colormap or color name), vmin, vmax, alpha returns: None method based on https://scitools.org.uk/cartopy/docs/latest/gallery/hurricane_katrina.html #sphx-glr-gallery-hurricane-katrina-py the add_geometries() method would accept a list of polygons, but then there is no obvious way to color them according to the colorbar - so, instead do them one at a time. """ n_vals = odata['lat'].shape[0] if 'cmap' in kw: if kw['cmap'] in plt.colormaps(): cmap_name = kw['cmap'] use_cmap = True elif kw['cmap'] in mpl.colors.cnames.keys(): color_name = kw['cmap'] use_cmap = False else: print(kw['cmap'] + " is not a recognized color or colormap. " + "Data will be displayed in red") color_name = 'red' use_cmap = False else: cmap_name = 'jet' use_cmap = True if 'alpha' in kw: alpha = kw['alpha'] else: alpha = 1.0 if use_cmap: if 'vmin' in kw: vmin = kw['vmin'] else: vmin = odata['var_data'].min() if 'vmax' in kw: vmax = kw['vmax'] else: vmax = odata['var_data'].max() C_func = mpl.cm.get_cmap(cmap_name) N_func = mpl.colors.Normalize(vmin=vmin, vmax=vmax) # vertices: loop over footprints and use add_geometries() if odata['lat'].ndim == 2: for n in range(n_vals): poly_pts = np.ma.stack([odata['lon'][n, :], odata['lat'][n, :]]).T polygon = sgeom.Polygon(poly_pts) if use_cmap: facecolor = C_func(N_func(odata['var_data'][n])) else: facecolor = color_name ax.add_geometries([polygon], ccrs.PlateCarree(), facecolor=facecolor, alpha=alpha, edgecolor='none') # footprint centers, use scatter() else: if use_cmap: ax.scatter(odata['lon'], odata['lat'], c=odata['var_data'], cmap=cmap_name, vmin=vmin, vmax=vmax, s=9, edgecolor='none', transform=ccrs.PlateCarree()) else: ax.scatter(odata['lon'], odata['lat'], c=color_name, s=9, edgecolor='none', transform=ccrs.PlateCarree()) if use_cmap: cb = mpl.colorbar.ColorbarBase(cb_ax, cmap=C_func, orientation='vertical', norm=N_func) if var_label: cb_lab = cb.ax.set_xlabel(var_label, labelpad=8, fontweight='bold') cb_lab.set_fontsize(10) cb.ax.xaxis.set_label_position("top") for t in cb.ax.yaxis.get_ticklabels(): t.set_weight("bold") t.set_fontsize(12)
class Generic2DPlotCtrl(object): tab_contents = ['Data', 'Labels', 'Axes', 'Lineout', 'Colormaps'] eps = 1e-40 colormaps_available = sorted(c for c in plt.colormaps() if not c.endswith("_r")) def __init__(self, data, slcs=(slice(None, ), ), title=None, norm=None, fig_handle=None, time_in_title=True, **kwargs): self._data, self._slcs, self.im_xlt, self.time_in_title = data, slcs, None, time_in_title # # # -------------------- Tab0 -------------------------- items_layout = Layout(flex='1 1 auto', width='auto') # normalization # general parameters: vmin, vmax, clip self.if_vmin_auto = widgets.Checkbox(value=True, description='Auto', layout=items_layout) self.if_vmax_auto = widgets.Checkbox(value=True, description='Auto', layout=items_layout) self.vmin_wgt = widgets.FloatText(value=np.min(data), description='vmin:', continuous_update=False, disabled=self.if_vmin_auto.value, layout=items_layout) self.vlogmin_wgt = widgets.FloatText(value=self.eps, description='vmin:', continuous_update=False, disabled=self.if_vmin_auto.value, layout=items_layout) self.vmax_wgt = widgets.FloatText(value=np.max(data), description='vmax:', continuous_update=False, disabled=self.if_vmin_auto.value, layout=items_layout) self.if_clip_cm = widgets.Checkbox(value=True, description='Clip', layout=items_layout) # PowerNorm specific self.gamma = widgets.FloatText(value=1, description='gamma:', continuous_update=False, layout=items_layout) # SymLogNorm specific self.linthresh = widgets.FloatText(value=self.eps, description='linthresh:', continuous_update=False, layout=items_layout) self.linscale = widgets.FloatText(value=1.0, description='linscale:', continuous_update=False, layout=items_layout) # build the widgets tuple ln_wgt = (LogNorm, widgets.VBox([ widgets.HBox([self.vlogmin_wgt, self.if_vmin_auto]), widgets.HBox([self.vmax_wgt, self.if_vmax_auto]), self.if_clip_cm ])) n_wgt = (Normalize, widgets.VBox([ widgets.HBox([self.vmin_wgt, self.if_vmin_auto]), widgets.HBox([self.vmax_wgt, self.if_vmax_auto]), self.if_clip_cm ])) pn_wgt = (PowerNorm, widgets.VBox([ widgets.HBox([self.vmin_wgt, self.if_vmin_auto]), widgets.HBox([self.vmax_wgt, self.if_vmax_auto]), self.if_clip_cm, self.gamma ])) sln_wgt = (SymLogNorm, widgets.VBox([ widgets.HBox([self.vmin_wgt, self.if_vmin_auto]), widgets.HBox([self.vmax_wgt, self.if_vmax_auto]), self.if_clip_cm, self.linthresh, self.linscale ])) # find out default value for norm_selector norm_avail = { 'Log': ln_wgt, 'Normalize': n_wgt, 'Power': pn_wgt, 'SymLog': sln_wgt } self.norm_selector = widgets.Dropdown(options=norm_avail, value=norm_avail.get( norm, n_wgt), description='Normalization:') # additional care for LorNorm() self.__handle_lognorm() # re-plot button self.norm_btn_wgt = widgets.Button(description='Apply', disabled=False, tooltip='set colormap', icon='refresh') tab0 = self.__get_tab0() # # # -------------------- Tab1 -------------------------- # title if not title: title = osh5vis.default_title(data, show_time=self.time_in_title) self.if_reset_title = widgets.Checkbox(value=True, description='Auto') self.title = widgets.Text(value=title, placeholder='data', continuous_update=False, description='Title:', disabled=self.if_reset_title.value) # x label self.if_reset_xlabel = widgets.Checkbox(value=True, description='Auto') self.xlabel = widgets.Text(value=osh5vis.axis_format( data.axes[1].long_name, data.axes[1].units), placeholder='x', continuous_update=False, description='X label:', disabled=self.if_reset_xlabel.value) # y label self.if_reset_ylabel = widgets.Checkbox(value=True, description='Auto') self.ylabel = widgets.Text(value=osh5vis.axis_format( data.axes[0].long_name, data.axes[0].units), placeholder='y', continuous_update=False, description='Y label:', disabled=self.if_reset_ylabel.value) # colorbar self.if_reset_cbar = widgets.Checkbox(value=True, description='Auto') self.cbar = widgets.Text(value=data.units.tex(), placeholder='a.u.', continuous_update=False, description='Colorbar:', disabled=self.if_reset_cbar.value) tab1 = widgets.VBox([ widgets.HBox([self.title, self.if_reset_title]), widgets.HBox([self.xlabel, self.if_reset_xlabel]), widgets.HBox([self.ylabel, self.if_reset_ylabel]), widgets.HBox([self.cbar, self.if_reset_cbar]) ]) # # # -------------------- Tab2 -------------------------- self.setting_instructions = widgets.Label( value="Enter invalid value to reset", layout=items_layout) self.apply_range_btn = widgets.Button(description='Apply', disabled=False, tooltip='set range', icon='refresh') self.axis_lim_wgt = widgets.HBox( [self.setting_instructions, self.apply_range_btn]) # x axis self.x_min_wgt = widgets.FloatText(value=self._data.axes[1].min, description='xmin:', continuous_update=False, layout=items_layout) self.x_max_wgt = widgets.FloatText(value=self._data.axes[1].max, description='xmax:', continuous_update=False, layout=items_layout) self.x_step_wgt = widgets.FloatText(value=self._data.axes[1].increment, continuous_update=False, description='$\Delta x$:', layout=items_layout) self.xaxis_lim_wgt = widgets.HBox( [self.x_min_wgt, self.x_max_wgt, self.x_step_wgt]) # y axis self.y_min_wgt = widgets.FloatText(value=self._data.axes[0].min, description='ymin:', continuous_update=False, layout=items_layout) self.y_max_wgt = widgets.FloatText(value=self._data.axes[0].max, description='ymax:', continuous_update=False, layout=items_layout) self.y_step_wgt = widgets.FloatText(value=self._data.axes[0].increment, continuous_update=False, description='$\Delta y$:', layout=items_layout) self.yaxis_lim_wgt = widgets.HBox( [self.y_min_wgt, self.y_max_wgt, self.y_step_wgt]) tab2 = widgets.VBox( [self.axis_lim_wgt, self.xaxis_lim_wgt, self.yaxis_lim_wgt]) # # # -------------------- Tab3 -------------------------- tab3 = self.if_lineout_wgt = widgets.Checkbox( value=False, description='X/Y Lineouts (incomplete feature)', layout=items_layout) # # # -------------------- Tab4 -------------------------- user_cmap = kwargs.pop('cmap', 'jet') self.cmap_selector = widgets.Dropdown(options=self.colormaps_available, value=user_cmap, description='Colormap:') self.cmap_reverse = widgets.Checkbox(value=False, description='Reverse', layout=items_layout) tab4 = widgets.HBox([self.cmap_selector, self.cmap_reverse]) # construct the tab self.tab = widgets.Tab() self.tab.children = [tab0, tab1, tab2, tab3, tab4] [self.tab.set_title(i, tt) for i, tt in enumerate(self.tab_contents)] # display(self.tab) # link and activate the widgets self.if_reset_title.observe(self.__update_title, 'value') self.if_reset_xlabel.observe(self.__update_xlabel, 'value') self.if_reset_ylabel.observe(self.__update_ylabel, 'value') self.if_reset_cbar.observe(self.__update_cbar, 'value') self.norm_btn_wgt.on_click(self.update_norm) self.if_vmin_auto.observe(self.__update_vmin, 'value') self.if_vmax_auto.observe(self.__update_vmax, 'value') self.norm_selector.observe(self.__update_norm_wgt, 'value') self.cmap_selector.observe(self.update_cmap, 'value') self.cmap_reverse.observe(self.update_cmap, 'value') self.title.observe(self.update_title, 'value') self.xlabel.observe(self.update_xlabel, 'value') self.ylabel.observe(self.update_ylabel, 'value') self.cbar.observe(self.update_cbar, 'value') self.y_max_wgt.observe(self.__update_y_max, 'value') self.y_min_wgt.observe(self.__update_y_min, 'value') self.x_max_wgt.observe(self.__update_x_max, 'value') self.x_min_wgt.observe(self.__update_x_min, 'value') self.x_step_wgt.observe(self.__update_delta_x, 'value') self.y_step_wgt.observe(self.__update_delta_y, 'value') self.apply_range_btn.on_click(self.update_plot_area) self.if_lineout_wgt.observe(self.toggle_lineout, 'value') # plotting and then setting normalization colors self.out = Output() self.out_main = Output() self.observer_thrd, self.cb = None, None self.fig = plt.figure() if fig_handle is None else fig_handle self.ax = self.fig.add_subplot(111) with self.out_main: self.im, self.cb = self.plot_data() display(self.fig) @property def self(self): return self @property def widgets_list(self): return self.tab, self.out_main, self.out @property def widget(self): return widgets.VBox([self.tab, self.out_main, self.out]) def update_data(self, data, slcs): self._data, self._slcs = data, slcs self.__update_title() self.__update_xlabel() self.__update_ylabel() def reset_plot_area(self): self.y_min_wgt.value, self.y_max_wgt.value, self.y_step_wgt.value = \ self._data.axes[0].min, self._data.axes[0].max, self._data.axes[0].increment self.x_min_wgt.value, self.x_max_wgt.value, self.x_step_wgt.value = \ self._data.axes[1].min, self._data.axes[1].max, self._data.axes[1].increment def redraw(self, data): """if the size of the data is the same we can just redraw part of figure""" self._data = data self.im.set_data(self.__pp(data[self._slcs])) self.fig.canvas.draw() def update_title(self, change): self.ax.axes.set_title(change['new']) def update_xlabel(self, change): self.ax.axes.xaxis.set_label_text(change['new']) def update_ylabel(self, change): self.ax.axes.yaxis.set_label_text(change['new']) def update_cbar(self, change): self.im.colorbar.set_label(change['new']) def update_cmap(self, _change): self.im.set_cmap(self.cmap_selector.value if not self.cmap_reverse. value else self.cmap_selector.value + '_r') def update_plot_area(self, *_): bnd = [(self.y_min_wgt.value, self.y_max_wgt.value, self.y_step_wgt.value), (self.x_min_wgt.value, self.x_max_wgt.value, self.x_step_wgt.value)] self._slcs = tuple( slice(*self._data.get_index_slice(self._data.axes[i], bd)) for i, bd in enumerate(bnd)) self.cb.remove() self.im, self.cb = self.plot_data(im=self.im) # dirty hack if self.norm_selector.value[0] == LogNorm: self.cb.set_norm(LogNorm()) def refresh_tab_wgt(self, update_list): """ the tab.children is a tuple so we have to reconstruct the whole tab widget when addition/deletion of children widgets happens """ tmp = self.tab.children newtab = [tmp[i] if not t else t for i, t in enumerate(update_list)] self.tab.children = tuple(newtab) def plot_data(self, **passthrough): return osh5vis.osimshow( self.__pp(self._data[self._slcs]), cmap=self.cmap_selector.value, norm=self.norm_selector.value[0](**self.__get_norm()), title=self.title.value, xlabel=self.xlabel.value, ylabel=self.ylabel.value, cblabel=self.cbar.value, ax=self.ax, fig=self.fig, **passthrough) def __get_tab0(self): return widgets.HBox([ widgets.VBox([self.norm_selector, self.norm_selector.value[1]]), self.norm_btn_wgt ]) @staticmethod def _idle(data): return data def __new_lineout_plot(self): with self.out: self.fig, self.outline_ax = plt.subplots(1, 2, figsize=(3, 2)) ldata = self._data[self._slcs] osh5vis.osplot1d(ldata[ldata.shape[0] // 2, :], ax=self.outline_ax[0]) osh5vis.osplot1d(ldata[:, ldata.shape[0] // 2], ax=self.outline_ax[1], title='') plt.suptitle( 'X Axis={:.2f}'.format(ldata.axes[0][ldata.shape[0] // 2]) + ', Y Axis={:.2f}'.format(ldata.axes[1][ldata.shape[1] // 2])) plt.title('Y lineout') plt.show() def toggle_lineout(self, change): if change['new']: # start a new thread so the interaction with original figure won't be blocked self.observer_thrd = threading.Thread( target=self.__new_lineout_plot) self.observer_thrd.daemon = True self.observer_thrd.start() # display(self.out) else: self.observer_thrd.join() # kill the thread Output.clear_output(self.out) def __handle_lognorm(self): if self.norm_selector.value[0] == LogNorm: self.__pp = np.abs self.vmax_wgt.value = np.max(np.abs(self._data)) self.__assgin_valid_vmin() else: self.vmax_wgt.value = np.max(self._data) self.__assgin_valid_vmin() self.__pp = self._idle def __update_norm_wgt(self, _change): """update tab1 (second tab) only and prepare _log_data if necessary""" tmp = [None] * len(self.tab_contents) tmp[0] = self.__get_tab0() self.refresh_tab_wgt(tmp) self.__handle_lognorm() def update_norm(self, *args): self.cb.remove() self.im.remove() self.im, self.cb = self.plot_data(im=self.im) # update norm self.set_norm(*args) def __get_norm(self): vmin = None if self.if_vmin_auto.value else self.norm_selector.value[ 1].children[0].children[0].value vmax = None if self.if_vmax_auto.value else self.vmax_wgt.value param = {'vmin': vmin, 'vmax': vmax, 'clip': self.if_clip_cm.value} if self.norm_selector.value[0] == PowerNorm: param['gamma'] = self.gamma.value if self.norm_selector.value[0] == SymLogNorm: param['linthresh'] = self.linthresh.value param['linscale'] = self.linscale.value return param def set_norm(self, *_): param = self.__get_norm() self.cb.set_norm(self.norm_selector.value[0](**param)) def __assgin_valid_vmin(self, v=None): # if it is log scale if self.norm_selector.value[0] == LogNorm: self.vlogmin_wgt.value = self.eps if v is None or v < self.eps else v else: self.vmin_wgt.value = np.min(self._data) if v is None else v def __update_vmin(self, _change): if self.if_vmin_auto.value: self.__assgin_valid_vmin() self.vmin_wgt.disabled = True self.vlogmin_wgt.disabled = True else: self.vmin_wgt.disabled = False self.vlogmin_wgt.disabled = False def __update_vmax(self, _change): if self.if_vmax_auto.value: self.vmax_wgt.value = np.max(self._data) self.vmax_wgt.disabled = True else: self.vmax_wgt.disabled = False def __update_title(self, *_): if self.if_reset_title.value: self.title.value = osh5vis.default_title( self._data, show_time=self.time_in_title) self.title.disabled = True else: self.title.disabled = False def __update_xlabel(self, *_): if self.if_reset_xlabel.value: self.xlabel.value = osh5vis.axis_format( self._data.axes[1].long_name, self._data.axes[1].units) self.xlabel.disabled = True else: self.xlabel.disabled = False def __update_ylabel(self, *_): if self.if_reset_ylabel.value: self.ylabel.value = osh5vis.axis_format( self._data.axes[0].long_name, self._data.axes[0].units) self.ylabel.disabled = True else: self.ylabel.disabled = False def __update_cbar(self, *_): if self.if_reset_cbar.value: self.cbar.value = self._data.units.tex() self.cbar.disabled = True else: self.cbar.disabled = False def __update_y_max(self, change): self.y_max_wgt.value = change['new'] if self.y_min_wgt.value < change['new'] < self._data.axes[0].max \ else self._data.axes[0].max def __update_x_max(self, change): self.x_max_wgt.value = change['new'] if self.x_min_wgt.value < change['new'] < self._data.axes[1].max \ else self._data.axes[1].max def __update_y_min(self, change): self.y_min_wgt.value = change['new'] if self._data.axes[0].min < change['new'] < self.y_max_wgt.value \ else self._data.axes[0].min def __update_x_min(self, change): self.x_min_wgt.value = change['new'] if self._data.axes[1].min < change['new'] < self.x_max_wgt.value \ else self._data.axes[1].min def __update_delta_y(self, change): if not (0 < round(change['new'] / self._data.axes[0].increment) <= self._data[self._slcs].shape[0]): self.y_step_wgt.value = self._data.axes[0].increment def __update_delta_x(self, change): if not (0 < round(change['new'] / self._data.axes[1].increment) <= self._data[self._slcs].shape[1]): self.x_step_wgt.value = self._data.axes[1].increment
def GOES_ABI_overlay_plot(cfg_d, ovr_d, odat, out_plot_name=None, var_label=None, fignum=10): """ make a GOES ABI overlay plot. This is function to integrate with the vistool plot data flow. inputs (all 3 are defined by the corresponding vistool functions) cfg_d: configuration dictionary ovr_d: overlay information dictionary odat: oco2 data dictionary returns a dictionary, containing all the axis objects created; this is generally most useful for testing or debugging. the dictionary contents are: gridspec: the gridspec object image_ax: the MPL axis object containing the image cb_ax: the MPL axis object for the colorbar; None if the colorbar was not created anno_axes: a list containing the 4 dummy axes where the lat/lon corner values are displayed inset_ax: the MPL axis object with the inset image """ # if there is overlay data present, use the OCO2 obs time # to get the data. # otherwise, use the datetime object if ovr_d: dt = datetime.datetime.utcfromtimestamp(np.mean(odat['time'])) else: dt = cfg_d['datetime'] center_lat = (cfg_d['lat_lr'] + cfg_d['lat_ul']) / 2.0 file_list, time_offsets = get_ABI_files(dt, center_lat, cfg_d['data_home'], cfg_d['sensor']) if len(file_list) == 0: raise ValueError('No ABI files were found for requested date in ' + cfg_d['data_home']) # convert the LL box corners (in degrees LL) to an extent box # [min_lon, min_lat, max_lon, max_lat] latlon_extent = [ cfg_d['lon_ul'], cfg_d['lat_lr'], cfg_d['lon_lr'], cfg_d['lat_ul'] ] scn = get_scene_obj(file_list, latlon_extent, resample_method=cfg_d['resample_method']) crs = scn['true_color'].attrs['area'].to_cartopy_crs() overlay_present = ovr_d is not None cbar_needed = overlay_present and ovr_d['cmap'] in plt.colormaps() gs, ax, cb_ax, inset_ax = setup_axes(fignum, crs, create_colorbar_axis=cbar_needed) im = plot_scene_obj(ax, scn) if overlay_present: if ovr_d['var_lims']: vmin, vmax = ovr_d['var_lims'] overlay_data(ax, cb_ax, odat, cmap=ovr_d['cmap'], vmin=vmin, vmax=vmax, var_label=var_label, alpha=ovr_d['alpha']) else: overlay_data(ax, cb_ax, odat, cmap=ovr_d['cmap'], var_label=var_label, alpha=ovr_d['alpha']) make_inset_map(inset_ax, cfg_d['geo_upper_left'], cfg_d['geo_lower_right']) mean_time_offset = np.mean(time_offsets) / 60.0 todays_date = datetime.datetime.now().strftime('%Y-%m-%d') if cfg_d['out_plot_title'] == 'auto': if ovr_d: title_string = ('Overlay data from {0:s}' + '\nBackground from {1:s}, ' + '\nOverlay time = {2:s}, ' + 'mean time offset = {3:4.1f} min., ' + 'plot created on {4:s}') title_string = title_string.format( os.path.split(ovr_d['var_file'])[1], os.path.split(file_list[1])[1], dt.strftime('%Y-%m-%d %H:%M:%S'), mean_time_offset, todays_date) else: title_string = ('Background from {0:s}' + '\n Request time = {1:s}, ' + 'mean time offset = {2:4.1f} min., ' + 'plot created on {3:s}') title_string = title_string.format( os.path.split(file_list[1])[1], dt.strftime('%Y-%m-%d %H:%M:%S'), mean_time_offset, todays_date) else: title_string = cfg_d['out_plot_title'] ax.set_title(title_string, size='x-small') ax.coastlines(resolution='10m', color='lightgray') anno_axes = annotate_ll_axes(gs, cfg_d['geo_upper_left'], cfg_d['geo_lower_right']) annotate_locations(ax, cfg_d) if out_plot_name: fig = plt.figure(fignum) fig.savefig(out_plot_name, dpi=150) ax_dict = dict(gridspec=gs, image_ax=ax, cb_ax=cb_ax, anno_axes=anno_axes, inset_ax=inset_ax) return ax_dict
def show_colormaps(subset="all", filename=None): """ Displays the colormaps available to yt. Note, most functions can use both the matplotlib and the native yt colormaps; however, there are some special functions existing within image_writer.py (e.g. write_image() write_bitmap(), etc.), which cannot access the matplotlib colormaps. In addition to the colormaps listed, one can access the reverse of each colormap by appending a "_r" to any map. If you wish to only see certain colormaps, include them in the cmap_list attribute. Parameters ---------- subset : string, or list of strings, optional valid values : "all", "yt_native", or list of cmap names default : "all" As mentioned above, a few functions can only access yt_native colormaps. To display only the yt_native colormaps, set this to "yt_native". If you wish to only see a few colormaps side by side, you can include them as a list of colormap names. Example: ['algae', 'gist_stern', 'kamae', 'spectral'] filename : string, opt default: None If filename is set, then it will save the colormaps to an output file. If it is not set, it will "show" the result interactively. """ from matplotlib import pyplot as plt a = np.outer(np.arange(0, 1, 0.01), np.ones(10)) if subset == "all": maps = [ m for m in plt.colormaps() if (not m.startswith("idl")) & (not m.endswith("_r")) ] elif subset == "yt_native": maps = [ m for m in _cm.color_map_luts if (not m.startswith("idl")) & (not m.endswith("_r")) ] else: try: maps = [m for m in plt.colormaps() if m in subset] if len(maps) == 0: raise AttributeError except AttributeError as e: raise AttributeError("show_colormaps requires subset attribute " "to be 'all', 'yt_native', or a list of " "valid colormap names.") from e if Version("2.0.0") <= MPL_VERSION < Version("2.2.0"): # the reason we do this filtering is to avoid spurious warnings in CI when # testing against old versions of matplotlib (currently not older than 2.0.x) # and we can't easily filter warnings at the level of the relevant test itself # because it's not yet run exclusively with pytest. # FUTURE: remove this completely when only matplotlib 2.2+ is supported deprecated_cmaps = { "spectral", "spectral_r", "Vega10", "Vega10_r", "Vega20", "Vega20_r", "Vega20b", "Vega20b_r", "Vega20c", "Vega20c_r", } for cmap in deprecated_cmaps: try: maps.remove(cmap) except ValueError: pass maps = sorted(set(maps)) # scale the image size by the number of cmaps plt.figure(figsize=(2.0 * len(maps) / 10.0, 6)) plt.subplots_adjust(top=0.7, bottom=0.05, left=0.01, right=0.99) l = len(maps) + 1 for i, m in enumerate(maps): plt.subplot(1, l, i + 1) plt.axis("off") plt.imshow(a, aspect="auto", cmap=plt.get_cmap(m), origin="lower") plt.title(m, rotation=90, fontsize=10, verticalalignment="bottom") if filename is not None: plt.savefig(filename, dpi=100, facecolor="gray") else: plt.show()
def plot_bad_fill_info(data_frame, x_data_label, y_data_label, z_data_label, title='', xlabel='', ylabel='', ymin=0.0, ymax=0.0, label_cms_status=True, energy_year_label='', mean=None, stdv=None, fig_size_shape='nsq', ratio_acceptance=0.01, filePath='', txtfileName="NoName"): fig_size = get_fig_size(fig_size_shape) fig, ax = plt.subplots(figsize=fig_size) x_data_list = list(data_frame[x_data_label]) y_data_list = list(data_frame[y_data_label]) z_data_list = list(data_frame[z_data_label]) data = pd.DataFrame() for i in range(0,len(x_data_list), 1): if str(x_data_list[i]) in data.keys(): data[str(x_data_list[i])][0] = data[str(x_data_list[i])][0] + y_data_list[i]*z_data_list[i] data[str(x_data_list[i])][1] = data[str(x_data_list[i])][1] + z_data_list[i] else: data[str(x_data_list[i])] = (y_data_list[i]*z_data_list[i], z_data_list[i]) total_lumi = 0 for i in list(data): data[i][0] = data[i][0]/data[i][1] total_lumi = total_lumi + data[i][1] x_data_reduced = [] y_data_mean = [] z_data_acum = [] ey = [] colors = [] for i in list(data): x_data_reduced.append(int(i)) y_data_mean.append(data[i][0]) z_data_acum.append(data[i][1]) ey.append(1000*data[i][1]/total_lumi) colors.append(100*data[i][1]/total_lumi) lumi_porcent = max(colors)*setts.lumisensitivity limratioUP = mean + stdv*ratio_acceptance limratioDOWN = mean - stdv*ratio_acceptance bad_fills = [] bad_fillsPos = [] for i in range(0,len(x_data_reduced), 1): if (y_data_mean[i] > limratioUP or y_data_mean[i] < limratioDOWN) and (100*z_data_acum[i]/total_lumi) > lumi_porcent: bad_fills.append(x_data_reduced[i]) bad_fillsPos.append((x_data_reduced[i],y_data_mean[i])) print(x_data_label + "s to analize", bad_fills) ##write file: ltools.check_and_create_folder(filePath) fileout = open(filePath + txtfileName + ".txt", "w+") for i in bad_fills: fileout.write(str(i) + ' ') fileout.close() sc = plt.scatter(x_data_reduced, y_data_mean, s=ey, c=colors, vmin=0, vmax=max(colors)) plt.plot(x_data_reduced, y_data_mean, linewidth=0.5, alpha=0.8, linestyle="--") plt.axhline(mean, color='black', lw=0.8, alpha=0.7, linestyle="--") plt.axhline(limratioUP, color='red', lw=0.8, alpha=0.7, linestyle="--") plt.axhline(limratioDOWN, color='red', lw=0.8, alpha=0.7, linestyle="--") for i, txt in enumerate(bad_fills): plt.annotate(txt, bad_fillsPos[i], xytext=(-25, 25), textcoords='offset points', ha='right', va='top', bbox=dict(boxstyle='round,pad=0.3', fc='yellow', alpha=0.5), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')) plt.colormaps() cbar = plt.colorbar(sc) cbar.set_label('% of the total integrated luminosity') # plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) if ymin != 0 and ymax != 0: plt.ylim(ymin, ymax) if label_cms_status: add_extra_text(ax, fig_size_shape, energy_year_label=energy_year_label, experiment=setts.experiment, work_status=setts.work_status) return fig
def _set_colors_for_categorical_obs(adata, value_to_plot, palette): """ Sets the adata.uns[value_to_plot + '_colors'] according to the given palette Parameters ---------- adata : annData object value_to_plot : name of a valid categorical observation palette : Palette should be either a valid `matplotlib.pyplot.colormaps()` string, a list of colors (in a format that can be understood by matplotlib, eg. RGB, RGBS, hex, or a cycler object with key='color' Returns ------- None """ from matplotlib.colors import to_hex from cycler import Cycler, cycler categories = adata.obs[value_to_plot].cat.categories # check is palette is a valid matplotlib colormap if isinstance(palette, str) and palette in pl.colormaps(): # this creates a palette from a colormap. E.g. 'Accent, Dark2, tab20' cmap = pl.get_cmap(palette) colors_list = [ to_hex(x) for x in cmap(np.linspace(0, 1, len(categories))) ] else: # check if palette is a list and convert it to a cycler, thus # it doesnt matter if the list is shorter than the categories length: if isinstance(palette, list): if len(palette) < len(categories): logg.warn( "Length of palette colors is smaller than the number of " "categories (palette length: {}, categories length: {}. " "Some categories will have the same color.".format( len(palette), len(categories))) # check that colors are valid from matplotlib.colors import is_color_like _color_list = [] for color in palette: if not is_color_like(color): # check if the color is a valid R color and translate it # to a valid hex color value if color in utils.additional_colors: color = utils.additional_colors[color] else: raise ValueError( "The following color value of the given palette is not valid: {}" .format(color)) _color_list.append(color) palette = cycler(color=_color_list) if not isinstance(palette, Cycler): raise ValueError( "Please check that the value of 'palette' is a " "valid matplotlib colormap string (eg. Set2), a " "list of color names or a cycler with a 'color' key.") if 'color' not in palette.keys: raise ValueError("Please set the palette key 'color'.") cc = palette() colors_list = [ to_hex(next(cc)['color']) for x in range(len(categories)) ] adata.uns[value_to_plot + '_colors'] = colors_list
# has that we missed. _mplcmaps = [ 'viridis', 'plasma', 'inferno', 'magma', 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn', 'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink', 'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper', 'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic', 'Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c', 'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv', 'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar' ] _mplcmaps = [cm for cm in _mplcmaps if cm in plt.colormaps()] _mplcmaps += [cm for cm in plt.colormaps() if cm not in _mplcmaps] class MPLPropCycler(object): def __init__(self, prop, options=[]): self._prop = prop self._options_orig = options self._options = options self._used = [] self._used_tmp = [] def __repr__(self): return '<{}cycler | cycle: {} | used: {}>'.format( self._prop, self.cycle, self.used)
def imshow_hs(data,ax=None,cmap='geosoft',cmap_norm='equalize',hs=True, zf=10,azdeg=45,altdeg=45,dx=1,dy=1,fraction=1.5,blend_mode='alpha', alpha=0.7,contours=False,levels=32,colorbar=True,cb_contours=False, cb_ticks='linear',nSigma=1,**kwargs): ''' Display an array with optional hillshading and contours. The colormap can be normalised by equalisation or by clipping extremes (autolevels). Parameters ---------- data : 2D array Grid to plot. Arrays with NaNs and masked arrays are supported. ax : matplotlib axes instance This indicates where to draw the figure. Create new figure if absent. cmap : string Name of the colormap to use to display the array. The default 'geosoft' is the blue to pink clra colormap from Geosoft Oasis Montaj. cmap_norm : string Type of normalisation of the colormap. Possible values are: 'equalize' (or 'equalization') Increases contrast by distributing intensities across all the possible colours. With this option, it is not the data that is normalised but the colormap, based on the data. 'auto' (or 'autolevels') Stretches the histogram of the colormap so that dark colours become darker and the bright colours become brighter. Two extra parameters control the amount of clipping at the extremes: minPercent (default to 10%) and maxPercent (default to 90%) hs : boolean If True, the array is displayed in colours over a grey hillshaded version of the data. zf : number Vertical exaggeration (Z factor) for hillshading. azdeg : number The azimuth (0-360, degrees clockwise from North) of the light source. altdeg : number The altitude (0-90, degrees up from horizontal) of the light source. dx : number, optional cell size in the x direction dy : number, optional cell size in the y direction fraction : number Increases or decreases the contrast of the hillshade. blend_mode : {'alpha', 'hsv', 'overlay', 'soft'} The type of blending used to combine the colormapped data values with the illumination intensity. Default is 'alpha' and the effect is controlled by the alpha parameter. alpha : float Controls the transparency of the data overlaid over the hillshade. 1.0 is fully opaque while 0.0 is fully transparent. contours : Boolean If True, adds contours to the map. The number of calculated contours is defined by: levels : integer Number of contour levels. colorbar : Boolean If True, draw a colorbar on the right-hand side of the map. The colorbar shows the distribution of colors, as modified by the normalization algorithm. cb_ticks : string If left as default ('linear') the ticks and labels on the colorbar are spaced linearly in the standard way. Otherwise (any other keyword, for example 'stats'), the mean and two ticks at + and - nSigma*(standard deviation) are shown instead. nSigma : integer (default is 1) Size of the interval to show between ticks on the colorbar. cb_contours : Boolean Add lines corresponding to contours on the colorbar. kwargs : other optional arguments Can be used to pass other arguments to imshow, such as 'origin' and 'extent'. Notes ----- This function exploits the hillshading capabilities implemented in matplotlib.colors.LightSource. It adds additional blending mode (alpha compositing, see https://en.wikipedia.org/wiki/Alpha_compositing) and normalising functions for the data (equalization). ''' # modify colormap if required if cmap_norm in ['equalize','equalization']: # histogram equalization cdf, bins = exposure.cumulative_distribution(data[~np.isnan(data)].flatten(),nbins=256) my_cmap = equalizeColormap(cmap,bins,cdf) elif cmap_norm in ['auto','autolevels']: # autolevels minP = kwargs.pop('minPercent',10) # also removes the key from the dictionary maxP = kwargs.pop('maxPercent',90) my_cmap = normalizeColormap(cmap,norm='autolevels',minPercent=minP,maxPercent=maxP) elif cmap in plt.colormaps(): # colormap defined as string (recognised name) my_cmap = plt.get_cmap(cmap) else: # colormap is one of the extra ones added by the colors module my_cmap = load_cmap(cmap) # raises error if not recognised # create figure or retrieve the one already there if ax: fig = ax.get_figure() else: fig,ax = plt.subplots(figsize=(8,8)) # convert input data to masked array data = np.ma.masked_array(data, np.isnan(data)) # add array to figure with hillshade or not if hs: # flip azimuth upside down if grid is also flipped if 'origin' in kwargs: if kwargs['origin'] == 'lower': azdeg = 180 - azdeg # extract keyword arguments that can be passed to ls.shade kwargs_norm = {} kwargs_norm['norm'] = kwargs.get('norm') kwargs_norm['vmin'] = kwargs.get('vmin') kwargs_norm['vmax'] = kwargs.get('vmax') # create light source ls = mcolors.LightSource(azdeg, altdeg) # calculate hillshade and combine the colormapped data with the intensity if alpha == 0: # special case when only the shaded relief is needed without blending rgb = ls.hillshade(data,vert_exag=zf,dx=dx,dy=dy,fraction=fraction) kwargs['cmap'] = 'gray' elif blend_mode == 'alpha': # transparency blending rgb = ls.shade(data,cmap=my_cmap,blend_mode=alpha_blend,vert_exag=zf,dx=dx,dy=dy, fraction=fraction,alpha=alpha,**kwargs_norm) else: # other blending modes from matplotlib function rgb = ls.shade(data,cmap=my_cmap,blend_mode=blend_mode,vert_exag=zf,dx=dx,dy=dy, fraction=fraction,**kwargs_norm) ax.imshow(rgb,**kwargs) else: # display data without hillshading im = ax.imshow(data,cmap=my_cmap,**kwargs) # add contours if contours: ct = plt.contour(data,levels,linewidths=0.5,colors='k',linestyles='solid',**kwargs) # add colorbar if colorbar and alpha != 0: if hs: # Use a proxy artist for the colorbar im = ax.imshow(data,cmap=my_cmap,**kwargs) im.remove() if cb_ticks=='linear': # normal equidistant ticks on a linear scale cb1 = fig.colorbar(im,shrink=0.8) else: # show ticks at min, max, mean and standard deviation interval newTicks = stats_boundaries(data,nSigma,nSigma) cb1 = fig.colorbar(im,shrink=0.8,ticks=newTicks) # add optional contour lines on colorbar if contours and cb_contours: cb1.add_lines(ct) cb1.update_normal(im) # final show plt.show()
def plotResListData(resList, plot_types=['IQ'], **kwargs): r"""Plot resonator data and fits. Parameters ---------- resList : list-like A list of ``pyres.Resonator`` objects. A single ``Resonator`` object can be passed, as long as it is in a list. plot_types : list, optional A list of plots to create, each one specified by a string. Possible plot types are: - 'IQ': Plots the real part of the transmission (`I`) vs the imaginary part (`Q`). This is the default plot. - 'rIQ': Plots the residual of `I` vs the residual of `Q`. This plot is only available if the ``do_lmfit`` method of each ``Resonator`` object has been called. The `I` and `Q` residuals are normalized by the uncertainty of the `I` and `Q` data respectively. If this is not explicitly supplied, it is calculated by taking the standard deviation of the first 10 data points. - 'LinMag': Plots the magnitude of the tranmission in Volts vs frequency. - 'LogMag': Plots the magnitude of the transmission in dB vs frequency. ``LogMag = 20*np.log10(LinMag)``. - 'rMag': Plots the difference of `LinMag` and the best-fit magnitude vs frequency. This plot is only available if the ``do_lmfit`` method of each ``Resonator`` object has been called. - 'Phase': Plots the phase of the transmision vs frequency. ``Phase = np.arctan2(Q, I)``. - 'rPhase': Plots the difference of `Phase` and the best-fit phase vs frequency. This plot is only available if the ``do_lmfit`` method of each ``Resonator`` object has been called. - 'uPhase': Plots the unwrapped phase vs frequency. ``uPhase = np.unwrap(Phase)``. - 'ruPhase': Plots the difference of `uPhase` and the unwrapped best-fit phase vs frequency. This plot is only available if the ``do_lmfit`` method of each ``Resonator`` object has been called. - 'I': Plots the real part of the transmission vs frequency. - 'rI': Plots the residual of `I` vs frequency. The residual is weighted by the uncertainty in `I`. This plot is only available if the ``do_lmfit`` method of each ``Resonator`` object has been called. - 'Q': Plots the imaginary part of the transmission vs frequency. - 'rQ': Plots the residual of `Q` vs frequency. The residual is weighted by the uncertainty in `Q`. This plot is only available if the ``do_lmfit`` method of each ``Resonator`` object has been called. Keyword Arguments ----------------- plot_fits : list-like, optional A list of boolean flags, one for each plot type specified. Determines whether or not to overplot the best fit on the data. This is only effective if the ``do_lmfit`` method of each ``Resonator`` object has been called. Default is all False. powers : list-like, optional A list of power values to plot. Default is to plot all of the unique powers that exist in the list of ``Resonator`` objects. max_pwr : float An upper bound on the powers to plot. Applies to values passed to powers also. min_pwr : float A lower bound on the powers to plot. Applies to values passed to powers also. temps : list-like, optional A list of temperature values to plot. Default is to plot all of the unique temperatures that exist in the list of ``Resonator`` obejcts. max_temp : float An upper bound on the temperatures to plot. Applies to values passed to temps also. min_temp : float A lower bound on the temperatures to plot. Applies to values passed to temps also. use_itemps : {False, True}, optional If a ``ResonatorSweep`` object has been generated from the resList it may have added the ``itemp`` attrubute to each ``ResonatorObject`` in the list. Specifying ``use_itemps = True`` will force the plotting routine to use those tempeartures. freq_units : {'GHz', 'Hz', 'kHz', 'MHz', 'THz'}, optional The units for the frequency axis, if it exists. Defaul is 'GHz'. detrend_phase : {False, True}, optional Whether or not to remove a linear trend from the `Phase` data. A typical reason for a steep linear offset in the phase is an uncorrected electrical delay due to long transmission lines. x_slice : slice, optional Pass in a slice object to index the results to only look at a portion of each resonator data. Default is to show all data. num_cols : int, optional The number of columns to include in the grid of subplots. Default is 1. fig_size : int, optional The size of an individual subplot in inches. Default is 3. force_square : {False, True}, optional Whether or not to force each subplot axis to be perfectly square. show_colorbar : {True, False}, optional Whether or not to add a colorbar to the right edge of the figure. The colorbar will correspond to the limits of the colored data. Default is True. color_by : {'temps', 'pwrs'}, optional If multiple temperatures and multiple powers are passed, this selects which variable will set the color of the plots. Default is 'temps'. color_map : str, optional The name of any colormap returned by calling ``matplotlib.pyplot.colormaps()`` is a valid option. Default is 'coolwarm'. waterfall : numeric, optional The value used to space out LogMag vs frequency plots. Currently only available for LogMag. Default is 0. plot_kwargs : dict, optional A dict of keyword arguments to pass through to the individual plots. Attempting to set 'color' will result in an error. fit_kwargs : dict, optional A dict of keyword arguments to pass through to the fit plots. Default is a dashed black line. Returns ------- figS : ``matplotlib.pyplot.figure`` A ``matplotlib.pyplot`` figure object. """ #TODO: Add temperature and power masking that makes more sense, like the ability #to set a temperature range, or maybe decimate the temperature data. Also #need to add the ability to waterfall the mag and phase plots. supported_types = ['IQ', 'rIQ', 'LogMag', 'LinMag', 'rMag', 'Phase', 'rPhase', 'uPhase', 'ruPhase', 'I', 'rI', 'Q', 'rQ'] assert all(plt_key in supported_types for plt_key in plot_types), "Unsupported plotType requested!" #Get a list of unique temps and powers powers = [] temps = [] #Should we use itemps? use_itemps = kwargs.pop('use_itemps', False) if use_itemps: assert all(hasattr(res, 'itemps') for res in resList), "Could not locate itemp for at least one resonator!" for res in resList: powers.append(res.pwr) if use_itemps: temps.append(res.itemp) else: temps.append(res.temp) powers = np.unique(powers) temps = np.unique(temps) #Optionally override either list powers = np.asarray(kwargs.pop('powers', powers)) temps = np.asarray(kwargs.pop('temps', temps)) #Get min/max pwrs/temps max_pwr = kwargs.pop('max_pwr', np.max(powers)+1) min_pwr = kwargs.pop('min_pwr', np.min(powers)-1) max_temp = kwargs.pop('max_temp', np.max(temps)+1) min_temp = kwargs.pop('min_temp', 0.0) #Enforce bounds powers = powers[np.where(np.logical_and(powers < max_pwr, powers > min_pwr))] temps = temps[np.where(np.logical_and(temps < max_temp, temps > min_temp))] #Get the slice x_slice = kwargs.pop('x_slice', slice(None)) #Should we plot best fits? plot_fits = kwargs.pop('plot_fits', [False]*len(plot_types)) assert len(plot_fits) == len(plot_types), "Must specify a fit bool for each plot type." if any(plot_fits): assert all(res.hasFit for res in resList), "At least one resonator has not been fit yet." #Set the units for the frequency axes freq_units = kwargs.pop('freq_units', 'GHz') assert freq_units in ['Hz', 'kHz', 'MHz', 'GHz', 'THz'], "Unsupported units request!" unitsDict = {'Hz':1, 'kHz':1e3, 'MHz':1e6, 'GHz':1e9, 'THz':1e12} #A spacing value to apply to magnitude vs. frequency plots. waterfall = kwargs.pop('waterfall', 0) #Remove linear phase variation? Buggy... detrend_phase = kwargs.pop('detrend_phase', False) #Set some plotting defaults num_cols = kwargs.pop('num_cols', 1) fig_size = kwargs.pop('fig_size', 3) force_square = kwargs.pop('force_square', False) show_colorbar = kwargs.pop('show_colorbar', True) #Should the temperatures or the powers iterate the colors? color_by = kwargs.pop('color_by', 'temps') assert color_by in ['temps', 'pwrs'], "color_by must be 'temps' or 'pwrs'." #Set the colormap: Default to a nice red/blue thing #TODO: Allow for custom colormaps (like from Seaborn, etc) color_map = kwargs.pop('color_map', 'coolwarm') assert color_map in plt.colormaps(), "Unknown colormap provided" color_gen = plt.get_cmap(color_map) #Any extra kwargs for plotting plot_kwargs = kwargs.pop('plot_kwargs', {}) fit_kwargs = kwargs.pop('fit_kwargs', {'color' : 'k', 'linestyle' : '--', 'linewidth' : 1.5}) if kwargs: raise NameError("Unknown keyword argument: " + kwargs.keys()[0]) #Set up the figure figS = plt.figure() #Calculate rows for figure size num_rows = int(np.ceil(1.0*len(plot_types)/num_cols)) #Set figure size, including some extra spacing for the colorbar #0.1 is the extra space for the colorbar. #*1.2 is the extra padding for the axis labels #15:1 is the ratio of axis width for regular axes to colorbar axis if show_colorbar: figS.set_size_inches(fig_size*(num_cols+0.1)*1.2, fig_size*num_rows) #Initialize the grid for plotting plt_grid = gs.GridSpec(num_rows, num_cols+1, width_ratios=[15]*num_cols+[1]) else: figS.set_size_inches(fig_size*(num_cols)*1.2, fig_size*num_rows) #Initialize the grid for plotting plt_grid = gs.GridSpec(num_rows, num_cols) #Initialize a dictionary of axes corresponding to plot types axDict = {} #Set up axes and make labels for ix, key in enumerate(plot_types): iRow = ix//num_cols iCol = ix%num_cols ax = figS.add_subplot(plt_grid[iRow, iCol]) if key == 'IQ': ax.set_xlabel('I (Volts)') ax.set_ylabel('Q (Volts)') if key == 'rIQ': ax.set_xlabel('Residual of I / $\sigma_\mathrm{I}$') ax.set_ylabel('Residual of Q / $\sigma_\mathrm{Q}$') if key in ['LogMag', 'LinMag', 'rMag', 'Phase', 'rPhase', 'uPhase', 'ruPhase', 'I', 'Q', 'rQ', 'rI']: ax.set_xlabel('Frequency (' + freq_units + ')') if key == 'LinMag': ax.set_ylabel('Magnitude (Volts)') if key == 'LogMag': ax.set_ylabel('Magnitude (dB)') if key == 'rMag': ax.set_ylabel('Residual of Magnitude (Volts)') if key == 'Phase': ax.set_ylabel('Phase (Radians)') if key == 'rPhase': ax.set_ylabel('Residual of Phase (Radians)') if key == 'uPhase': ax.set_ylabel('Unwrapped Phase (Radians)') if key == 'ruPhase': ax.set_ylabel('Residual of unwrapped Phase (Radians)') if key == 'I': ax.set_ylabel('I (Volts)') if key == 'Q': ax.set_ylabel('Q (Volts)') if key == 'rI': ax.set_ylabel('Residual of I / $\sigma_\mathrm{I}$') if key == 'rQ': ax.set_ylabel('Residual of Q / $\sigma_\mathrm{Q}$') #Stuff the axis into the axis dictionary axDict[key] = ax #Plot the data for pwr in powers: #The waterfall index should be reset each iteration wix = 0 for temp in temps: #Grab the right resonator from the list resIndex = indexResList(resList, temp, pwr, itemp=use_itemps) #Color magic! if color_by == 'temps': if len(temps) > 1: plt_color = color_gen(temp*1.0/max(temps)) else: plt_color = color_gen(0) elif color_by == 'pwrs': if len(powers) > 1: plt_color = color_gen(1-((max(powers)-pwr)*1.0/(max(powers)-min(powers)))) else: plt_color = color_gen(0) #Not every temp/pwr combo corresponds to a resonator. Ignore missing ones. if resIndex is not None: res = resList[resIndex] scaled_freq = res.freq/unitsDict[freq_units] for key, ax in axDict.items(): pix = plot_types.index(key) plot_fit = plot_fits[pix] if key == 'IQ': ax.plot(res.I[x_slice], res.Q[x_slice], color=plt_color, **plot_kwargs) if plot_fit: ax.plot(res.resultI[x_slice], res.resultQ[x_slice], **fit_kwargs) if key == 'rIQ': ax.plot(res.residualI[x_slice], res.residualQ[x_slice], color=plt_color, **plot_kwargs) if key == 'LogMag': ax.plot(scaled_freq[x_slice], res.logmag[x_slice]+wix*waterfall, color=plt_color, **plot_kwargs) if plot_fit: ax.plot(scaled_freq[x_slice], 20*np.log10(res.resultMag[x_slice])+wix*waterfall, **fit_kwargs) #Step the waterfall plot wix+=1 if key == 'LinMag': ax.plot(scaled_freq[x_slice], res.mag[x_slice], color=plt_color, **plot_kwargs) if plot_fit: ax.plot(scaled_freq[x_slice], res.resultMag[x_slice], **fit_kwargs) if key == 'rMag': ax.plot(scaled_freq[x_slice], res.resultMag[x_slice]-res.mag[x_slice], color=plt_color, **plot_kwargs) if key == 'Phase': if detrend_phase: ax.plot(scaled_freq[x_slice], sps.detrend(res.phase[x_slice]), color=plt_color, **plot_kwargs) if plot_fit: ax.plot(scaled_freq[x_slice], sps.detrend(res.resultPhase[x_slice]), **fit_kwargs) else: ax.plot(scaled_freq[x_slice], res.phase[x_slice], color=plt_color, **plot_kwargs) if plot_fit: ax.plot(scaled_freq[x_slice], res.resultPhase[x_slice], **fit_kwargs) if key == 'rPhase': ax.plot(scaled_freq[x_slice], res.resultPhase[x_slice]-res.phase[x_slice], color=plt_color, **plot_kwargs) if key == 'uPhase': if detrend_phase: ax.plot(scaled_freq[x_slice], sps.detrend(res.uphase[x_slice]), color=plt_color, **plot_kwargs) if plot_fit: ax.plot(scaled_freq[x_slice], sps.detrend(np.unwrap(res.resultPhase[x_slice])), **fit_kwargs) else: ax.plot(scaled_freq[x_slice], res.uphase[x_slice], color=plt_color, **plot_kwargs) if plot_fit: ax.plot(scaled_freq[x_slice], np.unwrap(res.resultPhase[x_slice]), **fit_kwargs) if key == 'ruPhase': ax.plot(scaled_freq[x_slice], np.unwrap(res.resultPhase[x_slice])-res.uphase[x_slice], color=plt_color, **plot_kwargs) if key == 'I': ax.plot(scaled_freq[x_slice], res.I[x_slice], color=plt_color, **plot_kwargs) if plot_fit: ax.plot(scaled_freq[x_slice], res.resultI[x_slice], **fit_kwargs) if key == 'rI': ax.plot(scaled_freq[x_slice], res.residualI[x_slice], color=plt_color, **plot_kwargs) if key == 'Q': ax.plot(scaled_freq[x_slice], res.Q[x_slice], color=plt_color, **plot_kwargs) if plot_fit: ax.plot(scaled_freq[x_slice], res.resultQ[x_slice], **fit_kwargs) if key == 'rQ': ax.plot(scaled_freq[x_slice], res.residualQ[x_slice], color=plt_color, **plot_kwargs) plt.setp(ax.xaxis.get_majorticklabels(), rotation=45) if force_square: #Make the plot a square x1, x2 = ax.get_xlim() y1, y2 = ax.get_ylim() #Explicitly passing a float to avoid a warning in matplotlib #when it gets a numpy.float64 ax.set_aspect(float((x2-x1)/(y2-y1))) #Add the colorbar if show_colorbar: #Set limits for the min and max colors if color_by == 'temps': cbar_norm= mpl.colors.Normalize(vmin=0, vmax=max(temps)) cbar_units = 'Kelvin' elif color_by == 'pwrs': cbar_norm = mpl.colors.Normalize(vmin=min(powers), vmax=max(powers)) cbar_units = 'dB' #Make an axis that spans all rows for the colorbar cax = figS.add_subplot(plt_grid[:, num_cols]) #Plot and label the colorbar cbar_plot = mpl.colorbar.ColorbarBase(cax, cmap=color_gen, norm=cbar_norm) cbar_plot.set_label(cbar_units) #Clean up the subfigs and make sure nothing overlaps figS.tight_layout() return figS
def mean_intensity_plotter(ax, dataframe, title="Mean intensities", xlabel="Frames", ylabel="Mean Intensity", cmap=None): """ This will plot mean intensities for ROIS' of the labeled array for different image sets. Parameters ---------- ax : list of Axes List of `Axes` objects. Should have length == len(df) dataframe : pd.Dataframe The dataframe that has columns as different datasets (probably for different locations in the same sample) and rows as ROIs title : str, optional title of the plot x_label : str, optional x axis label y_label : str, optional y axis label color_cycle : str Matplotlib string name of colormap (see matplotlib.pyplot.colormaps() for a list of valid colormaps on your machine) Returns ------- artists : pd.DataFrame Pandas DataFrame whose column/row names match the input `dataframe` Examples -------- """ if cmap is None: # TODO don't use viridis in production, yet... if 'viridis' in plt.colormaps(): cmap = 'viridis' else: cmap = 'rainbow' cmap = plt.get_cmap(cmap) ax[-1].set_xlabel(xlabel) # capture the artists in a nested dictionary artists = {col_name: {} for col_name in dataframe} # compute the offset for each column offsets = [] cur = 0 prev = 0 # determine how far to offset each data set for data in dataframe.ix[0]: cur = prev + len(data) offsets.append((prev, cur)) prev = cur # loop over the rows of the dataframe for idx, row_label in enumerate(dataframe.index): # do some axes housekeeping ax[idx].set_ylabel(ylabel) ax[idx].set_title(title + ' for %s' % row_label) row = dataframe.ix[row_label] # loop over the columns of the dataframe, creating each line plot # one at a time for idx2, (column_name, color_idx) in enumerate(zip( dataframe, np.arange(0, 1, 1/len(row)))): x = range(*offsets[idx2]) y = row.ix[column_name] art, = ax[idx].plot(x, y, label=column_name, color=cmap(color_idx)) # store the artists in a nested dictionary artists[column_name][row_label] = art # enable the legend for each plot after all data has been added ax[idx].legend() return pd.DataFrame(artists)
def main(): global data global wave_fish global load_spec global data_path # command line arguments: parser = argparse.ArgumentParser(add_help=False, description='View and explore properties of EOD waveforms.', epilog='version %s by Benda-Lab (2019-%s)' % (__version__, __year__)) parser.add_argument('-h', '--help', nargs=0, action=PrintHelp, help='show this help message and exit') parser.add_argument('--version', action='version', version=__version__) parser.add_argument('-l', dest='list_columns', action='store_true', help='list all available data columns and exit') parser.add_argument('-j', dest='jobs', nargs='?', type=int, default=None, const=0, help='number of jobs run in parallel for loading waveform data. Without argument use all CPU cores.') parser.add_argument('-D', dest='column_groups', default=[], type=str, action='append', choices=['all', 'allpower', 'noise', 'timing', 'ampl', 'relampl', 'power', 'relpower', 'phase', 'time', 'width', 'none'], help='default selection of data columns, check them with the -l option') parser.add_argument('-d', dest='add_data_cols', action='append', default=[], metavar='COLUMN', help='data columns to be appended or removed (if already listed) for analysis') parser.add_argument('-n', dest='max_harmonics', default=0, type=int, metavar='MAX', help='maximum number of harmonics or peaks to be used') parser.add_argument('-w', dest='add_waveforms', default=[], type=str, action='append', choices=['first', 'second', 'ampl', 'power', 'phase'], help='add first or second derivative of EOD waveform, or relative amplitude, power, or phase to the plot of selected EODs.') parser.add_argument('-s', dest='save_pca', action='store_true', help='save PCA components and exit') parser.add_argument('-c', dest='color_col', default='EODf', type=str, metavar='COLUMN', help='data column to be used for color code or "index"') parser.add_argument('-m', dest='color_map', default='jet', type=str, metavar='CMAP', help='name of color map') parser.add_argument('-p', dest='data_path', default='.', type=str, metavar='PATH', help='path to the analyzed EOD waveform data') parser.add_argument('-P', dest='rawdata_path', default='.', type=str, metavar='PATH', help='path to the raw EOD recordings') parser.add_argument('-f', dest='format', default='auto', type=str, choices=TableData.formats + ['same'], help='file format used for saving PCA data ("same" uses same format as input file)') parser.add_argument('file', default='', type=str, help='a wavefish.* or pulsefish.* summary file as generated by collectfish') args = parser.parse_args() # read in command line arguments: list_columns = args.list_columns jobs = args.jobs file_name = args.file column_groups = args.column_groups add_data_cols = args.add_data_cols max_harmonics = args.max_harmonics add_waveforms = args.add_waveforms save_pca = args.save_pca color_col = args.color_col color_map = args.color_map data_path = args.data_path rawdata_path = args.rawdata_path data_format = args.format # read configuration: cfgfile = __package__ + '.cfg' cfg = ConfigFile() add_eod_quality_config(cfg) add_write_table_config(cfg, table_format='csv', unit_style='row', align_columns=True, shrink_width=False) cfg.load_files(cfgfile, file_name, 3) # output format: if data_format == 'same': ext = os.path.splitext(file_name)[1][1:] if ext in TableData.ext_formats: data_format = TableData.ext_formats[ext] else: data_format = 'dat' if data_format != 'auto': cfg.set('fileFormat', data_format) # check color map: if not color_map in plt.colormaps(): parser.error('"%s" is not a valid color map' % color_map) # load summary data: wave_fish = 'wave' in file_name data = TableData(file_name) # basename: basename = os.path.splitext(os.path.basename(file_name))[0] # check quality: skipped = 0 for r in reversed(range(data.rows())): idx = 0 if 'index' in data: idx = data[r,'index'] clipped = 0.0 if 'clipped' in data: clipped = 0.01*data[r,'clipped'] skips = '' if wave_fish: harm_rampl = np.array([data[r,'relampl%d'%(k+1)] for k in range(3)]) skips, msg = wave_quality(idx, clipped, 0.01*data[r,'noise'], 0.01*data[r,'rmserror'], data[r,'power'], 0.01*harm_rampl, **wave_quality_args(cfg)) else: skips, msg = pulse_quality(idx, clipped, 0.01*data[r,'noise'], **pulse_quality_args(cfg)) if len(skips) > 0: print('skip fish %d from %s: %s' % (idx, data[r,'file'], skips)) del data[r,:] skipped += 1 if skipped > 0: print('') # add species column (experimental): if wave_fish: # wavefish species: species = np.zeros(data.rows(), object) species[(data[:,'phase1'] < 0) & (data[:,'EODf'] < 300.0)] = 'Sterno' species[(data[:,'phase1'] < 0) & (data[:,'EODf'] > 300.0)] = 'Eigen' species[data[:,'phase1'] > 0] = 'Aptero' data.append('species', '', '%d', species) if wave_fish: # maximum number of harmonics: if max_harmonics == 0: max_harmonics = 40 else: max_harmonics += 1 for k in range(1, max_harmonics): if not ('phase%d' % k) in data: max_harmonics = k break else: # minimum number of peaks: min_peaks = -10 for k in range(1, min_peaks, -1): if not ('P%dampl' % k) in data or not np.all(np.isfinite(data[:,'P%dampl' % k])): min_peaks = k+1 break # maximum number of peaks: if max_harmonics == 0: max_peaks = 20 else: max_peaks = max_harmonics + 1 for k in range(1, max_peaks): if not ('P%dampl' % k) in data or not np.all(np.isfinite(data[:,'P%dampl' % k])): max_peaks = k break # default columns: group_cols = ['EODf'] if len(column_groups) == 0: column_groups = ['all'] for group in column_groups: if group == 'none': group_cols = [] elif wave_fish: if group == 'noise': group_cols.extend(['noise', 'rmserror', 'p-p-amplitude', 'power']) elif group == 'timing' or group == 'time': group_cols.extend(['peakwidth', 'p-p-distance', 'leftpeak', 'rightpeak', 'lefttrough', 'righttrough']) elif group == 'ampl': for k in range(0, max_harmonics): group_cols.append('ampl%d' % k) elif group == 'relampl': for k in range(1, max_harmonics): group_cols.append('relampl%d' % k) elif group == 'relpower' or group == 'power': for k in range(1, max_harmonics): group_cols.append('relpower%d' % k) elif group == 'phase': for k in range(1, max_harmonics): group_cols.append('phase%d' % k) elif group == 'all': for k in range(1, max_harmonics): group_cols.append('relampl%d' % k) group_cols.append('phase%d' % k) elif group == 'allpower': for k in range(1, max_harmonics): group_cols.append('relampl%d' % k) group_cols.append('relpower%d' % k) group_cols.append('phase%d' % k) else: parser.error('"%s" is not a valid data group for wavefish' % group) else: # pulse fish if group == 'noise': group_cols.extend(['noise', 'p-p-amplitude', 'min-ampl', 'max-ampl']) elif group == 'timing': group_cols.extend(['tstart', 'tend', 'width', 'tau', 'firstpeak', 'lastpeak']) elif group == 'power': group_cols.extend(['peakfreq', 'peakpower', 'poweratt5', 'poweratt50', 'lowcutoff']) elif group == 'time': for k in range(min_peaks, max_peaks): if k != 1: group_cols.append('P%dtime' % k) elif group == 'ampl': for k in range(min_peaks, max_peaks): group_cols.append('P%dampl' % k) elif group == 'relampl': for k in range(min_peaks, max_peaks): if k != 1: group_cols.append('P%drelampl' % k) elif group == 'width': for k in range(min_peaks, max_peaks): if k != 1: group_cols.append('P%dwidth' % k) elif group == 'all': for k in range(min_peaks, max_peaks): if k != 1: group_cols.append('P%drelampl' % k) group_cols.append('P%dtime' % k) group_cols.append('P%dwidth' % k) group_cols.extend(['tau', 'peakfreq', 'poweratt5']) else: parser.error('"%s" is not a valid data group for pulsefish' % group) # additional data columns: group_cols.extend(add_data_cols) # translate to indices: data_cols = [] for c in group_cols: idx = data.index(c) if idx is None: parser.error('"%s" is not a valid data column' % c) elif idx in data_cols: data_cols.remove(idx) else: data_cols.append(idx) # color code: color_idx = data.index(color_col) colors = None color_label = None if color_idx is None and color_col != 'index': parser.error('"%s" is not a valid column for color code' % color_col) if color_idx is None: colors = -2 elif color_idx in data_cols: colors = data_cols.index(color_idx) else: if len(data.unit(color_idx)) > 0 and not data.unit(color_idx) in ['-', '1']: color_label = '%s [%s]' % (data.label(color_idx), data.unit(color_idx)) else: color_label = data.label(color_idx) colors = data[:,color_idx] # list columns: if list_columns: for k, c in enumerate(data.keys()): s = [' '] * 3 if k in data_cols: s[1] = '*' if k == color_idx: s[0] = 'C' print(''.join(s) + c) parser.exit() # load waveforms: load_spec = 'ampl' in add_waveforms or 'power' in add_waveforms or 'phase' in add_waveforms if jobs is not None: cpus = cpu_count() if jobs == 0 else jobs p = Pool(cpus) eod_data = p.map(load_waveform, range(data.rows())) del p else: eod_data = list(map(load_waveform, range(data.rows()))) # explore: eod_expl = EODExplorer(data, data_cols, wave_fish, eod_data, add_waveforms, load_spec, rawdata_path, cfg) # write pca: if save_pca: eod_expl.compute_pca(False) eod_expl.save_pca(basename, False, **write_table_args(cfg)) eod_expl.compute_pca(True) eod_expl.save_pca(basename, True, **write_table_args(cfg)) else: eod_expl.set_colors(colors, color_label, color_map) eod_expl.show()
def scatter_plot(self, **kwargs): """ Plots correlation in matplotlib plot Parameters ---------- traces: dict Data for the plot, with the format: traces = {1: {'devices': ['10751', '10751'], 'channels': ['TEMP', 'GB_2A'], 'subplot': 1}, 2: {'devices': ['10752', '10752'], 'channels': ['TEMP', 'GB_2A'], 'subplot': 1} 3: {'devices': ['10751', '10751'], 'channels': ['TEMP', 'GB_2W'], 'subplot': 2} 4: {'devices': ['10752', '10752'], 'channels': ['TEMP', 'GB_2W'], 'subplot': 2} } options: dict Options including data processing prior to plot. Defaults in config._plot_def_opt formatting: dict Formatting dict. Defaults in config._scatter_plot_def_fmt Returns ------- Matplotlib figure and axes """ if config.framework == 'jupyterlab': plt.ioff() plt.clf() if 'traces' not in kwargs: std_out('No traces defined', 'ERROR') return None else: traces = kwargs['traces'] if 'options' not in kwargs: std_out('Using default options') options = config._plot_def_opt else: options = dict_fmerge(config._plot_def_opt, kwargs['options']) if 'formatting' not in kwargs: std_out('Using default formatting') formatting = config._scatter_plot_def_fmt['mpl'] else: formatting = dict_fmerge(config._scatter_plot_def_fmt['mpl'], kwargs['formatting']) # Style if formatting['style'] is not None: style.use(formatting['style']) else: style.use(config._plot_style) # Palette if formatting['palette'] is not None: set_palette(formatting['palette']) # Font size if formatting['fontsize'] is not None: rcParams.update({'font.size': formatting['fontsize']}) # Make it standard ptraces = dict() for trace in traces: if 'subplot' not in traces[trace]: traces[trace]['subplot'] = 1 if 'channels' not in traces[trace]: ptraces = traces continue ptrace_1 = trace * 10 + 1 ptrace_2 = trace * 10 + 2 ptraces[ptrace_1] = { 'devices': traces[trace]['devices'][0], 'channel': traces[trace]['channels'][0], 'subplot': traces[trace]['subplot'] } ptraces[ptrace_2] = { 'devices': traces[trace]['devices'][1], 'channel': traces[trace]['channels'][1], 'subplot': traces[trace]['subplot'] } # Workaround to get the hue here if 'hue' in traces[trace]: ptrace_3 = trace * 10 + 3 ptraces[ptrace_3] = { 'devices': traces[trace]['hue'][0], 'channel': traces[trace]['hue'][1], 'subplot': traces[trace]['subplot'] } # Get dataframe df, subplots = prepare_data(self, ptraces, options) # If empty, nothing to do here if df is None: return None n_subplots = len(subplots) # Plot nrows = min(n_subplots, formatting['nrows']) ncols = ceil(n_subplots / nrows) figure, axes = plt.subplots(nrows, ncols, figsize=(formatting['width'], formatting['height'])) if n_subplots == 1: axes = array(axes) axes.shape = (1) cind = 0 y_axes = list() x_axes = list() for i in subplots: for j in range(int(len(i) / 2)): cind += 1 if cind > len(colors) - 1: cind = 0 if nrows > 1 and ncols > 1: row = floor(subplots.index(i) / ncols) col = subplots.index(i) - row * ncols ax = axes[row][col] else: ax = axes[subplots.index(i)] kwargs = { 'data': df, 'ax': ax, 'label': f'{i[2*j+1]} vs. {i[2*j]}' } if len(i) == 3: if formatting['palette'] is None: cmap = plt.colormaps()[cind] else: cmap = formatting['palette'] # Assume this is the hue norm = plt.Normalize(df[i[2]].min(), df[i[2]].max()) sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) sm.set_array([]) kwargs['hue'] = i[2] kwargs['palette'] = cmap kwargs['legend'] = False hashue = True else: if formatting['palette'] is None: kwargs['color'] = colors[cind] hashue = False scatterplot(df[i[2 * j]], df[i[2 * j + 1]], **kwargs) if hashue: plt.colorbar(sm, ax=ax, orientation='vertical', label=i[2]) if formatting['legend']: ax.set_title(f'{i[2*j+1]} vs. {i[2*j]}', fontsize=formatting['title_fontsize']) if formatting['ylabel'] is not None: try: ax.set_ylabel(formatting['ylabel']) except: std_out(f'y_label for subplot {subplots.index(i)} not set', 'WARNING') ax.set_ylabel('') pass else: ax.set_ylabel('') if formatting['xlabel'] is not None: try: ax.set_xlabel(formatting['xlabel']) except: std_out(f'x_label for subplot {subplots.index(i)} not set', 'WARNING') ax.set_xlabel('') pass else: ax.set_xlabel('') y_axes.append(ax.get_ylim()) x_axes.append(ax.get_xlim()) # Unify axes or set what was ordered for i in subplots: for j in range(int(len(i) / 2)): if nrows > 1 and ncols > 1: row = floor(subplots.index(i) / ncols) col = subplots.index(i) - row * ncols ax = axes[row][col] else: ax = axes[subplots.index(i)] # Set y axis limit if formatting['yrange'] is not None and not formatting['sharey']: try: ax.set_ylim(formatting['yrange']) except: std_out(f'yrange for subplot {subplots.index(i)} not set', 'WARNING') pass elif formatting['sharey']: ax.set_ylim(min([yl[0] for yl in y_axes]), max([yl[1] for yl in y_axes])) # Set x axis limit if formatting['xrange'] is not None and not formatting['sharex']: try: ax.set_xlim(formatting['xrange']) except: std_out(f'xrange for subplot {subplots.index(i)} not set', 'WARNING') pass elif formatting['sharex']: ax.set_xlim(min([xl[0] for xl in x_axes]), max([xl[1] for xl in x_axes])) if formatting['legend']: ax.legend(loc='best') else: ax.get_legend().remove() # Set title figure.suptitle(formatting['title'], fontsize=formatting['title_fontsize']) plt.subplots_adjust(top=formatting['suptitle_factor']) if options['show']: plt.show() return figure, axes
def _register_colormap_and_reverse(mapName, cmap): if mapName not in plt.colormaps(): plt.register_cmap(mapName, cmap) plt.register_cmap('{}_r'.format(mapName), cmap.reversed())
def plotResSweepParamsVsX(resSweep, plot_keys=None, ignore_keys=None, xvals='temperature', **kwargs): r"""Plot parameter data vs temperature from a ResonatorSweep object. Parameters ---------- resSweep : ``scraps.ResonatorSweep`` object or list of objects The object containing the data you want to look at. It is also possible to pass a list of these objects and it will combine them into one plot. plot_keys : list-like (optional) A list of strings corresponding to avaiable plot data. The available keys depend on your parameter definitions and may be found by executing ``print(resSweep.keys())``. Some keys may point to empty (NaN) objects. Default is to plot all of the keys that exist. If you pass plot_keys you may not pass ignore_ignore keys. ignore_keys : list-like (optional) A list of strings corresponding to plots that should not be made. This is useful if you want to plot most of the avaialble data, but ignore one or two sets of data. Default is ``None``. If you pass ignore_keys you may not pass plot_keys. xvals : string What axis you want to plot by. Deafult is 'temperature'. Could also be 'power'. Future releases may include other options. color_by : string Set this to 'index' if you pass multiple res sweeps and want to color them by index. Keyword Arguments ----------------- plot_labels : list-like A list of strings to use to label the y-axes of the plots. There must be one for each plot requested. ``None`` is acceptable for any position in the list and will default to using the key as the label. Default is to use the key as the label. unit_multipliers : list-like A list of numbers to multiply against the y-axes data. There must be one for each plot requested. ``None`` is acceptable for any position in the list and will default to 1. Default is 1. fitter : string {'lmfit', 'emcee'} Which fit data to use when overlaying best fits. Default is 'lmfit'. num_cols : int The number of columns to create in the plot grid. Default is 1. The number of rows will be calculated based on num_cols and the number of requested plots. powers : list List of powers to plot. Default is to plot all available. If xvals is 'power' this kwarg is ignored. temperatures : list List of temperatures to plot. Default is to plot all available. If xvals is 'temperature' this kwarg is ignored. xmax : numeric Don't plot any xvals above this value. Default is infinity. xmin : numeric Don't plot any xvals below this value. Default is 0 for temperature and -infinity for power. errorbars: {None, 'lmfit', 'emcee'} Add error bars to the data. Pulls from either the least-squares or the MCMC fits. Default is None. fig_size : numeric Size in inches for each plot in the figure. color_map : string Specifies the colormap to use. Any value in ``matplotlib.pyplot.colormaps()`` is a valid option. show_colorbar : {True, False}, optional Whether or not to add a colorbar to the right edge of the figure. The colorbar will correspond to the limits of the colored data. Default is True. force_square : bool Whether or not to force each subplot to have perfectly square axes. plot_kwargs : dict or list of dicts Dict of keyword args to pass through to the plotting function. Default is {'linestyle':'--', label='Power X dB'}. If errorbars is not None, then default linestyle is {'linestyle':'o'}. Attempting to set 'color' or 'yerr' will result in an exception. Use the color_map and errorbars keywords to set those. If you passed in a list of objects to resSweep, then you can also pass a list of plot_kwargs, one for each sweep object. """ if type(resSweep) is not list: resSweep = [resSweep] for rS in resSweep: #This will really only work for sure if block is sucessful assert rS.smartindex == 'block', "index must be 'block' for plotting to work." #TODO: fix for other smartindex types #set defaults plot_labels = kwargs.pop('plot_labels', None) unit_multipliers = kwargs.pop('unit_multipliers', None) #Which fit data should be plot? lmfit or emcee? fitter = kwargs.pop('fitter', 'lmfit') #Number of columns num_cols = int(kwargs.pop('num_cols', 4)) #Powers to plot powers = list(kwargs.pop('powers', [])) #Temperatures to plot temps = list(kwargs.pop('temps', [])) if xvals == 'temperature': #Just use all the powers that exist period! if len(powers) == 0: for rS in resSweep: powers.extend(rS.pvec) powers = list(set(powers)) else: for rS in resSweep: assert any(p in rS.pvec for p in powers), "No data exists at any requested power." if xvals == 'power': #Just use all the temperatures that exist period! if len(temps) == 0: for rS in resSweep: temps.extend(rS.tvec) temps = list(set(temps)) else: for rS in resSweep: assert any(t in rS.tvec for t in temps), "No data exists at any requested temperature." #Set up the x-axis mask xmax = kwargs.pop('xmax', None) xmin = kwargs.pop('xmin', None) if xmin is None: if xvals == 'temperature': xmin = 0 elif xvals == 'power': xmin = -np.inf #Just use the biggest temperature in any set if max isn't passed if xmax is None: xmax = np.inf xMask = [] for rS in resSweep: if xvals == 'temperature': xvec = rS.tvec elif xvals == 'power': xvec = rS.pvec xMask.append((xvec >= xmin) * (xvec <= xmax)) color_by = kwargs.pop('color_by', None) if color_by is None: if xvals == 'temperature': color_by = 'power' elif xvals == 'power': color_by = 'temperature' #Very early errobar code. Still in beta. errorbars = kwargs.pop('errorbars', None) assert errorbars in [None, 'lmfit', 'emcee'], "Invalid option for errorbars. Try None, 'lmfit', or 'emcee'." #Figure out which parameters to plot if ignore_keys is None: ignore_keys = ['listIndex', 'temps'] else: assert plot_keys is None, "Either pass plot_keys or ignore_keys, not both." for rS in resSweep: assert all(key in rS.keys() for key in ignore_keys), "Unknown key in ignore_keys" ignore_keys.append('listIndex') ignore_keys.append('temps') if plot_keys is None: plot_keys = [] for rS in resSweep: plot_keys.extend(set(rS.keys())) plot_keys = set(set(plot_keys)-set(ignore_keys)) else: for rS in resSweep: assert any(key in rS.keys() for key in plot_keys), "No data corresponding to any plot_key" #Some general defaults fig_size = kwargs.pop('fig_size', 3) force_square = kwargs.pop('force_square', False) #Set the colormap: Default to viridis color_map = kwargs.pop('color_map', None) if color_map is None: if color_by == 'index': color_map = 'tab10' elif xvals == 'temperature': color_map = 'viridis' elif xvals == 'power': color_map = 'coolwarm' assert color_map in plt.colormaps(), "Unknown colormap provided" color_gen = plt.get_cmap(color_map) #Set whether to show the colorbar show_colorbar = kwargs.pop('show_colorbar', True) #Defaults for this are set later plot_kwargs = kwargs.pop('plot_kwargs', [{}]*len(resSweep)) if type(plot_kwargs) == dict: plot_kwargs = [plot_kwargs]*len(resSweep) assert type(plot_kwargs) == list, "Must pass list of plot_kwargs of same length as list of resSweeps" #Unknown kwargs are discouraged if kwargs: raise NameError("Unknown keyword argument: " + list(kwargs.keys())[0]) #Set up the figure figS = plt.figure() num_keys = len(plot_keys) #Don't need more columns than plots if num_keys < num_cols: num_cols = num_keys #Calculate rows for figure size num_rows = int(np.ceil(num_keys/num_cols)) #Calculate rows for figure size num_rows = int(np.ceil(1.0*num_keys/num_cols)) #Set figure size, including some extra spacing for the colorbar #0.1 is the extra space for the colorbar. #*1.2 is the extra padding for the axis labels #15:1 is the ratio of axis width for regular axes to colorbar axis if show_colorbar: figS.set_size_inches(fig_size*(num_cols+0.1)*1.2, fig_size*num_rows) #Initialize the grid for plotting plt_grid = gs.GridSpec(num_rows, num_cols+1, width_ratios=[15]*num_cols+[1]) else: figS.set_size_inches(fig_size*(num_cols)*1.2, fig_size*num_rows) #Initialize the grid for plotting plt_grid = gs.GridSpec(num_rows, num_cols) #Loop through all the keys in the ResonatorSweep object and plot them for ix, key in enumerate(plot_keys): iRow = int(ix/num_cols) iCol = ix%num_cols axs = figS.add_subplot(plt_grid[iRow, iCol]) if unit_multipliers is not None: mult = unit_multipliers[ix] else: mult = 1 if xvals == 'power': iterator = temps elif xvals == 'temperature': iterator = powers for itr in iterator: if (xvals == 'temperature') and (color_by != 'index'): if len(powers) > 1: plt_color = color_gen(1-((max(powers)-itr)*1.0/(max(powers)-min(powers)))) else: plt_color = color_gen(0) elif (xvals == 'power') and (color_by != 'index'): if len(temps) > 1: plt_color = color_gen(1-((max(temps)-itr)*1.0/(max(temps)-min(temps)))) else: plt_color = color_gen(0) for rix, rS in enumerate(resSweep): if color_by == 'index': plt_color = color_gen(rix) if xvals == 'temperature': if itr in rS.pvec: x_data = rS.tvec[xMask[rix]] plt_data = mult*rS[key].loc[xMask[rix], itr].values else: plt_data = None elif xvals == 'power': if itr in rS.tvec: x_data = rS.pvec[xMask[rix]] plt_data = mult*rS[key].loc[itr, xMask[rix]].values else: plt_data = None if plt_data is not None: if 'label' not in plot_kwargs[rix].keys(): if color_by == 'index': plot_kwargs[rix]['label'] = 'Index: '+str(rix) else: plot_kwargs[rix]['label'] = xvals+ ": "+str(itr) if 'linestyle' not in plot_kwargs[rix].keys(): if errorbars is not None: plot_kwargs[rix]['marker'] = 'o' else: plot_kwargs[rix]['linestyle'] = '--' if errorbars is None: axs.plot(x_data ,plt_data, color=plt_color, **plot_kwargs[rix]) elif errorbars == 'lmfit': #lmfit uncertainty was stored in the _sigma key, so just grab it back out if xvals == 'temperature': plt_err = mult*rS[key + '_sigma'].loc[xMask[rix], itr].values elif xvals == 'power': plt_err = mult*rS[key + '_sigma'].loc[itr, xMask[rix]].values axs.errorbar(x_data, plt_data, yerr=plt_err, color=plt_color, **plot_kwargs[rix]) elif errorbars == 'emcee': #emcee uncertainty was placed in the _sigma_plus_mc and _sigma_minus_mc keys if xvals == 'temperature': plt_err_plus = mult*rS[key + '_sigma_plus_mc'].loc[xMask[rix], itr].values plt_err_minus = mult*rS[key + '_sigma_minus_mc'].loc[xMask[rix], itr].values elif xvals == 'power': plt_err_plus = mult*rS[key + '_sigma_plus_mc'].loc[itr, xMask[rix]].values plt_err_minus = mult*rS[key + '_sigma_minus_mc'].loc[itr, xMask[rix]].values plt_err = [plt_err_plus, plt_err_minus] axs.errorbar(x_data, plt_data, yerr=plt_err, color=plt_color, **plot_kwargs[rix]) if xvals == 'temperature': axs.set_xlabel('Temperature (mK)') elif xvals == 'power': axs.set_xlabel('Power (dB)') if plot_labels is not None: axs.set_ylabel(plot_labels[ix]) else: axs.set_ylabel(key) #No idea why this is necessary, but it all falls apart without it # axs.set_xlim(np.min(x_data), np.max(x_data)) # xticks = axs.get_xticks() # axs.set_xticks(xticks) # xticklabels = ['%g'%x for x in xticks] # axs.set_xticklabels(xticklabels,rotation=45) # Should remove above cruft after testing this fix plt.setp(axs.xaxis.get_majorticklabels(), rotation=45) if force_square: #Make the plot a square x1, x2 = axs.get_xlim() y1, y2 = axs.get_ylim() #Explicitly passing a float to avoid an warning in matplotlib #when it gets a numpy.float64 axs.set_aspect(float((x2-x1)/(y2-y1))) #Stick some legends where they won't crowd too much # if key == 'f0' or key == 'fmin': # axs.legend(loc='best') if show_colorbar: if color_by == 'index': cbar_norm = mpl.colors.BoundaryNorm(range(len(resSweep)+1), len(resSweep)) cbar_units = 'index' else: cbar_norm = mpl.colors.Normalize(vmin=min(iterator), vmax=max(iterator)) if xvals == 'temperature': cbar_units = 'dB' elif xvals == 'power': cbar_units = 'mK' #Make an axis that spans all rows cax = figS.add_subplot(plt_grid[:, num_cols]) #Plot and label cbar_plot = mpl.colorbar.ColorbarBase(cax, cmap=color_gen, norm=cbar_norm) cbar_plot.set_label(cbar_units) figS.tight_layout() return figS
def _check_configuration(self, config_dict, config_path): """ A routine to make sure that user specified configurations are indeed valid. :param config_dict: dictionary with configuration :param config_path: path from which the configuration has been read :return: None, but raises exceptions if errors are encountered """ # First check that the provided configuration has the same structure of the default configuration # (if a default configuration has been loaded) if (self._default_configuration_raw is not None) and \ (not self._check_same_structure(config_dict, self._default_configuration_raw)): # It does not, so of course is not valid (no need to check further) raise ConfigurationFileCorrupt( "Config file %s has a different structure than the expected " "one." % config_path) else: # Make a dictionary of known checkers and what they apply to known_checkers = { 'color': (self.is_matplotlib_color, 'a matplotlib color (name or html hex value)'), 'cmap': (self.is_matplotlib_cmap, 'a matplotlib color map (available: %s)' % ", ".join(plt.colormaps())), 'name': (self.is_string, "a valid name (string)"), 'switch': (self.is_bool, "one of yes, no, True, False"), 'ftp url': (self.is_ftp_url, "a valid FTP URL"), 'http url': (self.is_http_url, "a valid HTTP(S) URL"), 'optimizer': (self.is_optimizer, "one of scipy.optimize minimization methods (available: %s)" % ", ".join(_optimize_methods)), 'number': (self.is_number, "an int or float"), } # Now that we know that the provided configuration have the right structure, let's check that # each value is of the proper type for key, value in self._traverse_dict(config_dict): # Each key is in the form "element_name (element_type)", for example "background (color)" try: element_name, element_type = re.findall( "(.+) \((.+)\)", key)[0] except IndexError: raise ConfigurationFileCorrupt( "Cannot parse element '%s' in configuration file %s" % (key, config_path)) if element_type in known_checkers: checker, descr = known_checkers[element_type] if not checker(value): raise ValueError( "Value %s for key %s in file %s is not %s" % (value, element_name, config_path, descr)) else: raise ConfigurationFileCorrupt( "Cannot understand element type %s for " "key %s in config file %s" % (element_type, key, config_path)) # If we are here it means that all checks were successful # Return the new configuration, where all types are stripped out return self._get_copy_with_no_types(config_dict)
nc_fid = Dataset(filename, 'r') #reader = nc_fid.variables['membership'][:] reader = nc_fid.variables['data'][:] reader = reader[:,1] grid = reader.transpose().reshape(1,rows,cols) return grid; ## main with rasterio.open(fileraster) as src: profile=src.profile #a, b, c = src.read() raster_ori = src.read() bands,rows,cols = np.shape(src.read()) profile.update(dtype=rasterio.uint16, count=1) plt.imshow(raster_ori[0,:,:]) plt.colormaps() plt.show() if fileext == 'txt': image = baca_txt(filegrid,rows,cols) plt.imshow(image[1,:,:]) plt.colormaps() plt.show() with rasterio.open(fileraster+'hasil.tif', 'w', **profile) as dst: dst.write(image[1,:,:].astype(rasterio.uint16),1) elif fileext == 'nc': nc5to4 = 'ncks '+ filegrid + ' -4 ' + filegrid +'.4.nc' filegrid = filegrid+'.4.nc' os.system(nc5to4) image = baca_nc(filegrid,rows,cols)
import os ################# # Some default settings matplotlib.rcParams["figure.figsize"] = [4 * 6.4, 4 * 4.8] #https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html parser = argparse.ArgumentParser( description='Plot frames from TiFoSi simulation.') parser.add_argument('--cmap', dest='cmap', default="terrain", help='Color map for protein levels.', choices=plt.colormaps()) parser.add_argument('--cmapt', dest='cmapType', default="tab20", help='Color map for cell type.', choices=plt.colormaps()) parser.add_argument('--oformat', dest='oformat', default="png", help='Output images file format.', choices=["png", "eps", "svg", "jpg"]) parser.add_argument('--dpi', dest='dpi', default=300, help='Output images resolution.', type=int)
fragLogic = ''.join(fragLogic) tmplt = open('shader.template').read() tmplt = tmplt.format(ShaderName=r'Custom/%s' % cm_name, SegmentDataFields=segData, SegmentDataUniforms=segUniforms, FragLogic=fragLogic) with open(r"./shaders/%s.shader" % cm.name, 'w') as fid: fid.write(tmplt) def _strColor(colTup): return '(%0.5f, %0.5f, %0.5f, %0.5f)' % colTup if __name__ == "__main__": import pmkmp for cm_name in plt.colormaps(): if cm_name.endswith('_r'): continue cm = plt.get_cmap(cm_name) try: exportColorMap(cm) except: print('Could not export %s' % cm_name)
def validate(self, kwargs): """ The validate method checks if all the input arguments are corret. **Parameters**: kwargs : dict Contains a dict of all the arguments for the line chart visualisation. """ super().validate(kwargs) self.cc_log("INFO", "Data Visualization Bar: started validation") if not kwargs.get("type"): raise ValidationError(self, ["type"], "Parameter cannot be empty!") if not kwargs.get("dataAttribute"): raise ValidationError(self, ["dataAttribute"], "Parameter cannot be empty!") if not kwargs.get( "groupNameAttribute") and kwargs.get("type") != "histogram": raise ValidationError(self, ["groupNameAttribute"], "Parameter cannot be empty!") if kwargs.get("threshold"): try: int(kwargs.get("threshold")) except: raise ValidationError(self, ["threshold"], "Parameter has to be an int!") if kwargs.get("figureSize"): if not isinstance(kwargs.get("figureSize"), list) or len(kwargs.get("figureSize")) != 2: raise ValidationError( self, ["figureSize"], "Parameter has to be a list of two (E.g. 20, 10)!") if kwargs.get("rotateXTicks"): try: int(kwargs.get("rotateXTicks")) except: raise ValidationError(self, ["rotateXTicks"], "Parameter has to be an int!") if kwargs.get("rotateYTicks"): try: int(kwargs.get("rotateYTicks")) except: raise ValidationError(self, ["rotateYTicks"], "Parameter has to be an int!") if kwargs.get("colormap"): if kwargs.get("colormap") not in plt.colormaps(): raise ValidationError( self, ["colormap"], "Colormap has to be existing, check the matplotlibb docu!") # Optional #if not kwargs.get("title"): # raise ValidationError(self, ["title"], "Parameter cannot be empty!") #if not kwargs.get("ylabel"): # raise ValidationError(self, ["ylabel"], "Parameter cannot be empty!") #if not kwargs.get("xlabel"): # raise ValidationError(self, ["xlabel"], "Parameter cannot be empty!") #if not kwargs.get("zlabel"): # raise ValidationError(self, ["zlabel"], "Parameter cannot be empty!") #if not kwargs.get("horizontal"): # raise ValidationError(self, ["horizontal"], "Parameter cannot be empty!") #if not kwargs.get("scaledTo100"): # raise ValidationError(self, ["scaledTo100"], "Parameter cannot be empty!") self.cc_log("INFO", "Data Visualization Bar: finished validation")
def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs): """ Custom function that creates an estimation plot from an EffectSizeDataFrame. Keywords -------- EffectSizeDataFrame: A `dabest` EffectSizeDataFrame object. **plot_kwargs: color_col=None raw_marker_size=6, es_marker_size=9, swarm_label=None, contrast_label=None, swarm_ylim=None, contrast_ylim=None, custom_palette=None, swarm_desat=0.5, halfviolin_desat=1, halfviolin_alpha=0.8, float_contrast=True, show_pairs=True, group_summaries=None, group_summaries_offset=0.1, fig_size=None, dpi=100, ax=None, swarmplot_kwargs=None, violinplot_kwargs=None, slopegraph_kwargs=None, reflines_kwargs=None, group_summary_kwargs=None, legend_kwargs=None, """ import numpy as np import seaborn as sns import matplotlib.pyplot as plt import pandas as pd from .misc_tools import merge_two_dicts from .plot_tools import halfviolin, get_swarm_spans, gapped_lines from ._stats_tools.effsize import _compute_standardizers, _compute_hedges_correction_factor import logging # Have to disable logging of warning when get_legend_handles_labels() # tries to get from slopegraph. logging.disable(logging.WARNING) # Save rcParams that I will alter, so I can reset back. original_rcParams = {} _changed_rcParams = ['axes.grid'] for parameter in _changed_rcParams: original_rcParams[parameter] = plt.rcParams[parameter] plt.rcParams['axes.grid'] = False ytick_color = plt.rcParams["ytick.color"] axes_facecolor = plt.rcParams['axes.facecolor'] dabest_obj = EffectSizeDataFrame.dabest_obj plot_data = EffectSizeDataFrame._plot_data xvar = EffectSizeDataFrame.xvar yvar = EffectSizeDataFrame.yvar is_paired = EffectSizeDataFrame.is_paired all_plot_groups = dabest_obj._all_plot_groups idx = dabest_obj.idx # Disable Gardner-Altman plotting if any of the idxs comprise of more than # two groups. float_contrast = plot_kwargs["float_contrast"] effect_size_type = EffectSizeDataFrame.effect_size if len(idx) > 1 or len(idx[0]) > 2: float_contrast = False if effect_size_type in ['cliffs_delta']: float_contrast = False # Disable slopegraph plotting if any of the idxs comprise of more than # two groups. if np.all([len(i) == 2 for i in idx]) is False: is_paired = False # if paired is False, set show_pairs as False. if is_paired is False: show_pairs = False else: show_pairs = plot_kwargs["show_pairs"] # Set default kwargs first, then merge with user-dictated ones. default_swarmplot_kwargs = {'size': plot_kwargs["raw_marker_size"]} if plot_kwargs["swarmplot_kwargs"] is None: swarmplot_kwargs = default_swarmplot_kwargs else: swarmplot_kwargs = merge_two_dicts(default_swarmplot_kwargs, plot_kwargs["swarmplot_kwargs"]) # Violinplot kwargs. default_violinplot_kwargs = { 'widths': 0.5, 'vert': True, 'showextrema': False, 'showmedians': False } if plot_kwargs["violinplot_kwargs"] is None: violinplot_kwargs = default_violinplot_kwargs else: violinplot_kwargs = merge_two_dicts(default_violinplot_kwargs, plot_kwargs["violinplot_kwargs"]) # slopegraph kwargs. default_slopegraph_kwargs = {'lw': 1, 'alpha': 0.5} if plot_kwargs["slopegraph_kwargs"] is None: slopegraph_kwargs = default_slopegraph_kwargs else: slopegraph_kwargs = merge_two_dicts(default_slopegraph_kwargs, plot_kwargs["slopegraph_kwargs"]) # Zero reference-line kwargs. default_reflines_kwargs = { 'linestyle': 'solid', 'linewidth': 0.75, 'zorder': 2, 'color': ytick_color } if plot_kwargs["reflines_kwargs"] is None: reflines_kwargs = default_reflines_kwargs else: reflines_kwargs = merge_two_dicts(default_reflines_kwargs, plot_kwargs["reflines_kwargs"]) # Legend kwargs. default_legend_kwargs = {'loc': 'upper left', 'frameon': False} if plot_kwargs["legend_kwargs"] is None: legend_kwargs = default_legend_kwargs else: legend_kwargs = merge_two_dicts(default_legend_kwargs, plot_kwargs["legend_kwargs"]) gs_default = {'mean_sd', 'median_quartiles', None} if plot_kwargs["group_summaries"] not in gs_default: raise ValueError('group_summaries must be one of' ' these: {}.'.format(gs_default)) default_group_summary_kwargs = {'zorder': 3, 'lw': 2, 'alpha': 1} if plot_kwargs["group_summary_kwargs"] is None: group_summary_kwargs = default_group_summary_kwargs else: group_summary_kwargs = merge_two_dicts( default_group_summary_kwargs, plot_kwargs["group_summary_kwargs"]) # Create color palette that will be shared across subplots. color_col = plot_kwargs["color_col"] if color_col is None: color_groups = pd.unique(plot_data[xvar]) bootstraps_color_by_group = True else: if color_col not in plot_data.columns: raise KeyError( "``{}`` is not a column in the data.".format(color_col)) color_groups = pd.unique(plot_data[color_col]) bootstraps_color_by_group = False if show_pairs: bootstraps_color_by_group = False # Handle the color palette. names = color_groups n_groups = len(color_groups) custom_pal = plot_kwargs["custom_palette"] swarm_desat = plot_kwargs["swarm_desat"] contrast_desat = plot_kwargs["halfviolin_desat"] if custom_pal is None: unsat_colors = sns.color_palette(n_colors=n_groups) else: if isinstance(custom_pal, dict): groups_in_palette = { k: v for k, v in custom_pal.items() if k in color_groups } # # check that all the keys in custom_pal are found in the # # color column. # col_grps = {k for k in color_groups} # pal_grps = {k for k in custom_pal.keys()} # not_in_pal = pal_grps.difference(col_grps) # if len(not_in_pal) > 0: # err1 = 'The custom palette keys {} '.format(not_in_pal) # err2 = 'are not found in `{}`. Please check.'.format(color_col) # errstring = (err1 + err2) # raise IndexError(errstring) names = groups_in_palette.keys() unsat_colors = groups_in_palette.values() elif isinstance(custom_pal, list): unsat_colors = custom_pal[0:n_groups] elif isinstance(custom_pal, str): # check it is in the list of matplotlib palettes. if custom_pal in plt.colormaps(): unsat_colors = sns.color_palette(custom_pal, n_groups) else: err1 = 'The specified `custom_palette` {}'.format(custom_pal) err2 = ' is not a matplotlib palette. Please check.' raise ValueError(err1 + err2) swarm_colors = [sns.desaturate(c, swarm_desat) for c in unsat_colors] plot_palette_raw = dict(zip(names, swarm_colors)) contrast_colors = [sns.desaturate(c, contrast_desat) for c in unsat_colors] plot_palette_contrast = dict(zip(names, contrast_colors)) # Infer the figsize. fig_size = plot_kwargs["fig_size"] if fig_size is None: all_groups_count = np.sum([len(i) for i in dabest_obj.idx]) if is_paired is True and show_pairs is True: frac = 0.75 else: frac = 1 if float_contrast is True: height_inches = 4 each_group_width_inches = 2.5 * frac else: height_inches = 6 each_group_width_inches = 1.5 * frac width_inches = (each_group_width_inches * all_groups_count) fig_size = (width_inches, height_inches) # Initialise the figure. # sns.set(context="talk", style='ticks') init_fig_kwargs = dict(figsize=fig_size, dpi=plot_kwargs["dpi"], tight_layout=True) width_ratios_ga = [2.5, 1] h_space_cummings = 0.3 if plot_kwargs["ax"] is None: plot_kwargs["ax"] = plt.gca() # New in v0.2.6. # Use inset axes to create the estimation plot inside a single axes. # Author: Adam L Nekimken. (PR #73) inset_contrast = True rawdata_axes = plot_kwargs["ax"] ax_position = rawdata_axes.get_position() # [[x0, y0], [x1, y1]] fig = rawdata_axes.get_figure() if float_contrast is True: axins = rawdata_axes.inset_axes( [1, 0, width_ratios_ga[1] / width_ratios_ga[0], 1]) rawdata_axes.set_position( # [l, b, w, h] [ ax_position.x0, ax_position.y0, (ax_position.x1 - ax_position.x0) * (width_ratios_ga[0] / sum(width_ratios_ga)), (ax_position.y1 - ax_position.y0) ]) contrast_axes = axins # else: # axins = rawdata_axes.inset_axes([0, -1 - h_space_cummings, 1, 1]) # plot_height = ((ax_position.y1 - ax_position.y0) / # (2 + h_space_cummings)) # rawdata_axes.set_position( # [ax_position.x0, # ax_position.y0 + (1 + h_space_cummings) * plot_height, # (ax_position.x1 - ax_position.x0), # plot_height]) # # # If the contrast axes are NOT floating, create lists to store # # raw ylims and raw tick intervals, so that I can normalize # # their ylims later. # contrast_ax_ylim_low = list() # contrast_ax_ylim_high = list() # contrast_ax_ylim_tickintervals = list() contrast_axes = axins rawdata_axes.contrast_axes = axins else: inset_contrast = False # Here, we hardcode some figure parameters. if float_contrast is True: fig, axx = plt.subplots(ncols=2, gridspec_kw={ "width_ratios": width_ratios_ga, "wspace": 0 }, **init_fig_kwargs) else: fig, axx = plt.subplots(nrows=2, gridspec_kw={"hspace": 0.3}, **init_fig_kwargs) # If the contrast axes are NOT floating, create lists to store # raw ylims and raw tick intervals, so that I can normalize # their ylims later. contrast_ax_ylim_low = list() contrast_ax_ylim_high = list() contrast_ax_ylim_tickintervals = list() rawdata_axes = axx[0] contrast_axes = axx[1] rawdata_axes.set_frame_on(False) contrast_axes.set_frame_on(False) redraw_axes_kwargs = { 'colors': ytick_color, 'facecolors': ytick_color, 'lw': 1, 'zorder': 10, 'clip_on': False } swarm_ylim = plot_kwargs["swarm_ylim"] if swarm_ylim is not None: rawdata_axes.set_ylim(swarm_ylim) if show_pairs is True: # Plot the raw data as a slopegraph. # Pivot the long (melted) data. if color_col is None: pivot_values = yvar else: pivot_values = [yvar, color_col] pivoted_plot_data = pd.pivot(data=plot_data, index=dabest_obj.id_col, columns=xvar, values=pivot_values) for ii, current_tuple in enumerate(idx): if len(idx) > 1: # Select only the data for the current tuple. if color_col is None: current_pair = pivoted_plot_data.reindex( columns=current_tuple) else: current_pair = pivoted_plot_data[yvar].reindex( columns=current_tuple) else: if color_col is None: current_pair = pivoted_plot_data else: current_pair = pivoted_plot_data[yvar] # Iterate through the data for the current tuple. for ID, observation in current_pair.iterrows(): x_start = (ii * 2) x_points = [x_start, x_start + 1] y_points = observation.tolist() if color_col is None: slopegraph_kwargs['color'] = ytick_color else: color_key = pivoted_plot_data[color_col, current_tuple[0]].loc[ID] slopegraph_kwargs['color'] = plot_palette_raw[color_key] slopegraph_kwargs['label'] = color_key rawdata_axes.plot(x_points, y_points, **slopegraph_kwargs) # Set the tick labels, because the slopegraph plotting doesn't. rawdata_axes.set_xticks(np.arange(0, len(all_plot_groups))) rawdata_axes.set_xticklabels(all_plot_groups) else: # Plot the raw data as a swarmplot. rawdata_plot = sns.swarmplot(data=plot_data, x=xvar, y=yvar, ax=rawdata_axes, order=all_plot_groups, hue=color_col, palette=plot_palette_raw, zorder=1, **swarmplot_kwargs) # Plot the gapped line summaries, if this is not a Cumming plot. # Also, we will not plot gapped lines for paired plots. For now. group_summaries = plot_kwargs["group_summaries"] if float_contrast is False and group_summaries is None: group_summaries = "mean_sd" if group_summaries is not None: # Create list to gather xspans. xspans = [] line_colors = [] for jj, c in enumerate(rawdata_axes.collections): try: _, x_max, _, _ = get_swarm_spans(c) x_max_span = x_max - jj xspans.append(x_max_span) except TypeError: # we have got a None, so skip and move on. pass if bootstraps_color_by_group is True: line_colors.append(plot_palette_raw[all_plot_groups[jj]]) if len(line_colors) != len(all_plot_groups): line_colors = ytick_color gapped_lines( plot_data, x=xvar, y=yvar, # Hardcoded offset... offset=xspans + np.array(plot_kwargs["group_summaries_offset"]), line_color=line_colors, gap_width_percent=1.5, type=group_summaries, ax=rawdata_axes, **group_summary_kwargs) # Add the counts to the rawdata axes xticks. counts = plot_data.groupby(xvar).count()[yvar] ticks_with_counts = [] for xticklab in rawdata_axes.xaxis.get_ticklabels(): t = xticklab.get_text() N = str(counts.loc[t]) ticks_with_counts.append("{}\nN = {}".format(t, N)) rawdata_axes.set_xticklabels(ticks_with_counts) # Save the handles and labels for the legend. handles, labels = rawdata_axes.get_legend_handles_labels() legend_labels = [l for l in labels] legend_handles = [h for h in handles] if bootstraps_color_by_group is False: rawdata_axes.legend().set_visible(False) # Plot effect sizes and bootstraps. # Take note of where the `control` groups are. ticks_to_skip = np.cumsum([len(t) for t in idx])[:-1].tolist() ticks_to_skip.insert(0, 0) # Then obtain the ticks where we have to plot the effect sizes. ticks_to_plot = [ t for t in range(0, len(all_plot_groups)) if t not in ticks_to_skip ] # Plot the bootstraps, then the effect sizes and CIs. es_marker_size = plot_kwargs["es_marker_size"] halfviolin_alpha = plot_kwargs["halfviolin_alpha"] results = EffectSizeDataFrame.results contrast_xtick_labels = [] for j, tick in enumerate(ticks_to_plot): current_group = results.test[j] current_control = results.control[j] current_bootstrap = results.bootstraps[j] current_effsize = results.difference[j] current_ci_low = results.bca_low[j] current_ci_high = results.bca_high[j] # Create the violinplot. # New in v0.2.6: drop negative infinities before plotting. v = contrast_axes.violinplot( current_bootstrap[~np.isinf(current_bootstrap)], positions=[tick], **violinplot_kwargs) # Turn the violinplot into half, and color it the same as the swarmplot. # Do this only if the color column is not specified. # Ideally, the alpha (transparency) fo the violin plot should be # less than one so the effect size and CIs are visible. if bootstraps_color_by_group is True: fc = plot_palette_contrast[current_group] else: fc = "grey" halfviolin(v, fill_color=fc, alpha=halfviolin_alpha) # Plot the effect size. contrast_axes.plot([tick], current_effsize, marker='o', color=ytick_color, markersize=es_marker_size) # Plot the confidence interval. contrast_axes.plot([tick, tick], [current_ci_low, current_ci_high], linestyle="-", color=ytick_color, linewidth=group_summary_kwargs['lw']) contrast_xtick_labels.append( "") #"{}\nminus\n{}".format(current_group,current_control)) # Make sure the contrast_axes x-lims match the rawdata_axes xlims. contrast_axes.set_xticks(rawdata_axes.get_xticks()) if show_pairs is True: max_x = contrast_axes.get_xlim()[1] rawdata_axes.set_xlim(-0.375, max_x) if float_contrast is True: contrast_axes.set_xlim(0.5, 1.5) else: contrast_axes.set_xlim(rawdata_axes.get_xlim()) # Properly label the contrast ticks. for t in ticks_to_skip: contrast_xtick_labels.insert(t, "") contrast_axes.set_xticklabels(contrast_xtick_labels) if bootstraps_color_by_group is False: legend_labels_unique = np.unique(legend_labels) unique_idx = np.unique(legend_labels, return_index=True)[1] legend_handles_unique = ( pd.Series(legend_handles).loc[unique_idx]).tolist() if len(legend_handles_unique) > 0: # if float_contrast is True: # axes_with_legend = contrast_axes # if show_pairs is True: # bta = (1.75, 1.02) # else: # bta = (1.5, 1.02) # else: axes_with_legend = rawdata_axes if show_pairs is True: bta = (1.02, 1.) else: bta = (1., 1.) leg = axes_with_legend.legend(legend_handles_unique, legend_labels_unique, bbox_to_anchor=bta, **legend_kwargs) if show_pairs is True: for line in leg.get_lines(): line.set_linewidth(3.0) og_ylim_raw = rawdata_axes.get_ylim() if float_contrast is True: # For Gardner-Altman plots only. # Normalize ylims and despine the floating contrast axes. # Check that the effect size is within the swarm ylims. if effect_size_type in ["mean_diff", "cohens_d", "hedges_g"]: control_group_summary = plot_data.groupby(xvar)\ .mean().loc[current_control, yvar] test_group_summary = plot_data.groupby(xvar)\ .mean().loc[current_group, yvar] elif effect_size_type == "median_diff": control_group_summary = plot_data.groupby(xvar)\ .median().loc[current_control, yvar] test_group_summary = plot_data.groupby(xvar)\ .median().loc[current_group, yvar] if swarm_ylim is None: swarm_ylim = rawdata_axes.get_ylim() _, contrast_xlim_max = contrast_axes.get_xlim() difference = float(results.difference[0]) if effect_size_type in ["mean_diff", "median_diff"]: # Align 0 of contrast_axes to reference group mean of rawdata_axes. # If the effect size is positive, shift the contrast axis up. rawdata_ylims = np.array(rawdata_axes.get_ylim()) if current_effsize > 0: rightmin, rightmax = rawdata_ylims - current_effsize # If the effect size is negative, shift the contrast axis down. elif current_effsize < 0: rightmin, rightmax = rawdata_ylims + current_effsize else: rightmin, rightmax = rawdata_ylims contrast_axes.set_ylim(rightmin, rightmax) og_ylim_contrast = rawdata_axes.get_ylim() - np.array( control_group_summary) contrast_axes.set_ylim(og_ylim_contrast) contrast_axes.set_xlim(contrast_xlim_max - 1, contrast_xlim_max) elif effect_size_type in ["cohens_d", "hedges_g"]: if is_paired: which_std = 1 else: which_std = 0 temp_control = plot_data[plot_data[xvar] == current_control][yvar] temp_test = plot_data[plot_data[xvar] == current_group][yvar] stds = _compute_standardizers(temp_control, temp_test) if is_paired: pooled_sd = stds[1] else: pooled_sd = stds[0] if effect_size_type == 'hedges_g': gby_count = plot_data.groupby(xvar).count() len_control = gby_count.loc[current_control, yvar] len_test = gby_count.loc[current_group, yvar] hg_correction_factor = _compute_hedges_correction_factor( len_control, len_test) ylim_scale_factor = pooled_sd / hg_correction_factor else: ylim_scale_factor = pooled_sd scaled_ylim = ((rawdata_axes.get_ylim() - control_group_summary) / ylim_scale_factor).tolist() contrast_axes.set_ylim(scaled_ylim) og_ylim_contrast = scaled_ylim contrast_axes.set_xlim(contrast_xlim_max - 1, contrast_xlim_max) # Draw summary lines for control and test groups.. for jj, axx in enumerate([rawdata_axes, contrast_axes]): # Draw effect size line. if jj == 0: ref = control_group_summary diff = test_group_summary effsize_line_start = 1 elif jj == 1: ref = 0 diff = ref + difference effsize_line_start = contrast_xlim_max - 1.1 xlimlow, xlimhigh = axx.get_xlim() # # Draw reference line. axx.hlines( ref, # y-coordinates 0, xlimhigh, # x-coordinates, start and end. **reflines_kwargs) # Draw effect size line. axx.hlines(diff, effsize_line_start, xlimhigh, **reflines_kwargs) # Despine appropriately. sns.despine(ax=rawdata_axes, bottom=True) sns.despine(ax=contrast_axes, left=True, right=False) # Insert break between the rawdata axes and the contrast axes # by re-drawing the x-spine. rawdata_axes.hlines( og_ylim_raw[0], # yindex rawdata_axes.get_xlim()[0], 1.3, # xmin, xmax **redraw_axes_kwargs) rawdata_axes.set_ylim(og_ylim_raw) # contrast_axes.hlines(contrast_axes.get_ylim()[0], # contrast_xlim_max-0.8, contrast_xlim_max, # **redraw_axes_kwargs) # For Cumming Plots only. # Set custom contrast_ylim, if it was specified. if plot_kwargs['contrast_ylim'] is not None: custom_contrast_ylim = plot_kwargs['contrast_ylim'] if len(custom_contrast_ylim) != 2: err1 = "Please check `contrast_ylim` consists of " err2 = "exactly two numbers." raise ValueError(err1 + err2) if effect_size_type == "cliffs_delta": # Ensure the ylims for a cliffs_delta plot never exceed [-1, 1]. l = plot_kwargs['contrast_ylim'][0] h = plot_kwargs['contrast_ylim'][1] low = -1 if l < -1 else l high = 1 if h > 1 else h contrast_axes.set_ylim(low, high) else: contrast_axes.set_ylim(custom_contrast_ylim) # If 0 lies within the ylim of the contrast axes, # draw a zero reference line. contrast_axes_ylim = contrast_axes.get_ylim() if contrast_axes_ylim[0] < contrast_axes_ylim[1]: contrast_ylim_low, contrast_ylim_high = contrast_axes_ylim else: contrast_ylim_high, contrast_ylim_low = contrast_axes_ylim if contrast_ylim_low < 0 < contrast_ylim_high: contrast_axes.axhline(y=0, lw=0.75, color=ytick_color) # Compute the end of each x-axes line. rightend_ticks = np.array([len(i) - 1 for i in idx]) + np.array(ticks_to_skip) for ax in [rawdata_axes, contrast_axes]: sns.despine(ax=ax, bottom=True) ylim = ax.get_ylim() xlim = ax.get_xlim() redraw_axes_kwargs['y'] = ylim[0] for k, start_tick in enumerate(ticks_to_skip): end_tick = rightend_ticks[k] ax.hlines(xmin=start_tick, xmax=end_tick, **redraw_axes_kwargs) ax.set_ylim(ylim) del redraw_axes_kwargs['y'] # Set raw axes y-label. swarm_label = plot_kwargs['swarm_label'] if swarm_label is None and yvar is None: swarm_label = "value" elif swarm_label is None and yvar is not None: swarm_label = yvar # Place contrast axes y-label. contrast_label_dict = { 'mean_diff': "mean difference", 'median_diff': "median difference", 'cohens_d': "Cohen's d", 'hedges_g': "Hedges' g", 'cliffs_delta': "Cliff's delta" } default_contrast_label = contrast_label_dict[ EffectSizeDataFrame.effect_size] if plot_kwargs['contrast_label'] is None: if is_paired is True: contrast_label = "paired\n{}".format(default_contrast_label) else: contrast_label = default_contrast_label contrast_label = contrast_label.capitalize() else: contrast_label = plot_kwargs['contrast_label'] contrast_axes.set_ylabel(contrast_label) if float_contrast is True: contrast_axes.yaxis.set_label_position("right") # Set the rawdata axes labels appropriately rawdata_axes.set_ylabel(swarm_label) rawdata_axes.set_xlabel("") # Because we turned the axes frame off, we also need to draw back # the y-spine for both axes. og_xlim_raw = rawdata_axes.get_xlim() rawdata_axes.vlines(og_xlim_raw[0], og_ylim_raw[0], og_ylim_raw[1], **redraw_axes_kwargs) og_xlim_contrast = contrast_axes.get_xlim() if float_contrast is True: xpos = og_xlim_contrast[1] else: xpos = og_xlim_contrast[0] og_ylim_contrast = contrast_axes.get_ylim() contrast_axes.vlines(xpos, og_ylim_contrast[0], og_ylim_contrast[1], **redraw_axes_kwargs) # Make sure no stray ticks appear! rawdata_axes.xaxis.set_ticks_position('bottom') rawdata_axes.yaxis.set_ticks_position('left') contrast_axes.xaxis.set_ticks_position('bottom') if float_contrast is False: contrast_axes.yaxis.set_ticks_position('left') # Reset rcParams. for parameter in _changed_rcParams: plt.rcParams[parameter] = original_rcParams[parameter] # Return the figure. return fig
def test_flt(): """ Disperse a direct FLT """ import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt import matplotlib.gridspec import numpy as np import grizli reload(grizli.model); reload(grizli) ### (demo on aligned, background-subtracted FLT images) ######### ### Initialize the GrismFLT object flt = grizli.model.GrismFLT(flt_file='ibhj34h8q_flt.fits', direct_image='ibhj34h6q_flt.fits', refimage=None, segimage=None, verbose=True, pad=0) ## Make a catalog/segmetnation image from the direct FLT and make a full ## grism model for those detected objects flt.photutils_detection(detect_thresh=2, grow_seg=5, gauss_fwhm=2., compute_beams=['A','B', 'C','D'], verbose=True, save_detection=False, wcs=None) ## Find object near (x,y) = (495, 749) #dr = np.sqrt((flt.catalog['xcentroid']-330)**2+(flt.catalog['ycentroid']-744)**2) dr = np.sqrt((flt.catalog['xcentroid']-495)**2+(flt.catalog['ycentroid']-749)**2) dr = np.sqrt((flt.catalog['xcentroid']-712)**2+(flt.catalog['ycentroid']-52)**2) ix = np.argmin(dr) id, x0, y0 = flt.catalog['id'][ix], flt.catalog['xcentroid'][ix]+1, flt.catalog['ycentroid'][ix]+1 ## Get basic trace parameters, `conf` is a grizli.grism.aXeConf object, here for G141 & F140W # x pixels from the center of the direct image dx = np.arange(220) # ytrace and wavelength at x=dx dy, lam = flt.conf.get_beam_trace(x=x0, y=y0, dx=dx, beam='A') fig = plt.figure(figsize=[5,1.5]) #fig = plt.Figure(figsize=[5,1.5]) ax = fig.add_subplot(111) ax.imshow(flt.im_data['SCI'], cmap='gray_r', vmin=-0.05, vmax=0.2, interpolation='Nearest', aspect='auto') ax.set_xlim(x0-10, x0+230) ax.set_ylim(y0-10, y0+10) ax.plot(x0+dx-1, y0+dy-1, color='red', linewidth=3, alpha=0.7) ## 0.1 micron tick marks along the trace as in the next figure xint = np.interp(np.arange(1,1.81,0.1), lam/1.e4, dx) yint = np.interp(np.arange(1,1.81,0.1), lam/1.e4, dy) ax.scatter(x0+xint-1, y0+yint-1, marker='o', color='red', alpha=0.8) ax.set_xlabel(r'$x$ (FLT)'); ax.set_ylabel(r'$y$ (FLT)') fig.tight_layout(pad=0.1) fig.savefig('grizli_demo_0.pdf') ######### ### Spectrum cutout beam = grizli.model.BeamCutout(id=id, x=x0, y=y0, cutout_dimensions=[18,18], conf=flt.conf, GrismFLT=flt) # (mask bad pixel) beam.cutout_seg[(beam.thumb/beam.photflam > 100) | (beam.thumb < 0)] = 0 beam.total_flux = np.sum(beam.thumb[beam.cutout_seg == beam.id]) ### Compute the model in the FLT frame for a single object model_id = flt.compute_model(id=id, x=x0, y=y0, sh=[80,80], in_place=False).reshape(flt.sh_pad) beam.contam = beam.get_cutout(flt.model-model_id) ## 1D optimal extraction (Horne 1986) xspec, yspec, yerr = beam.optimal_extract(beam.cutout_sci, bin=0) ## Simple flat flambda continuum model, normalized at F140W beam.compute_model(beam.thumb, id=beam.id) cmodel = beam.model*1. xspecm, yspecm, yerrm = beam.optimal_extract(cmodel, bin=0) ## Fit simple line + continuum slope line_centers, coeffs, chi2, lmask, lmodel, l0, lflux = beam.simple_line_fit(fwhm=5., grid=[1.12e4, 1.65e4, 1, 20]) xspecl, yspecl, yerrl = beam.optimal_extract(lmodel, bin=0) ### Make a figure fig = plt.figure(figsize=(8,4)) #fig = plt.Figure(figsize=(8,4)) ## 1D plots gsb = matplotlib.gridspec.GridSpec(3,1) ax = fig.add_subplot(gsb[-2:,:]) ax.errorbar(xspec/1.e4, yspec, yerr, linestyle='None', marker='o', markersize=3, color='black', alpha=0.5, label='Data (id=%d)' %(beam.id)) ax.plot(xspecm/1.e4, yspecm, color='red', linewidth=2, alpha=0.8, label=r'Flat $f_\lambda$ (%s)' %(beam.filter)) ax.plot(xspecl/1.e4, yspecl, color='orange', linewidth=2, alpha=0.8, label='Cont+line (%.3f, %.2e)' %(l0/1.e4, lflux*1.e-17)) ax.legend(fontsize=8, loc='lower center', scatterpoints=1) ax.set_xlabel(r'$\lambda$'); ax.set_ylabel('flux (e-/s)') ax = fig.add_subplot(gsb[-3,:]) ax.plot(line_centers/1.e4, chi2/lmask.sum()) ax.set_xticklabels([]) ax.set_ylabel(r'$\chi^2/(\nu=%d)$' %(lmask.sum())) xt = np.arange(1.,1.82,0.1) for ax in fig.axes: ax.set_xlim(1., 1.8) ax.set_xticks(xt) axt = ax.twiny() axt.set_xlim(np.array(ax.get_xlim())*1.e4/6563.-1) axt.set_xlabel(r'$z_\mathrm{H\alpha}$') ## 2D spectra gst = matplotlib.gridspec.GridSpec(3,1) if 'viridis_r' in plt.colormaps(): cmap = 'viridis_r' else: cmap = 'cubehelix_r' ax = fig.add_subplot(gst[0,:]) ax.imshow(beam.cutout_sci, vmin=-0.05, vmax=0.2, cmap=cmap, interpolation='Nearest', origin='lower', aspect='auto') ax.set_ylabel('Observed') ax = fig.add_subplot(gst[1,:]) ax.imshow(lmodel+beam.contam, vmin=-0.05, vmax=0.2, cmap=cmap, interpolation='Nearest', origin='lower', aspect='auto') ax.set_ylabel('Model') ax = fig.add_subplot(gst[2,:]) ax.imshow(beam.cutout_sci-lmodel-beam.contam, vmin=-0.05, vmax=0.2, cmap=cmap, interpolation='Nearest', origin='lower', aspect='auto') ax.set_ylabel('Resid.') for ax in fig.axes[-3:]: ax.set_yticklabels([]) xi = np.interp(xt, beam.wave/1.e4, np.arange(beam.shg[1])) xl = np.interp([1,1.8], beam.wave/1.e4, np.arange(beam.shg[1])) ax.set_xlim(xl) ax.set_xticks(xi) ax.set_xticklabels([]) gsb.tight_layout(fig, pad=0.1,h_pad=0.01, rect=(0,0,0.5,1)) gst.tight_layout(fig, pad=0.1,h_pad=0.01, rect=(0.5,0.1,1,0.9)) fig.savefig('grizli_demo_1.pdf') # ## Emission line with calibrated flux # lpar = lflux*1.e-17,l0, 3 # line parameters: flux, wave, rms width # wave_array = np.arange(lpar[1]-100, lpar[1]+100, lpar[2]/10.) # gauss_line = lpar[0]/np.sqrt(2*np.pi*lpar[2]**2)*np.exp(-(wave_array - lpar[1])**2/2/lpar[2]**2) # line_model = beam.compute_model(beam.thumb/beam.total_flux, xspec=wave_array, yspec=gauss_line, id=beam.id, in_place=False) # xspecl, yspecl, yerrl = beam.optimal_extract(line_model.reshape(beam.shg), bin=0) # plt.plot(xspecl, yspecl+yspecm, color='green', linewidth=2, alpha=0.8, linestyle='steps-mid') # # ## Check line flux with pysynphot # import pysynphot as S # g = S.ArraySpectrum(wave_array, gauss_line, fluxunits='flam') # g = S.GaussianSource(lflux*1.e-17, l0, lpar[2]*2.35) # bp = S.ObsBandpass('wfc3,ir,g141') # obs = S.Observation(g, bp) #, force='extrap') # print 'Model countrate: %.2f, pySynphot countrate: %.2f' %(line_model.sum(), obs.countrate()) #######################################