def plot_cpu_usr_sys(dstat_dict): figs = [x for x in dstat_dict.keys() if "cpu" in x] if not figs: print "No CPU data available to plot" datetimes = [DT.datetime.strptime(t, "%d-%m %H:%M:%S") for t in dstat_dict['time']] hfmt = dates.DateFormatter('%H:%M:%S') if len(figs) == 1: plots = [plots] for i in range(len(figs)): f, plots = pl.subplots(1) plots.plot(datetimes, dstat_dict[figs[i]]['usr'], 'r-', label="usr", linewidth=.5) plots.plot(datetimes, dstat_dict[figs[i]]['sys'], 'b-', label="sys", linewidth=.5) plots.set_title(figs[i]+" usr/sys") plots.legend(loc='best') plots.xaxis.set_major_locator(dates.MinuteLocator()) plots.xaxis.set_major_formatter(hfmt) pl.xticks(rotation='vertical') pl.subplots_adjust(bottom=.15) pl.autoscale() fil = figs[i]+"-usr-sys.svg" print fil pl.savefig(fil, format="svg")
def getOptCandGamma(cv_train, cv_label): print "Finding optimal C and gamma for SVM with RBF Kernel" C_range = 10.0 ** np.arange(-2, 9) gamma_range = 10.0 ** np.arange(-5, 4) param_grid = dict(gamma=gamma_range, C=C_range) cv = StratifiedKFold(y=cv_label, n_folds=40) # Use the svm.SVC() as the cost function to evaluate parameter choices # NOTE: Perhaps we should run computations in parallel if needed. Does it # do that already within the class? grid = GridSearchCV(svm.SVC(), param_grid=param_grid, cv=cv) grid.fit(cv_train, cv_label) score_dict = grid.grid_scores_ scores = [x[1] for x in score_dict] scores = np.array(scores).reshape(len(C_range), len(gamma_range)) pl.figure(figsize=(8,6)) pl.subplots_adjust(left=0.05, right=0.95, bottom=0.15, top=0.95) pl.imshow(scores, interpolation='nearest', cmap=pl.cm.spectral) pl.xlabel('gamma') pl.ylabel('C') pl.colorbar() pl.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45) pl.yticks(np.arange(len(C_range)), C_range) pl.show() print "The best classifier is: ", grid.best_estimator_
def simplegrid(): nzones = 7 gr = gpu.grid(nzones, xmin=0, xmax=1) gpu.drawGrid(gr, edgeTicks=0) # label a few cell-centers gpu.labelCenter(gr, nzones/2, r"$i$") gpu.labelCenter(gr, nzones/2-1, r"$i-1$") gpu.labelCenter(gr, nzones/2+1, r"$i+1$") # label a few edges gpu.labelEdge(gr, nzones/2, r"$i-1/2$") gpu.labelEdge(gr, nzones/2+1, r"$i+1/2$") # draw an average quantity gpu.drawCellAvg(gr, nzones/2, 0.4, color="r") gpu.labelCellAvg(gr, nzones/2, 0.4, r"$\,\langle a \rangle_i$", color="r") pylab.axis([gr.xmin-1.5*gr.dx,gr.xmax+1.5*gr.dx, -0.25, 1.5]) pylab.axis("off") pylab.subplots_adjust(left=0.05,right=0.95,bottom=0.05,top=0.95) f = pylab.gcf() f.set_size_inches(10.0,2.5) pylab.savefig("simplegrid2.png") pylab.savefig("simplegrid2.eps")
def plot_variable_importance(feature_importance, names_cols, save_name, save): """Show Variable importance graph.""" # scale by max importance first 20 variables in column names feature_importance = feature_importance / feature_importance.max() sorted_idx = np.argsort(feature_importance)[::-1][:20] barPos = np.arange(sorted_idx.shape[0]) + .8 barPos = barPos[::-1] #plot.figure(num=None, facecolor='w', edgecolor='r') plot.figure(num=None, facecolor='w') plot.barh(barPos, feature_importance[sorted_idx]*100, align='center') plot.yticks(barPos, names_cols[sorted_idx]) plot.xticks(np.arange(0, 120, 20), \ ['0 %', '20 %', '40 %', '60 %', '80 %', '100 %']) plot.margins(0.02) plot.subplots_adjust(bottom=0.15) plot.title('Variable Importance') if save: plot.savefig(save_name, bbox_inches='tight', dpi = 300) plot.close("all") else: plot.show()
def savePlotSequence(fileBase,stride=1,imgRatio=(5,7),title=None,titleFrames=20,lastFrames=30): '''Save sequence of plots, each plot corresponding to one line in history. It is especially meant to be used for :yref:`yade.utils.makeVideo`. :param stride: only consider every stride-th line of history (default creates one frame per each line) :param title: Create title frame, where lines of title are separated with newlines (``\\n``) and optional subtitle is separated from title by double newline. :param int titleFrames: Create this number of frames with title (by repeating its filename), determines how long the title will stand in the movie. :param int lastFrames: Repeat the last frame this number of times, so that the movie does not end abruptly. :return: List of filenames with consecutive frames. ''' createPlots(subPlots=True,scatterSize=60,wider=True) sqrtFigs=math.sqrt(len(plots)) pylab.gcf().set_size_inches(8*sqrtFigs,5*sqrtFigs) # better readable pylab.subplots_adjust(left=.05,right=.95,bottom=.05,top=.95) # make it more compact if len(plots)==1 and plots[plots.keys()[0]]==None: # only pure snapshot is there pylab.gcf().set_size_inches(5,5) pylab.subplots_adjust(left=0,right=1,bottom=0,top=1) #if not data.keys(): raise ValueError("plot.data is empty.") pltLen=max(len(data[data.keys()[0]]) if data else 0,len(imgData[imgData.keys()[0]]) if imgData else 0) if pltLen==0: raise ValueError("Both plot.data and plot.imgData are empty.") global current, currLineRefs ret=[] print 'Saving %d plot frames, it can take a while...'%(pltLen) for i,n in enumerate(range(0,pltLen,stride)): current=n for l in currLineRefs: l.update() out=fileBase+'-%03d.png'%i pylab.gcf().savefig(out) ret.append(out) if len(ret)==0: raise RuntimeError("No images created?!") if title: titleImgName=fileBase+'-title.png' createTitleFrame(titleImgName,Image.open(ret[-1]).size,title) ret=titleFrames*[titleImgName]+ret if lastFrames>1: ret+=(lastFrames-1)*[ret[-1]] return ret
def show_image(*imgs): for idx, img in enumerate(imgs): subplot = 101 + len(imgs)*10 +idx pl.subplot(subplot) pl.imshow(img, cmap=pl.cm.gray) pl.gca().set_axis_off() pl.subplots_adjust(0.02, 0, 0.98, 1, 0.02, 0)
def command(args): from pylab import bar, yticks, subplots_adjust, show from numpy import arange import sr.tools.bom.bom as bom import sr.tools.bom.parts_db as parts_db db = parts_db.get_db() m = bom.MultiBoardBom(db) m.load_boards_args(args.arg) m.prime_cache() prices = [] for srcode, pg in m.items(): if srcode == "sr-nothing": continue prices.append((srcode, pg.get_price())) prices.sort(key=lambda x: x[1]) bar(0, 0.8, bottom=range(0, len(prices)), width=[x[1] for x in prices], orientation='horizontal') yticks(arange(0, len(prices)) + 0.4, [x[0] for x in prices]) subplots_adjust(left=0.35) show()
def show_frequencies(video_filename, bounds=None): """Graph the average value of the video as well as the frequency strength""" original_video, fps = load_video(video_filename) print fps averages = [] if bounds: for x in range(1, original_video.shape[0] - 1): averages.append(original_video[x, bounds[2]:bounds[3], bounds[0]:bounds[1], :].sum()) else: for x in range(1, original_video.shape[0] - 1): averages.append(original_video[x, :, :, :].sum()) charts_x = 1 charts_y = 2 pylab.figure(figsize=(charts_y, charts_x)) pylab.subplots_adjust(hspace=.7) pylab.subplot(charts_y, charts_x, 1) pylab.title("Pixel Average") pylab.plot(averages) frequencies = scipy.fftpack.fftfreq(len(averages), d=1.0 / fps) pylab.subplot(charts_y, charts_x, 2) pylab.title("FFT") pylab.axis([0, 15, -2000000, 5000000]) pylab.plot(frequencies, scipy.fftpack.fft(averages)) pylab.show()
def draw(self): print self.edgeno pos = 0 dy = 8 edgeno = self.edgeno edge = self.edges[edgeno] edgeprev = self.edges[edgeno-1] p = np.round(edge["top"](1024)) top = min(p+2*dy, 2048) bot = min(p-2*dy, 2048) self.cutout = self.flat[1][bot:top,:].copy() pl.figure(1) pl.clf() start = 0 dy = 512 for i in xrange(2048/dy): pl.subplot(2048/dy,1,i+1) pl.xlim(start, start+dy) if i == 0: pl.title("edge %i] %s|%s" % (edgeno, edgeprev["Target_Name"], edge["Target_Name"])) pl.subplots_adjust(left=.07,right=.99,bottom=.05,top=.95) pl.imshow(self.flat[1][bot:top,start:start+dy], extent=(start, start+dy, bot, top), cmap='Greys', vmin=2000, vmax=6000) pix = np.arange(start, start+dy) pl.plot(pix, edge["top"](pix), 'r', linewidth=1) pl.plot(pix, edgeprev["bottom"](pix), 'r', linewidth=1) pl.plot(edge["xposs_top"], edge["yposs_top"], 'o') pl.plot(edgeprev["xposs_bot"], edgeprev["yposs_bot"], 'o') hpp = edge["hpps"] pl.axvline(hpp[0],ymax=.5, color='blue', linewidth=5) pl.axvline(hpp[1],ymax=.5, color='red', linewidth=5) hpp = edgeprev["hpps"] pl.axvline(hpp[0],ymin=.5,color='blue', linewidth=5) pl.axvline(hpp[1],ymin=.5,color='red', linewidth=5) if False: L = top-bot Lx = len(edge["xposs"]) for i in xrange(Lx): xp = edge["xposs"][i] frac1 = (edge["top"](xp)-bot-1)/L pl.axvline(xp,ymin=frac1) for xp in edgeprev["xposs"]: frac2 = (edgeprev["bottom"](xp)-bot)/L pl.axvline(xp,ymax=frac2) start += dy
def test(cv,model,data,user,code,comp): test_power=np.array([float(r[2+comp])/max_power for r in data ]) times=[datetime.datetime.strptime(r[0],'%Y-%m-%d %H:%M:%S UTC') for r in data] features=np.array([d[8:] for d in data],dtype=np.float) features[:,0]=features[:,0]/time_scale jobs=list(set([(r[1],r[2]) for r in data])) name_features=cv.transform([d[2] for d in data]).toarray() features=np.hstack((features,name_features)) job_ids=[r[1] for r in data] prediction=model.predict(features) rmse=math.sqrt(np.average(((prediction-test_power)*max_power)**2)) nrmse=math.sqrt(np.average(((prediction-test_power)/test_power)**2)) corr=np.corrcoef(prediction,test_power)[0,1] r2=1-(sum((prediction-test_power)**2)/sum((test_power-np.average(test_power))**2)) pl.figure(figsize=(6,7)) pl.subplot(211) pl.plot(prediction*max_power,test_power*max_power,'+') if math.isnan(corr) or math.isnan(r2) or math.isnan(rmse): pl.title("RMSPE="+str(nrmse)+"RMSE="+str(rmse)+" Corr="+str(corr)+" R2="+str(r2)) else: pl.title("RMSPE="+str(int(nrmse*1000)/1000.0)+" RMSE="+str(int(rmse*1000)/1000.0)+" Corr="+str(int(corr*1000)/1000.0)+" R2="+str(int(r2*1000)/1000.0)) pl.xlabel('Predicted power') pl.ylabel('Real power') pl.plot([max(pl.xlim()[0],pl.ylim()[0]),min(pl.xlim()[1],pl.ylim()[1])],[max(pl.xlim()[0],pl.ylim()[0]),min(pl.xlim()[1],pl.ylim()[1])]) pl.subplot(212) pl.plot(test_power*max_power) pl.plot(prediction*max_power) pl.ylabel('Power') pl.xlabel('Data point') #pl.legend(('Real power','Predicted power')) pl.subplots_adjust(hspace=0.35) pl.savefig('results'+str(month)+'global'+str(min_train)+'/'+user+code+'.pdf') pl.close() pkl.dump((nrmse,rmse,corr,r2,prediction*max_power,test_power*max_power,times,job_ids),file=gzip.open('results'+str(month)+'global'+str(min_train)+'/'+user+'test'+code+'.pkl.gz','w')) return prediction*max_power
def plot_vector_diff_F160W(): """ Using the xym2mat analysis on the F160W filter in the 2010 data set, plot up the positional offset vectors for each reference star in each star list relative to the average list. This should show us if there are systematic problems with the distortion solution or PSF variations. """ x, y, m, xe, ye, me, cnt = load_catalog() dx = x - x[0] dy = y - y[0] dr = np.hypot(dx, dy) py.clf() py.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95) for ii in range(1, x.shape[0]): idx = np.where((x[ii,:] != 0) & (y[ii,:] != 0) & (dr[ii,:] < 0.1) & (xe < 0.05) & (ye < 0.05))[0] py.clf() q = py.quiver(x[0,idx], y[0,idx], dx[ii, idx], dy[ii, idx], scale=1.0) py.quiverkey(q, 0.9, 0.9, 0.03333, label='2 mas', color='red') py.title('{0} stars in list {1}'.format(len(idx), ii)) py.xlim(0, 4500) py.ylim(0, 4500) foo = input('Continue?') if foo == 'q' or foo == 'Q': break
def plot_C_gamma_grid_search(grid, C_range, gamma_range, score): ''' Plots the scores computed on a grid. Arguments: grid - the grid search object created using GridSearchCV() C_range - the C parameter range gamma_range - the gamma parameter range score - the scoring function ''' # grid_scores_ contains parameter settings and scores # We extract just the scores scores = [x[1] for x in grid.grid_scores_] scores = np.array(scores).reshape(len(C_range), len(gamma_range)) # draw heatmap of accuracy as a function of gamma and C pl.figure(figsize=(8, 6)) pl.subplots_adjust(left=0.05, right=0.95, bottom=0.15, top=0.95) pl.imshow(scores, interpolation='nearest', cmap=pl.cm.spectral) pl.title("Grid search on C and gamma for best %s" % score) pl.xlabel('gamma') pl.ylabel('C') pl.colorbar() pl.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45) pl.yticks(np.arange(len(C_range)), C_range) pl.show()
def walker_plot(samples, nwalkers, limit): s = samples.reshape(nwalkers, -1, 4) s = s[:, :limit, :] fig = P.figure(figsize=(8, 10)) ax1 = P.subplot(4, 1, 1) ax2 = P.subplot(4, 1, 2) ax3 = P.subplot(4, 1, 3) ax4 = P.subplot(4, 1, 4) for n in range(len(s)): ax1.plot(s[n, :, 0], "k") ax2.plot(s[n, :, 1], "k") ax3.plot(s[n, :, 2], "k") ax4.plot(s[n, :, 3], "k") ax1.tick_params(axis="x", labelbottom="off") ax2.tick_params(axis="x", labelbottom="off") ax3.tick_params(axis="x", labelbottom="off") ax4.set_xlabel(r"step number") ax1.set_ylabel(r"$t_{smooth}$") ax2.set_ylabel(r"$\tau_{smooth}$") ax3.set_ylabel(r"$t_{disc}$") ax4.set_ylabel(r"$\tau_{disc}$") P.subplots_adjust(hspace=0.1) save_fig = ( "/Users/becky/Projects/Green-Valley-Project/bayesian/find_t_tau/gv/not_clean/walkers_steps_all_" + str(time.strftime("%H_%M_%d_%m_%y")) + ".pdf" ) fig.savefig(save_fig) return fig
def animation_compare(filename1, filename2, prefix, tend): num = 0 for t in timestep(0,tend): t1,s1 = FieldInitializer.LoadState(filename1, t) t2,s2 = FieldInitializer.LoadState(filename2, t) rot1 = s1.CalculateRotationRodrigues()['z'] rot2 = s2.CalculateRotationRodrigues()['z'] pylab.figure(figsize=(5.12*2,6.12)) rho = s2.CalculateRhoFourier().modulus() rot = s2.CalculateRotationRodrigues()['z'] pylab.subplot(121) #pylab.imshow(rho*(rho>0.5), alpha=1, cmap=pylab.cm.bone_r) pylab.imshow(rot1) pylab.xticks([]) pylab.yticks([]) pylab.xlabel(r'$d_0$', fontsize=25) pylab.subplot(122) pylab.imshow(rot2) pylab.xticks([]) pylab.yticks([]) pylab.subplots_adjust(0.,0.,1.,1.,0.01,0.05) pylab.xlabel(r'$d_0/2$', fontsize=25) pylab.suptitle("Nabarro-Herring", fontsize=25) pylab.savefig("%s%04i.png" %(prefix, num)) pylab.close('all') num = num + 1
def plot_bases(self, autoscale=True, stampsize=None): import pylab as plt N = len(self.psfbases) cols = int(np.ceil(np.sqrt(N))) rows = int(np.ceil(N / float(cols))) plt.clf() plt.subplots_adjust(hspace=0, wspace=0) cut = 0 if stampsize is not None: H, W = self.shape assert H == W cut = max(0, (H - stampsize) / 2) ima = dict(interpolation="nearest", origin="lower") if autoscale: mx = self.psfbases.max() ima.update(vmin=-mx, vmax=mx) nil, xpows, ypows = self.polynomials(0.0, 0.0, powers=True) for i, (xp, yp, b) in enumerate(zip(xpows, ypows, self.psfbases)): plt.subplot(rows, cols, i + 1) if cut > 0: b = b[cut:-cut, cut:-cut] if autoscale: plt.imshow(b, **ima) else: mx = np.abs(b).max() plt.imshow(b, vmin=-mx, vmax=mx, **ima) plt.xticks([]) plt.yticks([]) plt.title("x^%i y^%i" % (xp, yp)) plt.suptitle("PsfEx eigen-bases")
def default_frame31shareX(self, data_label, x_tpl, y1_tpl, y2_tpl, y3_tpl, pltstyle_dict): x, labelx = x_tpl y1, label1 = y1_tpl y2, label2 = y2_tpl y3, label3 = y3_tpl pylab.subplots_adjust(hspace=0.001) self.ax1 = pylab.subplot(311) self.ax1.plot(x, y1, label=data_label, **pltstyle_dict) pylab.ylabel(label1, fontsize=20, horizontalalignment='left') pylab.yticks(fontsize=13) #self.ax1.yaxis.set_label_coords(-.12, 0.5) self.ax1.yaxis.set_label_coords(-.10, 0.25) pylab.ylim(-30, -10) self.ax1.legend() self.ax2 = pylab.subplot(312, sharex=self.ax1) self.ax2.plot(x, y2, **pltstyle_dict) self.ax2.yaxis.tick_right() pylab.ylim(0, 3) self.ax2.yaxis.set_label_coords(1.12, 0.5) pylab.ylabel(label2, fontsize=20) self.ax3 = pylab.subplot(313, sharex=self.ax1) self.ax3.plot(x, y3, **pltstyle_dict) xticklabels = self.ax1.get_xticklabels()+self.ax2.get_xticklabels() pylab.setp(xticklabels, visible=False) pylab.ylabel(label3, fontsize=20) pylab.xlabel(labelx, fontsize=20) pylab.xticks(fontsize=13)
def gui_repr(self): """Generate a GUI to represent the sentence alignments """ if __pylab_loaded__: fig_width = max(len(self.text_e), len(self.text_f)) + 1 fig_height = 3 pylab.figure(figsize=(fig_width*0.8, fig_height*0.8), facecolor='w') pylab.box(on=False) pylab.subplots_adjust(left=0, right=1, bottom=0, top=1) pylab.xlim(-1, fig_width - 1) pylab.ylim(0, fig_height) pylab.xticks([]) pylab.yticks([]) e = [0 for _ in xrange(len(self.text_e))] f = [0 for _ in xrange(len(self.text_f))] for (i, j) in self.align: e[i] = 1 f[j] = 1 # draw the middle line pylab.arrow(i, 2, j - i, -1, color='r') for i in xrange(len(e)): # draw e side line pylab.text(i, 2.5, self.text_e[i], ha = 'center', va = 'center', rotation=30) if e[i] == 1: pylab.arrow(i, 2.5, 0, -0.5, color='r', alpha=0.3, lw=2) for i in xrange(len(f)): # draw f side line pylab.text(i, 0.5, self.text_f[i], ha = 'center', va = 'center', rotation=30) if f[i] == 1: pylab.arrow(i, 0.5, 0, 0.5, color='r', alpha=0.3, lw=2) pylab.draw()
def main(): args = sys.argv[1:] dataset_path = None if args and '-save' in args: try: dataset_path = args[args.index('-save') + 1] except: dataset_path = 'dataset.p' # Generate the dataset print "...Generating Dataset..." X1, Y1 = make_circles(n_samples=800, noise=0.07, factor=0.4) frac0 = len(np.where(Y1 == 0)[0]) / float(len(Y1)) frac1 = len(np.where(Y1 == 1)[0]) / float(len(Y1)) print "Percentage of '0' labels:", frac0 print "Percentage of '1' labels:", frac1 # (Optionally) save the dataset to DATASET_PATH if dataset_path: print "...Saving dataset to {0}...".format(dataset_path) pickle.dump((X1, Y1, frac0, frac1), open(dataset_path, 'wb')) # Plot the dataset print "...Showing dataset in new window..." pl.figure(figsize=(10, 8)) pl.subplots_adjust(bottom=.05, top=.9, left=.05, right=.95) pl.subplot(111) pl.title("Our Dataset: N=200, '0': {0} '1': {1} ".format(frac0, frac1), fontsize="large") pl.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1) pl.show() print "...Done."
def __init__(self, x, y): f = matplotlib.figure.Figure(figsize=(5, 4), dpi=100) # f = matplotlib.figure.Figure(dpi=100) ax = f.add_subplot(111) canvas = ax.figure.canvas line, = p.plot(x, y, animated=True, lw=2) canvas = FigureCanvasTkAgg(f, master=root) canvas.show() canvas.get_tk_widget().grid() toolbar = NavigationToolbar2TkAgg(canvas, root) toolbar.update() canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) p.subplots_adjust(left=-0.1, bottom=0.0) manager = p.get_current_fig_manager() manager.window.after(100, self.run) self.canvas = canvas self.ax = ax self.line = line self.x = x self.y = y
def walker_plot(samples, nwalkers, limit): s = samples.reshape(nwalkers, -1, 4) s = s[:,:limit, :] fig = P.figure(figsize=(8,10)) ax1 = P.subplot(4,1,1) ax2 = P.subplot(4,1,2) ax3 = P.subplot(4,1,3) ax4 = P.subplot(4,1,4) for n in range(len(s)): ax1.plot(s[n,:,0], 'k') ax2.plot(s[n,:,1], 'k') ax3.plot(s[n,:,2], 'k') ax4.plot(s[n,:,3], 'k') ax1.tick_params(axis='x', labelbottom='off') ax2.tick_params(axis='x', labelbottom='off') ax3.tick_params(axis='x', labelbottom='off') ax4.set_xlabel(r'step number') ax1.set_ylabel(r'$t_{smooth}$') ax2.set_ylabel(r'$\tau_{smooth}$') ax3.set_ylabel(r'$t_{disc}$') ax4.set_ylabel(r'$\tau_{disc}$') P.subplots_adjust(hspace=0.1) save_fig = '/Users/becky/Projects/Green-Valley-Project/bayesian/find_t_tau/walkers_steps_red_s_'+str(time.strftime('%H_%M_%d_%m_%y'))+'.pdf' fig.savefig(save_fig) return fig
def plotall(delta=1.5,nrow=3,ncol=3): sorted_index = np.argsort(pointing_ra) count = 0 nfig = 0 if max_pointing != None: pointing_range = list(range(max_pointing)) else: pointing_range = list(range(len(pointing_ra))) for i in pointing_range: if count == 0: plt.figure(figsize=(10,8)) plt.subplots_adjust(wspace=.2,hspace=.4) allax = [] plt.subplot(nrow,ncol,count+1) zoomin(sorted_index[i]+1,plotsingle=False,delta=delta) plt.title('NSAID '+str(pointing_id[i])) allax.append(plt.gca()) count += 1 if count == nrow*ncol: plt.colorbar(ax = allax,label='$ \log_{10}(M_*/M_\odot) $',fraction=0.08) plt.legend() plt.savefig(outfile_prefix+'plotall-%i.png'%(nfig)) nfig += 1 count = 0 if count != 0: plt.colorbar(ax = allax,label='$ \log_{10}(M_*/M_\odot) $',fraction=0.08) plt.legend() plt.savefig(outfile_prefix+'plotall-%i.png'%(nfig))
def clusterSample(): clusters = [ {"name": "M17", "distance": 2, "ra": "18:20:26", "dec": "-17:10:01"}, {"name": "Wd2", "distance": 5, "ra": "10:23:58", "dec": "-57:45:49"}, {"name": "Wd1", "distance": 5, "ra": "16:47:04", "dec": "-45:51:05"}, {"name": "RSGC1", "distance": 6, "ra": "18:37:58", "dec": "-06:52:53"}, {"name": "RSGC2", "distance": 6, "ra": "18:39:20", "dec": "-06:05:10"}, ] py.close(1) py.figure(1, linewidth=2, figsize=(16, 10)) py.subplots_adjust(left=0.05, right=0.97, bottom=0.1, top=0.95, wspace=0.2, hspace=0.25) for ii in range(len(clusters)): clust = clusters[ii] obj = ephem.FixedBody() obj._ra = ephem.hours(clust["ra"]) obj._dec = ephem.degrees(clust["dec"]) obj._epoch = 2000 obj.compute() gal = ephem.Galactic(obj) longitude = math.degrees(float(gal.lon)) print "" print "%-10s %s %s l = %.1f" % (clust["name"], clust["ra"], clust["dec"], longitude) py.subplot(2, 3, ii + 1) properMotions(longitude, clust["distance"], clust["name"])
def write_rgb(): #g,r,z = [fitsio.read('detmap-%s.fits' % band) for band in 'grz'] g,r,z = [fitsio.read('coadd-%s.fits' % band) for band in 'grz'] plt.figure(figsize=(10,10)) plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95) plt.clf() for (im1,cc),scale in zip([(g,'b'),(r,'g'),(z,'r')], [2.0, 1.2, 0.4]): im = im1 * scale im = im[im != 0] plt.hist(im.ravel(), histtype='step', color=cc, range=[np.percentile(im, p) for p in (1,98)], bins=50) ps.savefig() #rgb = get_rgb_image(g,r,z, alpha=0.8, m=0.02) #rgb = get_rgb_image(g,r,z, alpha=16., m=0.005, m2=0.002, #rgb = get_rgb_image(g,r,z, alpha=32., m=0.01, m2=0.002, rgb = get_rgb_image(g,r,z, alpha=8., m=0.0, m2=0.0, scale_g = 2., scale_r = 1.1, scale_z = 0.5, Q = 10) #for im in g,r,z: # mn,mx = [np.percentile(im, p) for p in [20,99]] # print 'mn,mx:', mn,mx plt.clf() plt.imshow(rgb, interpolation='nearest', origin='lower') ps.savefig() fitsio.write('rgb.fits', rgb)
def plot_stable_features(X_train,y_train,featnames,**kwargs): from sklearn.linear_model import LassoLarsCV,RandomizedLasso n_resampling = kwargs.pop('n_resampling',200) n_jobs = kwargs.pop('n_jobs',-1) with warnings.catch_warnings(): warnings.simplefilter('ignore', UserWarning) # estimate alphas via xvalidation lars_cv = LassoLarsCV(cv=6,n_jobs=n_jobs).fit(X_train,y_train) alphas = np.linspace(lars_cv.alphas_[0], .1 * lars_cv.alphas_[0], 6) clf = RandomizedLasso(alpha=alphas, random_state=42, n_jobs=n_jobs, n_resampling=n_resampling) clf.fit(X_train,y_train) importances = clf.scores_ indices = np.argsort(importances)[::-1] pl.bar(range(len(featnames)), importances[indices], color="r", align="center") pl.xticks(np.arange(len(featnames))+0.5,featnames[indices], rotation=45,horizontalalignment='right') pl.xlim(-0.5,len(featnames)-0.5) pl.subplots_adjust(bottom=0.2) pl.ylim(0,np.max(importances)*1.01) pl.ylabel('Selection frequency (%) for %d resamplings '%n_resampling) pl.title("Stability Selection: Selection Frequencies")
def plot_importances(clf,featnames,outfile,**kwargs): pl.figure(figsize=(16,4)) featnames = np.array(featnames) importances = clf.feature_importances_ imp_std = np.std([tree.feature_importances_ for tree in clf.estimators_], axis=0) indices = np.argsort(importances)[::-1] #for featname in featnames[indices]: # print featname trunc_featnames = featnames[indices] trunc_featnames = trunc_featnames[0:24] trunc_importances = importances[indices] trunc_importances = trunc_importances[0:24] trunc_imp_std = imp_std[indices] trunc_imp_std = trunc_imp_std[0:24] pl.bar(range(len(trunc_featnames)), trunc_importances, color="r", yerr=trunc_imp_std, align="center") pl.xticks(np.arange(len(trunc_featnames))+0.5,trunc_featnames,rotation=45, horizontalalignment='right') pl.xlim(-0.5,len(trunc_featnames)-0.5) pl.ylim(0,np.max(trunc_importances+trunc_imp_std)*1.01) # pl.bar(range(len(featnames)), importances[indices], # color="r", yerr=imp_std[indices], align="center") # pl.xticks(np.arange(len(featnames))+0.5,featnames[indices],rotation=45, # horizontalalignment='right') # pl.xlim(-0.5,len(featnames)-0.5) # pl.ylim(0,np.max(importances+imp_std)*1.01) pl.subplots_adjust(bottom=0.2) pl.show()
def create_fig(self): print "Creating fig..." self.fig_size = (14, 10) self.fig = pylab.figure(figsize=self.fig_size) pylab.subplots_adjust(hspace=0.4) pylab.subplots_adjust(wspace=0.35) return self.fig
def plot_cluster_context(sizes, densities, dir, name=None, k=None, suffix="png"): """ so many conditionals! """ print("plot_cluster_context(): plotting", name) if name is None: K = len(sizes) fn = "{}/clusters_{:04d}.{}".format(dir, K, suffix) else: fn = "{}/{}_context.{}".format(dir, name, suffix) if os.path.exists(fn): print("plot_cluster_context(): {} exists already".format(fn)) return if k is None: fig = plt.figure(figsize=(6,6)) plt.subplots_adjust(left=0.15, right=0.97, bottom=0.15, top=0.97) ms = 7.5 else: fig = plt.figure(figsize=(4,4)) plt.subplots_adjust(left=0.2, right=0.96, bottom=0.2, top=0.96) ms = 5.0 plt.clf() if name is not None and k is None: plt.savefig(fn) print("plot_cluster_context(): wrote", fn) return _clusterplot(sizes, densities, k, ms=ms) _clusterlims(sizes, densities) plt.ylabel("cluster abundance-space density") plt.xlabel("number in abundance-space cluster") plt.loglog() [l.set_rotation(45) for l in plt.gca().get_xticklabels()] [l.set_rotation(45) for l in plt.gca().get_yticklabels()] plt.savefig(fn) print("plot_cluster_context(): wrote", fn)
def corner_plot(s, labels): x, y = s[:,0], s[:,1] fig = P.figure(figsize=(10,10)) ax2 = P.subplot(223) ax2.set_xlabel(labels[0]) ax2.set_ylabel(labels[1]) im = triangle.histo2d(x, y, ax=ax2, extent=[[0, 13.807108309208775],[0, 3.0]]) [l.set_rotation(45) for l in ax2.get_xticklabels()] [j.set_rotation(45) for j in ax2.get_yticklabels()] ax1 = P.subplot(221, xlim=[0, 13.807108309208775]) ax1.tick_params(axis='x', labelbottom='off') ax1.tick_params(axis='y', labelleft='off') ax1.hist(x, bins=50, histtype='step', color='k', range=(0, 13.807108309208775)) ax3 = P.subplot(224) ax3.tick_params(axis='x', labelbottom='off') ax3.tick_params(axis='y', labelleft='off') ax3.hist(y, bins=50, orientation='horizontal', histtype='step',color='k', range=(0,3)) P.subplots_adjust(wspace=0.05) P.subplots_adjust(hspace=0.05) cbar_ax = fig.add_axes([0.55, 0.565, 0.02, 0.405]) cb = fig.colorbar(im, cax = cbar_ax) cb.solids.set_edgecolor('face') cb.set_label(r'predicted SFR $[M_{\odot} yr^{-1}]$', labelpad = 20, fontsize=16) P.tight_layout() return fig
def drawCity(self): """ 作图 :return: """ pl.title("pm25 / time " + str(self.numMonitors) + "_monitors")# give plot a title pl.xlabel('time')# make axis labels pl.ylabel('pm2.5') self.fill_cityPm25List() for monitorStr in self.cityPm25List: data = np.loadtxt(StringIO(monitorStr), dtype=np.dtype([("t", "S13"),("v", float)])) datestr = np.char.replace(data["t"], "T", " ") t = pl.datestr2num(datestr) v = data["v"] pl.plot_date(t, v, fmt="-o") pl.subplots_adjust(bottom=0.3) # pl.legend(loc=4)#指定legend的位置,读者可以自己help它的用法 ax = pl.gca() ax.fmt_xdata = pl.DateFormatter('%Y-%m-%d %H:%M:%S') pl.xticks(rotation=70) # pl.xticks(t, datestr) # 如果以数据点为刻度,则注释掉这一行 ax.xaxis.set_major_formatter(pl.DateFormatter('%Y-%m-%d %H:%M')) pl.grid() pl.show()# show the plot on the screen
def plot_data(yRange=None): ''' Plots and saves the cell measurement data. Returns nothing. ''' fig = plt.figure(figsize=(18,12)) ax = plt.subplot(111) plt.errorbar(range(len(avgCells.index)), avgCells[column], yerr=stdCells[column], fmt='o') ax = plt.gca() ax.set(xticks=range(len(avgCells.index)), xticklabels=avgCells.index) xlims = ax.get_xlim() ax.set_xlim([lim-1 for lim in xlims]) # adjust yRange if it was specified if yRange!=None: ax.set_ylim(yRange) fileName = column + ' exlcuding outliers' else: fileName = column plt.subplots_adjust(bottom=0.2, right=0.98, left=0.05) plt.title(column) plt.ylabel('mm') locs, labels = plt.xticks() plt.setp(labels, rotation=90) mng = plt.get_current_fig_manager() mng.window.state('zoomed') #plt.show() path1 = 'Y:/Test data/ACT02/vision inspection/plot_100_cells/' path2 = 'Y:/Nate/git/nuvosun-python-lib/vision system/plot_100_cells/' fig.savefig(path1 + fileName, bbox_inches = 'tight') fig.savefig(path2 + fileName, bbox_inches = 'tight') plt.close()
pl.ylabel('Total intensity \n[p$^+$]') pl.legend(loc = 'upper left', prop = {'size':14}, bbox_to_anchor=(1,1)) sp2 = pl.subplot(4,1,2, sharex = sp1) pl.plot((array(beams_bct[SPS_user]['timestamp_float'])-t_ref)/3600./24., array(beams_bct[SPS_user]['bct_integrated']),'.') ms.sciy() pl.grid('on') pl.ylabel('BCT integ [p*s]') sp3 = pl.subplot(4,1,3, sharex = sp1) pl.plot((array(beams_ecm[ecm_id][SPS_user]['timestamp_bct'])-t_ref)/3600./24., -array(beams_ecm[ecm_id][SPS_user]['ecld_1st_inj']),'.') ms.sciy() # pl.ylim(-0.1e5, None) pl.grid('on') pl.ylabel('EC signal @1s \n[ECM units]') sp4 = pl.subplot(4,1,4, sharex = sp1) pl.plot((array(beams_ecm[ecm_id][SPS_user]['timestamp_bct'])-t_ref)/3600./24.,-array(beams_ecm[ecm_id][SPS_user]['ecld_max']),'.') pl.xlabel('Time [days]') pl.ylabel('Max. EC signal \n[ECM units]') # pl.ylim(-0.1e5, None) ms.sciy() pl.grid('on') pl.subplots_adjust(left=.12, bottom=.10, right=.84, top=.94, hspace=.32) pl.suptitle('ECM %s from %s'%(ecm_id,t_ref_str)) fig.set_size_inches(15.725*0.75 , 11.1875*0.75) pl.savefig('W16_ecm_evol_ecm%s.png'%ecm_id, dpi=200) pl.show()
pl.ylabel("Counts/bin") pl.xlim([0, 1400]) pl.subplot(236) pl.plot(energy, counts_energy, linewidth=0.5, label='Neutron energy spectrum') pl.plot(energy.ravel()[Gate_bgr], counts_bgr, label='Background region') pl.plot(energy.ravel()[Gate_peak_n0], counts_peak_n0, label='(p,n0) region') pl.plot(energy.ravel()[Gate_peak_n2], counts_peak_n2, label='(p,n2) region') pl.legend(loc='best') pl.xlabel("Energy (MeV)") pl.ylabel("Counts/bin") #pl.xlim([0,1400]) pl.subplots_adjust(top=0.95, bottom=0.08, left=0.06, right=0.98, hspace=0.22, wspace=0.21) #TOF = histo_tof_calibrated[1][:-1] #Counts_TOF = histo_tof_calibrated[0] #f = open("neutron.dat","wb") #f.write("TOF(nsec) Counts_TOF En(MeV) Counts_En\n") #for i in xrange(0,len(TOF),1): # try: f.write("{} {} {} {}\n".format(TOF[i],Counts_TOF[i],energy[i],counts_energy[i]) ) # except: f.write("{} {}\n".format(TOF[i],Counts_TOF[i]) ) #f.close() pl.show()
def PlotVelSig(plotDir, velXarr, velYarr): Njackknifes = len(velXarr) Nstars = [] for i in range(Njackknifes): Nstars.append(len(velXarr[i])) for i in range(np.max(Nstars)): velXstar = [] velYstar = [] for j in range(Njackknifes): if i < Nstars[j]: velXstar.append(velXarr[j][i] * 10.0) velYstar.append(velYarr[j][i] * 10.0) rmsX = np.std(np.array(velXstar)) rmsY = np.std(np.array(velYstar)) maxX = np.max(np.array(velXstar)) minX = np.min(np.array(velXstar)) maxY = np.max(np.array(velYstar)) minY = np.min(np.array(velYstar)) medX = np.median(np.array(velXstar)) medY = np.median(np.array(velYstar)) py.paxes = py.subplot(1, 2, 1) py.xlim(0, np.max(Nstars) + 1) # py.plot(np.zeros(len(velXstar))+(i+1), velXstar, 'k*-', markersize=3) py.errorbar(i + 1, medX, yerr=[[medX - minX], [maxX - medX]], color='k', markersize=0) rwidth = 0.5 rect = py.Rectangle((i + 0.5 + rwidth / 2., medX - rmsX), rwidth, 2 * rmsX, ec='red', fc='red', zorder=10) py.gca().add_patch(rect) py.xlabel('Star #') py.ylabel('X velocity [mas/yr]') if i < Njackknifes: py.plot(np.zeros(1) + (i + 1), velXstar[i], 'b*', markersize=3, markeredgecolor='b', zorder=10) py.paxes = py.subplot(1, 2, 2) py.xlim(0, np.max(Nstars) + 1) py.errorbar(i + 1, medY, yerr=[[medY - minY], [maxY - medY]], color='k', markersize=0) rect = py.Rectangle((i + 0.5 + rwidth / 2., medY - rmsY), rwidth, 2 * rmsY, ec='red', fc='red', zorder=10) py.gca().add_patch(rect) # py.plot(np.zeros(len(velYstar))+(i+1), velYstar, 'k*-') py.xlabel('Star #') py.ylabel('Y velocity [mas/yr]') if i < Njackknifes: py.plot(np.zeros(1) + (i + 1), velYstar[i], 'b*', markersize=3, markeredgecolor='b', zorder=10) py.subplots_adjust(left=0.15, wspace=0.4, hspace=0.33, right=0.9, top=0.9) py.savefig(plotDir + '/Vel_JackKnife.png') py.clf()
def ablateFeaturesForCls(engineCls): mpl.figure() trainer = Trainer() engine = engineCls() trainer.configureClassifier(engine) markers = [ '.', ',', 'v', '^', '<', '>', '1', '2', '3', '4', 's', 'p', '*', 'h', 'H', ] colors = ["b", "g", "r", "c", "m", "y"] sub_engines = [] for i, name in enumerate(sorted(engine.masterList)): sub_engine = engineCls() sub_engine.setFeatureList([name]) sub_engines.append((name, sub_engine)) markers = markers[0:len(sub_engines)] colors = colors[0:len(sub_engines)] sub_engines.append(("all", engineCls())) markers.append("o") colors.append("k") for i, (name, sub_engine) in enumerate(sub_engines): table = trainer.configureClassifier(sub_engine) cv_indices = orange.MakeRandomIndices2(table, p0=0.75) training = table.select(cv_indices, 0, negate=True) testing = table.select(cv_indices, 0, negate=False) #classifier = orange.LogRegLearner(training) classifier = orngBayes.BayesLearner(training) results = orngTest.testOnData([classifier], testing) displayResults(results) line = rocCurve( results, "", stepSize=0.001, marker=markers[i % len(markers)], plotArgs=dict(linewidth=5, markersize=10, color=colors[i % len(colors)]), ) line[0].set_label(name) mpl.title(engine.name(), size=30) mpl.xlabel("FP", fontsize=30) mpl.ylabel("TP", fontsize=30) mpl.xticks([0, 1], fontsize=17) mpl.yticks([0, 1], fontsize=17) mpl.subplots_adjust(bottom=0.14, top=0.91) mpl.legend(loc="lower right", prop=dict(size=17)) mpl.savefig("roc.ablate.%s.png" % engine.name())
def do_plot(plotfile, component, component2, outFile, log, minval, maxval, minval2, maxval2, eps, dpi, origin, annotation, xmin_pass, ymin_pass, zmin_pass, xmax_pass, ymax_pass, zmax_pass): pylab.rc("font", size=9) #-------------------------------------------------------------------------- # construct the output file name #-------------------------------------------------------------------------- if (outFile == ""): outFile = os.path.normpath(plotfile) + "_" + component if (not component2 == ""): outFile += "_" + component2 if (not eps): outFile += ".png" else: outFile += ".eps" else: # make sure the proper extension is used if (not eps): if (not string.rfind(outFile, ".png") > 0): outFile = outFile + ".png" else: if (not string.rfind(outFile, ".eps") > 0): outFile = outFile + ".eps" #-------------------------------------------------------------------------- # read in the meta-data from the plotfile #-------------------------------------------------------------------------- (nx, ny, nz) = fsnapshot.fplotfile_get_size(plotfile) time = fsnapshot.fplotfile_get_time(plotfile) (xmin, xmax, ymin, ymax, zmin, zmax) = \ fsnapshot.fplotfile_get_limits(plotfile) dx = (xmax - xmin) / nx x = xmin + numpy.arange((nx), dtype=numpy.float64) * dx dy = (ymax - ymin) / ny y = ymin + numpy.arange((ny), dtype=numpy.float64) * dy if (nz > 0): dz = (zmax - zmin) / nz z = zmin + numpy.arange((nz), dtype=numpy.float64) * dz if (nz == -1): #---------------------------------------------------------------------- # 2-d plots #---------------------------------------------------------------------- extent = [xmin, xmax, ymin, ymax] ix0 = 0 ix = nx iy0 = 0 iy = ny if (not xmin_pass == None): extent[0] = xmin_pass ix0 = int((xmin_pass - xmin) / dx) if (not ymin_pass == None): extent[2] = ymin_pass iy0 = int((ymin_pass - ymin) / dy) if (not xmax_pass == None): extent[1] = xmax_pass ix = int((xmax_pass - xmin) / dx) if (not ymax_pass == None): extent[3] = ymax_pass iy = int((ymax_pass - ymin) / dy) sparseX = 0 if (ny >= 3 * nx): sparseX = 1 # read in the main component data = numpy.zeros((nx, ny), dtype=numpy.float64) (data, err) = fsnapshot.fplotfile_get_data_2d(plotfile, component, data) if (not err == 0): sys.exit(2) data = numpy.transpose(data) # read in the component #2, if present if (not component2 == ""): data2 = numpy.zeros((nx, ny), dtype=numpy.float64) (data2, err) = fsnapshot.fplotfile_get_data_2d(plotfile, component2, data2) if (not err == 0): sys.exit(2) data2 = numpy.transpose(data2) # log? if log: if (numpy.min(data) < 0): data = numpy.log10(numpy.abs(data)) else: data = numpy.log10(data) if (not component2 == ""): if (numpy.min(data2) < 0.0): data2 = numpy.log10(numpy.abs(data2)) else: data2 = numpy.log10(data2) if (not minval == None): minval = math.log10(minval) if (not maxval == None): maxval = math.log10(maxval) if (not minval2 == None): minval2 = math.log10(minval2) if (not maxval2 == None): maxval2 = math.log10(maxval2) #---------------------------------------------------------------------- # plot main component #---------------------------------------------------------------------- if (not component2 == ""): ax = pylab.subplot(1, 2, 1) pylab.subplots_adjust(wspace=0.5) else: ax = pylab.subplot(1, 1, 1) divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax) im = pylab.imshow(data[iy0:iy, ix0:ix], origin='lower', extent=extent, vmin=minval, vmax=maxval) pylab.title(component) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) if (sparseX): ax.xaxis.set_major_locator(pylab.MaxNLocator(3)) # make space for a colorbar -- getting it the same size as the # vertical extent of the plot is surprisingly tricky. See # http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#colorbar-whose-height-or-width-in-sync-with-the-master-axes ax_cb = divider.new_horizontal(size="10%", pad=0.1) fig1 = ax.get_figure() fig1.add_axes(ax_cb) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cb = pylab.colorbar(im, format=formatter, cax=ax_cb) # make the font size for the colorbar axis labels small. Note # the offsetText is the 10^N that appears at the top of the # y-axis. cl = pylab.getp(cb.ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cb.ax.yaxis.offsetText.set_fontsize("small") # do a fixed offset in pixels from the (xmin,ymin) data point trans = matplotlib.transforms.offset_copy(ax.transData, x=0, y=-0.5, fig=fig1, units='inches') pylab.text(xmin, ymin, "time = %7.3g s" % (time), verticalalignment="bottom", transform=trans, clip_on=False, fontsize=10) if (not annotation == ""): trans = matplotlib.transforms.offset_copy(ax.transData, x=0, y=-0.65, fig=fig1, units='inches') pylab.text(xmin, ymin, "%s" % (annotation), verticalalignment="bottom", transform=trans, clip_on=False, fontsize=10) #---------------------------------------------------------------------- # plot component #2 #---------------------------------------------------------------------- if (not component2 == ""): ax = pylab.subplot(1, 2, 2) divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax) im = pylab.imshow(data2[iy0:iy, ix0:ix], origin='lower', extent=extent, vmin=minval2, vmax=maxval2) pylab.title(component2) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax.xaxis.set_major_formatter( pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter( pylab.ScalarFormatter(useMathText=True)) if (sparseX): ax.xaxis.set_major_locator(pylab.MaxNLocator(3)) # make space for a colorbar -- getting it the same size as # the vertical extent of the plot is surprisingly tricky. # See # http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#colorbar-whose-height-or-width-in-sync-with-the-master-axes ax_cb = divider.new_horizontal(size="10%", pad=0.1) fig1 = ax.get_figure() fig1.add_axes(ax_cb) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cb = pylab.colorbar(im, format=formatter, cax=ax_cb) # make the font size for the colorbar axis labels small. Note the # offsetText is the 10^N that appears at the top of the y-axis. cl = pylab.getp(cb.ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cb.ax.yaxis.offsetText.set_fontsize("small") #ax_cb.yaxis.tick_right() else: #---------------------------------------------------------------------- # 3-d plot #---------------------------------------------------------------------- # starting points for the figure positions # assume that the width of the plotting area is 0.05 to 0.95, # leaving 0.9 to be split amongst the 3 plots. So each has a # width of 0.3 # for the height, we will assume that the colorbar at the # bottom gets 0.15, and that we go until 0.95, leaving 0.8 of # height for the plots. pos1 = [0.05, 0.15, 0.3, 0.8] pos2 = [0.35, 0.15, 0.3, 0.8] pos3 = [0.65, 0.15, 0.3, 0.8] fig = pylab.figure() # read in the slices # x-y data_xy = numpy.zeros((nx, ny), dtype=numpy.float64) indir = 3 (data_xy, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, indir, origin, data_xy) if (not err == 0): sys.exit(2) data_xy = numpy.transpose(data_xy) if log: if (numpy.min(data_xy) < 0): data_xy = numpy.log10(numpy.abs(data_xy)) else: data_xy = numpy.log10(data_xy) # x-z data_xz = numpy.zeros((nx, nz), dtype=numpy.float64) (data_xz, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, 2, origin, data_xz) if (not err == 0): sys.exit(2) data_xz = numpy.transpose(data_xz) if log: if (numpy.min(data_xz) < 0): data_xz = numpy.log10(numpy.abs(data_xz)) else: data_xz = numpy.log10(data_xz) # y-z data_yz = numpy.zeros((ny, nz), dtype=numpy.float64) (data_yz, err) = \ fsnapshot.fplotfile_get_data_3d(plotfile, component, 1, origin, data_yz) if (not err == 0): sys.exit(2) data_yz = numpy.transpose(data_yz) if log: if (numpy.min(data_yz) < 0): data_yz = numpy.log10(numpy.abs(data_yz)) else: data_yz = numpy.log10(data_yz) if (not minval == None): if (log): minval = math.log10(minval) else: minval = numpy.min(data_xy) minval = min(minval, numpy.min(data_xz)) minval = min(minval, numpy.min(data_yz)) if (not maxval == None): if (log): maxval = math.log10(maxval) else: maxval = numpy.max(data_xy) maxval = max(maxval, numpy.max(data_xz)) maxval = max(maxval, numpy.max(data_yz)) # x-y extent = [xmin, xmax, ymin, ymax] ix0 = 0 ix = nx iy0 = 0 iy = ny if (not xmin_pass == None): extent[0] = xmin_pass ix0 = int((xmin_pass - xmin) / dx) if (not ymin_pass == None): extent[2] = ymin_pass iy0 = int((ymin_pass - ymin) / dy) if (not xmax_pass == None): extent[1] = xmax_pass ix = int((xmax_pass - xmin) / dx) if (not ymax_pass == None): extent[3] = ymax_pass iy = int((ymax_pass - ymin) / dy) ax = pylab.subplot(1, 3, 1) pylab.subplots_adjust(wspace=0.4) #fig.add_axes(pos1) im = pylab.imshow(data_xy[iy0:iy, ix0:ix], origin='lower', extent=extent, vmin=minval, vmax=maxval) #, axes=pos1) pylab.xlabel("x") pylab.ylabel("y") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # do a fixed offset in pixels from the (xmin,ymin) data point fig1 = ax.get_figure() trans = matplotlib.transforms.offset_copy(ax.transData, x=0, y=-0.5, fig=fig1, units='inches') # pylab.text(xmin_pass, ymin_pass, "time = %7.3g s" % (time), # verticalalignment="bottom", transform = trans, # clip_on=False, fontsize=10) # x-z extent = [xmin, xmax, zmin, zmax] ix0 = 0 ix = nx iz0 = 0 iz = nz if (not xmin_pass == None): extent[0] = xmin_pass ix0 = int((xmin_pass - xmin) / dx) if (not zmin_pass == None): extent[2] = zmin_pass iz0 = int((zmin_pass - zmin) / dz) if (not xmax_pass == None): extent[1] = xmax_pass ix = int((xmax_pass - xmin) / dx) if (not zmax_pass == None): extent[3] = zmax_pass iz = int((zmax_pass - zmin) / dz) ax = pylab.subplot(1, 3, 2) #fig.add_axes(pos2) im = pylab.imshow(data_xz[iz0:iz, ix0:ix], origin='lower', extent=extent, vmin=minval, vmax=maxval) #, axes=pos2) pylab.xlabel("x") pylab.ylabel("z") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # y-z extent = [ymin, ymax, zmin, zmax] iy0 = 0 iy = ny iz0 = 0 iz = nz if (not ymin_pass == None): extent[0] = ymin_pass iy0 = int((ymin_pass - ymin) / dy) if (not zmin_pass == None): extent[2] = zmin_pass iz0 = int((zmin_pass - zmin) / dz) if (not ymax_pass == None): extent[1] = ymax_pass iy = int((ymax_pass - ymin) / dy) if (not zmax_pass == None): extent[3] = zmax_pass iz = int((zmax_pass - zmin) / dz) ax = pylab.subplot(1, 3, 3) #fig.add_axes(pos3) im = pylab.imshow(data_yz[iz0:iz, iy0:iy], origin='lower', extent=extent, vmin=minval, vmax=maxval) #, axes=pos3) pylab.xlabel("y") pylab.ylabel("z") # axis labels in scientific notation with LaTeX ax = pylab.gca() ax.xaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.yaxis.set_major_formatter(pylab.ScalarFormatter(useMathText=True)) ax.xaxis.offsetText.set_fontsize("small") ax.yaxis.offsetText.set_fontsize("small") cl = pylab.getp(ax, 'ymajorticklabels') pylab.setp(cl, fontsize=10) cl = pylab.getp(ax, 'xmajorticklabels') pylab.setp(cl, fontsize=10) # colorbar pylab.subplots_adjust(bottom=0.1, left=0.05, right=0.95) formatter = matplotlib.ticker.ScalarFormatter(useMathText=True) cax = pylab.axes([0.05, 0.06, 0.9, 0.04]) pylab.colorbar(orientation="horizontal", cax=cax, format=formatter) pylab.title(component) #-------------------------------------------------------------------------- # save the figure #-------------------------------------------------------------------------- # try: pylab.tight_layout() # requires matplotlib >= 1.1 # except: # pass if (not eps): pylab.savefig(outFile, bbox_inches='tight', dpi=dpi, pad_inches=0.33) else: pylab.savefig(outFile) #, bbox_inches='tight', pad_inches=0.33)
plt.show() #asymmetrical arrangements http://matplotlib.org/users/gridspec.html fig = plt.figure() ax1 = plt.subplot2grid((3,3), (0,0), colspan=3); ax1.text(.5,.5,'ax1') ax2 = plt.subplot2grid((3,3), (1,0), colspan=2); ax2.text(.5,.5,'ax2') ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2); ax3.text(.5,.5,'ax3') ax4 = plt.subplot2grid((3,3), (2, 0)); ax4.text(.5,.5,'ax4') ax5 = plt.subplot2grid((3,3), (2, 1)); ax5.text(.5,.5,'ax5') plt.show() ## native matplotlib syntax for graphing fig = plt.figure() plt.subplots_adjust(hspace=0.2) #adjust horiz space between graphs ax1 = plt.subplot(211, title='foreign') #2x1grid 1st graph plt.scatter(auto[auto.foreign=='Foreign'].weight, auto[auto.foreign=='Foreign'].length) ax2 = plt.subplot(212, sharex=ax1, sharey=ax1, title='doemstic') #2x1grid 2nd graph plt.scatter(auto[auto.foreign=='Domestic'].weight, auto[auto.foreign=='Domestic'].length) xticklabels = ax1.get_xticklabels() #+ ax2.get_xticklabels() #drop labelling of 1st x axis plt.setp(xticklabels, visible=False) plt.show() ## pandas syntax for graphing fig = plt.figure() plt.subplots_adjust(hspace=0.2) #adjust horiz space between graphs
def oneblob(ra, dec, addToHeader, cutToPrimary, outdir): plotfn = os.path.join(outdir, 'stamps-%.4f-%.4f.png' % (ra, dec)) if os.path.exists(plotfn): print 'Exists:', plotfn return [] outfns = [] # Resample test blobs to a common pixel grid. sdss = DR9(basedir=tempdir) #sdss.useLocalTree() sdss.saveUnzippedFiles(tempdir) pixscale = 0.396 pixradius = 25 bands = 'ugriz' stamp_pattern = os.path.join(outdir, 'stamp-%%s-%.4f-%.4f.fits' % (ra, dec)) catfn = os.path.join(outdir, 'cat-%.4f-%.4f.fits' % (ra, dec)) plots = False srcband = 'r' Lanczos = 3 W, H = pixradius * 2 + 1, pixradius * 2 + 1 targetwcs = Tan(ra, dec, pixradius + 1, pixradius + 1, -pixscale / 3600., 0., 0., pixscale / 3600., W, H) radius = pixradius * pixscale / 3600. wlistfn = sdss.filenames.get('window_flist', 'window_flist.fits') #wfn = os.path.join(os.environ['PHOTO_RESOLVE'], 'window_flist.fits') RCF = radec_to_sdss_rcf(ra, dec, tablefn=wlistfn) print 'Found', len(RCF), 'fields in range.' keepRCF = [] for run, camcol, field, r, d in RCF: rr = sdss.get_rerun(run, field) print 'Rerun:', rr if rr == '157': continue keepRCF.append((run, camcol, field)) RCF = keepRCF if len(RCF) == 0: print 'No run/camcol/fields in rerun 301' return TT = [] for ifield, (run, camcol, field) in enumerate(RCF): # Retrieve SDSS catalog sources in the field srcs, objs = get_tractor_sources_dr9(run, camcol, field, bandname=srcband, sdss=sdss, radecrad=(ra, dec, radius * np.sqrt(2.)), nanomaggies=True, cutToPrimary=cutToPrimary, getsourceobjs=True, useObjcType=True) print 'Got sources:' for src in srcs: print ' ', src # Write out the sources T = fits_table() T.ra = [src.getPosition().ra for src in srcs] T.dec = [src.getPosition().dec for src in srcs] # for band in bands: # T.set('psfflux_%s' % band, # [src.getBrightness().getBand(band) for src in srcs]) # same objects, same order assert (len(objs) == len(srcs)) assert (np.all(T.ra == objs.ra)) # r-band bandnum = 2 T.primary = ((objs.resolve_status & 256) > 0) T.run = objs.run T.camcol = objs.camcol T.field = objs.field T.is_star = (objs.objc_type == 6) T.frac_dev = objs.fracdev[:, bandnum] T.theta_dev = objs.theta_dev[:, bandnum] T.theta_exp = objs.theta_exp[:, bandnum] T.phi_dev = objs.phi_dev_deg[:, bandnum] T.phi_exp = objs.phi_exp_deg[:, bandnum] T.ab_dev = objs.ab_dev[:, bandnum] T.ab_exp = objs.ab_exp[:, bandnum] for band in bands: bi = band_index(band) T.set('psfflux_%s' % band, objs.psfflux[:, bi]) T.set('devflux_%s' % band, objs.devflux[:, bi]) T.set('expflux_%s' % band, objs.expflux[:, bi]) T.set('cmodelflux_%s' % band, objs.cmodelflux[:, bi]) TT.append(T) T = merge_tables(TT) T.writeto(catfn) outfns.append(catfn) written = set() plt.figure(figsize=(8, 8)) plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, hspace=0.05, wspace=0.05) # Retrieve SDSS images for band in bands: if band == 'r': rimgs = [] for ifield, (run, camcol, field) in enumerate(RCF): fn = sdss.retrieve('photoField', run, camcol, field) print 'Retrieved', fn F = fits_table(fn) F.cut((F.run == run) * (F.camcol == camcol) * (F.field == field)) print len(F), 'fields' assert (len(F) == 1) F = F[0] boundpixradius = int(np.ceil(np.sqrt(2.) * pixradius)) print 'RA,Dec,size', (ra, dec, boundpixradius) tim, tinfo = get_tractor_image_dr9(run, camcol, field, band, sdss=sdss, nanomaggies=True, roiradecsize=(ra, dec, boundpixradius)) print 'Got tim:', tim frame = sdss.readFrame(run, camcol, field, band) if tim is None: continue x, y = tim.getWcs().positionToPixel(RaDecPos(ra, dec)) x, y = int(x), int(y) # Grab calibration information also tim.sdss_calib = np.median(frame.getCalibVec()) tim.sdss_sky = frame.getSkyAt(x, y) iband = band_index(band) tim.sdss_gain = F.gain[iband] tim.sdss_darkvar = F.dark_variance[iband] #tims.append(tim) #tinfs.append(tinfo) #tims = [] #tinfs = [] # Write out the images #for band,tim,tinfo in zip(bands, tims, tinfs): roi = tinfo['roi'] x0, x1, y0, y1 = roi if plots: plt.clf() img = tim.getImage() mn, mx = [np.percentile(img, p) for p in [25, 99]] dimshow(img, vmin=mn, vmax=mx) xx, yy = [], [] for src in srcs: x, y = tim.getWcs().positionToPixel(src.getPosition()) xx.append(x) yy.append(y) ax = plt.axis() plt.plot(xx, yy, 'r+') plt.axis(ax) plt.savefig('tim-%s%i.png' % (band, ifield)) # Resample to common grid th, tw = tim.shape wwcs = TractorWCSWrapper(tim.getWcs(), tw, th) try: Yo, Xo, Yi, Xi, [rim ] = resample_with_wcs(targetwcs, wwcs, [tim.getImage()], Lanczos) except OverlapError: continue img = np.zeros((H, W)) img[Yo, Xo] = rim iv = np.zeros((H, W)) iv[Yo, Xo] = tim.getInvvar()[Yi, Xi] if plots: plt.clf() mn, mx = [np.percentile(img, p) for p in [25, 99]] dimshow(img, vmin=mn, vmax=mx) xx, yy = [], [] for src in srcs: rd = src.getPosition() ok, x, y = targetwcs.radec2pixelxy(rd.ra, rd.dec) xx.append(x - 1) yy.append(y - 1) ax = plt.axis() plt.plot(xx, yy, 'r+') plt.axis(ax) plt.savefig('rim-%s%i.png' % (band, ifield)) # Convert PSF params also cd = tim.getWcs().cdAtPixel(tw / 2, th / 2) #print 'Tim CD matrix', cd targetcd = np.array(targetwcs.cd).copy().reshape((2, 2)) #print 'Target CD matrix:', targetcd trans = np.dot(np.linalg.inv(targetcd), cd) #print 'Transformation matrix:', trans psf = tim.getPsf() #print 'PSF', psf K = psf.mog.K newmean = np.zeros_like(psf.mog.mean) #print 'newmean', newmean newvar = np.zeros_like(psf.mog.var) #print 'newvar', newvar for i, (dx, dy) in enumerate(psf.mog.mean): #print 'dx,dy', dx,dy x, y = tim.getWcs().positionToPixel(RaDecPos(ra, dec)) r, d = tim.getWcs().pixelToPosition(x + dx, y + dy) #print 'ra,dec', r,d ok, x0, y0 = targetwcs.radec2pixelxy(ra, dec) ok, x1, y1 = targetwcs.radec2pixelxy(r, d) #print 'dx2,dy2', x1-x0, y1-y0 vv = np.array([dx, dy]) tv = np.dot(trans, vv) #print 'dot', tv newmean[i, :] = tv for i, var in enumerate(psf.mog.var): #print 'var', var newvar[i, :, :] = np.dot(trans, np.dot(var, trans.T)) #print 'newvar', newvar[i,:,:] newpsf = GaussianMixturePSF(psf.mog.amp, newmean, newvar) hdr = fitsio.FITSHDR() targetwcs.add_to_header(hdr) hdr.add_record(dict(name='RUN', value=run, comment='SDSS run')) hdr.add_record( dict(name='CAMCOL', value=camcol, comment='SDSS camcol')) hdr.add_record( dict(name='FIELD', value=field, comment='SDSS field')) hdr.add_record(dict(name='BAND', value=band, comment='SDSS band')) # Copy from input "frame" header orighdr = tinfo['hdr'] for key in ['NMGY']: hdr.add_record( dict(name=key, value=orighdr[key], comment=orighdr.get_comment(key))) hdr.add_record( dict(name='CALIB', value=tim.sdss_calib, comment='Mean "calibvec" value for this image')) hdr.add_record( dict(name='SKY', value=tim.sdss_sky, comment='SDSS sky estimate at image center')) hdr.add_record( dict(name='GAIN', value=tim.sdss_gain, comment='SDSS gain')) hdr.add_record( dict(name='DARKVAR', value=tim.sdss_darkvar, comment='SDSS dark variance')) for (key, value, comment) in addToHeader: hdr.add_record(dict(name=key, value=value, comment=comment)) newpsf.toFitsHeader(hdr, 'PSF_') # First time, overwrite existing file. Later, append clobber = not band in written written.add(band) if band == 'r': rimgs.append(img) fn = stamp_pattern % band print 'writing', fn fitsio.write(fn, img.astype(np.float32), clobber=clobber, header=hdr) fitsio.write(fn, iv.astype(np.float32)) if clobber: outfns.append(fn) if band == 'r': N = len(rimgs) ncols = int(np.ceil(np.sqrt(float(N)))) nrows = int(np.ceil(float(N) / ncols)) plt.clf() for k, img in enumerate(rimgs): plt.subplot(nrows, ncols, k + 1) dimshow(img, vmin=-0.1, vmax=1., ticks=False) plt.savefig('stamps-%.4f-%.4f.png' % (ra, dec)) return outfns
'--cnts', dest="cnts", type='str', default=None, help="horizontal data") ############################################################################## ###################### STEP 1: LOAD FILE ################################## ############################################################################## # colors = ['black','darkblue','limegreen','olive','purple','pink','Chartreuse','brown','cyan','black','Brown','DarkGray'] (options, args) = parser.parse_args() if len(args) == 1: stream = open(args[0]) else: stream = sys.stdin estimate_isos(stream) sys.exit() plt.suptitle(gene.split(';')[-1]) plt.subplots_adjust(left=0.05, bottom=0.05, right=0.9, top=0.95, wspace=0.3, hspace=0.9) plt.show()
X = self.transformer_.transform(X) return LinearSVC.predict(self, X) print 80 * '=' print "LinearSVC with L1-based feature selection" results.append(benchmark(L1LinearSVC())) # make some plots indices = np.arange(len(results)) results = [[x[i] for x in results] for i in xrange(4)] clf_names, score, training_time, test_time = results training_time = np.array(training_time) / np.max(training_time) test_time = np.array(test_time) / np.max(test_time) pl.title("Score") pl.barh(indices, score, .2, label="score", color='r') pl.barh(indices + .3, training_time, .2, label="training time", color='g') pl.barh(indices + .6, test_time, .2, label="test time", color='b') pl.yticks(()) pl.legend(loc='best') pl.subplots_adjust(left=.25) for i, c in zip(indices, clf_names): pl.text(-.3, i, c) pl.show()
label='Training Set MSE') plot.plot(range(1, nEst + 1), msError, label='Test Set MSE') plot.legend(loc='upper right') plot.xlabel('Number of Trees in Ensemble') plot.ylabel('Mean Squared Error') plot.show() # Plot feature importance featureImportance = wineGBMModel.feature_importances_ # normalize by max importance featureImportance = featureImportance / featureImportance.max() idxSorted = numpy.argsort(featureImportance) barPos = numpy.arange(idxSorted.shape[0]) + .5 plot.barh(barPos, featureImportance[idxSorted], align='center') plot.yticks(barPos, wineNames[idxSorted]) plot.xlabel('Variable Importance') plot.subplots_adjust(left=0.2, right=0.9, top=0.9, bottom=0.1) plot.show() # Printed Output: # for: #nEst = 2000 #depth = 7 #learnRate = 0.01 #subSamp = 0.5 # # MSE # 0.313361215728 # 840
X = np.dot(np.random.randn(n_samples, n_features), gen_cov) # add some outliers outliers_cov = np.eye(n_features) outliers_cov[np.arange(1, n_features), np.arange(1, n_features)] = 7. X[-n_outliers:] = np.dot(np.random.randn(n_outliers, n_features), outliers_cov) # fit a Minimum Covariance Determinant (MCD) robust estimator to data robust_cov = MinCovDet().fit(X) # compare estimators learnt from the full data set with true parameters emp_cov = EmpiricalCovariance().fit(X) ############################################################################### # Display results fig = pl.figure() pl.subplots_adjust(hspace=-.1, wspace=.4, top=.95, bottom=.05) # Show data set subfig1 = pl.subplot(3, 1, 1) inlier_plot = subfig1.scatter(X[:, 0], X[:, 1], color='black', label='inliers') outlier_plot = subfig1.scatter(X[:, 0][-n_outliers:], X[:, 1][-n_outliers:], color='red', label='outliers') subfig1.set_xlim(subfig1.get_xlim()[0], 11.) subfig1.set_title("Mahalanobis distances of a contaminated data set:") # Show contours of the distance functions xx, yy = np.meshgrid(np.linspace(pl.xlim()[0], pl.xlim()[1], 100), np.linspace(pl.ylim()[0],
def main(outDir, subArea, runList, chartTitle, ownership): varList = [' Potential HighSev Fire (ha) PMG1', ' Potential HighSev Fire (ha) PMG2', ' Potential HighSev Fire (ha) PMG3', ' Potential HighSev Fire (ha) PMG4', ' Potential HighSev Fire (ha) PMG5'] # remove Tribal from owners to graph ownersToGraph = ['Federal','State','Private Non-Industrial','Private Industrial','Homeowner'] reporterName = r'PotentialDisturbance_by_OWNER_pivot.csv' ownerLabelField = ' OWNER_label' # reset list of owners to graph if ownership == 'All': pdfFile = PdfPages(outDir + chartTitle + '_Potential_HS_Fire_landscape.pdf') elif ownership in ownersToGraph: ownersToGraph = [ownership] pdfFile = PdfPages(outDir + chartTitle + '_Potential_HS_Fire_' + ownersToGraph[0] + '.pdf') else: ownersToGraph = [ownership] pdfFile = PdfPages(outDir + chartTitle + '_Potential_HS_Fire_' + ownersToGraph[0] + '.pdf') ownerLabelField = ' OWNER_DETL_label' reporterName = r'PotentialDisturbance_by_OWNER_DETL_pivot.csv' # load area stats by owner areaStats = pd.io.parsers.read_csv(outDir + runList[0] + "\\AreaStats_by_OWNER_pivot.csv") areaStats = areaStats[areaStats[' Year'] == 1] # sum total areas over selected ownerships for ownerToGraph in ownersToGraph: ownerStats = areaStats[areaStats[ownerLabelField] == ownerToGraph] if ownerToGraph == ownersToGraph[0]: PMG345Area = ownerStats[' PVT MNG GR 3 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 4 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 5 (ha)'].iloc[0] PMG12345Area = ownerStats[' PVT MNG GR 1 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 2 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 3 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 4 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 5 (ha)'].iloc[0] PMG1Area = ownerStats[' PVT MNG GR 1 (ha)'].iloc[0] PMG2Area = ownerStats[' PVT MNG GR 2 (ha)'].iloc[0] else: PMG345Area += ownerStats[' PVT MNG GR 3 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 4 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 5 (ha)'].iloc[0] PMG12345Area += ownerStats[' PVT MNG GR 1 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 2 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 3 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 4 (ha)'].iloc[0] + ownerStats[' PVT MNG GR 5 (ha)'].iloc[0] PMG1Area += ownerStats[' PVT MNG GR 1 (ha)'].iloc[0] PMG2Area += ownerStats[' PVT MNG GR 2 (ha)'].iloc[0] for splot in [1,3,5,7]: # setup plot for all scenarios fig = pl.figure(1, figsize=(4,4)) ax = fig.add_subplot(1,1,1) for scenario in runList: print "Potential HS Fire Running scenario " + scenario inDir = outDir + scenario + "\\" if os.path.isdir(inDir): totalArea = pd.io.parsers.read_csv(inDir + reporterName) yearList = list(set(totalArea[' Year'])) repList = list(set(totalArea[' Run'])) # get stats from multiple reps statsList = [] for year in range(1,max(yearList) + 1): yearArea = totalArea[totalArea[' Year'] == year] dataList = [] for rep in repList: repArea = yearArea[yearArea[' Run'] == rep] # sum output over selected ownerships for ownerToGraph in ownersToGraph: if ownerToGraph == ownersToGraph[0]: ownerArea = pd.DataFrame(repArea[repArea[' OWNER_label'] == ownerToGraph]) else: tempArea = repArea[repArea[' OWNER_label'] == ownerToGraph] for varName in varList: # totalArea.loc[list(ownerArea.index)[0],varName] += tempArea[varName].iloc[0] ownerArea[varName].iloc[0] += tempArea[varName].iloc[0] ownerToGraph = 'All' if splot == 1 or splot == 2: dataList.append(ownerArea[' Potential HighSev Fire (ha) PMG1'].iloc[0] + ownerArea[' Potential HighSev Fire (ha) PMG2'].iloc[0] + ownerArea[' Potential HighSev Fire (ha) PMG3'].iloc[0] + ownerArea[' Potential HighSev Fire (ha) PMG4'].iloc[0] + ownerArea[' Potential HighSev Fire (ha) PMG5'].iloc[0]) elif splot == 3 or splot == 4: dataList.append(ownerArea[' Potential HighSev Fire (ha) PMG3'].iloc[0] + ownerArea[' Potential HighSev Fire (ha) PMG4'].iloc[0] + ownerArea[' Potential HighSev Fire (ha) PMG5'].iloc[0]) elif splot == 5 or splot == 6: dataList.append(ownerArea[' Potential HighSev Fire (ha) PMG2'].iloc[0]) elif splot == 7 or splot == 8: dataList.append(ownerArea[' Potential HighSev Fire (ha) PMG1'].iloc[0]) # convert to numpy array numpyList = np.array(dataList) lower95th = np.mean(numpyList, axis=0) - ((1.96 * np.std(numpyList, axis=0)) / np.sqrt(len(repList))) upper95th = np.mean(numpyList, axis=0) + ((1.96 * np.std(numpyList, axis=0)) / np.sqrt(len(repList))) if lower95th < 0: lower95th = 0.0 # add year data to dictionary dataDict = {'timeStep': year, 'mean': np.mean(numpyList, axis=0), 'std': np.std(numpyList, axis=0), 'lower': lower95th, 'upper': upper95th} # convert to list for DataFrame statsList.append(dataDict) # convert to DataFrame dataTable = pd.DataFrame(statsList) labelXtick = True labelYtick = True plotLegend = (0.5,0.99) xLabelText = 'Simulation Year' dataTable['mean'] = dataTable['mean'] / 1000 dataTable['lower'] = dataTable['lower'] / 1000 dataTable['upper'] = dataTable['upper'] / 1000 if splot == 1: yLabelText = 'Area of high-severity fire hazard (1000 ha)' elif splot == 2: yLabelText = 'Area of high-severity fire hazard (1000 ac)' dataTable['mean'] = dataTable['mean'] * 2.471 dataTable['lower'] = dataTable['lower'] * 2.471 dataTable['upper'] = dataTable['upper'] * 2.471 elif splot == 3: yLabelText = 'Area of fire frequent landscape in HS hazard (1000 ha)' elif splot == 4: yLabelText = 'Area of fire frequent landscape in HS hazard (1000 ac)' dataTable['mean'] = dataTable['mean'] * 2.471 dataTable['lower'] = dataTable['lower'] * 2.471 dataTable['upper'] = dataTable['upper'] * 2.471 elif splot == 5: yLabelText = 'Area of lodgepole landscape in HS hazard (1000 ha)' elif splot == 7: yLabelText = 'Area of high elevation landscape in HS hazard (1000 ha)' mnAxis, mxAxis = reporterFunc.plotReporter4(fig, ax, '', pdfFile, dataTable, ['mean','lower','upper'], xLabelText, yLabelText, scenario, labelXtick, labelYtick, plotLegend, '') # repeat y axis on right with percent ax.set_ylim(mnAxis,mxAxis) print "Max y axis value found: " + str(mxAxis) + " and min y axis value: " + str(mnAxis) if splot == 1: yLabelText = 'Area of high-severity fire hazard (%)' mnAxis = mnAxis / PMG12345Area * 100000 mxAxis = mxAxis / PMG12345Area * 100000 elif splot == 2: yLabelText = 'Area of high-severity fire hazard (%)' mnAxis = mnAxis / PMG12345Area * 100000 / 2.471 mxAxis = mxAxis / PMG12345Area * 100000 / 2.471 elif splot == 3: yLabelText = 'Area of fire frequent landscape in HS hazard (%)' mnAxis = mnAxis / PMG345Area * 100000 mxAxis = mxAxis / PMG345Area * 100000 elif splot == 4: yLabelText = 'Area of fire frequent landscape in HS hazard (%)' mnAxis = mnAxis / PMG345Area * 100000 / 2.471 mxAxis = mxAxis / PMG345Area * 100000 / 2.471 elif splot == 5: yLabelText = 'Area of lodgepole landscape in HS hazard (%)' mnAxis = mnAxis / PMG2Area * 100000 mxAxis = mxAxis / PMG2Area * 100000 elif splot == 7: yLabelText = 'Area of high elevation landscape in HS hazard (%)' mnAxis = mnAxis / PMG1Area * 100000 mxAxis = mxAxis / PMG1Area * 100000 reporterFunc.plotSecondYAxis(ax.twinx(), yLabelText, mnAxis, mxAxis) pl.subplots_adjust(right=0.89) pdfFile.savefig() pl.close() pdfFile.close() print "Done with HS hazard."
ind1 = s3.argmax() ind2 = s2.argmax() d1 = s3[int(ind1 - lw/delta):int(ind1 + rw/delta)+1] d2 = s2[int(ind2 - lw/delta):int(ind2 + rw/delta)+1] t = arange(-1*lw,rw+delta,delta) md1 = d1/d1.max() md2 = d2/d2.max() cor = correlate(md1,md2,'full') tc = arange(-1*(lw+rw),lw+rw+delta,delta) ax1 = pl.subplot(121) ax1.plot(t,md1,'r',alpha=0.8,label='NVAR') ax1.plot(t,md2,'g',alpha=0.8,label='PDAR') ax1.set_xlabel('Time[s]') ax1.legend() ax2 = pl.subplot(122) ax2.plot(tc,cor,'k') ax2.set_xlabel('Time[s]') ax2.set_xlim(-1*(lw+rw),lw+rw) ax1.figure.set_size_inches(12,4,forward=True) pl.tight_layout() pl.subplots_adjust(wspace=0.25) #pl.show() pl.savefig('wf_cor.eps',bbox_inches='tight')
def arches_figure(): """ Plot a 3 panel figure showing seeing-limited, HST, and AO data on the Arches cluster to illustrate the power of AO. """ # ---------- # NIRC2 # ---------- hroot = 'mag06maylgs2_arch_f1_h' kroot = 'mag06maylgs2_arch_f1_kp' lroot = 'mag06maylgs2_arch_f1_lp' cooStar = 'f1_psf0' scaleMinH = 1000 scaleMinK = 800 scaleMinL = 600 scaleMaxH = 4500 scaleMaxK = 8000 scaleMaxL = 10000 img = np.zeros((1500, 1500, 3), dtype=float) origin = np.array([750.0, 750.0]) labelFile = '/u/ghezgroup/data/gc/source_list/label_arch.dat' labels = starTables.Labels(labelFile=labelFile) dataDir = '/u/ghezgroup/data/gc/06maylgs2/combo/' # Load up the images h = pyfits.getdata(dataDir + hroot + '.fits') k = pyfits.getdata(dataDir + kroot + '.fits') l = pyfits.getdata(dataDir + lroot + '.fits') # Make the arrays into the largest size. h_new = np.zeros((img.shape[0], img.shape[1]), dtype=float) k_new = np.zeros((img.shape[0], img.shape[1]), dtype=float) l_new = np.zeros((img.shape[0], img.shape[1]), dtype=float) h_new[0:h.shape[0], 0:h.shape[1]] = h k_new[0:k.shape[0], 0:k.shape[1]] = k l_new[0:l.shape[0], 0:l.shape[1]] = l # Load up the coo stars tmpH = open(dataDir + hroot + '.coo').readline().split() cooH = np.array([float(tmpH[0]), float(tmpH[1])]) tmpK = open(dataDir + kroot + '.coo').readline().split() cooK = np.array([float(tmpK[0]), float(tmpK[1])]) tmpL = open(dataDir + lroot + '.coo').readline().split() cooL = np.array([float(tmpL[0]), float(tmpL[1])]) # Get the coordinates of each coo star in arcsec. idxH = np.where(labels.name == cooStar)[0][0] idxK = np.where(labels.name == cooStar)[0][0] idxL = np.where(labels.name == cooStar)[0][0] asecH = np.array([labels.x[idxH], labels.y[idxH]]) asecK = np.array([labels.x[idxK], labels.y[idxK]]) asecL = np.array([labels.x[idxL], labels.y[idxL]]) scale = np.array([-0.00995, 0.00995]) # Now figure out the necessary shifts originH = cooH - asecH / scale originK = cooK - asecK / scale originL = cooL - asecL / scale # Shift the J and H images to be lined up with K-band shiftL = origin - originL shiftK = origin - originK shiftH = origin - originH l = interp.shift(l_new, shiftL[::-1]) k = interp.shift(k_new, shiftK[::-1]) h = interp.shift(h_new, shiftH[::-1]) print shiftH print shiftL xx, yy = np.meshgrid(np.arange(img.shape[0]), np.arange(img.shape[1])) idx = np.where((h >= 1) & (k >= 1) & (l >= 1)) # Trim off the bottom 10 rows where there is data ymin = yy[idx[0], idx[1]].min() ydx = np.where(yy[idx[0], idx[1]] > (ymin + 10))[0] idx = (idx[0][ydx], idx[1][ydx]) # gcutil.rmall(['arches_f1_h.fits', 'arches_f1_kp.fits', 'arches_f1_lp.fits']) # pyfits.writeto('arches_f1_h.fits', h) # pyfits.writeto('arches_f1_kp.fits', k) # pyfits.writeto('arches_f1_lp.fits', l) img[idx[0], idx[1], 0] = img_scale.sqrt(l[idx[0], idx[1]], scale_min=scaleMinL, scale_max=scaleMaxL) img[idx[0], idx[1], 1] = img_scale.sqrt(k[idx[0], idx[1]], scale_min=scaleMinK, scale_max=scaleMaxK) img[idx[0], idx[1], 2] = img_scale.sqrt(h[idx[0], idx[1]], scale_min=scaleMinH, scale_max=scaleMaxH) # Define the axes xaxis = np.arange(-0.5, img.shape[1] + 0.5, 1) xaxis = ((xaxis - origin[0]) * scale[0]) yaxis = np.arange(-0.5, img.shape[0] + 0.5, 1) yaxis = ((yaxis - origin[1]) * scale[1]) extent = [xaxis[0], xaxis[-1], yaxis[0], yaxis[-1]] img_nirc2 = img ext_nirc2 = extent # ---------- # UKIDSS # ---------- scaleMinJ = 20 scaleMinH = 400 scaleMinK = 1000 scaleMaxJ = 5000 scaleMaxH = 35000 scaleMaxK = 90000 dataDir = '/u/jlu/data/arches/ukidss/' # Load up the images j = pyfits.getdata(dataDir + 'ukidss_arches_j.fits') h = pyfits.getdata(dataDir + 'ukidss_arches_h.fits') k = pyfits.getdata(dataDir + 'ukidss_arches_k.fits') img = np.zeros((j.shape[0], j.shape[1], 3), dtype=float) origin = [173, 198] scale = [-0.2, 0.2] xx, yy = np.meshgrid(np.arange(img.shape[0]), np.arange(img.shape[1])) img[:, :, 0] = img_scale.sqrt(k, scale_min=scaleMinK, scale_max=scaleMaxK) img[:, :, 1] = img_scale.sqrt(h, scale_min=scaleMinH, scale_max=scaleMaxH) img[:, :, 2] = img_scale.sqrt(j, scale_min=scaleMinJ, scale_max=scaleMaxJ) # Define the axes xaxis = np.arange(-0.5, img.shape[1] + 0.5, 1) xaxis = ((xaxis - origin[0]) * scale[0]) yaxis = np.arange(-0.5, img.shape[0] + 0.5, 1) yaxis = ((yaxis - origin[1]) * scale[1]) extent = [xaxis[0], xaxis[-1], yaxis[0], yaxis[-1]] img_ukidss = img ext_ukidss = extent py.figure(2, figsize=(6, 12)) py.clf() py.subplots_adjust(bottom=0.05, top=0.95, hspace=0.25) py.subplot(2, 1, 1) py.imshow(img_ukidss, extent=ext_ukidss) py.axis('equal') py.axis([4.5, -6.5, -6.5, 4.5]) py.title('UKIDSS JHK') py.xlabel('R.A. Offset (arcsec)') py.ylabel('Dec. Offset (arcsec)') py.subplot(2, 1, 2) py.imshow(img_nirc2, extent=ext_nirc2) py.axis('equal') py.axis([4.5, -6.5, -6.5, 4.5]) py.title("Keck AO HK'L'") py.xlabel('R.A. Offset (arcsec)') py.ylabel('Dec. Offset (arcsec)') py.savefig('arches_see_vs_ao.png')
def create_image(date, bmap, lead_time=1): print "Testing the day-%d outlook for %s ..." % (lead_time, date.strftime("%d %B %Y")) colors = { 'categorical': { 'TSTM': '#76ff7b', 'MRGL': '#008b00', 'SLGT': '#ffc800', 'ENH': '#f97306', 'MDT': '#ff0000', 'HIGH': '#ff00ff' }, 'tornado': { 0.02: '#008b00', 0.05: '#8b4726', 0.1: '#ffc800', 0.15: '#ff0000', 0.3: '#ff00ff', 0.45: '#912cee', 0.6: '#104e8b' }, 'wind': { 0.05: '#8b4726', 0.15: '#ffc800', 0.3: '#ff0000', 0.45: '#ff00ff', 0.6: '#912cee' }, 'hail': { 0.05: '#8b4726', 0.15: '#ffc800', 0.3: '#ff0000', 0.45: '#ff00ff', 0.6: '#912cee' }, 'any severe': { 0.05: '#8b4726', 0.15: '#ffc800', 0.3: '#ff0000', 0.45: '#ff00ff', 0.6: '#912cee' }, } def do_subplot(product): prod_name = product.name for con_val in product.contour_vals: clr = colors[prod_name][con_val] conts = product[con_val] for cont in conts: proj_cont = transform(lambda lon, lat: bmap(lon, lat), cont) pylab.gca().add_patch(PolygonPatch(proj_cont, fc=clr, ec=clr)) if 'SIGN' in product: sig = product['SIGN'] for cont in sig: proj_cont = transform(lambda lon, lat: bmap(lon, lat), cont) pylab.gca().add_patch( PolygonPatch(proj_cont, fc='none', ec='k', hatch='xx')) pylab.title(prod_name.title(), size='small') bmap.drawcoastlines() bmap.drawcountries() bmap.drawstates() pylab.figure(dpi=200) swo = SPCSWO.download(date, lead_time=lead_time) # print "Outlook valid %s, ending %s." % (swo.valid_start.strftime("%H%MZ %d %b %Y"), swo.valid_end.strftime("%H%MZ %d %b %Y")) pylab.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.9, hspace=0.05, wspace=0.05) for idx, prod_name in enumerate(['categorical', 'tornado', 'wind', 'hail']): pylab.subplot(2, 2, idx + 1) do_subplot(swo[prod_name]) pylab.suptitle( "%s Day-%d SPC Convective Outlook from %s" % (date.strftime("%H%MZ"), lead_time, date.strftime("%d %B %Y"))) pylab.savefig("otlk_%s.png" % date.strftime("%Y%m%d_%H%MZ"), dpi=pylab.gcf().dpi) pylab.close()
t = np.linspace(0, 10, 100) wc = 20 c = np.array([1]) g = 0.1 for T in temp: nu = np.sqrt(1 - 1 / 4 * g**2) sxx, spp, sxp = si.sigma_num(t, c, nu, g, wc, 0, T) sxx, spp = np.average([sxx]), np.average([spp]) dispX = np.append(dispX, [sxx]) dispP = np.append(dispP, [spp]) print T a = np.loadtxt('Temp.dat', skiprows=5) au_T = a[:, 0] au_dispX = a[:, 1] au_dispP = a[:, 2] plt.clf() plt.plot(temp, dispX, 'ob', label='dispX', linewidth=1) plt.plot(temp, dispP, 'or', label='dispP', linewidth=1) plt.plot(au_T, au_dispX, 'ob', label='dispX') plt.plot(au_T, au_dispP, 'or', label='dispP') plt.xlabel('$T$', size=25) plt.legend(loc=(0.1, .6)) plt.subplots_adjust(bottom=0.22) #plt.savefig('/home/martin/Desktop/fig.jpg') print 'done'
timestep.append(i[0]) atoms.append(i[1]) break_atom=list() for i in range(len(atoms)-1): break_atom.append((atoms[i+1]-atoms[i])/13) break_atom.append(0) break_atom[0]=break_atom[0]-1# one particle outside the system broken plt.subplot(414) l42=plt.plot(timestep,break_atom,linewidth=5.0,color='lime',linestyle='-') plt.xlim((xlow,xhigh)) plt.ylim((0.5,3.5)) # plt.xticks([3e7,3.5e7,4e7,4.5e7,5e7,5.5e7,6e7]) plt.yticks(np.arange(1,3.1,1)) # plt.title('Particles',loc='right') plt.xlabel('TimeStep',fontlabel) plt.ylabel('Particle Number',fontlabel) plt.legend([l41,l42],labels=['Breakable','Breakable with mass loss'],loc='upper left',prop=font1) plt.subplots_adjust(left=0.1,right=0.97,bottom=0.05,top=0.95, wspace= 0.1,hspace=0.4) # plt.show() fig = plt.gcf() fig.savefig('../../FIG/fig1.png',dpi=300)
def plot_uvj_vs_icd(): galaxies = pickle.load(open('galaxies.pickle', 'rb')) galaxies = filter(lambda galaxy: galaxy.ICD_IH != None, galaxies) galaxies = filter(lambda galaxy: galaxy.sersic != None and \ galaxy.ston_I > 30, galaxies) #Upper and Lower limit arrow verts arrowup_verts = [[0., 0.], [-1., -1], [0., 0.], [0., -2.], [0., 0.], [1, -1]] #arrowdown_verts = [[0.,0.], [-1., 1], [0.,0.], # [0.,2.], [0.,0.], [1, 1]] F = pyl.figure(1, figsize=(8, 3)) grid = AxesGrid(F, 111, nrows_ncols=(1, 4), axes_pad=0.1, add_all=True, aspect=False, share_all=True) ax1 = grid[0] ax2 = grid[1] ax3 = grid[2] ax4 = grid[3] for galaxy in galaxies: if galaxy.sersic < 1.: col1 = ax1.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') if 1. < galaxy.sersic < 2.: col2 = ax2.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') if 2. < galaxy.sersic < 3.: col3 = ax3.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') if 3. < galaxy.sersic: if galaxy.ICD_IH * 100 < 50: col4 = ax4.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') else: col4 = ax4.scatter(galaxy.Mass, 50, marker=None, s=100, verts=arrowup_verts) # Add the box and whiskers galaxies1 = filter(lambda galaxy: galaxy.ston_I > 30. and \ galaxy.sersic < 1, galaxies) galaxies1 = pyl.asarray(galaxies1) galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30. and \ 1 < galaxy.sersic < 2, galaxies) galaxies2 = pyl.asarray(galaxies2) galaxies3 = filter(lambda galaxy: galaxy.ston_I > 30. and \ 2 < galaxy.sersic < 3, galaxies) galaxies3 = pyl.asarray(galaxies3) galaxies4 = filter(lambda galaxy: galaxy.ston_I > 30. and \ 3 < galaxy.sersic, galaxies) galaxies4 = pyl.asarray(galaxies4) x1 = [galaxy.Mass for galaxy in galaxies1] x2 = [galaxy.Mass for galaxy in galaxies2] x3 = [galaxy.Mass for galaxy in galaxies3] x4 = [galaxy.Mass for galaxy in galaxies4] ll = 8.5 ul = 12 bins_x = pyl.arange(8.5, 12.5, 0.5) grid1 = [] grid2 = [] grid3 = [] grid4 = [] for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1] cond = [cond1 and cond2 for cond1, cond2 in zip(x1 >= xmin, x1 < xmax)] grid1.append(galaxies1.compress(cond)) cond = [cond1 and cond2 for cond1, cond2 in zip(x2 >= xmin, x2 < xmax)] grid2.append(galaxies2.compress(cond)) cond = [cond1 and cond2 for cond1, cond2 in zip(x3 >= xmin, x3 < xmax)] grid3.append(galaxies3.compress(cond)) cond = [cond1 and cond2 for cond1, cond2 in zip(x4 >= xmin, x4 < xmax)] grid4.append(galaxies4.compress(cond)) icd1 = [] icd2 = [] icd3 = [] icd4 = [] for i in range(len(grid1)): icd1.append([galaxy.ICD_IH * 100 for galaxy in grid1[i]]) icd2.append([galaxy.ICD_IH * 100 for galaxy in grid2[i]]) icd3.append([galaxy.ICD_IH * 100 for galaxy in grid3[i]]) icd4.append([galaxy.ICD_IH * 100 for galaxy in grid4[i]]) from boxplot_percentile import percentile_box_plot as pbp bp1 = pbp(ax1, icd1, indexer=list(pyl.delete(bins_x, -1) + 0.25)) bp2 = pbp(ax2, icd2, indexer=list(pyl.delete(bins_x, -1) + 0.25)) bp3 = pbp(ax3, icd3, indexer=list(pyl.delete(bins_x, -1) + 0.25)) bp4 = pbp(ax4, icd4, indexer=list(pyl.delete(bins_x, -1) + 0.25)) ax1.set_xticks([8, 9, 10, 11]) ax2.set_xticks([8, 9, 10, 11]) ax3.set_xticks([8, 9, 10, 11]) ax4.set_xticks([8, 9, 10, 11]) ax1.set_ylim(0, 50) ax2.set_ylim(0, 50) ax3.set_ylim(0, 50) ax4.set_ylim(0, 50) ax1.set_ylabel(r'$\xi[i_{775},H_{160}]$ (%)') ax1.set_title('n < 1') ax2.set_title('1 < n < 2') ax3.set_title('2 < n < 3') ax4.set_title('3 < n') pyl.figtext(.5, .05, r'Log Mass $(M_{\odot})$', fontsize=18, horizontalalignment='center') ax1.axhline(0, lw=2, zorder=0) ax2.axhline(0, lw=2, zorder=0) ax3.axhline(0, lw=2, zorder=0) ax4.axhline(0, lw=2, zorder=0) import matplotlib.font_manager line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8, linewidth=0) line2 = pyl.Line2D([], [], marker='s', mec='blue', mfc='None', markersize=10, linewidth=0, markeredgewidth=2) line3 = pyl.Line2D([], [], color='r', linewidth=2) prop = matplotlib.font_manager.FontProperties(size='small') ax3.legend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'), 'upper center', prop=prop, ncol=1) pyl.tight_layout() pyl.subplots_adjust(bottom=0.21, left=0.11) pyl.show()
background = canvas.copy_from_bbox(ax.bbox) # for profiling tstart = time.time() while 1: # restore the clean slate background canvas.restore_region(background) # update the data line.set_ydata(npy.sin(x + run.cnt / 10.0)) # just draw the animated artist ax.draw_artist(line) # just redraw the axes rectangle canvas.blit(ax.bbox) if run.cnt == 1000: # print the timing info and quit print 'FPS:', 1000 / (time.time() - tstart) sys.exit() run.cnt += 1 run.cnt = 0 p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs p.grid() # to ensure proper background restore manager = p.get_current_fig_manager() manager.window.after(100, run) p.show()
#pic gather rho rho_multigrid_n = pic_multigrid.gather_rho(xn, yn) rho_multigrid_matrix = np.reshape(rho_multigrid_n, (len(y_grid_probes), len(x_grid_probes)), 'F').T rho_singlegrid_n = pic_singlegrid.gather_rho(xn, yn) rho_singlegrid_matrix = np.reshape(rho_singlegrid_n, (len(y_grid_probes), len(x_grid_probes)), 'F').T #plot vmin = -7 vmax = -2 pl.figure(2, figsize=(16, 9)).patch.set_facecolor('w') pl.subplots_adjust(hspace=0.5, wspace=0.3, left=.07, right=.95) sp1 = pl.subplot(2, 3, 1) pl.pcolormesh( x_grid_probes, y_grid_probes, np.log10(np.sqrt(Ex_singlegrid_matrix**2 + Ey_singlegrid_matrix**2).T), vmin=vmin, vmax=vmax) for ii in xrange(pic_multigrid.n_grids): sp1.plot(pic_multigrid.pic_list[ii].pic_internal.chamb.Vx, pic_multigrid.pic_list[ii].pic_internal.chamb.Vy, '.-') pl.xlabel('x [m]') pl.ylabel('y [m]') cb = pl.colorbar() pl.axis('equal') cb.formatter.set_powerlimits((0, 0))
def animate(self, *args, **kwargs): ''' Animate the transmission tree. Args: animate (bool): whether to animate the plot (otherwise, show when finished) verbose (bool): print out progress of each frame markersize (int): size of the markers sus_color (list): color for susceptibles fig_args (dict): arguments passed to pl.figure() axis_args (dict): arguments passed to pl.subplots_adjust() plot_args (dict): arguments passed to pl.plot() delay (float): delay between frames in seconds font_size (int): size of the font colors (list): color of each person cmap (str): colormap for each person (if colors is not supplied) Returns: fig: the figure object ''' # Settings animate = kwargs.get('animate', True) verbose = kwargs.get('verbose', False) msize = kwargs.get('markersize', 10) sus_color = kwargs.get('sus_color', [0.5, 0.5, 0.5]) fig_args = kwargs.get('fig_args', dict(figsize=(24, 16))) axis_args = kwargs.get( 'axis_args', dict(left=0.10, bottom=0.05, right=0.85, top=0.97, wspace=0.25, hspace=0.25)) plot_args = kwargs.get('plot_args', dict(lw=2, alpha=0.5)) delay = kwargs.get('delay', 0.2) font_size = kwargs.get('font_size', 18) colors = kwargs.get('colors', None) cmap = kwargs.get('cmap', 'parula') pl.rcParams['font.size'] = font_size if colors is None: colors = sc.vectocolor(self.pop_size, cmap=cmap) # Initialization n = self.n_days + 1 frames = [list() for i in range(n)] tests = [list() for i in range(n)] diags = [list() for i in range(n)] quars = [list() for i in range(n)] # Construct each frame of the animation for ddict in self.detailed: # Loop over every person if ddict is None: continue # Skip the 'None' node corresponding to seeded infections frame = sc.objdict() tdq = sc.objdict() # Short for "tested, diagnosed, or quarantined" target = ddict.t target_ind = ddict['target'] if not np.isnan(ddict['date']): # If this person was infected source_ind = ddict[ 'source'] # Index of the person who infected the target target_date = ddict['date'] if source_ind is not None: # Seed infections and importations won't have a source source_date = self.detailed[source_ind]['date'] else: source_ind = 0 source_date = 0 # Construct this frame frame.x = [source_date, target_date] frame.y = [source_ind, target_ind] frame.c = colors[source_ind] frame.i = True # If this person is infected frames[int(target_date)].append(frame) # Handle testing, diagnosis, and quarantine tdq.t = target_ind tdq.d = target_date tdq.c = colors[int(target_ind)] date_t = target['date_tested'] date_d = target['date_diagnosed'] date_q = target['date_known_contact'] if ~np.isnan(date_t) and date_t < n: tests[int(date_t)].append(tdq) if ~np.isnan(date_d) and date_d < n: diags[int(date_d)].append(tdq) if ~np.isnan(date_q) and date_q < n: quars[int(date_q)].append(tdq) else: frame.x = [0] frame.y = [target_ind] frame.c = sus_color frame.i = False frames[0].append(frame) # Configure plotting fig = pl.figure(**fig_args) pl.subplots_adjust(**axis_args) ax = fig.add_subplot(1, 1, 1) # Create the legend ax2 = pl.axes([0.85, 0.05, 0.14, 0.9]) ax2.axis('off') lcol = colors[0] na = np.nan # Shorten pl.plot(na, na, '-', c=lcol, **plot_args, label='Transmission') pl.plot(na, na, 'o', c=lcol, markersize=msize, **plot_args, label='Source') pl.plot(na, na, '*', c=lcol, markersize=msize, **plot_args, label='Target') pl.plot(na, na, 'o', c=lcol, markersize=msize * 2, fillstyle='none', **plot_args, label='Tested') pl.plot(na, na, 's', c=lcol, markersize=msize * 1.2, **plot_args, label='Diagnosed') pl.plot(na, na, 'x', c=lcol, markersize=msize * 2.0, label='Known contact') pl.legend() # Plot the animation pl.sca(ax) for day in range(n): pl.title(f'Day: {day}') pl.xlim([0, n]) pl.ylim([0, len(self)]) pl.xlabel('Day') pl.ylabel('Person') flist = frames[day] tlist = tests[day] dlist = diags[day] qlist = quars[day] for f in flist: if verbose: print(f) pl.plot(f.x[0], f.y[0], 'o', c=f.c, markersize=msize, **plot_args) # Plot sources pl.plot(f.x, f.y, '-', c=f.c, **plot_args) # Plot transmission lines if f.i: # If this person is infected pl.plot(f.x[1], f.y[1], '*', c=f.c, markersize=msize, **plot_args) # Plot targets for tdq in tlist: pl.plot(tdq.d, tdq.t, 'o', c=tdq.c, markersize=msize * 2, fillstyle='none') # Tested; No alpha for this for tdq in dlist: pl.plot(tdq.d, tdq.t, 's', c=tdq.c, markersize=msize * 1.2, **plot_args) # Diagnosed for tdq in qlist: pl.plot(tdq.d, tdq.t, 'x', c=tdq.c, markersize=msize * 2.0) # Quarantine; no alpha for this pl.plot([0, day], [0.5, 0.5], c='k', lw=5) # Plot the endless march of time if animate: # Whether to animate pl.pause(delay) return fig
plt.ylim(( mean(coords,axis=0)[1]-3.0, mean(coords,axis=0)[1]+3.0 )) #~ # Plot an ellipse to show the Gaussian component #~ angle = arctan2(w[0][1], w[0][0]) #~ angle = 180 * angle / pi # convert to degrees #~ v *= 4 #~ ell = mpl.patches.Ellipse(mean, v[0], v[1], 180 + angle, color=color) #~ ell.set_clip_box(splotclf.bbox) #~ ell.set_alpha(.5) #~ splot.add_artist(ell) #~ pl.xlim(-10, 10) #~ pl.ylim(-3, 6) #~ pl.xticks(()) #~ pl.yticks(()) #~ pl.title('Selected GMM: full model, 2 components') pl.subplots_adjust(hspace=.15) #~ pl.show() #~ ax = fig.add_subplot(2,2,3) #~ ax.scatter(cluster31[:,0],cluster31[:,1],c=colors[0],marker='o',s=2,edgecolors='none',label="centroid (%5.2f %5.2f ). Size %d" %(means3[0,0], means3[0,1], cluster31.shape[0])) #~ ax.scatter(cluster32[:,0],cluster32[:,1],c=colors[9],marker='o',s=2,edgecolors='none',label="centroid (%5.2f %5.2f ). Size %d" %(means3[1,0], means3[1,1], cluster32.shape[0])) #~ ax.scatter(cluster33[:,0],cluster33[:,1],c=colors[4],marker='o',s=2,edgecolors='none',label="centroid (%5.2f %5.2f ). Size %d" %(means3[2,0], means3[2,1], cluster33.shape[0])) #~ ax.scatter(means3[:,0], means3[:,1], c='k', marker='*', s=60, edgecolors='k', label=None) #~ plt.xlabel('x') #~ plt.ylabel('y') #~ plt.xlim(( mean(coords,axis=0)[0]-3.0, mean(coords,axis=0)[0]+3.0 )) #~ plt.ylim(( mean(coords,axis=0)[1]-3.0, mean(coords,axis=0)[1]+3.0 )) #~ plt.legend(bbox_to_anchor=(0, 0, .99, .99),prop=font,ncol=1) #~ ax.grid(True) #ax = fig.add_subplot(2,2,4)
def plot_solar_elevation(args, mdate, xup, xdown, output_date): """PLots the solar zenith angle as a function of mean time on Mars highlights the sunrise and sunset times, colors and scales the markers appropriately. """ #generate a number of points to plot x = mdate[0] + numpy.arange( args.number_of_points) / (float(args.number_of_points) - 1) xh = marstime.Local_Mean_Solar_Time(west_longitude, x) #define the figure size and resolution so that we can scale the marker appropriately dpi_plot = 72. dpi_save = 150. plot_size = 4. border = 0.12 box_size = plot_size * (1. - 2 * border) resolution = box_size * dpi_plot / 180. #pixels per degree elev = solelev(x, west_longitude, north_latitude) day = numpy.where(elev > -solar_angular_radius) night = numpy.where(elev < -solar_angular_radius) #scale the marker size if necessary if not args.use_marker_radius and solar_angular_radius > 0: markersize = 2 * solar_angular_radius * resolution * args.scale_marker else: markersize = 2 * args.marker_radius * resolution title_str = "Solar Elevation Angle for {date}".format( date="MSD {0}".format(output_date) if args.msd else "{0} (UTC)". format(output_date)) sunrise_str = str_hm(xup, prefix="Sunrise:", suffix="LMST") sunset_str = str_hm(xdown, prefix="Sunset:", suffix="LMST") #open the figure pl.figure(figsize=(plot_size, plot_size)) pl.subplots_adjust(left=border, top=1. - border, right=1. - border, bottom=border) #plot markers pl.plot(xh[day], elev[day], markeredgecolor='gold', marker='o', ls='none', markerfacecolor='none', markersize=markersize) pl.plot(xh[night], elev[night], markeredgecolor='navy', marker='o', ls='none', markerfacecolor='none', markersize=markersize) #grid pl.plot([xup, xup], [-90, 90], color='grey', ls='--') pl.plot([xdown, xdown], [-90, 90], color='grey', ls='--') pl.plot([0, 24], [0, 0], color='grey', ls='--') #axes pl.ylim(-90, 90) pl.yticks([-90, -45, 0, 45, 90]) pl.xticks([0, 4, 8, 12, 16, 20, 24]) pl.xlim(0, 24) #labels pl.title(title_str) pl.text(12, -45, sunrise_str, horizontalalignment='center', verticalalignment='center') pl.text(12, -55, sunset_str, horizontalalignment='center', verticalalignment='center') pl.xlabel("Time (LMST)") pl.ylabel("Elevation Angle") #save pl.savefig(args.filename, dpi=dpi_save)
def plot_histogram(self, bins=None, fig_args=None, width=0.8, font_size=18): ''' Plots a histogram of the number of transmissions ''' if bins is None: max_infections = self.n_targets.max() bins = np.arange(0, max_infections + 2) # Analysis counts = np.histogram(self.n_targets, bins)[0] bins = bins[:-1] # Remove last bin since it's an edge total_counts = counts * bins # counts = counts*100/counts.sum() # total_counts = total_counts*100/total_counts.sum() n_bins = len(bins) n_trans = sum(total_counts) index = np.linspace(0, 100, len(self.n_targets)) sorted_arr = np.sort(self.n_targets) sorted_sum = np.cumsum(sorted_arr) sorted_sum = sorted_sum / sorted_sum.max() * 100 change_inds = sc.findinds(np.diff(sorted_arr) != 0) # Plotting fig_args = sc.mergedicts(dict(figsize=(24, 15))) pl.rcParams['font.size'] = font_size fig = pl.figure(**fig_args) pl.set_cmap('Spectral') pl.subplots_adjust(left=0.08, right=0.92, bottom=0.08, top=0.92) colors = sc.vectocolor(n_bins) pl.subplot(1, 2, 1) w05 = width * 0.5 w025 = w05 * 0.5 pl.bar(bins - w025, counts, width=w05, facecolor='k', label='Number of events') for i in range(n_bins): label = 'Number of transmissions (events × transmissions per event)' if i == 0 else None pl.bar(bins[i] + w025, total_counts[i], width=w05, facecolor=colors[i], label=label) pl.xlabel('Number of transmissions per person') pl.ylabel('Count') pl.xticks(ticks=bins) pl.legend() pl.title('Numbers of events and transmissions') pl.subplot(2, 2, 2) total = 0 for i in range(n_bins): new = total_counts[i] / n_trans * 100 pl.bar(bins[i:], new, width=width, bottom=total, facecolor=colors[i]) total += new pl.xticks(ticks=bins) pl.xlabel('Number of transmissions per person') pl.ylabel('Proportion of infections caused (%)') pl.title('Proportion of transmissions, by number of transmissions') pl.subplot(2, 2, 4) pl.plot(index, sorted_sum, lw=3, c='k', alpha=0.5) for i in range(len(change_inds)): pl.scatter([index[change_inds[i]]], [sorted_sum[change_inds[i]]], s=150, zorder=10, c=[colors[i]], label=f'Transmitted to {i+1} people') pl.xlabel( 'Proportion of population, ordered by the number of people they infected (%)' ) pl.ylabel('Proportion of infections caused (%)') pl.legend() pl.ylim([0, 100]) pl.title('Proportion of transmissions, by proportion of population') pl.axes([0.25, 0.65, 0.2, 0.2]) berry = [0.8, 0.1, 0.2] pl.plot(self.sim_results.t, self.sim_results.cum_infections, lw=2, c=berry) pl.xlabel('Day') pl.ylabel('Cumulative infections') return fig
dp = rfc.DataProvider(a_min=0, a_max=200, files=[fil],label_name='RFI_MASK',n_class=2) data,mask = dp(1) pred,dt = net.predict(model_dir+'/model.cpkt', data,time_it=1) times.append(dt) mask = util.crop_to_shape(mask, pred.shape) fig, (ax1,ax2,ax3) = plt.subplots(3,1,figsize=(18,8)) ax1.imshow(data[0,:,:,0],aspect='auto') ax2.imshow(mask[0,:,:,1],aspect='auto') ax3.imshow(pred[0,:,:,1],aspect='auto') np.save(pred_dir+fname+'_mask',mask) np.save(pred_dir+fname+'_pred',pred) plt.subplots_adjust(left=0.04, right=0.99, top=0.99, bottom=0.04) plt.savefig(pred_dir+fname+'.jpg',dpi=30) plt.close() y_true = mask[0,:,:,1].reshape(-1).astype(int) y_score = pred[0,:,:,1].reshape(-1) y_score /= y_score.max() recall,precision = rfc.prc(y_true, y_score, trsh) pr_list.append(np.stack([recall,precision]).T) fpr,tpr = rfc.rocc(y_true, y_score, trsh) roc_list.append(np.stack([fpr,tpr]).T) np.save(res_file+'_pr',np.array(pr_list)) np.save(res_file+'_roc',np.array(roc_list))
def riemann(): # grid info xmin = 0.0 xmax = 1.0 nzones = 2 ng = 0 dx = (xmax - xmin) / float(nzones) xl = (numpy.arange(2 * ng + nzones) - ng) * dx xr = (numpy.arange(2 * ng + nzones) + 1 - ng) * dx xc = 0.5 * (xl + xr) #------------------------------------------------------------------------ # plot a domain without ghostcells pylab.plot([xmin, xmax], [0, 0], color="k", lw=2) # domain left edge pylab.plot([xl[ng], xl[ng]], [0, 0.5], color="k", lw=2) n = ng while (n < ng + nzones): # draw right edge pylab.plot([xr[n], xr[n]], [0, 0.5], color="k", lw=2) # draw center marker pylab.plot([xc[n], xc[n]], [-0.05, 0], color="k") n += 1 # domain left edge pylab.plot([xr[ng + nzones - 1], xr[ng + nzones - 1]], [0, 0.5], color="k", lw=2) # label a few pylab.text(xc[ng + nzones / 2 - 1], -0.1, r"$i$", horizontalalignment='center', verticalalignment='top') pylab.text(xc[ng + nzones / 2], -0.1, r"$i+1$", horizontalalignment='center', verticalalignment='top') pylab.text(xl[ng + nzones / 2], -0.1, r"$i+1/2$", horizontalalignment='center', verticalalignment='top') # L pylab.scatter(xl[ng + nzones / 2] - 0.05 * dx, 0.25, marker="x") pylab.text(xl[ng + nzones / 2] - 0.075 * dx, 0.25, r"$a_{i+1/2,L}^{n+1/2}$", horizontalalignment='right', verticalalignment='center') pylab.text(xc[ng + nzones / 2 - 1], 0.25, r"$a_i$", horizontalalignment='center', verticalalignment='center', fontsize="16") # R pylab.scatter(xl[ng + nzones / 2] + 0.05 * dx, 0.25, marker="x") pylab.text(xl[ng + nzones / 2] + 0.075 * dx, 0.25, r"$a_{i+1/2,R}^{n+1/2}$", horizontalalignment='left', verticalalignment='center') pylab.text(xc[ng + nzones / 2], 0.25, r"$a_{i+1}$", horizontalalignment='center', verticalalignment='center', fontsize="16") pylab.xlim(xl[0] - 0.15 * dx, xr[2 * ng + nzones - 1] + 0.15 * dx) pylab.ylim(-0.25, 0.6) pylab.axis("off") pylab.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95) f = pylab.gcf() f.set_size_inches(7.0, 2.0) pylab.tight_layout() pylab.savefig("riemann.png") pylab.savefig("riemann.eps")
Bn.cut((Bn.b > 0) * (Bn.dec > decsplit)) Bs.cut(np.logical_or(Bs.b <= 0, (Bs.b > 0) * (Bs.dec <= decsplit))) # Daniel #Bn.cut(Bn.dec >= -10) #Bs.cut(Bs.dec >= -20) ### subsample #Bs.cut(np.random.permutation(len(Bs))[:int(0.1*len(Bs))]) #Bn.cut(np.random.permutation(len(Bn))[:int(0.1*len(Bn))]) for band in 'zrg': plt.clf() plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95) lo, hi = {'g': (23.0, 25.0), 'r': (22.4, 24.4), 'z': (21.5, 23.5)}[band] if False: plt.plot(Bn.x, Bn.y, 'o', ms=1, color='0.5') plt.plot(Bs.x, Bs.y, 'o', ms=1, color='0.5') else: kw = dict(s=1, vmin=lo, vmax=hi, cmap='RdYlBu') plt.scatter(Bn.x, Bn.y, c=Bn.get('galdepth_' + band) - Bn.get('ext_' + band), **kw) plt.scatter(Bs.x, Bs.y, c=Bs.get('galdepth_' + band) - Bs.get('ext_' + band),
def plots(opt): from astrometry.util.plotutils import antigray import tractor.sfd T = fits_table(opt.files[0]) print('Read', len(T), 'bricks summarized in', opt.files[0]) import pylab as plt import matplotlib B = fits_table('survey-bricks.fits.gz') print('Looking up brick bounds') ibrick = dict([(n, i) for i, n in enumerate(B.brickname)]) bi = np.array([ibrick[n] for n in T.brickname]) T.ra1 = B.ra1[bi] T.ra2 = B.ra2[bi] T.dec1 = B.dec1[bi] T.dec2 = B.dec2[bi] assert (np.all(T.ra2 > T.ra1)) T.area = ((T.ra2 - T.ra1) * (T.dec2 - T.dec1) * np.cos(np.deg2rad((T.dec1 + T.dec2) / 2.))) del B del bi del ibrick print('Total sources:', sum(T.nobjs)) print('Approx area:', len(T) / 16., 'sq deg') print('Area:', np.sum(T.area)) print('g,r,z coverage:', sum((T.nexp_g > 0) * (T.nexp_r > 0) * (T.nexp_z > 0)) / 16.) decam = True # vs MzLS+BASS #release = 'MzLS+BASS DR4' release = 'DECaLS DR5' #release = 'DECaLS DR3' if decam: # DECam #ax = [360, 0, -21, 36] ax = [300, -60, -21, 36] def map_ra(r): return r + (-360 * (r > 300)) else: # MzLS+BASS ax = [310, 90, 30, 80] def map_ra(r): return r udec = np.unique(T.dec) print('Number of unique Dec values:', len(udec)) print('Number of unique Dec values in range', ax[2], ax[3], ':', np.sum((udec >= ax[2]) * (udec <= ax[3]))) def radec_plot(): plt.axis(ax) plt.xlabel('RA (deg)') if decam: # plt.xticks(np.arange(0, 361, 45)) #tt = np.arange(0, 361, 60) #plt.xticks(tt, map_ra(tt)) plt.xticks([-60, 0, 60, 120, 180, 240, 300], [300, 0, 60, 120, 180, 240, 300]) else: plt.xticks(np.arange(90, 311, 30)) plt.ylabel('Dec (deg)') def plot_broken(rr, dd, *args, **kwargs): dr = np.abs(np.diff(rr)) I = np.flatnonzero(dr > 90) #print('breaks:', rr[I]) #print('breaks:', rr[I+1]) if len(I) == 0: plt.plot(rr, dd, *args, **kwargs) return for lo, hi in zip(np.append([0], I + 1), np.append(I + 1, -1)): #print('Cut:', lo, ':', hi, '->', rr[lo], rr[hi-1]) plt.plot(rr[lo:hi], dd[lo:hi], *args, **kwargs) # Galactic plane lines gl = np.arange(361) gb = np.zeros_like(gl) from astrometry.util.starutil_numpy import lbtoradec rr, dd = lbtoradec(gl, gb) plot_broken(map_ra(rr), dd, 'k-', alpha=0.5, lw=1) rr, dd = lbtoradec(gl, gb + 10) plot_broken(map_ra(rr), dd, 'k-', alpha=0.25, lw=1) rr, dd = lbtoradec(gl, gb - 10) plot_broken(map_ra(rr), dd, 'k-', alpha=0.25, lw=1) plt.figure(1, figsize=(8, 5)) plt.subplots_adjust(left=0.1, right=0.98, top=0.93) plt.figure(2, figsize=(8, 4)) #plt.subplots_adjust(left=0.06, right=0.98, top=0.98) plt.subplots_adjust(left=0.08, right=0.98, top=0.98) plt.figure(1) # Map of the tile centers we want to observe... if decam: O = fits_table('obstatus/decam-tiles_obstatus.fits') else: O = fits_table('mosaic-tiles_obstatus.fits') O.cut(O.in_desi == 1) rr, dd = np.meshgrid(np.linspace(ax[1], ax[0], 700), np.linspace(ax[2], ax[3], 200)) from astrometry.libkd.spherematch import match_radec I, J, d = match_radec(O.ra, O.dec, rr.ravel(), dd.ravel(), 1.) desimap = np.zeros(rr.shape, bool) desimap.flat[J] = True # Smoothed DESI boundary contours from scipy.ndimage.filters import gaussian_filter from scipy.ndimage.morphology import binary_dilation C = plt.contour(gaussian_filter( binary_dilation(desimap).astype(np.float32), 2), [0.5], extent=[ax[1], ax[0], ax[2], ax[3]]) plt.clf() desi_map_boundaries = C.collections[0] def desi_map_outline(): segs = desi_map_boundaries.get_segments() for seg in segs: plt.plot(seg[:, 0], seg[:, 1], 'b-') def desi_map(): # Show the DESI tile map in the background. plt.imshow(desimap, origin='lower', interpolation='nearest', extent=[ax[1], ax[0], ax[2], ax[3]], aspect='auto', cmap=antigray, vmax=8) base_cmap = 'viridis' # Dust map -- B&W version nr, nd = 610, 350 plt.figure(2) plt.clf() dmap = np.zeros((nd, nr)) rr = np.linspace(ax[0], ax[1], nr) dd = np.linspace(ax[2], ax[3], nd) rr = rr[:-1] + 0.5 * (rr[1] - rr[0]) dd = dd[:-1] + 0.5 * (dd[1] - dd[0]) rr, dd = np.meshgrid(rr, dd) I, J, d = match_radec(rr.ravel(), dd.ravel(), O.ra, O.dec, 1.0, nearest=True) iy, ix = np.unravel_index(I, rr.shape) #dmap[iy,ix] = O.ebv_med[J] sfd = tractor.sfd.SFDMap() ebv = sfd.ebv(rr[iy, ix], dd[iy, ix]) dmap[iy, ix] = ebv mx = np.percentile(dmap[dmap > 0], 98) plt.imshow(dmap, extent=[ax[0], ax[1], ax[2], ax[3]], interpolation='nearest', origin='lower', aspect='auto', cmap='Greys', vmin=0, vmax=mx) #desi_map_outline() radec_plot() cax = colorbar_axes(plt.gca(), frac=0.12) cbar = plt.colorbar(cax=cax) cbar.set_label('Extinction E(B-V)') plt.savefig('ext-bw.pdf') plt.clf() dmap = sfd.ebv(rr.ravel(), dd.ravel()).reshape(rr.shape) plt.imshow(dmap, extent=[ax[0], ax[1], ax[2], ax[3]], interpolation='nearest', origin='lower', aspect='auto', cmap='Greys', vmin=0, vmax=0.25) desi_map_outline() radec_plot() cax = colorbar_axes(plt.gca(), frac=0.12) cbar = plt.colorbar(cax=cax) cbar.set_label('Extinction E(B-V)') plt.savefig('ext-bw-2.pdf') plt.figure(1) #sys.exit(0) plt.clf() depthlo, depthhi = 21.5, 25.5 for band in 'grz': depth = T.get('galdepth_%s' % band) ha = dict(histtype='step', bins=50, range=(depthlo, depthhi)) ccmap = dict(g='g', r='r', z='m') plt.hist(depth[depth > 0], label='%s band' % band, color=ccmap[band], **ha) plt.xlim(depthlo, depthhi) plt.xlabel('Galaxy depth (median per brick) (mag)') plt.ylabel('Number of Bricks') plt.title(release) plt.savefig('galdepths.png') for band in 'grz': depth = T.get('galdepth_%s' % band) nexp = T.get('nexp_%s' % band) #lo,hi = 22.0-0.05, 24.2+0.05 lo, hi = depthlo - 0.05, depthhi + 0.05 nbins = 1 + int((depthhi - depthlo) / 0.1) ha = dict(histtype='step', bins=nbins, range=(lo, hi)) ccmap = dict(g='g', r='r', z='m') area = 0.25**2 plt.clf() I = np.flatnonzero((depth > 0) * (nexp == 1)) plt.hist(depth[I], label='%s band, 1 exposure' % band, color=ccmap[band], lw=1, weights=area * np.ones_like(depth[I]), **ha) I = np.flatnonzero((depth > 0) * (nexp == 2)) plt.hist(depth[I], label='%s band, 2 exposures' % band, color=ccmap[band], lw=2, alpha=0.5, weights=area * np.ones_like(depth[I]), **ha) I = np.flatnonzero((depth > 0) * (nexp >= 3)) plt.hist(depth[I], label='%s band, 3+ exposures' % band, color=ccmap[band], lw=3, alpha=0.3, weights=area * np.ones_like(depth[I]), **ha) plt.title('%s: galaxy depths, %s band' % (release, band)) plt.xlabel('5-sigma galaxy depth (mag)') plt.ylabel('Square degrees') plt.xlim(lo, hi) plt.xticks(np.arange(depthlo, depthhi + 0.01, 0.2)) plt.legend(loc='upper right') plt.savefig('depth-hist-%s.png' % band) for band in 'grz': plt.clf() desi_map() N = T.get('nexp_%s' % band) I = np.flatnonzero(N > 0) #cm = matplotlib.cm.get_cmap('jet', 6) #cm = matplotlib.cm.get_cmap('winter', 5) mx = 10 cm = cmap_discretize(base_cmap, mx) plt.scatter(map_ra(T.ra[I]), T.dec[I], c=N[I], s=3, edgecolors='none', vmin=0.5, vmax=mx + 0.5, cmap=cm) radec_plot() cax = colorbar_axes(plt.gca(), frac=0.08) plt.colorbar(cax=cax, ticks=range(mx + 1)) plt.title('%s: Number of exposures in %s' % (release, band)) plt.savefig('nexp-%s.png' % band) #cmap = cmap_discretize(base_cmap, 15) cmap = cmap_discretize(base_cmap, 10) plt.clf() desi_map() psf = T.get('psfsize_%s' % band) I = np.flatnonzero(psf > 0) plt.scatter(map_ra(T.ra[I]), T.dec[I], c=psf[I], s=3, edgecolors='none', cmap=cmap, vmin=0.5, vmax=2.5) #vmin=0, vmax=3.) radec_plot() plt.colorbar() plt.title('%s: PSF size, band %s' % (release, band)) plt.savefig('psfsize-%s.png' % band) plt.clf() desi_map() depth = T.get('galdepth_%s' % band) - T.get('ext_%s' % band) mn, mx = np.percentile(depth[depth > 0], [10, 98]) mn = np.floor(mn * 10) / 10. mx = np.ceil(mx * 10) / 10. cmap = cmap_discretize(base_cmap, 1 + int((mx - mn + 0.001) / 0.1)) I = (depth > 0) plt.scatter(map_ra(T.ra[I]), T.dec[I], c=depth[I], s=3, edgecolors='none', vmin=mn - 0.05, vmax=mx + 0.05, cmap=cmap) radec_plot() plt.colorbar() plt.title( '%s: galaxy depth, band %s, median per brick, extinction-corrected' % (release, band)) plt.savefig('galdepth-%s.png' % band) # B&W version plt.figure(2) plt.clf() mn, mx = np.percentile(depth[depth > 0], [2, 98]) print('Raw mn,mx', mn, mx) mn = np.floor((mn + 0.05) * 10) / 10. - 0.05 mx = np.ceil((mx - 0.05) * 10) / 10. + 0.05 print('rounded mn,mx', mn, mx) nsteps = int((mx - mn + 0.001) / 0.1) print('discretizing into', nsteps, 'colormap bins') #nsteps = 1+int((mx-mn+0.001)/0.1) cmap = cmap_discretize(antigray, nsteps) nr, nd = 610, 228 dmap = np.zeros((nd, nr)) rr = np.linspace(ax[0], ax[1], nr) dd = np.linspace(ax[2], ax[3], nd) rr = rr[:-1] + 0.5 * (rr[1] - rr[0]) dd = dd[:-1] + 0.5 * (dd[1] - dd[0]) rr, dd = np.meshgrid(rr, dd) I, J, d = match_radec(rr.ravel(), dd.ravel(), T.ra, T.dec, 0.2, nearest=True) iy, ix = np.unravel_index(I, rr.shape) dmap[iy, ix] = depth[J] plt.imshow(dmap, extent=[ax[0], ax[1], ax[2], ax[3]], interpolation='nearest', origin='lower', aspect='auto', cmap=cmap, vmin=mn, vmax=mx) desi_map_outline() radec_plot() cax = colorbar_axes(plt.gca(), frac=0.12) cbar = plt.colorbar( cax=cax, ticks=np.arange(20, 26, 0.5) ) #ticks=np.arange(np.floor(mn/5.)*5., 0.1+np.ceil(mx/5.)*5, 0.2)) cbar.set_label('Depth (5-sigma, galaxy profile, AB mag)') plt.savefig('galdepth-bw-%s.pdf' % band) plt.figure(1) plt.clf() desi_map() ext = T.get('ext_%s' % band) mn = 0. mx = 0.5 cmap = 'hot' cmap = cmap_discretize(cmap, 10) #cmap = cmap_discretize(base_cmap, 1+int((mx-mn+0.001)/0.1)) plt.scatter(map_ra(T.ra), T.dec, c=ext, s=3, edgecolors='none', vmin=mn, vmax=mx, cmap=cmap) radec_plot() plt.colorbar() plt.title('%s: extinction, band %s' % (release, band)) plt.savefig('ext-%s.png' % band) T.ngal = T.nsimp + T.nrex + T.nexp + T.ndev + T.ncomp for col in [ 'nobjs', 'npsf', 'nsimp', 'nrex', 'nexp', 'ndev', 'ncomp', 'ngal' ]: if not col in T.get_columns(): continue plt.clf() desi_map() N = T.get(col) / T.area mx = np.percentile(N, 99.5) plt.scatter(map_ra(T.ra), T.dec, c=N, s=3, edgecolors='none', vmin=0, vmax=mx) radec_plot() cbar = plt.colorbar() cbar.set_label('Objects per square degree') tt = 'of type %s' % col[1:] if col == 'nobjs': tt = 'total' plt.title('%s: Number of objects %s' % (release, tt)) plt.savefig('nobjs-%s.png' % col[1:]) # B&W version plt.figure(2) plt.clf() # plt.scatter(map_ra(T.ra), T.dec, c=N, s=3, # edgecolors='none', vmin=0, vmax=mx, cmap=antigray) # Approximate pixel size in PNG plot # This doesn't work correctly -- we've already binned to brick resolution, so get moire patterns # nobjs,xe,ye = np.histogram2d(map_ra(T.ra), T.dec, weights=T.get(col), # bins=(nr,nd), range=((ax[1],ax[0]),(ax[2],ax[3]))) # nobjs = nobjs.T # area = np.diff(xe)[np.newaxis,:] * (np.diff(ye) * np.cos(np.deg2rad(ye[:-1])))[:,np.newaxis] # nobjs /= area # plt.imshow(nobjs, extent=[ax[1],ax[0],ax[2],ax[3]], interpolation='nearest', origin='lower', # aspect='auto') #print('Computing neighbours for nobjs plot...') nr, nd = 610, 228 nobjs = np.zeros((nd, nr)) rr = np.linspace(ax[0], ax[1], nr) dd = np.linspace(ax[2], ax[3], nd) rr = rr[:-1] + 0.5 * (rr[1] - rr[0]) dd = dd[:-1] + 0.5 * (dd[1] - dd[0]) rr, dd = np.meshgrid(rr, dd) I, J, d = match_radec(rr.ravel(), dd.ravel(), T.ra, T.dec, 0.2, nearest=True) iy, ix = np.unravel_index(I, rr.shape) nobjs[iy, ix] = T.get(col)[J] / T.area[J] #print('done') #mx = 2. * np.median(nobjs[nobjs > 0]) mx = np.percentile(N, 99) plt.imshow(nobjs, extent=[ax[0], ax[1], ax[2], ax[3]], interpolation='nearest', origin='lower', aspect='auto', cmap='Greys', vmin=0, vmax=mx) desi_map_outline() radec_plot() #cax = colorbar_axes(plt.gca(), frac=0.08) cax = colorbar_axes(plt.gca(), frac=0.12) cbar = plt.colorbar(cax=cax, format=matplotlib.ticker.FuncFormatter( lambda x, p: format(int(x), ','))) cbar.set_label('Objects per square degree') plt.savefig('nobjs-bw-%s.pdf' % col[1:]) #plt.savefig('nobjs-bw-%s.png' % col[1:]) plt.figure(1) Ntot = T.nobjs for col in ['npsf', 'nsimp', 'nrex', 'nexp', 'ndev', 'ncomp', 'ngal']: if not col in T.get_columns(): continue plt.clf() desi_map() N = T.get(col) / (Ntot.astype(np.float32)) N[Ntot == 0] = 0. print(col, 'max frac:', N.max()) mx = np.percentile(N, 99.5) print('mx', mx) plt.scatter(map_ra(T.ra), T.dec, c=N, s=3, edgecolors='none', vmin=0, vmax=mx) radec_plot() plt.colorbar() plt.title('%s: Fraction of objects of type %s' % (release, col[1:])) plt.savefig('fobjs-%s.png' % col[1:]) # B&W version plt.figure(2) plt.clf() #plt.scatter(map_ra(T.ra), T.dec, c=N * 100., s=3, # edgecolors='none', vmin=0, vmax=mx*100., cmap=antigray) fobjs = np.zeros((nd, nr)) rr = np.linspace(ax[0], ax[1], nr) dd = np.linspace(ax[2], ax[3], nd) rr = rr[:-1] + 0.5 * (rr[1] - rr[0]) dd = dd[:-1] + 0.5 * (dd[1] - dd[0]) rr, dd = np.meshgrid(rr, dd) I, J, d = match_radec(rr.ravel(), dd.ravel(), T.ra, T.dec, 0.2, nearest=True) iy, ix = np.unravel_index(I, rr.shape) fobjs[iy, ix] = N[J] * 100. #mx = 2. * np.median(fobjs[fobjs > 0]) mx = np.percentile(N * 100., 99) plt.imshow(fobjs, extent=[ax[0], ax[1], ax[2], ax[3]], interpolation='nearest', origin='lower', aspect='auto', cmap='Greys', vmin=0, vmax=mx) desi_map_outline() radec_plot() cax = colorbar_axes(plt.gca(), frac=0.12) cbar = plt.colorbar( cax=cax, format=matplotlib.ticker.FuncFormatter(lambda x, p: '%.2g' % x)) cbar.set_label('Percentage of objects of type %s' % col[1:].upper()) plt.savefig('fobjs-bw-%s.pdf' % col[1:]) #plt.savefig('fobjs-bw-%s.png' % col[1:]) plt.figure(1) return 0
import math import random import pylab from pyteomics import biolccc peptides = [] random.seed() for i in [5, 10, 20, 30, 40]: peptides.append(''.join( [random.choice("QWERTYIPASDFGHKLCVNM") for j in range(i)])) pylab.figure(figsize=(9, 4)) pylab.subplots_adjust(top=0.8, hspace=0.30, wspace=0.30, right=0.96) pylab.suptitle('The dependency of log(Kd) on the second solvent concentration ' 'for five random peptides') for chembasis, subplot_num, title in [(biolccc.rpAcnTfaChain, 121, 'rpAcnTfaChain'), (biolccc.rpAcnFaRod, 122, 'rpAcnFaRod')]: pylab.subplot(subplot_num) for peptide in peptides: x = range(0, 101, 1) y = [math.log(biolccc.calculateKd(peptide, i, chembasis)) for i in x] pylab.plot(x, y, label='%d aa residues' % (len(peptide))) pylab.rcParams['legend.fontsize'] = 10 pylab.legend(loc='upper right') pylab.xlim((0, 100)) pylab.xlabel('ACN concentration, %') pylab.ylabel('log(Kd)') pylab.title(title) pylab.show()
for clusterIndex, cluster in enumerate(data): pylab.subplot(nRows, nCols, clusterIndex + 1) pylab.imshow(memberships[clusterIndex, :, :], interpolation='nearest', vmin=-1, vmax=1, aspect='auto') #pylab.yticks(range(len(prefixes)), [[]*4+[prefix] for i, prefix in enumerate(prefixes) if i%5==0]) #pylab.yticks(range(0,len(prefixes),1), prefixes ) pylab.yticks(range(0, len(groupPrefixes), 1), groupPrefixes) pylab.xlabel('Frame') pylab.title('Cluster %i' % (clusterIndex)) pylab.subplots_adjust(left=0.06, right=0.99, bottom=0.025, top=0.975, hspace=0.250) pylab.show() #Analyze inter-cluster transitions #nSimulations = len(prefixes) nSimulations = memberships.shape[0] nCols = 3 # First +1 because the first plot is of all the simulations summed up nRows = (nSimulations + 1 + (nCols - 1)) / nCols allTransitions = np.zeros((nClusters, nClusters)) #transitions = dict(((prefix,np.zeros((nClusters, nClusters))) for prefix in prefixes))