def Map_plot_subregion(subregions, ref_dataset, directory): lons, lats = np.meshgrid(ref_dataset.lons, ref_dataset.lats) fig = plt.figure() ax = fig.add_subplot(111) m = Basemap( ax=ax, projection='cyl', llcrnrlat=lats.min(), urcrnrlat=lats.max(), llcrnrlon=lons.min(), urcrnrlon=lons.max(), resolution='l') m.drawcoastlines(linewidth=0.75) m.drawcountries(linewidth=0.75) m.etopo() x, y = m(lons, lats) #subregion_array = ma.masked_equal(subregion_array, 0) #max=m.contourf(x, y, subregion_array, alpha=0.7, cmap='Accent') for subregion in subregions: draw_screen_poly(subregion[1], m, 'w') plt.annotate( subregion[0], xy=(0.5 * (subregion[1][2] + subregion[1][3]), 0.5 * (subregion[1][0] + subregion[1][1])), ha='center', va='center', fontsize=8) fig.savefig(directory + 'map_subregion', bbox_inches='tight')
def scatter_time_vs_s(time, norm, point_labels, title): plt.figure() size = 100 for i, l in enumerate(sorted(norm.keys())): if l is not "fbpca": plt.scatter(time[l], norm[l], label=l, marker='o', c='b', s=size) for label, x, y in zip(point_labels, list(time[l]), list(norm[l])): plt.annotate(label, xy=(x, y), xytext=(0, -80), textcoords='offset points', ha='right', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"), va='bottom', size=11, rotation=90) else: plt.scatter(time[l], norm[l], label=l, marker='^', c='red', s=size) for label, x, y in zip(point_labels, list(time[l]), list(norm[l])): plt.annotate(label, xy=(x, y), xytext=(0, 30), textcoords='offset points', ha='right', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"), va='bottom', size=11, rotation=90) plt.legend(loc="best") plt.suptitle(title) plt.ylabel("norm discrepancy") plt.xlabel("running time [s]")
def plot_words (V,labels=None,color='b',mark='o',fa='bottom'): W = tsne(V,2) i = 0 plt.scatter(W[:,0], W[:,1],c=color,marker=mark,s=50.0) for label,x,y in zip(labels, W[:,0], W[:,1]): plt.annotate(label.decode('utf8'), xy=(x,y), xytext=(-1,1), textcoords='offset points', ha= 'center', va=fa, bbox=dict(boxstyle='round,pad=0.1', fc='white', alpha=0)) i += 1
def plot_error(station): fig_length = 10 fig_width = 1 background_color = '#e0e0e0' output_file = '%s.png' % station fig = plt.figure(facecolor=background_color, figsize =(fig_length, fig_width), dpi=100) ax = plt.subplot(1, 1, 1) plt.annotate('Waveforms currently unavailable', (0, 0), (250, 15), xycoords='axes fraction', textcoords='offset points', va='top') for ylabel in ax.get_yticklabels(): ylabel.set_visible(False) for xlabel in ax.get_xticklabels(): xlabel.set_visible(False) ax.yaxis.set_major_locator(plt.NullLocator()) ax.yaxis.grid(False) plt.tight_layout() #plt.show() plt.savefig(output_file, dpi=100, facecolor=fig.get_facecolor(), edgecolor='none', bbox_inches='tight') plt.close()
def plot(i, pcanc, lr, pp, labelFlag, Y): if len(str(i)) == 1: fig = plt.figure(i) else: fig = plt.subplot(i) if pcanc == 0: plt.title( ' learning_rate: ' + str(lr) + ' perplexity: ' + str(pp)) print("Plotting tSNE") else: plt.title( 'PCA-n_components: ' + str(pcanc) + ' learning_rate: ' + str(lr) + ' perplexity: ' + str(pp)) print("Plotting PCA-tSNE") plt.scatter(Y[:, 0], Y[:, 1], c=colors) if labelFlag == 1: for label, cx, cy in zip(y, Y[:, 0], Y[:, 1]): plt.annotate( label.decode('utf-8'), xy = (cx, cy), xytext = (-10, 10), fontproperties=font, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.9)) #arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) plt.axis('tight') print("Done.")
def plot_contour_with_labels(contour, frame_index=0): """ Makes a beautiful plot with all the points labeled. Parameters: One frame's worth of a contour """ contour_x = contour[:, 0, frame_index] contour_y = contour[:, 1, frame_index] plt.plot(contour_x, contour_y, 'r', lw=3) plt.scatter(contour_x, contour_y, s=35) labels = list(str(l) for l in range(0, len(contour_x))) for label_index, (label, x, y), in enumerate( zip(labels, contour_x, contour_y)): # Orient the label for the first half of the points in one direction # and the other half in the other if label_index <= len(contour_x) // 2 - \ 1: # Minus one since indexing xytext = (20, -20) # is 0-based else: xytext = (-20, 20) plt.annotate( label, xy=( x, y), xytext=xytext, textcoords='offset points', ha='right', va='bottom', bbox=dict( boxstyle='round,pad=0.5', fc='yellow', alpha=0.5), arrowprops=dict( arrowstyle='->', connectionstyle='arc3,rad=0')) # , xytext=(0,0))
def execute_solver(IMAGE_FILE): sample4x4_crop = import_image(IMAGE_FILE) cluster_image = get_clustering_image(sample4x4_crop) cluster_groupings_dict = cluster_grouper(cluster_image).execute() final = pre_process_image(IMAGE_FILE) prediction_dict = clean_prediction_dict(get_predictions(final)) write_puzzle_file(cluster_groupings_dict,prediction_dict) try: solution = solve_puzzle('cv_puzzle.txt',False) except: return 'error' #get image of result fig = plt.figure(figsize=(2, 2), dpi=100,frameon=False) plt.axis('off') plt.imshow(sample4x4_crop, cmap=mpl.cm.Greys_r) for k,v in solution.items(): if v == None: return 'error' plt.annotate('{}'.format(v), xy=(k[0]*50+12,k[1]*50+40), fontsize=14) plt.tight_layout() plt.savefig('static/images/solution.jpg', bbox_inches='tight', dpi=100) #theres an issue with the saved layout, tight_layout #doesn't appear to work so I need to apply my own cropping again resize_final = import_image('static/images/solution.jpg',80) imsave('static/images/solution.jpg',resize_final) return 'good'
def plot_degree_distribution(): session = create_session('cownet', port=3306, check=False) query = session.query(Loadboard).group_by(Loadboard.origin) origin = [loadboard.origin for loadboard in query] degree = [] for this_origin in origin: query = session.query(Loadboard).filter_by(**{'origin': this_origin}).group_by(Loadboard.destination) degree.append(query.count()) session.close() counts, bins, patches = plt.hist(degree, tuple(range(max(degree) + 1))) plt.xlim([1, 40]) plt.xlabel('degree') plt.ylabel('frequency') # Label the raw counts below the x-axis... bin_centers = 0.5 * np.diff(bins) + bins[:-1] for count, x in zip(counts, bin_centers): if count > 0: # Label the raw counts plt.annotate( "{:.0f}".format(count), xy=(x, 1), xycoords=('data', 'axes fraction'), xytext=(0, 25), textcoords='offset points', va='top', ha='center', rotation=90, ) plt.savefig('loadboard_degree_distribution.pdf')
def debugFormicMetaGraph (fmg, mazeLong): """ Format a output for pyplot that renders the formic meta graph """ # import try: import matplotlib.pyplot as plt except: debug ("Matplotlib not found") return # plot for i in fmg: for j in fmg[i]: dotY = [-i[0], -j[0]] dotX = [i[1], j[1]] plt.plot(dotX, dotY, "o:") if (i[0]-mazeLong)**2+i[1]**2 > (j[0]-mazeLong)**2+j[1]**2: plt.annotate (str(j)+'<-'+str(i)+' '+str(fmg[i][j][0])+' '+str(fmg[i][j][1]), ( (i[1]+j[1])/2.0 - 0.4, -((i[0]+j[0])/2.0 -0.1) )) else: plt.annotate (str(i)+'->'+str(j)+' '+str(fmg[i][j][0])+' '+str(fmg[i][j][1]), ( (i[1]+j[1])/2.0 - 0.4, -((i[0]+j[0])/2.0 +0.1) )) # render plt.axis((-0.5,mazeLong-0.5, -mazeLong+0.5, 0.5)) plt.show()
def plot_minos_fit(self,p,decay="X",title="Fit Results",erange=9,step=4.07,lum=4200): fig = plt.figure(figsize=(8,6)) plt.errorbar(self.x,self.y,self.yerr,fmt='o') M = p[0][0] G = p[1][0] B = p[2][0] dMl = p[0][1] dMu = p[0][2] dGl = p[1][1] dGu = p[1][2] dBl = p[2][1] dBu = p[2][2] x_fit = np.linspace(min(self.x),max(self.x),num=100) plt.plot(x_fit,self.convBWG(x_fit,M,G,B)) plt.xlabel("$\sqrt{\hat{s}} (MeV)$",fontsize=16) plt.ticklabel_format(useOffset=False) plt.ylabel("Counts",fontsize=16) plt.title(title,fontsize=16) lbl1 = "Input:\n$\mathcal{L}=%d pb^{-1}$\n$\Delta=%.3f\ MeV$\n$\delta\sqrt{\hat{s}} = %.3f MeV$" % (lum,step,self.beam) lbl1 = lbl1 + "\n$M_h = 125.0 GeV$\n$\Gamma_h = 4.07 MeV$\n$Br(h^0\\rightarrow$%s$) = %.3f$" % (decay, self.higgs[2]) lbl1 = lbl1 + "\n$\sigma_{bkg} = %.2f pb^{-1}$" % (self.bkg) lbl2 = "\nFit results:\n" lbl2 = lbl2 + "$\Delta M_h = %.3f_{-%.3f}^{+%.3f}\ MeV$\n" % (M-self.higgs[0], -1*dMl, dMu) lbl2 = lbl2 + "$\Gamma_h = %.3f_{-%.3f}^{+%.3f} \ MeV$\n" % (G, -1*dGl, dGu) lbl2 = lbl2 + "$Br(h^0\\rightarrow$%s$) = %.3f_{-%.3f}^{+%.3f}$\n" % (decay, B, -1*dBl, dBu) plt.annotate(lbl1, [0.1,0.6], xycoords='axes fraction',fontsize=15) plt.annotate(lbl2, [0.7,0.6], xycoords='axes fraction',fontsize=15) return plt
def main(): svd = TruncatedSVD() Z = svd.fit_transform(X) plt.scatter(Z[:,0], Z[:,1]) for i in xrange(D): plt.annotate(s=index_word_map[i], xy=(Z[i,0], Z[i,1])) plt.show()
def plotAotiHour(): fig = plt.figure(figsize=(18, 5)) rect = fig.patch rect.set_facecolor("white") df = pd.read_csv("urban-country/aoti_pm25.csv", sep=",", header=0) df = df[df["date"] >= 20140514] df = df[df["date"] <= 20140527] df["date"] = df["date"].astype(str) df["hour"] = df["hour"].astype(str) dateAndTime = pd.to_datetime(df["date"] + df["hour"], format="%Y%m%d%H") aoti = df["奥体中心"].tolist() ts_aoti = Series(aoti, index=dateAndTime) plot = ts_aoti.plot(linestyle="-", color="black", marker="8", markersize=4, label=u"奥体中心") time = dt.datetime(2014, 05, 17, 10) df = df[df["date"] == "20140517"] df = df[df["hour"] == "10"] value = df.iloc[0, 3] print mdates.date2num(time), value plt.annotate( u"aoti24", xy=(mdates.date2num(time), value), xytext=(30, 20), textcoords="offset points", arrowprops=dict(arrowstyle="-|>"), ) plt.show()
def plot_confusion_matrix(self, y_test, y_pred, list_classes): cm = self.confusion_matrix(y_test, y_pred) plt.figure() plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues) plt.title('Confusion matrix') plt.colorbar() tick_marks = np.arange(len(list_classes)) plt.xticks(tick_marks, list_classes, rotation=90) plt.yticks(tick_marks, list_classes) plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') plt.grid(True) width, height = len(list_classes), len(list_classes) for x in range(width): for y in range(height): if cm[x][y] > 100: color = 'white' else: color = 'black' if cm[x][y] > 0: plt.annotate(str(cm[x][y]), xy=(y, x), horizontalalignment='center', verticalalignment='center', color=color) plt.show()
def advance(self, t, plotresult=False): y0 = self.concs * self.molWeight y0 = append(y0, self.thickness) yt = odeint(self.rightSideofODE, y0, t) if (plotresult): import matplotlib.pyplot as plt plt.figure() plt.axes([0.1, 0.1, 0.6, 0.85]) plt.semilogy(t, yt) plt.ylabel('mass concentrations (kg/m3)') plt.xlabel('time(s)') #plt.legend(self.speciesnames) for i in range(len(self.speciesnames)): plt.annotate( self.speciesnames[i], (t[-1], yt[-1, i]), xytext=(20, -5), textcoords='offset points', arrowprops=dict(arrowstyle="-")) plt.show() self.thickness = yt[-1][-1] ytt = yt[-1][:-1] # for iii in range(len(ytt)): # if ytt[iii]<0: # ytt[iii]=0. molDens = ytt / self.molWeight self.concs = molDens self.molFrac = molDens / sum(molDens) self.massFrac = ytt / sum(ytt)
def plot_results(algo, datas, xlabel, ylabel, note, factor=None): plt.clf() fig1, ax1 = plt.subplots() plt.figtext(0.90, 0.94, "Note: " + note, va='top', ha='right') w, h = fig1.get_size_inches() fig1.set_size_inches(w*1.5, h) ax1.set_xscale('log') ax1.get_xaxis().set_major_formatter(ticker.ScalarFormatter()) ax1.get_xaxis().set_minor_locator(ticker.NullLocator()) ax1.set_xticks(datas[0][:,0]) ax1.grid(color="lightgrey", linestyle="--", linewidth=1, alpha=0.5) if factor: ax1.set_xticklabels([str(int(x)) for x in datas[0][:,0]/factor]) plt.xlabel(xlabel, fontsize=16) plt.ylabel(ylabel, fontsize=16) plt.xlim(datas[0][0,0]*.9, datas[0][-1,0]*1.1) plt.suptitle("%s Performance" % (algo), fontsize=28) for backend, data in zip(backends, datas): N = data[:, 0] plt.plot(N, data[:, 1], 'o-', linewidth=2, markersize=5, label=backend) dy = max(data[:,1]) / 20.0 for x, y in zip(N, data[:, 1]): plt.annotate('%.1f' % y, (x, y+dy)) plt.legend(loc='upper left', fontsize=18) plt.savefig(algo + '.png')
def function3(filelist, answerfile, annotated_list): count = 0 for i in filelist: if i != answerfile: print "working" count = count + 1 color_me = "black" if "cons" in i: color_me = "red" print "balanced" else: color_me = "blue" ifile = open(i) total_ann = 0 total_match = 0 total_mismatch = 0 for line in ifile: if (line[0:3] == "chr" in line) and ("-5" not in line): match = 0 mismatch = 0 thisline = line.split("\t") chr = thisline[0] coord1 = int(thisline[1]) coord2 = int(thisline[2]) total_ann = 0 for ann in annotated_list: total_ann = total_ann + ann[2] - ann[1] if (chr != ann[0]) or (ann[1] > coord2) or (coord1 > ann[2]): pass else: match = ann[2] - ann[1] if (coord2 >= (ann[2] + 50)): mismatch = mismatch + (coord2 - (ann[2] + 50)) if (ann[2] > coord2): match = match - (ann[2] - coord2) if (ann[1] >= (coord1 + 50)): mismatch = mismatch + (ann[1] - (coord1 + 50)) if (coord1 > ann[1]): match = match - (coord1 - ann[1]) if match == 0: total_mismatch = total_mismatch + (coord2- coord1) else: total_mismatch = total_mismatch + mismatch total_match = total_match + match thisfile_accuracy = [float(total_match)/float(total_ann), float(total_mismatch)/float(total_ann), i] plt.scatter(thisfile_accuracy[1],thisfile_accuracy[0], color = color_me) x = thisfile_accuracy[1] y = thisfile_accuracy[0] text_x = thisfile_accuracy[1] + -.1 text_y = thisfile_accuracy[0] - 0.5 plt.annotate(i.replace(".bedgraph", ""), xy = (x, y), xytext = (count, count), textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.1', fc = color_me, alpha = 0.2), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'), size = 8) plt.ylim([-1,1]) plt.xlim([-1,5]) plt.xlabel("False Positive") plt.ylabel("True Positive") savefig("temp_annotate")
def plot_Features(sort_p_lower,sort_p_upper,X,y,vectorizer,n=5): for i in range(n): feature_Ind = sort_p_lower[i][2] ind_pos = np.nonzero(y) ind_neg = np.nonzero(y==0) sum_pos = np.sum(X[ind_pos,feature_Ind].toarray()) sum_neg = np.sum(X[ind_neg,feature_Ind].toarray()) a = plt.scatter(sum_pos,sum_neg,c='blue') plt.annotate(vectorizer.get_feature_names()[feature_Ind],(sum_pos,sum_neg)) plt.xlabel("number of times in positive instances") plt.ylabel("number of times in negative instances") plt.title("top features for news coverage prediction") for i in range(n): feature_Ind = sort_p_upper[i][2] ind_pos = np.nonzero(y) ind_neg = np.nonzero(y==0) sum_pos = np.sum(X[ind_pos,feature_Ind].toarray()) sum_neg = np.sum(X[ind_neg,feature_Ind].toarray()) b = plt.scatter(sum_pos,sum_neg,c='red') plt.annotate(vectorizer.get_feature_names()[feature_Ind],(sum_pos,sum_neg)) xmin,xmax = plt.xlim() ymin,ymax = plt.ylim() min_value = min([xmax,ymax]) plt.xlim(0, xmax) plt.ylim(0, ymax) plt.plot(range(int(min_value)),range(int(min_value)),0.01,'-') plt.legend((a,b),('positive feature','negative feature'),scatterpoints=1,loc=4) #plt.show() plt.savefig("BS_NConPR/top_features_NC")
def plot_matrix(M, xlabel = None, ylabels = None, annotate = False, legend = True, annx = None, anny = None): (m,n) = M.shape if annotate and (annx == None): max_x = max(M[:,0]) min_x = min(M[:,0]) hx = max_x - min_x max_y = max(colmax(M[:,1:n])) min_y = min(colmin(M[:,1:n])) hy = max_y - min_y annx = [] anny = [] for j in range (n-1): i = (j+1)*m*9/(n*10) annx.append(M[i,0]) anny.append(M[i,j+1]) print 'Annotate ', i, ylabels[j] , ' at: ', annx[j], anny[j] for j in range(n - 1): plt.plot(M[:,0], M[:,j+1]) if annotate: plt.annotate(ylabels[j], xy=(annx[j], anny[j]), xytext=(annx[j]+0.1*hx, anny[j]+0.1*hy), arrowprops=dict(facecolor='black', shrink=0.2), ) plt.plot(annx, anny, 'x', color = 'black') if legend: ''' I should work on that ... ''' plt.legend() plt.show()
def multi_trial_stats(): """ Note: a lot of this should probably be in helper function tbh May want to add recursive copy of trial folders into summary folder, idk I also forgot to take sensitivity into account, I'll fix that once this all works """ # Ask user for directory with all data directories to be used inside selected_dir = tkFileDialog.askdirectory() sub_dirs = [os.path.join(selected_dir, name) for name in os.listdir(selected_dir) if os.path.isdir(os.path.join(selected_dir, name))] # Create a TrialSummary Object for each trial trial_summaries = [] for trial_dir in sub_dirs: trial_summaries.append(TrialSummary(trial_dir)) # Create Plot of Averages and save plt.figure(2) # off screen figure plt.clf() plt.grid(True) for summary in trial_summaries: x, y = summary.get_average_response() plt.plot(x, y, label=summary.name) plt.legend() plt.savefig(os.path.join(selected_dir, 'average_plot.png')) # Check if each trial can be given a number based on sample name summary_plot_numbered = [] flag = True for trial in trial_summaries: try: num = float(trial.name) summary_plot_numbered.append((num, trial)) except: flag = False break if flag: # Sort list by associated trial number (lest to greatest) summary_plot_numbered = sorted(summary_plot_numbered, key=lambda i: i[0]) # Create the Summary Plots measurement_title = {0: "Amplitude (V)", 2: "Max amplitude current (A)", 4: "Min amplitude current (A)", 6: "Peak distance (V)", 8: "Peak separation (A)"} for measurement in range(0, 9, 2): plt.figure(2) plt.clf() values = [data.get_stats_list()[measurement] for number, data in summary_plot_numbered] errors = [data.get_stats_list()[measurement + 1] for number, data in summary_plot_numbered] plt.errorbar(range(len(values)), values, yerr=errors) for (label, x, y) in zip([x[0] for x in summary_plot_numbered], range(len(values)), values): plt.annotate(label, xy=(x, y), xytext=(5, 5), textcoords="offset points") plt.title(measurement_title[measurement]) plt.xlabel("Sample") plt.ylabel("Value and error (one standard deviation)") plt.xlim([-1, len(values)]) plt.savefig(os.path.join(selected_dir, measurement_title[measurement])) # Create Summary.txt for number, trial in summary_plot_numbered: trial.save_data(selected_dir) return
def annotatePlot(X, index, annotes, button): """Create popover label in 3d chart Args: X (np.array) - array of points, of shape (numPoints, 3) index (int) - index (into points array X) of item which should be printed Returns: None """ # If we have previously displayed another label, remove it first if hasattr(annotatePlot, 'label'): annotatePlot.label.remove() if button == 2: # Get data point from array of points X, at position index x2, y2, _ = proj3d.proj_transform(X[index, 0], X[index, 1], X[index, 2], ax.get_proj()) annote = annotes[index] annotatePlot.label = plt.annotate(annote, xy=(x2, y2), xytext=(-20, 20), textcoords='offset points', ha='right', va='bottom', bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')) # if we press another button (i.e. to pan the axis we need to create a dummy (blank) annotate # this is because the annotePlot still has an attribute (it has previously been called elif button != 2: annotatePlot.label = plt.annotate('', xy=(0, 0), xytext=(0, 0)) fig.canvas.draw()
def main(): gcConts, specScores, labels = parseGcSpec("out/annotOfftargets.tsv") studies = sorted(gcConts.keys()) colors = ["green", "blue", "green", "blue", "red", "grey", "orange", "blue", "orange", "red", "magenta", "blue"] markers = ["o", "s", "+", ">", "<", "o", ".", "o", "x", "+", ".", "<"] for i, study in enumerate(studies): xVals = gcConts[study] yVals = specScores[study] studyFig = plt.scatter(xVals, yVals, \ alpha=0.9, \ marker=markers[i], \ s=30, \ color=colors[i]) for x, y, label in zip(xVals, yVals, labels[study]): plt.annotate( \ label, fontsize=9, rotation=0, ha="right", rotation_mode="anchor", \ xy = (x, y), xytext = (-2,-1), alpha=1.0, textcoords = 'offset points', va = 'bottom') plt.xlabel("GC content of guide sequence") plt.ylabel("Specificity score of guide sequence") outfname = "out/gcSpecScores.pdf" plt.savefig(outfname, format = 'pdf') plt.savefig(outfname.replace(".pdf", ".png")) print "wrote %s" % outfname
def Main(): args=ParseArg() data=np.loadtxt(args.input,delimiter='\t',dtype=float) min_x=int(args.xlim[0]) max_x=int(args.xlim[1]) start=int(data[0,0]) peak=data[:,1].argmax() plt.ioff() plt.plot(np.array(range(min_x,max_x)),data[(min_x-start):(max_x-start),1],color='r',label="real_count") if args.distogram: plt.annotate('local max: '+str(peak+start)+"bp",xy=(peak+start,data[peak,1]),xytext=(peak+start+30,0.8*data[peak,1]),) # arrowprops=dict(facecolor='black', shrink=0.05)) # smoth the plot xnew=np.linspace(min_x,max_x,(max_x-min_x)/5) smooth=spline(np.array(range(min_x,max_x)),data[(min_x-start):(max_x-start),1],xnew) plt.plot(xnew,smooth,color='g',label='smooth(5bp)') max_y=max(data[(min_x-start):(max_x-start),1]) min_y=min(data[(min_x-start):(max_x-start),1]) plt.xlabel("Distance") plt.ylabel("Counts") plt.xlim(min_x,max_x) plt.ylim(min_y*0.9,max_y*1.1) plt.title(os.path.basename(args.input).split("_"+str(start))[0]) plt.legend() plt.savefig(os.path.basename(args.input).split("_"+str(start))[0]+"_%d~%dbp."%(min_x,max_x)+args.output) print >>sys.stderr,"output figure file generated!!"
def plot_confusion_matrix(self, y_true, y_pred, list_classes, title='Confusion matrix', filename='confusion_matrix.png'): # compute confusion matrix cm = confusion_matrix(y_true,y_pred) conf_mat_norm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] conf_mat2 = np.around(conf_mat_norm,decimals=2) # rounding to display in figure fig = plt.figure(figsize=(16,16), dpi=100) plt.imshow(conf_mat2,interpolation='nearest') for x in xrange(len(list_classes)): for y in xrange(len(list_classes)): plt.annotate(str(conf_mat2[x][y]),xy=(y,x),ha='center',va='center') plt.xticks(range(len(list_classes)),list_classes,rotation=90,fontsize=11) plt.yticks(range(len(list_classes)),list_classes,fontsize=11) plt.tight_layout(pad=3.) plt.title(title) plt.colorbar() # plt.show() fig.savefig(filename)
def make_xi_plot(): from numpy import linspace from matplotlib import pyplot as pl pl.rc('text', usetex=True) pl.rc('font', family='serif') lamdas = [-1,-0.9,-0.7,0,0.7,0.9,1] for lam in lamdas: plotx(lam,0,linestyle='k',minval=-0.9,maxval=10.0,logplot=True) plotx(lam,1,linestyle='k--',logplot=True) plotx(lam,2,logplot=True) plotx(lam,3,linestyle='k--',logplot=True) pl.vlines(x=1,ymin=-2,ymax=-0.076,color='k') pl.xlabel(r'$$\xi$$',fontsize=16) pl.ylabel(r'$$\tau$$',fontsize=16) pl.text(0.0, 0, r'$$M=0$$', bbox=dict(facecolor='white', alpha=1)) pl.text(0.0, 1.4, r'$$M=1$$', bbox=dict(facecolor='white', alpha=1)) pl.text(0, 2.48, r'$$M=2$$', bbox=dict(facecolor='white', alpha=1)) pl.text(1.3, -1.5, r'hyperbolic') pl.text(0.3, -1.5, r'elliptic') pl.annotate(r'$$\lambda = 1$$', xy=(-0.29, -0.19), xytext=(-1, -1), arrowprops=dict(facecolor='black', shrink=0.15, width=1,headwidth=5), ) pl.annotate(r'$$\lambda = -1$$', xy=(0.7, 0.4), xytext=(0.8,0.8), arrowprops=dict(facecolor='black', shrink=0.15, width=1,headwidth=5), ) pl.xlim((-2,2)) pl.ylim((-2,3))
def make_starter_plot(): from numpy import linspace from math import pi from matplotlib import pyplot as pl pl.rc('text', usetex=True) pl.rc('font', family='serif') pl.axvline(x=1,color='k') pl.xlabel(r'$$x$$',fontsize=16) pl.ylabel(r'$$T$$',fontsize=16) pl.text(0.5, 4, r'hyperbolic') pl.text(1.2, 4, r'elliptic') plot_x_curves([-1,-0.85,0.85,1],N=0,logplot=False,maxval=10,minval=-0.9) T0s = [pi,pi/2,1.1] for T0 in T0s: T =linspace(T0,10*pi) pl.plot((T0/T)**(2.0/3.0)-1.0,T,':k') T =linspace(0.1,T0) pl.plot((T0/T)**(1.0)-1.0,T,':k') pl.plot([],'k-',label="tof curves [-1,-0.85,0.85,1]") pl.plot([],'k:',label="initial guesses") pl.legend() pl.annotate(r'$$\lambda \le -0.85$$', xy=(-0.4, 7.5), xytext=(-0.1, 8), arrowprops=dict(facecolor='black', shrink=0.15, width=1,headwidth=5), ) pl.annotate(r'$$\lambda \ge 0.85$$', xy=(-0.33, 2.1), xytext=(-0.9, 1.09), arrowprops=dict(facecolor='black', shrink=0.15, width=1,headwidth=5), )
def analyzeSound(self): """ highlights N first peaks in frequency diagram """ # on recharge les données data = self.data sample_freq = self.sample_freq from scipy.fftpack import fftfreq freq_vect = fftfreq(data.size) * sample_freq # on trouve les maxima y0 = abs(fft(data)) # y1 = abs(fft(data[:, 1])) maxi0 = ((diff(sign(diff(y0))) < 0) & (y0[1:-1] > y0.max()/10.)).nonzero()[0] + 1 # local max # maxi1 = ((diff(sign(diff(y1))) < 0) & (y1[1:-1] > y1.max()/10.)).nonzero()[0] + 1 # local max # fréquence ax = self.main_figure.figure.add_subplot(212) ax.plot(freq_vect[maxi0], y0[maxi0], "o") # ax.plot(freq_vect[maxi1], y1[maxi1], "o") # annotations au dessus d'une fréquence de coupure fc = 100 for point in maxi0[(freq_vect[maxi0] > fc).nonzero()][:self.ui.spinBox.value()]: plt.annotate("%.2f" % freq_vect[point], (freq_vect[point], y0[point])) # for point in maxi1[(freq_vect[maxi0] > fc).nonzero()][:self.ui.spinBox.value()]: # plt.annotate("%.2f" % freq_vect[point], (freq_vect[point], y1[point])) self.ui.main_figure.canvas.draw()
def make_x_plot(): from numpy import linspace from matplotlib import pyplot as pl pl.rc('text', usetex=True) pl.rc('font', family='serif') lamdas = [-1,-0.9,-0.7,0,0.7,0.9,1] for lam in lamdas: plotx(lam,0) plotx(lam,1,linestyle='k--') plotx(lam,2) plotx(lam,3,linestyle='k--') pl.axvline(x=1,color='k') pl.xlabel(r'$$x$$',fontsize=16) pl.ylabel(r'$$T$$',fontsize=16) pl.text(0.0, 1.5, r'$$M=0$$', bbox=dict(facecolor='white', alpha=1)) pl.text(0.0, 4.7, r'$$M=1$$', bbox=dict(facecolor='white', alpha=1)) pl.text(0.0, 8.0, r'$$M=2$$', bbox=dict(facecolor='white', alpha=1)) pl.text(0.5, 4, r'hyperbolic') pl.text(1.2, 4, r'elliptic') pl.annotate(r'$$\lambda = 1$$', xy=(-0.25, 1.1), xytext=(-0.8, 0.2), arrowprops=dict(facecolor='black', shrink=0.15, width=1,headwidth=5), ) pl.annotate(r'$$\lambda = -1$$', xy=(0.5, 2.0), xytext=(0.7, 3.0), arrowprops=dict(facecolor='black', shrink=0.15, width=1,headwidth=5), )
def makeplots(): fig = plt.figure() ax = fig.add_subplot(111, autoscale_on=False, xlim=(0,10), ylim=(0,10)) point_list = [(5,5), (8,4), (3,2), (7,7), (2,6), (9,2), (4,7)] x = [5, 8, 3, 7, 2, 9, 4] y = [5, 4, 2, 7, 6, 2, 7] ax.axvline(5, 0, 1) ax.axhline(4, .5, 1, color='r') ax.axhline(2, 0, .5, color='r') ax.axvline(7, .4, 1) ax.axvline(2, .2, 1) ax.axvline(9, 0, .4) ax.axhline(7, .2, .5, color="r") #plt.annotate("root", xy=(5,5), xytext=(5.2,5)) #plt.annotate("root.right", xy=(8,4), xytext=(7.65,4.35)) #plt.annotate("root.right.left", xy=(9,2), xytext=(7,2)) #plt.annotate("root.left", xy=(3,2), xytext=(2.65,1.6)) #plt.annotate("root.left.right", xy=(1,6), xytext=(.2,6.1)) #plt.annotate("root.left.right.right\n (new node)", xy=(4,7), # xytext=(2.35,7.3)) ax.plot(x, y, 'ok') ax.plot([8], [4.45], '*k') mid = plt.Circle(xy=(8,4), radius=.5, color="green", alpha=.3) upp = plt.Circle(xy=(7,7), radius=.5, color="purple", alpha=.3) ax.add_artist(mid) ax.add_artist(upp) plt.annotate("target", xy=(8,4.5), xytext=(8.2,4.5))
def euclSpaceMapp(gDirected,distMat,top100List,top100ListIdxs): print('extract euclidean space mapping') allCoordinates = euclideanCoords(gDirected,distMat) print('Mapped nodes to euclidean space') xpl=[x[0] for x in allCoordinates] minXpl = min(xpl) if minXpl < 0: aminXpl = abs(minXpl) xpl = np.array([x+aminXpl+1 for x in xpl]) ypl=[x[1] for x in allCoordinates] minYpl = min(ypl) if minYpl < 0: aminYpl = abs(minYpl) ypl = np.array([y+aminYpl+1 for y in ypl]) fig = pyplot.figure() ax = pyplot.gca() ax.scatter(xpl,ypl) ax.set_ylim(min(ypl)-1,max(ypl)+1) ax.set_xlim(min(xpl)-1,max(xpl)+1) labels = top100List for label, x, y in zip(labels, xpl[top100ListIdxs], ypl[top100ListIdxs]): pyplot.annotate(label, xy = (x, y), xytext = (-10, 10),textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.2', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) interactive(True) pyplot.show() # pyplot.savefig('./images/'+str(year)+'_euclSpaceMapping_via_shortestPaths.jpg', bbox_inches='tight', format='jpg') pyplot.savefig('./images/'+str(year)+'_euclSpaceMapping_via_distMatrix.jpg', bbox_inches='tight', format='jpg') pyplot.close()
def plotCurves(losses,rateOfExceedance,return_periods,lossLevels): plt.figure(1, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k') plt.scatter(losses,rateOfExceedance,s=20) if len(return_periods) > 0: annual_rate_exc = 1.0/np.array(return_periods) for rate in annual_rate_exc: if rate > min(rateOfExceedance): plt.plot([min(losses),max(losses)],[rate,rate],color='red') plt.annotate('%.6f' % rate,xy=(max(losses),rate),fontsize = 12) plt.yscale('log') plt.xscale('log') plt.ylim([min(rateOfExceedance),1]) plt.xlim([min(losses),max(losses)]) plt.xlabel('Losses', fontsize = 16) plt.ylabel('Annual rate of exceedance', fontsize = 16) setReturnPeriods = 1/rateOfExceedance plt.figure(2, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k') plt.scatter(setReturnPeriods,losses,s=20) if len(return_periods) > 0: for period in return_periods: if period < max(setReturnPeriods): plt.plot([period,period],[min(losses),max(losses)],color='red') plt.annotate(str(period),xy=(period,max(losses)),fontsize = 12) plt.xscale('log') plt.xlim([min(setReturnPeriods),max(setReturnPeriods)]) plt.ylim([min(losses),max(losses)]) plt.xlabel('Return period (years)', fontsize = 16) plt.ylabel('Losses', fontsize = 16)
def plot1(lin, log, knn, svm, rf, ann, title): objects = ('Lin', 'Log', 'KNN', 'SVM', 'Random Forest', 'ANN') performance = [lin, log, knn, svm, rf, ann] y_pos = np.arange(len(objects)) plt.figure(figsize=(15, 8), dpi=100) plt.bar(y_pos, performance, align='center', alpha=1.0) plt.xticks(y_pos, objects) plt.ylim([0.0, 1.0]) plt.ylabel('Percent Acurate') plt.title('Accuracies of Algorithms Predicting DJIA Prices with ' + title + ' Days Data') plt.annotate(str("{0:.2f}".format(lin)), xy=(.8, lin + .01), xytext=(-0.15, lin + .01)) plt.annotate(str("{0:.2f}".format(log)), xy=(.8, log + .01), xytext=(.8, log + .01)) plt.annotate(str("{0:.2f}".format(knn)), xy=(.8, knn + .01), xytext=(1.8, knn + .01)) plt.annotate(str("{0:.2f}".format(svm)), xy=(.8, svm + .01), xytext=(2.8, svm + .01)) plt.annotate(str("{0:.2f}".format(rf)), xy=(.8, rf + .01), xytext=(3.8, rf + .01)) plt.annotate(str("{0:.2f}".format(ann)), xy=(.8, ann + .01), xytext=(4.8, ann + .01)) #plt.savefig('Accu_' + title +'.png') plt.show()
lat, diff_ta850[i], np.linspace(vmin, vmax, levs), cmap=plt.get_cmap("Reds"), extend="both") plt.contourf(lon, lat, sig_ta850[i], levels=[0.5, 1.5], colors="none", hatches=["//"]) m.drawcoastlines() plt.title(s_title[i]) if cnt == 0: plt.ylabel("T850") plt.annotate(alph[cnt], xy=(0.05, 0.05), xycoords='axes fraction') plt.subplot(4, 4, cnt + 5) c = plt.contourf(lon, lat, diff_ta500[i], np.linspace(vmin, vmax, levs), cmap=plt.get_cmap("Reds"), extend="both") plt.contourf(lon, lat, sig_ta500[i], levels=[0.5, 1.5], colors="none", hatches=["//"]) m.drawcoastlines()
fig, ax = plt.subplots(figsize=(9, 6)) fig.subplots_adjust(bottom=0.2, left=0.2) ax.plot(x1, np.cumsum(y1**2)) ax.set_xlabel('time [s] \n This was a long experiment') ax.set_ylabel(r'$\int\ Y^2\ dt\ \ [V^2 s]$') plt.show() # <codecell> anotating text ax = plt.subplot(111) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.annotate('local min', xy=(2.5, -1), xytext=(3, -1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show() # <codecell> logarithmic scale from matplotlib.ticker import NullFormatter # useful for `logit` scale # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the interval ]0, 1[
catering_sale = 'E:\\PythonTestCode\\dadm\\chapter3\\chapter3\\demo\\data\\catering_sale.xls' #数据路径 data = pd.read_excel(catering_sale, index_col = False) #读取数据 data_y = data[u'销量'] data_x = data[u'日期'] ymean = np.mean(data_y) ystd = np.std(data_y) threshold1 = ymean - n * ystd threshold2 = ymean + n * ystd outlier = [] #将异常值保存 outlier_x = [] for i in range(0, len(data_y)): if (data_y[i] < threshold1)|(data_y[i] > threshold2): outlier.append(data_y[i]) outlier_x.append(data_x[i]) else: continue print('\n异常数据如下:\n') print(outlier) print(outlier_x) plt.plot(data_x, data_y) plt.plot(outlier_x, outlier, 'ro') for j in range(len(outlier)): plt.annotate(outlier[j], xy=(outlier_x[j], outlier[j]), xytext=(outlier_x[j],outlier[j])) plt.show()
def plot_Japan_map(self, lat_array, lon_array, intersting_satellites, dphi, dlambda): #Japan boundary south, north = 32, 45 west, east = 132.5, 151 center = [(east+west)/2, (north+south)/2] m = Basemap(llcrnrlon=west, llcrnrlat=south, urcrnrlon=east, urcrnrlat=north, resolution='h', area_thresh = 0.1, projection='tmerc', lat_ts=0, lon_0=center[0], lat_0=center[1]) # Plot features m.drawcoastlines() m.drawparallels(np.arange(-81., 81., 2),labels=[1,0,0,0], linewidth=0.0) #-81., 81., 4 m.drawmeridians(np.arange(-180., 181., 4),labels=[0,0,0,1], linewidth=0.0) #-180., 181., 10 m.shadedrelief() if 'iDerry_iMac' in self.pathList: m.readshapefile(r'<your_path>', name='PB2002_plates', drawbounds=False, color='r') else: m.readshapefile(r'<your_path>', name='PB2002_plates', drawbounds=False, color='orange') plate_names = [] for plate_dict in m.PB2002_plates_info: plate_names.append(plate_dict['PlateName']) plates_of_interest = ['Okhotsk', 'Philippine Sea', 'Pacific', 'Eurasia', 'North America'] #Kermadec #Pacific #Australia for info, shape in zip(m.PB2002_plates_info, m.PB2002_plates): if info['PlateName'] in plates_of_interest: x, y = zip(*shape) m.plot(x, y, marker=None, color='sandybrown') m.drawmapscale(146.3, 33.2, 145, 40, 500, barstyle='fancy', fontcolor='whitesmoke', fillcolor1='whitesmoke', fillcolor2='silver') earthquake_index_original = 973 # Japan MIZU=973/2772 eq_longitude_values = [] eq_latitude_values = [] satellite_list = [] start = [0, 0, 0, 0, 0, 0, 0, 0] end = [6480, 10800, 10800, 10800, 10080, 9360, 10800, 7200] for i in range(len(intersting_satellites)): spec_sv_lam = lon_array[: ,i] spec_sv_phi = lat_array[: ,i] #Code added here to splice arrays depending on when the satellites were tracked. Use VTEC plots (time axes) for reference spec_sv_lam = spec_sv_lam[start[i]:end[i]] spec_sv_phi = spec_sv_phi[start[i]:end[i]] # Find index of EQ once the splice has happened: earthquake_index = earthquake_index_original - start[i] #earthquake_index = 973 track_len = len(spec_sv_lam) earthquake_long = spec_sv_lam[earthquake_index] earthquake_lat = spec_sv_phi[earthquake_index] longitude_values = [] latitude_values = [] for j in range(track_len): latitude = spec_sv_phi[j] longitude = spec_sv_lam[j] latitude_values.append(latitude) longitude_values.append(longitude) # Convert latitude and longitude to coordinates X and Y x, y = m(longitude_values, latitude_values) x_eq, y_eq = m(earthquake_long, earthquake_lat) eq_longitude_values.append(x_eq) eq_latitude_values.append(y_eq) d = intersting_satellites[i] +1 d_replace = str(d).zfill(2) prn = 'G%s' % d_replace satellite_list.append(prn) # Plot the points on the map plt.plot(x,y,'-', color='blue', zorder=7) print "plot_Japan_map: Track plotted for PRN{0}".format(intersting_satellites[i]+1) #Plotting exact points on the ground track where earthquake occurs m.scatter(eq_longitude_values, eq_latitude_values, s=40, marker='*', color='r', zorder=10) for i, (x, y) in enumerate(zip(eq_longitude_values, eq_latitude_values), start=0): plt.annotate(str(satellite_list[i]), (x,y), xytext=(2, 2), textcoords='offset points', color='k', fontsize=9, zorder=9) # Plot points and text lons, lats = [142.372, 139.840, dlambda, 145.5, 133, 136.2, 138.9, 146.1, 139.0], [38.297, 35.653, dphi, 36.0, 39, 42, 43.5, 41.1, 33.3] x, y = m(lons, lats) x_offsets = [26000, 20000] y_offsets = [-25000, 12000] m.scatter(x[1], y[1], s=60, marker='s', color='k') #For the capital square symbol for Tokyo m.scatter(x[2], y[2], s=100, marker='^', color='k') plt.text(x[2] +x_offsets[1], y[2] +y_offsets[1], 'mizu', style='italic', color='k', fontsize=15) plt.text(x[1] +x_offsets[1], y[1] +y_offsets[1], 'Tokyo', color='k', fontsize=15, zorder=3) plt.text(x[3], y[3], 'Pacific\n Ocean', style='italic', color='dodgerblue', fontsize=17, zorder=8) plt.text(x[4], y[4], 'Sea of\nJapan', style='italic', color='dodgerblue', fontsize=17, zorder=8) plt.text(x[5], y[5], 'Eurasian\n Plate', fontsize=11) plt.text(x[6], y[6], 'North\nAmerican\nPlate', fontsize=11) plt.text(x[7], y[7], 'Pacific\n Plate', fontsize=11) plt.text(x[8], y[8], 'Philippine\n Sea Plate', fontsize=10) #Beachball plotting ax = plt.gca() b = beach([193, 9, 78], facecolor='red', xy=(x[0], y[0]), width=70000, linewidth=1, alpha=0.85) b.set_zorder(10) ax.add_collection(b) if 'iMac' in self.pathList: plt.savefig(r'<your_path>', bbox_inches="tight") else: plt.savefig(r'<your_path>', bbox_inches="tight") plt.close() print "Japan Map generated"
def annotate_plot(): cs = ['#008fd5', '#fc4f30', '#e5ae38'] ratings = ['5', '4', '3', '2', '1'] for i, (xy, s) in enumerate( zip([(-0.15, 0.075), (0.38, 0.868), (0.98, 0.075)], ratings)): plt.annotate(s, xy, color=cs[i], size=20)
# calculate sums of squares Sxx, Syy = (vec1**2).sum(axis=0) Sxy = vec1.prod(axis=1).sum(axis=0) # Calculate parameters n = vec1.shape[0] # get length of array (number of x and y) Slope = (n*Sxy - Sx*Sy)/(n*Sxx - Sx**2) # calculate slope of best-fit Inter = (Sy - Slope*Sx)/n # calculate intercept of best-fit # calculate uncertainties s2e = (1/(n*(n-2)))*(n*Syy - Sy**2 - (n*Sxx - Sx**2)*Slope**2) s2Slope = n*s2e/(n*Sxx - Sx**2) s2Inter = s2Slope*Sxx/n # print regression stats to the console print('The data has a best-fit regression with a slope of {:.5} +/- {:.5}, and a y-intercept of {:.5} +/- {:.5}'.format(Slope, s2Slope, Inter, s2Inter)) # generate best-fit line for plotting Xfit = np.linspace(1.4,1.9,num=25) Yfit = Inter + Slope*Xfit # plot results fig = plt.figure() # initiate figure object plt.plot(vec1[:,0],vec1[:,1],'bo',Xfit,Yfit,'r-') # define x, y, and what they look like plt.xlabel('Independent Variable (x)') # add x-axis label plt.ylabel('Dependent Variable (y)') # add y-axis label plt.title('HW4A Linear Regression') # add plot title plt.legend(['Raw data','Linear Regression'], loc=4) # add legend for points and best-fit line plt.annotate(xy=[1.4,70], s='y = {:.5}*x + {:.5}'.format(Slope, Inter)) # add regression metrics to plot area plt.show() # print figure to window
def plot_error(tomato_pred, tomato_act, error, pwd=None, name=None, use_mm=False, title=None, dpi=None, ext=None): if ext is None: ext = EXT if dpi is None: dpi = DPI fig = plt.gcf() ax = plt.gca() if title is not None: plt.title(title) if use_mm: unit = 'mm' else: unit = 'px' n_true_pos = len(tomato_pred['true_pos']['centers']) n_false_pos = len(tomato_pred['false_pos']['centers']) n_false_neg = len(tomato_act['false_neg']['centers']) if 'com' in tomato_pred.keys(): n_com = len(tomato_pred['com']) else: n_com = 0 centers = [] centers.extend(tomato_pred['true_pos']['centers']) centers.extend(tomato_pred['false_pos']['centers']) centers.extend(tomato_act['false_neg']['centers']) if 'com' in tomato_pred.keys(): centers.extend([tomato_pred['com']]) labels = [] labels.extend(['true_pos'] * n_true_pos) labels.extend(['false_pos'] * n_false_pos) labels.extend(['false_neg'] * n_false_neg) labels.extend(['com'] * n_com) error_centers = [] error_centers.extend(error['centers']) error_centers.extend(n_false_pos * [None]) error_centers.extend(n_false_neg * [None]) if 'com' in tomato_pred.keys(): error_centers.append(error['com']) if 'radii' in error.keys(): error_radii_val = error['radii'] else: error_radii_val = [None] * n_true_pos error_radii = [] error_radii.extend(error_radii_val) error_radii.extend(n_false_pos * [None]) error_radii.extend(n_false_neg * [None]) error_radii.extend(n_com * [None]) # sort based on the y location of the centers zipped = zip(centers, error_centers, error_radii, labels) zipped.sort(key=lambda x: x[0][1]) centers, error_centers, error_radii, labels = zip(*zipped) # default bbox style bbox_props = dict(boxstyle="square,pad=0.2", fc="w", ec="w", lw=0) kw_default = dict(arrowprops=dict(arrowstyle="-", linewidth=0.5), bbox=bbox_props, va="center", size=8, color='k') y_lim = ax.get_ylim() h = y_lim[0] - y_lim[1] x_lim = ax.get_xlim() w = x_lim[1] - x_lim[0] # n determines the spacing of the baxes from the top of the image if 'radii' in error.keys(): n = 0.5 else: n = 3 y_increment = h / (len(centers) + n) y_text = (n / 2.0 + 0.5) * y_increment # 1.0/n* h # kw_list = [] for center, error_center, error_radius, label in zip( centers, error_centers, error_radii, labels): # copy default style kw = copy.deepcopy(kw_default) if label == 'true_pos': center_error = int(round(error_center)) if error_radius is not None: radius_error = int(round(error_radius)) text = 'loc: {c:d}{u:s} \nr: {r:d}{u:s}'.format(c=center_error, r=radius_error, u=unit) else: text = 'loc: {c:d}{u:s}'.format(c=center_error, u=unit) arrow_color = mpl.colors.colorConverter.to_rgba('w', alpha=1) elif label == 'com': center_error = int(round(error_center)) text = 'com: {c:d}{u:s}'.format(c=center_error, u=unit) kw['bbox']['fc'] = 'k' kw['bbox']['ec'] = 'k' kw['color'] = 'w' arrow_color = mpl.colors.colorConverter.to_rgba('w', alpha=1) elif label == 'false_pos': text = 'false positive' kw['bbox']['fc'] = 'r' kw['bbox']['ec'] = 'r' arrow_color = 'r' elif label == 'false_neg': text = 'false negative' kw['bbox']['ec'] = 'lightgrey' kw['bbox']['fc'] = 'lightgrey' arrow_color = 'lightgrey' # print center, label y = center[1] x = center[0] if x <= 0.35 * w: x_text = 0.6 * w elif x <= 0.5 * w: x_text = 0.05 * w elif x <= 0.65 * w: x_text = 0.8 * w else: x_text = 0.2 * w # w x_diff = x_text - x y_diff = y_text - y if (x_diff > 0 and y_diff > 0) or (x_diff < 0 and y_diff < 0): ang = -45 else: ang = 45 connectionstyle = "angle,angleA=0,angleB={}".format(ang) kw["arrowprops"].update({ "connectionstyle": connectionstyle, 'color': arrow_color }) # kw_list.append(kw) plt.annotate(text, xy=(x, y), xytext=(x_text, y_text), zorder=high_layer, **kw) # y_text = y_text + y_increment if pwd: save_fig(fig, pwd=pwd, name=name, dpi=dpi, ext=ext)
df = pd.read_csv(csvpass, index_col=0) selectedparts = df[selectpartscolumns] polardata = Coortra.DimentionUni(selectedparts,1) df2 = pd.DataFrame(polardata,columns = selectpartscolumns) featurevector = Extractor.Feature(df2,Feature_Flag) #print(featurevector) #featurevector.column = [subject + str(number + 1)] featurevector = pd.DataFrame(featurevector, columns = [subject + str(number + 1)]) print(featurevector) print("iの値は" +str(i)) beforefeature = pd.concat([beforefeature,featurevector], axis = 1) i = i+1 dfall = beforefeature.T corr = dfall.corr() corr.head(len(corr)) corr.to_csv( basepass + '/result_dev/time_table.csv') print(corr) #sns.heatmap(corr, center = 0, square = True, vmax = 1, vmin = -1) #plt.savefig(basepass + 'all_fig.png') dfall.to_csv(basepass + "/result_dev/dev_FeatureMatrix.csv") tsne = TSNE(n_components = 2, random_state=42) data_tsne = tsne.fit_transform(dfall) plt.rcParams["font.family"] = "Times New Roman" plt.rcParams['font.size'] = 10 #フォントサイズを設定 plt.scatter(data_tsne[:, 0], data_tsne[:, 1], c = "b", marker= "o") labels = dfall.index for label,x,y in zip(labels,data_tsne[:,0],data_tsne[:,1]): plt.annotate(label,xy = (x, y)) plt.savefig("/home/kei/document/experiments/ICTH2019/result_dev/dev_tsne_map.png")
def plot_stats_histogram_one_entry(pos, cat, vtype, data_b, data_a, num_bin, var_reduction): plt.subplot(pos) plt.xlabel(vtype, fontsize=15, fontweight='bold') plt.ylabel(cat, fontsize=15) if vtype == "CC": ax_min = min(min(data_b), min(data_a)) ax_max = max(max(data_b), max(data_a)) elif vtype == r"$\chi$": ax_min = 0.0 ax_max = max(max(data_b), max(data_a)) else: ax_min = min(min(data_b), min(data_a)) ax_max = max(max(data_b), max(data_a)) abs_max = max(abs(ax_min), abs(ax_max)) ax_min = -abs_max ax_max = abs_max MU1 = np.mean(data_b) MU2 = np.mean(data_a) STD1 = np.std(data_b) STD2 = np.std(data_a) binwidth = (ax_max - ax_min) / num_bin plt.hist(data_b, bins=np.arange(ax_min, ax_max + binwidth / 2., binwidth), facecolor='blue', alpha=0.3) plt.hist(data_a, bins=np.arange(ax_min, ax_max + binwidth / 2., binwidth), facecolor='red', alpha=0.5) if vtype == r"$\chi$": plt.yscale('log') #MU1 *= len(data_a) #MU2 *= len(data_a) if vtype in ["CC", "TS", "dlnA"]: annotation_string = r'$\mu: %.2f\to%.2f$' % ( MU1, MU2) + '\n' + r'$\sigma: %.2f\to%.2f$' % (STD1, STD2) plt.annotate(annotation_string, xy=(0.05, 0.88), xycoords='axes fraction', fontsize=12) elif vtype in [r"$\chi$"]: percentage = '{:.2f}%'.format(100 * (var_reduction[0])) percentage1 = '{:.2f}%'.format(100 * (var_reduction[1])) #plt.annotate('$\mu={:.2f}, {:.2f}$\n$\sigma={:.2f}, {:.2f}$\n$\Delta Var={:.2f}% $'.format(MU1,MU2,STD1,STD2,var_reduction), xy=(0.5, 0.81), xycoords='axes fraction', fontsize =12) annotation_string = r'$\mu: %.2f\to%.2f$' % ( MU1, MU2) + '\n' + r'$\sigma: %.2f\to%.2f$' % (STD1, STD2) plt.annotate(annotation_string, xy=(0.5, 0.81), xycoords='axes fraction', fontsize=12) plt.annotate( '$\Delta (\sum \chi) = {} $%\n$\Delta \phi = {}$%'.format( percentage, percentage1), xy=(0.5, 0.60), xycoords='axes fraction', fontsize=12) elif vtype not in ['CC']: annotation_string = r'$\mu: %.2f\to%.2f$' % ( MU1, MU2) + '\n' + r'$\sigma: %.2f\to%.2f$' % (STD1, STD2) plt.annotate(annotation_string, xy=(0.5, 0.88), xycoords='axes fraction', fontsize=12)
# Author: Joey_Cheng # Date: 2021 / 4 / 13 import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv("../data/Special-Events-2020-updated.csv") plt.figure(figsize=(18, 12)) plt.title("Special Events OTHOURS 2020") plt.xlabel("OT Hours") plt.ylabel("Description") OTHours = data.groupby("DESCRIPTION")["OTHOURS"].sum().sort_values() bars = plt.barh(OTHours.index, OTHours.values, color=["sienna", "olive", "brown", "peru"]) # add annotates for bar, label in zip(bars, OTHours.values): plt.annotate(label, xy=(label, bar.get_y() + 0.4), ha='left', va='center') plt.tight_layout() plt.savefig("../img/OTHOURS by Description 2020.png")
plt.xlabel("Samples") plt.ylabel("Time difference /ms") plt.xlim(0, len(data)) # find a measure of spread of data # find order of range dataRange = max(data) - min(data) order = 1 while (order < dataRange): order *= 10 order /= 10 if (order > 1): order /= 10 plt.ylim( int(min(data) / order - 1) * order, int(max(data) / order + 1) * order) axes = plt.axes() axes.yaxis.set_major_formatter(y_formatter) plt.annotate("Average: " + str(round(np.average(data), 2)) + " ms; Std dev: " + str(round(np.std(data), 2)) + " ms", xy=(0.05, 0.95), xycoords='axes fraction') saveFileName = filename + "_" + name + "(" + str(start) + "-" + str( end) + ")" plt.savefig("../../Results/" + saveFileName + ".png", dpi=400) plt.savefig("../../Results/" + filename + "_" + name + "(" + str(start) + "-" + str(end) + ").svg") plt.show() plt.clf()
r = np.random.choice(range(len(sgp)), size, replace=False) for i in r: x_data.append(sgp[i][0]) # n dim y_data.append([sgp[i][1]]) # n, 1 dim return x_data, y_data with tf.Session() as sess: tf.global_variables_initializer().run() for i in range(100): batch_inputs, batch_labels = generate_batch(25) _, loss_val = sess.run([train, loss], { X: batch_inputs, Y_: batch_labels }) if i % 10 == 0: print("Loss :", i, loss_val) # loss # normalize final_embeddings = embeddings.eval() for i, l in enumerate(rdictionary[:10]): x, y = final_embeddings[i, :] plt.scatter(x, y) plt.annotate(l, xy=(x, y), xytext=(5, 2), textcoords='offset points', ha='right', va='bottom') plt.savefig("tf_word2vec.png")
# dim 32 take 1.501346 seconds to execute per time # dim 64 take 5.951431 seconds to execute per time # dim 128 take 23.950499 seconds to execute per time # dim 256 take 100.562294 seconds to execute per time dims = [16, 32, 64, 128, 256] time_t = [0.401588, 1.501346, 5.951431, 23.950499, 100.562294] x = np.arange(len(dims)) # the label locations rects = plt.bar(x, time_t, align = 'center', alpha = 0.7) for rect in rects: height = rect.get_height() plt.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width()/2, height), xytext = (0, 3), textcoords = 'offset points', ha = 'center', va = 'bottom') plt.xlabel("Dimension " + r"$d$", fontsize = 16) plt.ylabel("Time Costed(s)", fontsize = 16) plt.title(r"$t \sim O(d^2)$", fontsize = 16) plt.xticks(x, dims) plt.tight_layout() plt.savefig("convergence_speed.pdf") plt.show()
def prec_rec_curve(y, y_pred, n_classes, fontsize=20): """ Precision - Recall Curve """ plt.rc('font', size=fontsize) colors = [ 'xkcd:sky blue', 'xkcd:forest green', 'xkcd:dark red', 'xkcd:dark yellow' ] y = label_binarize(y, classes=np.arange(n_classes)) # For each class precision = dict() recall = dict() average_precision = dict() for i in range(n_classes): precision[i], recall[i], _ = precision_recall_curve( y[:, i], y_pred[:, i]) average_precision[i] = average_precision_score(y[:, i], y_pred[:, i]) # A "micro-average": quantifying score on all classes jointly precision["micro"], recall["micro"], _ = precision_recall_curve( y.ravel(), y_pred.ravel()) average_precision["micro"] = average_precision_score(y, y_pred, average="micro") print('Average precision score, micro-averaged over all classes: {0:0.2f}'. format(average_precision["micro"])) plt.figure(figsize=(12, 12)) f_scores = np.linspace(0.2, 0.8, num=4) lines = [] labels = [] for f_score in f_scores: x = np.linspace(0.01, 1) y_ = f_score * x / (2 * x - f_score) l, = plt.plot(x[y_ >= 0], y_[y_ >= 0], color='gray', alpha=0.5) plt.annotate('F1={0:0.1f}'.format(f_score), xy=(0.9, y_[45] + 0.02)) lines.append(l) labels.append('F1 curves') l, = plt.plot(recall["micro"], precision["micro"], color='gold', lw=2) lines.append(l) labels.append('Micro-average (area = {0:0.2f})' ''.format(average_precision["micro"])) for i in range(n_classes): l, = plt.plot(recall[i], precision[i], lw=2, color=colors[i]) lines.append(l) labels.append('Class {0} (area = {1:0.2f})'.format( i, average_precision[i])) fig = plt.gcf() fig.subplots_adjust(bottom=0.25) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xticks(np.arange(.2, 1., .2)) plt.xlabel('Recall', labelpad=20) plt.ylabel('Precision', labelpad=20) plt.title('Precision-Recall Curve', pad=20) plt.legend(lines, labels, loc=(0, -.2), ncol=2)
def netcdf2png(url, colormapPath, colormapName, dirDest, lat_name, lon_name, data_name, geos=False): # Dataset is the class behavior to open the file # and create an instance of the ncCDF4 class nc_fid = netCDF4.Dataset(url, 'r') if data_name == 'Band1': name = basename(url) # obtengo el nombre base del archivo else: t_coverage = repr(nc_fid.getncattr('time_coverage_end')) # print t_coverage ds_name = repr(nc_fid.getncattr('dataset_name')) # print ds_name date = re.search('\'(.*?)\'', t_coverage).group(1) print date channel = re.search('-M\d(.*?)_', ds_name).group(1) print channel channelInfo(channel) yyyy = date[0:4] mm = date[5:7] dd = date[8:10] hhmm = date[11:16] name = channel + " " + dd + "-" + mm + "-" + yyyy + " " + hhmm + " UTC" print "name: " + name # if name # extract/copy the data lats = nc_fid.variables[lat_name][:] lons = nc_fid.variables[lon_name][:] data = nc_fid.variables[data_name][:] if data_name == 'CMI' or data_name == 'Rad': # Satellite height sat_h = nc_fid.variables[ 'goes_imager_projection'].perspective_point_height sat_h -= 10000 # Satellite longitude sat_lon = nc_fid.variables[ 'goes_imager_projection'].longitude_of_projection_origin # Satellite sweep sat_sweep = nc_fid.variables['goes_imager_projection'].sweep_angle_axis X = nc_fid.variables[lon_name][:] * sat_h # longitud, eje X Y = nc_fid.variables[lat_name][:] * sat_h # latitud, eje Y # si el canal es el 2 divido las dimensiones de los elementos if not geos and channel == 'C02': X = X[7000:9500] Y = Y[7000:9500] data = data[7000:9500, 7000:9500] elif not geos: X = X[3500:4800] Y = Y[3500:4800] data = data[3500:4800, 3500:4800] elif geos: X = X[::4] Y = Y[::4] data = data[::4, ::4] print "sat_h: " + str(sat_h) print "Sat_lon: " + str(sat_lon) # end if data_name == 'CMI' nc_fid.close() min_lon = numpy.amin(lons) max_lon = numpy.amax(lons) min_lat = numpy.amin(lats) max_lat = numpy.amax(lats) print "min_lat: " + str(min_lon) print "max_lat: " + str(max_lon) print "min_lon: " + str(min_lat) print "max_lon: " + str(max_lat) # for i in data: # print i print "Data min: " + str(numpy.amin(data)) print "Data max: " + str(numpy.amax(data)) # seteo los minimos y maximos de la imagen en funcion de los min y max de lat y long # axes = plt.gca() # axes.set_xlim([min_lon, max_lon]) # axes.set_ylim([min_lat, max_lat]) zona = 'plata' if data_name == 'Band1': # para archivos nc ya proyectados a mercator X = map(lambda x: x + 0.11, lons) # incremento X Y = map(lambda y: y + 0.11, lats) # incremento Y ax = Basemap(projection='merc',\ llcrnrlat=-42.94,urcrnrlat=-22.0,\ llcrnrlon=-67.0,urcrnrlon=-45.04,\ resolution='f') # resolution: c, l, i, h, f lons2d, lats2d = numpy.meshgrid(X, Y) # dadas las lat y lon del archivo, obtengo las coordenadas x y para # la ventana seleccionada como proyeccion x, y = ax(lons2d, lats2d) elif geos: # proyecto toda la foto completa de geo estacionario print "Ventana Globo geoestacionario" min_Y = numpy.amin(Y) max_Y = numpy.amax(Y) min_X = numpy.amin(X) max_X = numpy.amax(X) XX = map(lambda x: x + max_X, X) # incremento X YY = map(lambda y: y + max_Y, Y) # incremento Y print "min_Y: " + str(min_Y) print "max_Y: " + str(max_Y) print "min_X: " + str(min_X) print "max_X: " + str(max_X) print numpy.amin(XX) print numpy.amin(YY) x, y = numpy.meshgrid(XX, YY) ax = Basemap(projection='geos', lon_0=sat_lon, satellite_height=sat_h,\ llcrnrx=-x.max()/2,llcrnry=-y.max()/2,\ urcrnrx=x.max()/2,urcrnry=y.max()/2,\ resolution='l') elif zona == 'plata': # proyecto con mercator en la región del río de la plata print "Ventana Ŕío de la Plata" # https://github.com/blaylockbk/pyBKB_v2/blob/master/BB_goes16/mapping_GOES16_data.ipynb min_Y = numpy.amin(Y) max_Y = numpy.amax(Y) min_X = numpy.amin(X) max_X = numpy.amax(X) # parche para alinear la fotografía con las coordenadas geográficas # supongo que una vez esté calibrado el satélite hay que eliminar estas líneas X = map(lambda x: x + 10000, X) # incremento X Y = map(lambda y: y + 10000, Y) # incremento Y print "min_Y: " + str(min_Y) print "max_Y: " + str(max_Y) print "min_X: " + str(min_X) print "max_X: " + str(max_X) print numpy.amin(X) print numpy.amin(Y) # Región ax = Basemap(projection='merc',\ llcrnrlat=-42.94,urcrnrlat=-22.0,\ llcrnrlon=-67.0,urcrnrlon=-45.04,\ resolution='f') projection = Proj(proj='geos', h=sat_h, lon_0=sat_lon, sweep=sat_sweep) x_mesh, y_mesh = numpy.meshgrid(X, Y) lons, lats = projection(x_mesh, y_mesh, inverse=True) x, y = ax(lons, lats) elif zona == 'sur': # proyecto con mercator en la región del río de la plata print "Ventana Sur" # https://github.com/blaylockbk/pyBKB_v2/blob/master/BB_goes16/mapping_GOES16_data.ipynb min_Y = numpy.amin(Y) max_Y = numpy.amax(Y) min_X = numpy.amin(X) max_X = numpy.amax(X) # parche para alinear la fotografía con las coordenadas geográficas # supongo que una vez esté calibrado el satélite hay que eliminar estas líneas X = map(lambda x: x + 10000, X) # incremento X Y = map(lambda y: y + 3000, Y) # incremento Y # X = map(lambda x: x+10000, X) # incremento X # Y = map(lambda y: y+10000, Y) # incremento Y print "min_Y: " + str(min_Y) print "max_Y: " + str(max_Y) print "min_X: " + str(min_X) print "max_X: " + str(max_X) print numpy.amin(X) print numpy.amin(Y) # Región ax = Basemap(projection='merc',\ llcrnrlat=-49.4947,urcrnrlat=-13.6169,\ llcrnrlon=-73.4699,urcrnrlon=-39.2205,\ resolution='f') projection = Proj(proj='geos', h=sat_h, lon_0=sat_lon, sweep=sat_sweep) x_mesh, y_mesh = numpy.meshgrid(X, Y) lons, lats = projection(x_mesh, y_mesh, inverse=True) x, y = ax(lons, lats) elif zona == 'uy': # proyecto con mercator en la región del río de la plata print "Ventana Uruguay" # https://github.com/blaylockbk/pyBKB_v2/blob/master/BB_goes16/mapping_GOES16_data.ipynb min_Y = numpy.amin(Y) max_Y = numpy.amax(Y) min_X = numpy.amin(X) max_X = numpy.amax(X) # parche para alinear la fotografía con las coordenadas geográficas # supongo que una vez esté calibrado el satélite hay que eliminar estas líneas X = map(lambda x: x + 10000, X) # incremento X Y = map(lambda y: y + 10000, Y) # incremento Y print "min_Y: " + str(min_Y) print "max_Y: " + str(max_Y) print "min_X: " + str(min_X) print "max_X: " + str(max_X) print numpy.amin(X) print numpy.amin(Y) # Región ax = Basemap(projection='merc',\ llcrnrlat=-35.2138,urcrnrlat=-29.7466,\ llcrnrlon=-58.9073,urcrnrlon=-52.7591,\ # llcrnrx=-x.max()/2,llcrnry=-y.max()/2,\ # urcrnrx=x.max()/2,urcrnry=y.max()/2,\ resolution='f') projection = Proj(proj='geos', h=sat_h, lon_0=sat_lon, sweep=sat_sweep) x_mesh, y_mesh = numpy.meshgrid(X, Y) lons, lats = projection(x_mesh, y_mesh, inverse=True) x, y = ax(lons, lats) # end if # agrego los vectores de las costas, departamentos/estados/provincias y paises ax.drawcoastlines(linewidth=0.25) ax.drawcountries(linewidth=0.50) ax.drawstates(linewidth=0.25) if not geos: # dibujo los valores de latitudes y longitudes al margen de la imagen par = ax.drawparallels(numpy.arange(-45, -20, 5), labels=[1, 0, 0, 0], linewidth=0.0, fontsize=10, color='white') mer = ax.drawmeridians(numpy.arange(-70, -45, 5), labels=[0, 0, 1, 0], linewidth=0.0, fontsize=10, color='white') setcolor(par, 'white') setcolor(mer, 'white') # llamo al garbage collector para que borre los elementos que ya no se van a usar gc.collect() if channel == 'C02': data *= 100 else: # Los datos de estan en kelvin, asi que los paso a Celsius data -= 273.15 # defino el min y max en funcion de la banda if data_name == 'Band1': vmin = numpy.amin(data) vmax = numpy.amax(data) else: vmin, vmax = rangoColorbar(channel) print numpy.amin(data) print numpy.amax(data) data = numpy.ma.masked_where(numpy.isnan(data), data) # dibujo img en las coordenadas x e y calculadas # cmap = gmtColormap(colormapName, colormapPath, 2048) # defino el colormap y la disposicion de los ticks segun la banda if channel == 'C02': ticks = [0, 20, 40, 60, 80, 100] ticksLabels = ticks elif channel == 'C08' or channel == 'C09': ticks = [-60, -50, -40, -30, -20, -10, 0] ticksLabels = ticks else: ticks = [ -80, -75.2, -70.2, -65.2, -60.2, -55.2, -50.2, -45.2, -40.2, -35.2, -30.2, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70 ] # defino las etiquetas del colorbar ticksLabels = [ -80, -75, -70, -65, -60, -55, -50, -45, -40, -35, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70 ] # if FR o RP cmap = gmtColormap(colormapName, colormapPath, 2048) cs = ax.pcolormesh(x, y, data, vmin=vmin, vmax=vmax, cmap=cmap) # seteo los limites del colorbar plt.clim(vmin, vmax) # agrego el colorbar cbar = ax.colorbar(cs, location='bottom', pad='3%', ticks=ticks) if channel == 'C02': cbar.ax.set_xticklabels(ticksLabels, fontsize=7, color='white') else: cbar.ax.set_xticklabels(ticksLabels, rotation=45, fontsize=7, color='white') cbar.ax.set_xlabel("Temperatura de brillo ($^\circ$C)", fontsize=7, color='white') if channel != 'C02': cbar.ax.xaxis.labelpad = 0 # agrego el logo en el documento logo = plt.imread('./logo_300_bw.png') plt.figimage(logo, 5, 5) # si estoy dibujando toda la proyección geos adjunto el string al nombre del archivo if geos: destFile = dirDest + name + '_geos.png' # determino el nombre del archivo a escribir else: destFile = dirDest + name + '.png' # determino el nombre del archivo a escribir # llamo al garbage collector para que borre los elementos que ya no se van a usar gc.collect() print destFile # genero el pie de la imagen, con el logo y la info del archivo plt.annotate(name, (0, 0), (106, -60), xycoords='axes fraction', textcoords='offset points', va='top', fontsize=14, family='monospace', color='white') plt.savefig(destFile, bbox_inches='tight', dpi=300, transparent=True) # , facecolor='#4F7293' plt.close()
leg = mp.legend(('linear', 'square'), loc='lower right') #specify markers for both lines in one command #notice 'g:+' and 'r--o' mp.plot(ar, linear, 'g:+', ar, sq, 'r--o') #specify legend mp.legend(('linear', 'square')) #specify symbol #notice format r'$\pi$' symbol = mp.text(2, 30, r'$\pi$') #draw annotation arrow = mp.annotate('Symbol of pi', xy=(2, 30), xytext=(1, 60), arrowprops={'facecolor': 'r'}) #label x-axis points mp.xticks(range(11), ('I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI')) #get axes #use locator to intrapolate ax = mp.gca() major_locator = mp.MultipleLocator(6) ax.xaxis.set_major_locator(major_locator) mp.clf() #plot bar
key_text = """Key: $\log \mathcal{R}_0$: Reproduction number $\log k$: Dispersion factor $D$: Generation time interval [days] $n$: Number of index cases $\Delta t$: Time since index case [days] $\\alpha$: Gamma function shape parameter""" std_bin_size = 25 bins = [std_bin_size, 10, std_bin_size, 20, std_bin_size, std_bin_size] if samples_plot: samples[:, 0] = np.log10(samples[:, 0]) samples[:, 1] = np.log10(samples[:, 1]) hist_kwargs = dict(plot_contours=False, plot_datapoints=False, no_fill_contours=False, bins=bins) corner(samples, labels=['$\log \mathcal{R}_0$', '$\log k$', '$D$', '$n$', '$\Delta t$', '$\\alpha$'], smooth=True, contour=False, **hist_kwargs) plt.annotate(key_text, xy=(0.55, 0.8), fontsize=18, ha='left', va='bottom', xycoords='figure fraction') plt.savefig('plots/corner.pdf', bbox_inches='tight') plt.show()
def find_all_star_team(conference): max_pe_pg = -sys.maxsize best_pg = '' max_pe_sg = -sys.maxsize best_sg = '' max_pe_c = -sys.maxsize best_c = '' max_pe_pf = -sys.maxsize best_pf = '' max_pe_sf = -sys.maxsize best_sf = '' skip_list = set() finalized = [False, False, False, False, False] while (check_arr(finalized) == False): if finalized[0] == False: max_pe_pg = -sys.maxsize best_pg = '' if finalized[1] == False: max_pe_sg = -sys.maxsize best_sg = '' if finalized[2] == False: max_pe_c = -sys.maxsize best_c = '' if finalized[3] == False: max_pe_pf = -sys.maxsize best_pf = '' if finalized[4] == False: max_pe_sf = -sys.maxsize best_sf = '' first_row = True for row in myresult: #only consider a single conference if row[13] != conference: continue if row[0] in skip_list: continue factors = row[1:11] new_pe_pg = 0 new_pe_sg = 0 new_pe_c = 0 new_pe_pf = 0 new_pe_sf = 0 # dot product for i in range(len(factors)): new_pe_pg += float(factors[i]) * float(solution_pg[i]) new_pe_sg += float(factors[i]) * float(solution_sg[i]) new_pe_c += float(factors[i]) * float(solution_c[i]) new_pe_pf += float(factors[i]) * float(solution_pf[i]) new_pe_sf += float(factors[i]) * float(solution_sf[i]) if new_pe_pg > max_pe_pg and finalized[0] == False: best_pg = row[0] max_pe_pg = new_pe_pg if new_pe_sg > max_pe_sg and finalized[1] == False: best_sg = row[0] max_pe_sg = new_pe_sg if new_pe_c > max_pe_c and finalized[2] == False: best_c = row[0] max_pe_c = new_pe_c if new_pe_pf > max_pe_pf and finalized[3] == False: best_pf = row[0] max_pe_pf = new_pe_pf if new_pe_sf > max_pe_sf and finalized[4] == False: best_sf = row[0] max_pe_sf = new_pe_sf # evaluate no two positions are taken by the same player # if they are: keep the player in the higher PE position and redo the algorithm for the other position all_star = [best_pg, best_sg, best_c, best_pf, best_sf] all_star_rating = [ max_pe_pg, max_pe_sg, max_pe_c, max_pe_pf, max_pe_sf ] Positions = [ 'Point Guard', 'Shooting Guard', 'Center', 'Power Forward', 'Small Forward' ] for i in range(len(all_star)): if count(all_star, all_star[i]) == 1: # found best player for that position finalized[i] = True skip_list.add(all_star[i]) else: dupes = [i] for j in range(len(all_star)): if i == j: continue # if duplicates if all_star[j] == all_star[i]: dupes.append(j) best_fit_positon = dupes[0] best_fit_points = all_star_rating[dupes[0]] for k in dupes: if all_star_rating[k] > best_fit_points: best_fit_points = all_star_rating[k] best_fit_positon = k finalized[best_fit_positon] = True skip_list.add(all_star[best_fit_positon]) # print("The best Point Guard in the NBA " + str(conference) +" conference is "+ str(best_pg) + " with a PE of "+ str(max_pe_pg)) # print("The best Shooting Guard in the NBA " + str(conference) +" conference is "+ str(best_sg) + " with a PE of "+ str(max_pe_sg)) # print("The best Center in the NBA " + str(conference) + " conference is "+ str(best_c) + " with a PE of "+ str(max_pe_c)) # print("The best Power Forward in the NBA " + str(conference) + " is "+ str(best_pf) + " with a PE of "+ str(max_pe_pf)) # print("The best Small Forward in the NBA " + str(conference) + " is "+ str(best_sf) + " with a PE of "+ str(max_pe_sf)) image = Image.open('basketball_positions.png') plt.imshow(image) a1 = plt.annotate(best_pg, (600, 500), color='white', weight='bold') a2 = plt.annotate(best_sg, (200, 650), color='white', weight='bold') a3 = plt.annotate(best_c, (830, 800), color='white', weight='bold') a4 = plt.annotate(best_pf, (400, 1000), color='white', weight='bold') a5 = plt.annotate(best_sf, (980, 1100), color='white', weight='bold') plt.title('Conference ' + str(conference), color='white', weight='bold') plt.axis('off') plt.savefig('../frontend/src/' + str(conference) + '.png', facecolor="#17408B") a1.remove() a2.remove() a3.remove() a4.remove() a5.remove() # plt.show() pg_player = player(best_pg, max_pe_pg, Positions[0]) sg_player = player(best_sg, max_pe_sg, Positions[1]) c_player = player(best_c, max_pe_c, Positions[2]) pf_player = player(best_pf, max_pe_pf, Positions[3]) sf_player = player(best_sf, max_pe_sf, Positions[4]) return [pg_player, sg_player, c_player, pf_player, sf_player]
#summarize the loaded model print(model) #summarize vocabulary words = list(model.wv.vocab) print(words) #access vector for one word print(model['sentence']) #save model model.save('model.bin') #load model new_model = Word2Vec.load('model.bin') print(new_model)''' X = model[model.wv.vocab] pca = PCA(n_components=2) result = pca.fit_transform(X) pyplot.scatter(result[:, 0], result[:, 1]) words = list(model.wv.vocab) for i, word in enumerate(words): pyplot.annotate(word, xy=(result[i, 0], result[i, 1])) pyplot.show() # In[ ]:
def forecast(): if request.method == 'POST': try: Field_Genotype = request.form['Field_Genotype'] Lesion = request.form['Lesion'] or "0" City = request.form["City"] or "Athens" r = requests.get( 'http://api.openweathermap.org/data/2.5/forecast?q=' + City + '&appid=8f2980aea5484e2d60be6e013e5c0fda') json_object = r.json() d1dt_txt = json_object['list'][6]['dt_txt'] d1tempk = json_object['list'][6]['main']['temp_min'] d1main = json_object['list'][6]['weather'][0]['main'] #we can replace "description" by "main" d2dt_txt = json_object['list'][14]['dt_txt'] d2tempk = json_object['list'][14]['main']['temp_min'] d2main = json_object['list'][14]['weather'][0]['main'] d3dt_txt = json_object['list'][22]['dt_txt'] d3tempk = json_object['list'][22]['main']['temp_min'] d3main = json_object['list'][22]['weather'][0]['main'] d4dt_txt = json_object['list'][30]['dt_txt'] d4tempk = json_object['list'][30]['main']['temp_min'] d4main = json_object['list'][30]['weather'][0]['main'] d5dt_txt = json_object['list'][38]['dt_txt'] d5tempk = json_object['list'][38]['main']['temp_min'] d5main = json_object['list'][38]['weather'][0]['main'] d1temp = f'{(d1tempk - 273.15):.2f}' d2temp = f'{(d2tempk - 273.15):.2f}' d3temp = f'{(d3tempk - 273.15):.2f}' d4temp = f'{(d4tempk - 273.15):.2f}' d5temp = f'{(d5tempk - 273.15):.2f}' # msg1 = "day 1 time: " + str(d1dt_txt) + ", weather: " + str(d1main)+ ", temp (min): "+ str(d1temp) # msg2 = "day 2 time: " + str(d2dt_txt) + ", weather: " + str(d2main)+ ", temp (min): "+ str(d2temp) # msg3 = "day 3 time: " + str(d3dt_txt) + ", weather: " + str(d3main)+ ", temp (min): "+ str(d3temp) # msg4 = "day 4 time: " + str(d4dt_txt) + ", weather: " + str(d4main)+ ", temp (min):"+ str(d4temp) # msg5 = "day 5 time: " + str(d5dt_txt) + ", weather: " + str(d5main)+ ", temp (min):"+ str(d5temp) with sql.connect("GHdata.db") as con: con.execute(""" drop table if exists forecast """) con.execute(""" CREATE TABLE forecast (City,time,weather,temp); """) cur = con.cursor() cur.execute( "INSERT INTO forecast (City,time,weather,temp) VALUES(?,?,?,?)", (City, d1dt_txt, d1main, d1temp)) cur.execute( "INSERT INTO forecast (City,time,weather,temp) VALUES(?,?,?,?)", (City, d2dt_txt, d2main, d2temp)) cur.execute( "INSERT INTO forecast (City,time,weather,temp) VALUES(?,?,?,?)", (City, d3dt_txt, d3main, d3temp)) cur.execute( "INSERT INTO forecast (City,time,weather,temp) VALUES(?,?,?,?)", (City, d4dt_txt, d4main, d4temp)) cur.execute( "INSERT INTO forecast (City,time,weather,temp) VALUES(?,?,?,?)", (City, d5dt_txt, d5main, d5temp)) con.execute(""" drop table if exists fielddata """) con.execute(""" CREATE TABLE fielddata (Field_Genotype,Lesion); """) cur.execute( "INSERT INTO fielddata (Field_Genotype,Lesion) VALUES(?,?)", (Field_Genotype, Lesion)) con.commit() msgrecord2 = "Record successfully added" cur.execute("select * from fielddata") data_field = pd.DataFrame(cur.fetchall(), columns=['Field_Genotype', 'Lesion']) genotype = data_field.iloc[0, 0] lesion = data_field.iloc[0, 1] #Weather cur.execute("select * from forecast") data_temp = pd.DataFrame( cur.fetchall(), columns=['City', 'time', 'weather', 'temp']) d1weather = data_temp.iloc[0, 2] d2weather = data_temp.iloc[1, 2] d3weather = data_temp.iloc[2, 2] d4weather = data_temp.iloc[3, 2] d5weather = data_temp.iloc[4, 2] d1temp1 = data_temp.iloc[0, 3] d2temp1 = data_temp.iloc[1, 3] d3temp1 = data_temp.iloc[2, 3] d4temp1 = data_temp.iloc[3, 3] d5temp1 = data_temp.iloc[4, 3] d1temp = float(d1temp1) d2temp = float(d2temp1) d3temp = float(d3temp1) d4temp = float(d4temp1) d5temp = float(d5temp1) # The threshold 0.3 cm is based on the disease progress from previous study tested both in greenhouse and field trails which show significant difference between resistant and susceptible genotypes. # The threshold 0.3 cm is the disease progress from previous study tested both in greenhouse and field trails which show significant difference between resistant and susceptible genotypes. #if str(PD) > "51.94": if str(lesion) > "0.3": if str(d1weather) == "Rain": msgsug = "Chemical should be applied today" elif str(d2weather) == "Rain": msgsug = "Chemical should be applied tomorrow" elif str(d3weather) == "Rain": msgsug = "Chemical should be applied 2 days later" elif str(d4weather) == "Rain": msgsug = "Chemical should be applied 3 days later" elif str(d5weather) == "Rain": msgsug = "Chemical should be applied 4 days later" elif d1temp > 24.00: if str(genotype) == "628": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today.1 " elif str(genotype) == "660": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " elif str(genotype) == "668": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " elif str(genotype) == "708": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " else: msgsug = "Symptom of disease is observered, but it won't rain this week. Though temerperature might be higher than 24°C, the genotype of peanut is in low risk. Chemical is not neccessary now." elif d2temp > 24.00: if str(genotype) == "628": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today.2 " elif str(genotype) == "660": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " elif str(genotype) == "668": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " elif str(genotype) == "708": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " else: msgsug = "Symptom of disease is observered, but it won't rain this week. Though temerperature might be higher than 24°C, the genotype of peanut is in low risk. Chemical is not neccessary now." elif d3temp > 24.00: if str(genotype) == "628": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. 3" elif str(genotype) == "660": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " elif str(genotype) == "668": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " elif str(genotype) == "708": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " else: msgsug = "Symptom of disease is observered, but it won't rain this week. Though temerperature might be higher than 24°C, the genotype of peanut is in low risk. Chemical is not neccessary now." elif d4temp > 24.00: if str(genotype) == "628": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today.4 " elif str(genotype) == "660": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " elif str(genotype) == "668": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " elif str(genotype) == "708": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " else: msgsug = "Symptom of disease is observered, but it won't rain this week. Though temerperature might be higher than 24°C, the genotype of peanut is in low risk. Chemical is not neccessary now." elif d5temp > 24.00: if str(genotype) == "628": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today 5. " elif str(genotype) == "660": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " elif str(genotype) == "668": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " elif str(genotype) == "708": msgsug = "Symptom of disease is observed. Though it won't rain this week, the genotype of peanut is in high risk. Chemical should be applied today. " else: msgsug = "Symptom of disease is observered, but it won't rain this week. Though temerperature is higher than 24°C, the genotype of peanut is in low risk. Chemical is not neccessary now." else: msgsug = "Symptom of disease is observed, but it won't rain this week and the temperature might be lower than 24°C. Chemical is not necessary now. " else: msgsug = "Symptom of disease is not significant. Chemical is not necessary now." except: con.rollback() msgrecord2 = "error in insert operation" finally: con = sql.connect("GHdata.db") con.row_factory = sql.Row cur = con.cursor() cur.execute("select * from forecast") rows2 = cur.fetchall() cur.execute("select * from fielddata") rows5 = cur.fetchall() # farmer tool, expected disease by genotype cur.execute("select * from fielddata") data_field = pd.DataFrame(cur.fetchall(), columns=['Field_Genotype', 'Lesion']) genotype2 = data_field.iloc[0, 0] lesion = data_field.iloc[0, 1] conn = sql.connect("GHdata.db") cursor = conn.execute("select * from Greenhousedata") c = conn.cursor() df1 = pd.DataFrame(cursor.fetchall(), columns=[ 'GenotypeNumber', 'Genotype', 'rep', 'exp', 'DAI3', 'DAI5', 'DAI7', 'DAI9' ], dtype=float) convert_dict = { 'GenotypeNumber': str, 'Genotype': str, } df = df1.astype(convert_dict) df3 = df.groupby('Genotype')['DAI3'].mean() df5 = df.groupby('Genotype')['DAI5'].mean() df7 = df.groupby('Genotype')['DAI7'].mean() df9 = df.groupby('Genotype')['DAI9'].mean() dfp = df3.to_frame().join(df5.to_frame()).join( df7.to_frame()).join(df9.to_frame()) dfp['Genotype'] = [ '12Y', '13M', '628', '630', '660', '668', '683', '694', '695', '696', '704', '708', '730', '736' ] #dfp.rename(index={0:'12Y',1:'13M',2:'628',3:'630',4:'660',5:'668',6:'683',7:'694',8:'695',9:'696',10:'704',11:'708',12:'730',13:'736'}, inplace=True) a = genotype2 option = dfp['Genotype'] == a dfp_res = dfp[option] dfp_res = pd.DataFrame(dfp_res, columns=['DAI3', 'DAI5', 'DAI7', 'DAI9']) dfp_res = dfp_res.T dfp_res['DAI'] = [3, 5, 7, 9] dfp_res.index.names = ['DAI'] plt.close('all') sns.reset_orig() sns.set() sns.set(style="whitegrid") ax = sns.pointplot(data=dfp_res, x="DAI", y=a, color="#bb3f3f") ax.set_title("Predicted Disease Curve") plt.annotate('DAI: days after inoculation', (0, 0), (0, -50), xycoords='axes fraction', textcoords='offset points', va='top') img_html6 = getCurrFigAsBase64HTML6() # farmers tool, expected disease at end of season by genotype cur.execute("select * from fielddata") data_field = pd.DataFrame(cur.fetchall(), columns=['Field_Genotype', 'Lesion']) genotype = data_field.iloc[0, 0] lesion = data_field.iloc[0, 1] field = pd.read_csv('field_INFO8000.txt', delim_whitespace=True) field = field.drop('REP', 1) field = field.rename(columns={"TRT": "Genotype"}) m1 = field.groupby('Genotype')['WM1'].mean().to_frame() m2 = field.groupby('Genotype')['WM2'].mean().to_frame() m3 = field.groupby('Genotype')['WM3'].mean().to_frame() m4 = field.groupby('Genotype')['WM4'].mean().to_frame() m5 = field.groupby('Genotype')['WM5'].mean().to_frame() m6 = field.groupby('Genotype')['WM6'].mean().to_frame() field_2018 = m1.merge(m2, left_index=True, right_index=True).merge(m3, left_index=True, right_index=True) field_2018 = field_2018.merge(m4, left_index=True, right_index=True).merge( m5, left_index=True, right_index=True) field_2018 = field_2018.merge(m6, left_index=True, right_index=True) field_2018['mean'] = field_2018.mean(axis=1) field_2018 = field_2018.drop( columns=['WM1', 'WM2', 'WM3', 'WM4', 'WM5', 'WM6']) field_2018.reset_index(inplace=True) field_2018 = field_2018.set_index('Genotype') field_2018 = field_2018.drop(['594', '680', '703', '765']) field_2018['Genotype'] = field_2018.index field_2018.index.names = ['index'] conn = sql.connect("GHdata.db") cursor = conn.execute("select * from Greenhousedata") c = conn.cursor() df1 = pd.DataFrame(cursor.fetchall(), columns=[ 'GenotypeNumber', 'Genotype', 'rep', 'exp', 'DAI3', 'DAI5', 'DAI7', 'DAI9' ], dtype=float) convert_dict = { 'GenotypeNumber': str, 'Genotype': str, } df = df1.astype(convert_dict) df3 = df.groupby('Genotype')['DAI3'].mean() df5 = df.groupby('Genotype')['DAI5'].mean() df7 = df.groupby('Genotype')['DAI7'].mean() df9 = df.groupby('Genotype')['DAI9'].mean() dfp = df3.to_frame().join(df5.to_frame()).join( df7.to_frame()).join(df9.to_frame()) dfp.reset_index(inplace=True) dfh_n = dfp dfh_n["Genotype"] = dfh_n["Genotype"].apply(str) dfr = dfh_n.merge(field_2018, left_on='Genotype', right_on='Genotype', suffixes=('_left', '_right')) dfr = dfr.drop('Genotype', axis=1) dfr = dfr.rename(columns={'mean': 'field'}) dfr = dfr.round(2) # farmers tool, expected disease at end of season by genotype corr_n = dfr.corr() corr_n = corr_n.drop(columns=['DAI3', 'DAI5', 'DAI7', 'DAI9'], index='field') corr_max = corr_n[corr_n.field == corr_n.field.max()] corr_max['select'] = corr_max.index corr_max['select'] = corr_max.select.str.extract('(\d+)') DAI_select = corr_max['select'].astype(int) DAI_select.reset_index(drop=True) x3 = pd.DataFrame() x3['DE'] = dfr['DAI3'] x3['DAI'] = '3' x5 = pd.DataFrame() x5['DE'] = dfr['DAI5'] x5['DAI'] = '5' x7 = pd.DataFrame() x7['DE'] = dfr['DAI7'] x7['DAI'] = '7' x9 = pd.DataFrame() x9['DE'] = dfr['DAI9'] x9['DAI'] = '9' dfr_reg = pd.concat([x3, x5, x7, x9], axis=0, join='outer') dfr_reg['DAI'] = dfr_reg['DAI'].astype(int) option = dfr_reg['DAI'] == DAI_select[0] dfr_reg = dfr_reg[option] dfr_reg['y'] = dfr['field'] dfr_reg.drop(columns='DAI', inplace=True) dfr_reg.rename(columns={'DE': 'x'}, inplace=True) fit1 = sm.GLM(dfr_reg['y'].astype(float), dfr_reg['x'].astype(float)) fit1_res = fit1.fit() lesion1 = float(lesion) a = lesion1 PD = fit1_res.params[0] * a PD = PD.round(2) msg_predict = str(PD) return render_template( "WeatherResult.html", msgrecord2=msgrecord2, msg_predict=msg_predict, rows2=rows2, rows5=rows5, msgsug=msgsug ) + '<br>' + ' <h3> E. Predicted disease curve in early days. Genotype : ' + genotype2 + '</h3>' + img_html6 + '<br>' con.close()
def _plot(infile): """Plotting logic.""" fig, ax = plt.subplots(figsize=FIGSIZE) # type,b,f1,precision,recall data = pd.read_csv(infile, skipinitialspace=True) # Filter users with too high of accuracy error data = data[data.max_err < 10.0] data["e2e_delay"] = data["delay_ms"] + 14 baselines = {"barscene": 67396533, "square_timelapse": 33822082} estimates = { "barscene": { 0: { 9: 4.2, 8: 5.9, 7: 7.2, 6: 8.0, 5: 8.6, 4: 9.7, 3: 10.7, 2: 11.8, 1: 13.8, 0: 17.8, -1: 1.0, }, 31: { 9: 4.2, 8: 5.9, 7: 7.2, 6: 8.0, 5: 8.6, 4: 9.7, 3: 10.7, 2: 11.8, 1: 13.8, 0: 17.8, -1: 1.0, }, 67: { 9: 1.2, 8: 2.0, 7: 3.0, 6: 4.5, 5: 5.9, 4: 7.2, 3: 8.6, 2: 9.7, 1: 10.7, 0: 11.8, -1: 1.0, }, }, "square_timelapse": { 0: { 9: 4.0, 8: 4.7, 7: 5.2, 6: 6.7, 5: 7.4, 4: 8.7, 3: 9.9, 2: 10.7, 1: 11.6, 0: 12.4, -1: 1.0, }, 31: { 9: 3.2, 8: 3.6, 7: 4.0, 6: 4.7, 5: 5.2, 4: 6.7, 3: 7.4, 2: 8.7, 1: 9.9, 0: 10.7, -1: 1.0, }, 67: { 9: 1.5, 8: 1.7, 7: 2.2, 6: 3.2, 5: 4.4, 4: 4.7, 3: 5.2, 2: 6.7, 1: 7.4, 0: 8.7, -1: 1.0, }, }, } bitrate = [] for row in data.itertuples(): bitrate.append((100 / estimates[row.video][row.delay_ms][row.quality])) data["bitrate"] = bitrate plot = sns.relplot( x="e2e_delay", y="bitrate", hue="video", data=data, kind="line", style="video", markers=True, ci=95, palette="colorblind", err_style="bars", err_kws={"capsize": 8.0, "capthick": 2.0}, ) # Draw minimum line plt.plot([14, 14], [0, 100], color="gray", linewidth=2, linestyle="dotted") plt.annotate( "min. latency", color="black", xy=(14, 83), xytext=(20, 95), size=15, arrowprops=dict(color="black", arrowstyle="->"), ) # Tweak legend leg = plot._legend leg.set_title("") leg.set_bbox_to_anchor([0.98, 0.95]) leg._loc = 1 sns.despine(bottom=True, left=True) plot.set(xlabel="End-to-end Latency (ms)") plot.set(ylabel="Bitrate (% of baseline)") plot.set(xlim=(0, 90), ylim=(0, 100)) outfile = "user_study.pdf" pp = PdfPages(outfile) pp.savefig(plot._fig.tight_layout()) pp.close() run(["pdfcrop", outfile, outfile], stdout=DEVNULL, check=True) logger.info(f"Plot saved to {outfile}") ttests(data[data["e2e_delay"] == 14]["bitrate"], data[data["e2e_delay"] == 45]["bitrate"], data[data["e2e_delay"] == 81]["bitrate"])
def LabelEvent(self, GraphicsObject, PlotOptions, labelString=None): import matplotlib import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d import proj3d description = '' if labelString == None: if PlotOptions.ShowTextDescriptions: EventTypeFormatted = self.EventType if EventTypeFormatted == 'upwr_flyby': EventTypeFormatted = 'unpowered flyby' elif EventTypeFormatted == 'pwr_flyby': EventTypeFormatted = 'powered flyby' elif EventTypeFormatted == 'chem_burn': EventTypeFormatted = 'chemical burn' elif EventTypeFormatted == 'LT_rndzvs': EventTypeFormatted = 'rendezvous' elif EventTypeFormatted == 'begin_spiral': EventTypeFormatted = 'begin spiral' elif EventTypeFormatted == 'end_spiral': EventTypeFormatted = 'end spiral' elif EventTypeFormatted == 'mission_end': EventTypeFormatted = 'end of mission' elif EventTypeFormatted == 'momtransfer': EventTypeFormatted = 'momentum transfer' if PlotOptions.NumberEventLabels: EventNumber = 'Event # ' + str( PlotOptions.EventCounter) + ':\n' else: EventNumber = '' description = EventNumber + EventTypeFormatted.capitalize( ) + '\n' + self.Location if PlotOptions.DisplayEventDates: description += '\n' + self.GregorianDate if PlotOptions.DisplayEventSpecs: #for launches a C3 and DLA are needed if self.EventType == 'launch': description += '\n$C_3$ = ' + "{0:.3f}".format( self.C3) + ' $km^2/s^2$' #add the LV to the description? description += '\nDLA = ' + "{0:.1f}".format( self.Declination) + '$^{\circ}$' #for non-launch departures only the C3 is needed if self.EventType == 'departure' and self.C3 > 0.0: description += '\n$C_3$ = ' + "{0:.3f}".format( self.C3) + ' $km^2/s^2$' #for other events, output v-infinity and DLA if self.EventType in [ 'upwr_flyby', 'pwr_flyby', 'intercept', 'interface', 'insertion', 'momtransfer' ]: description += '\n$v_\infty$ = ' + "{0:.3f}".format( math.sqrt(self.C3)) + ' $km/s$' description += '\nDEC = ' + "{0:.1f}".format( self.Declination) + '$^{\circ}$' #for flybys, altitude should be outputed if self.EventType in [ 'upwr_flyby', 'pwr_flyby', 'periapse' ]: description += '\naltitude = ' + "{0:.0f}".format( self.Altitude) + ' $km$' #for propulsive events, a deltaV is needed if (self.EventType == 'departure' or self.EventType == 'pwr_flyby' or self.EventType == 'insertion' or self.EventType == 'chem_burn' or self.EventType == 'rendezvous') and self.DVmagorThrottle > 0.0: description += '\n$\Delta v$ = ' + "{0:.3f}".format( self.DVmagorThrottle) + ' $km/s$' #always append the spacecraft Mass if PlotOptions.DisplayEventMass: if self.Mass < 1.0e+5: description += '\nm = ' + "{0:.0f}".format( self.Mass) + ' $kg$' else: description += '\nm = ' + "{0:.4e}".format( self.Mass) + ' $kg$' if PlotOptions.DisplayArrivalPhaseAngle and self.EventType == 'intercept': R = self.SpacecraftState[0:3] V = self.DeltaVorThrustVectorControl r = math.sqrt(R[0] * R[0] + R[1] * R[1] + R[2] * R[2]) v = math.sqrt(V[0] * V[0] + V[1] * V[1] + V[2] * V[2]) try: PhaseAngle = math.acos( -(R[0] * V[0] + R[1] * V[1] + R[2] * V[2]) / (r * v)) * 180.0 / math.pi description += '\n' + r'$\beta $' + ' = ' + "{0:.1f}".format( PhaseAngle) + ' degrees' except: print( "Bad velocity vector while calculating phase angle, event" + str(self.EventNumber)) else: description = str(PlotOptions.EventCounter) else: description = labelString #draw the text #note, do not draw anything for chemical burns below 10 m/s if not (self.EventType == "chem_burn" and self.DVmagorThrottle < 0.0001): x2D, y2D, _ = proj3d.proj_transform(self.SpacecraftState[0], self.SpacecraftState[1], self.SpacecraftState[2], GraphicsObject.get_proj()) if PlotOptions.ShowTextDescriptions: self.eventlabel = plt.annotate( description, xycoords='data', xy=(x2D, y2D), xytext=(20, (PlotOptions.EventCounter + 20)), textcoords='offset points', ha='left', va='bottom', #self.eventlabel = plt.annotate(description, xycoords = 'data', xy = (self.SpacecraftState[0],self.SpacecraftState[1]), xytext = (20, (PlotOptions.EventCounter + 20)), textcoords = 'offset points', ha = 'left', va = 'bottom', bbox=dict(boxstyle='round,pad=0.5', fc='white', alpha=1.0), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'), size=PlotOptions.FontSize, family='serif') # changed to usurp drag-fu else: self.eventlabel = plt.annotate( description, xycoords='data', xy=(x2D, y2D), ha='left', va='bottom', bbox=dict(boxstyle='round,pad=0.5', fc='white', alpha=1.0), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'), size=PlotOptions.FontSize, family='serif') # changed to usurp drag-fu self.AnnotationHelper = self.eventlabel.draggable(use_blit=True) self.pcid = GraphicsObject.figure.canvas.mpl_connect( 'button_press_event', self.ClickAnnotation) self.rcid = GraphicsObject.figure.canvas.mpl_connect( 'button_release_event', self.ReleaseAnnotation) #update the event counter only if the event is printable PlotOptions.EventCounter += 1
cff = sl.cholesky(GCfG).T cft = sl.cholesky(GCtG).T # Generate some random numbers for signal generation xi = np.random.randn(GCtG.shape[0]) # Construct the signals yf = np.dot(G, np.dot(cff, xi)) yt = np.dot(G, np.dot(cft, xi)) # Plot the lot plt.figure() plt.subplot(3, 1, 1) plt.errorbar(toas, yf, yerr=toaerrs, fmt='.', c='blue') plt.annotate('frequency domain', xy=(0.02, 0.20), xycoords='axes fraction', bbox=dict(boxstyle='round', fc='1.0')) plt.grid(True) plt.subplot(3, 1, 2) plt.errorbar(toas, yt, yerr=toaerrs, fmt='.', c='blue') plt.annotate('time domain', xy=(0.02, 0.20), xycoords='axes fraction', bbox=dict(boxstyle='round', fc='1.0')) plt.grid(True) plt.subplot(3, 1, 3) plt.errorbar(np.log10(Ffreqs), np.log10(pcoefs), fmt='.', c='blue') plt.annotate('Power spectrum', xy=(0.02, 0.20),
case_rate_v_containment = np.array(case_rate_v_containment) label = case_rate_v_containment[:,0] x = case_rate_v_containment[:,1].astype(float) y = case_rate_v_containment[:,2].astype(float) norm = plt.Normalize(vmin=0, vmax=90) # normalize for consistent colour gradient plt.scatter(x, y, s=300, c=y, norm=norm, cmap='RdYlGn_r', alpha=0.4) plt.xlim((0, 100)) plt.ylim((0, 175)) plt.xlabel('Containment Index', fontsize=14) plt.ylabel('New Cases per 100,000', fontsize=14) plt.xticks(fontsize=14) plt.yticks(fontsize=14) text = globals()["{0}_gr_df".format(cc1)].index[j].strftime("%d-%b-%Y") plt.text(50, 165, text, horizontalalignment='center', verticalalignment='center', fontsize=18) for i, txt in enumerate(label): plt.annotate(txt, (x[i]-1, y[i]-2), fontsize=12) camera.snap() anim = camera.animate() plt.close() # HTML(anim.to_html5_video()) # ## Plot country-specific containment index, policy levels, and cases per capita # In[11]: def plot_containment_index(epi_data, gr_data, demo_data, country): '''Plots a specified country's containment policies and index over time''' case_rate_100K = epi_data['rolling_cases']/demo_data['population'].values[0]*100000 # cases per 100K people months = {"Jan":0, "Feb":1, "Mar":2, "Apr":3, "May":4, "Jun":5, "Jul":6,
cpick = cm.ScalarMappable(cmap=cm.get_cmap('coolwarm'), norm=col.Normalize(vmin=0, vmax=1.0)) cpick.set_array([]) y = 39500.00 probs = [compute(y, ci) for ci in conf_ints] plt.bar(df_mean.index, df_mean, width=0.95, yerr=yerr, color=cpick.to_rgba(probs)) plt.axhline(y, color='black') plt.annotate('y={}'.format('%.2f' % y), [1991.5, 50000]) plt.colorbar(cpick) plt.show() def onclick(event): plt.cla() plt.gca().set_xlabel('Years') plt.gca().set_ylabel('Judgements made each Year') plt.gca().set_title( 'Confidence in the Judgements that people made [1992-1995]') y = event.y * 110 probs = [compute(y, ci) for ci in conf_ints] plt.bar(df_mean.index,
def Roc_curve(y_test, y_score, n_classes): import numpy as np import pandas as pd import matplotlib.pyplot as plt from itertools import cycle from sklearn.metrics import roc_curve, auc from scipy import interp fpr = dict() tpr = dict() roc_auc = dict() l = len(set(y_score)) for i in range(l): fpr[i], tpr[i], _ = roc_curve( np.array(pd.get_dummies(y_test))[:, i], np.array(pd.get_dummies(y_score))[:, i]) roc_auc[i] = auc(fpr[i], tpr[i]) all_fpr = np.unique(np.concatenate([fpr[i] for i in range(l)])) mean_tpr = np.zeros_like(all_fpr) for i in range(l): mean_tpr += interp(all_fpr, fpr[i], tpr[i]) mean_tpr = mean_tpr / (n_classes - 1) fpr["macro"] = all_fpr tpr["macro"] = mean_tpr roc_auc["macro"] = auc(fpr["macro"], tpr["macro"]) lw = 2 plt.figure(figsize=(8, 5)) plt.plot(fpr["macro"], tpr["macro"], label='macro-average ROC curve (area = {0:0.2f})' ''.format(roc_auc["macro"]), color='green', linestyle=':', linewidth=4) colors = cycle([ 'chocolate', 'aqua', 'darkorange', 'cornflowerblue', 'cadetblue', 'burntsienna', 'cornflowerblue' ]) for i, color in zip(range(l), colors): plt.plot(fpr[i], tpr[i], color=color, lw=lw, label='ROC curve of class {0} (area = {1:0.2f})' ''.format(i, roc_auc[i])) plt.plot([0, 1], [0, 1], 'k--', color='red', lw=lw) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.annotate('Random Guess', (.5, .48), color='red') plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver Operating Characteristic for Multi Layered Perceptron') plt.legend(loc="lower right") plt.show()
plt.plot(dxx, fplot(cdx_exp, dxx), "-", label="Wei et al.") for i in range(5): plt.plot(dxx, np.ones(len(dxx)) * cdxall_exp[i], "-", color="C0", alpha=0.5) #plt.plot(dxx, fplot(cdx_exp - vdx_exp, dxx), "-", color="C1") #plt.plot(dxx, fplot(cdx_exp + vdx_exp, dxx), "-", color="C1") #plt.fill_between(dxx, fplot(cdx_exp - vdx_exp, dxx), # fplot(cdx_exp + vdx_exp, dxx), alpha=0.5) #plt.plot(dx, fplot(cdx, dx), "o", label="Simulation") plt.plot(dx, fplot(cdx, dx), "o", label="Simulations", color="C1") for i in (0, 1, 2): plt.plot(dx, fplot(cdxall[:, i], dx), "o", color="C1", alpha=0.5) #plt.fill_between(dx, fplot(cdx - cdxstd, dx), fplot(cdx + cdxstd, dx), alpha=0.5) plt.annotate(r"$\delta$=5.5nm", (5.5, cdxall[4, 0] - 1.), xytext=(5.5 - .79, cdxall[4, 0] - 8.), color="C1", arrowprops=dict(arrowstyle="->", color="C1")) plt.xlabel(r"Bond rupture length $\delta$ [nm]") plt.ylabel(r"$\alpha$ [1/V]") plt.legend(loc="upper left", frameon=False) import folders nanopores.savefigs("tau_off2", folders.FIGDIR + "/wei", ending=".pdf")
def plot_pic(self, plot_info, pic_name=None): x_inner_list = [] y_inner_list = [] analysis_num = plot_info['analysis_num'] sum_num, get_90per_time, get_95per_time, get_99per_time, get_below_10ms_num = 0, 0.0, 0.0, 0.0, 0 for key in self.sort_humanly(plot_info.keys()): if 'inner_total_time_' in key: x_inner = int(re.findall('\d+', key)[-1]) y_inner = plot_info[key] x_inner_list.append(x_inner) y_inner_list.append(y_inner) sum_num += y_inner if get_90per_time == 0.0 and sum_num >= 0.9 * analysis_num: get_90per_time = x_inner if get_95per_time == 0.0 and sum_num >= 0.95 * analysis_num: get_95per_time = x_inner if get_99per_time == 0.0 and sum_num >= 0.99 * analysis_num: get_99per_time = x_inner if x_inner <= 10: get_below_10ms_num += y_inner x_min = plot_info['inner_min_time'] x_max = plot_info['inner_max_time'] x_av = plot_info['inner_av_time'] per_of_below_10ms = round(get_below_10ms_num / analysis_num, 4) print(get_below_10ms_num / analysis_num) plt.figure(figsize=(30, 10), dpi=80) # 设置画布大小 xticks_gap = 5 # 设置刻度间距 xticks = [i for i in range(0, x_max + xticks_gap, xticks_gap)] plt.xticks(xticks, rotation=90) # 设置刻度 plot_info.pop('analysis_num') max_times = max(list(plot_info.values())) plt.axis([0, 1.01 * x_max, 0, 1.01 * max_times]) plt.scatter(x_inner_list, y_inner_list, color='blue', label='Inner cost time') # 散点图 plt.grid(alpha=0.4, ls='--') # 网格线设置 plt.axvline(x_av, color='green', label='Average time for inner cost') plt.annotate(' Average cost time is ' + str(x_av) + 'ms', (x_av, 120000), color='green') plt.axvline(get_90per_time, color='c', label='90% data\'s time is equal or below this line') plt.annotate(' 90% data\'s time is equal or below ' + str(get_90per_time) + 'ms', (get_90per_time, 20000), color='c') plt.axvline(get_95per_time, color='m', label='95% data\'s time is equal or below this line') plt.annotate(' 95% data\'s time is equal or below ' + str(get_95per_time) + 'ms', (get_95per_time, 40000), color='m') plt.axvline(get_99per_time, color='r', label='99% data\'s time is equal or below this line') plt.annotate(' 99% data\'s time is equal or below ' + str(get_99per_time) + 'ms', (get_99per_time, 60000), color='r') plt.axvline(10, color='y', label='10 ms time') plt.annotate(' {}% data\'s time is equal or below 10ms'.format( per_of_below_10ms * 100), (10, 200000), color='y') plt.xlabel('内部耗时(ms)', fontproperties=self.my_font, fontsize=18) plt.ylabel('出现次数', fontproperties=self.my_font, fontsize=18) plt.title('内部程序耗时分布图(样本数: %d)' % (analysis_num), fontproperties=self.my_font, fontsize=18) plt.legend() if pic_name: plt.savefig('./{}'.format(pic_name)) plt.show()
print 'Making CMD' # Turn off interactive plotting to save plots without displaying them: plt.ioff() # Make plot figure and axes: fig, ax = plt.subplots(ncols=1, sharey=True, figsize=(7, 7)) # plot 2d histogram: hb = ax.hexbin(color,GMag,gridsize=500,cmap='inferno',bins='log',mincnt=1) # configure plots: ax.set_title("CMD of all Gaia targets") ax.set_ylabel('M$_{G}$') ax.set_xlabel('G$_{BP}$-G$_{RP}$') ax.invert_yaxis() cb = fig.colorbar(hb, ax=ax) cb.set_label('log10(count)') plt.annotate('{0} stars'.format(color.shape[0]),xy=(0.55,0.85),xycoords='figure fraction') # write out plot: plt.savefig('master_cmd.png',format='png') plt.close(fig) print 'Making Mollweide' # Make Sky Coord objects: pra = coord.Angle(f['ra'].values*u.degree) pra = pra.wrap_at(180*u.degree) pdec = coord.Angle(f['decl'].values*u.degree) # Plot 2d histogram of objects on Mollweide projection: fig2 = plt.figure(2,figsize=(8,6)) ax = fig2.add_subplot(111, projection="mollweide") hb = ax.hexbin(pra.radian, pdec.radian,gridsize=500,cmap='inferno',bins='log',mincnt=1) ax.set_xticklabels(['14h','16h','18h','20h','22h','0h','2h','4h','6h','8h','10h'],color='grey')