def show_sigma_transform(with_text=False): fig = plt.figure() ax=fig.gca() x = np.array([0, 5]) P = np.array([[4, -2.2], [-2.2, 3]]) plot_covariance_ellipse(x, P, facecolor='b', alpha=0.6, variance=9) sigmas = MerweScaledSigmaPoints(2, alpha=.5, beta=2., kappa=0.) S = sigmas.sigma_points(x=x, P=P) plt.scatter(S[:,0], S[:,1], c='k', s=80) x = np.array([15, 5]) P = np.array([[3, 1.2],[1.2, 6]]) plot_covariance_ellipse(x, P, facecolor='g', variance=9, alpha=0.3) ax.add_artist(arrow(S[0,0], S[0,1], 11, 4.1, 0.6)) ax.add_artist(arrow(S[1,0], S[1,1], 13, 7.7, 0.6)) ax.add_artist(arrow(S[2,0], S[2,1], 16.3, 0.93, 0.6)) ax.add_artist(arrow(S[3,0], S[3,1], 16.7, 10.8, 0.6)) ax.add_artist(arrow(S[4,0], S[4,1], 17.7, 5.6, 0.6)) ax.axes.get_xaxis().set_visible(False) ax.axes.get_yaxis().set_visible(False) if with_text: plt.text(2.5, 1.5, r"$\chi$", fontsize=32) plt.text(13, -1, r"$\mathcal{Y}$", fontsize=32) #plt.axis('equal') plt.show()
def draw_img_for_viewing_ice(self): #print "Press 'p' to save PNG." global colmax global colmin fig = P.figure(num=None, figsize=(13.5, 5), dpi=100, facecolor='w', edgecolor='k') cid1 = fig.canvas.mpl_connect('key_press_event', self.on_keypress_for_viewing) cid2 = fig.canvas.mpl_connect('button_press_event', self.on_click) canvas = fig.add_subplot(121) canvas.set_title(self.filename) self.axes = P.imshow(self.inarr, origin='lower', vmax = colmax, vmin = colmin) self.colbar = P.colorbar(self.axes, pad=0.01) self.orglims = self.axes.get_clim() canvas = fig.add_subplot(122) canvas.set_title("Angular Average") maxAngAvg = (self.inangavg).max() numQLabels = len(eDD.iceHInvAngQ.keys())+1 labelPosition = maxAngAvg/numQLabels for i,j in eDD.iceHInvAngQ.iteritems(): P.axvline(j,0,colmax,color='r') P.text(j,labelPosition,str(i), rotation="45") labelPosition += maxAngAvg/numQLabels P.plot(self.inangavgQ, self.inangavg) P.xlabel("Q (A-1)") P.ylabel("I(Q) (ADU/srad)") pngtag = original_dir + "peakfit-gdvn_%s.png" % (self.filename) P.savefig(pngtag) print "%s saved." % (pngtag) P.close()
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') print(cm) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label')
def view(key,reciprocals=None,show=True,suspend=False,save=True,name='KMap'): ''' View the KMap. Parameters ---------- key : 'L','S','H' The key of the database of KMap. reciprocals : iterable of 1d ndarray, optional The translation vectors of the reciprocal lattice. show : logical, optional True for showing the view. Otherwise not. suspend : logical, optional True for suspending the view. Otherwise not. save : logical, optional True for saving the view. Otherwise not. name : str, optional The title and filename of the view. Otherwise not. ''' assert key in KMap.database if key=='L': reciprocals=np.asarray(reciprocals) or np.array([1.0])*2*np.pi elif key=='S': reciprocals=np.asarray(reciprocals) or np.array([[1.0,0.0],[0.0,1.0]])*2*np.pi elif key=='H': reciprocals=np.asarray(reciprocals) or np.array([[1.0,-1.0/np.sqrt(3.0)],[0,-2.0/np.sqrt(3.0)]])*2*np.pi plt.title(name) plt.axis('equal') for tag,position in KMap.database[key].items(): if '1' not in tag: coords=reciprocals.T.dot(position) assert len(coords)==2 plt.scatter(coords[0],coords[1]) plt.text(coords[0],coords[1],'%s(%s1)'%(tag,tag) if len(tag)==1 else tag,ha='center',va='bottom',color='green',fontsize=14) if show and suspend: plt.show() if show and not suspend: plt.pause(1) if save: plt.savefig('%s.png'%name) plt.close()
def drawVectors(transformed_features, components_, columns, plt, scaled): if not scaled: return plt.axes() # No cheating ;-) num_columns = len(columns) # This funtion will project your *original* feature (columns) # onto your principal component feature-space, so that you can # visualize how "important" each one was in the # multi-dimensional scaling # Scale the principal components by the max value in # the transformed set belonging to that component xvector = components_[0] * max(transformed_features[:,0]) yvector = components_[1] * max(transformed_features[:,1]) ## visualize projections # Sort each column by it's length. These are your *original* # columns, not the principal components. important_features = { columns[i] : math.sqrt(xvector[i]**2 + yvector[i]**2) for i in range(num_columns) } important_features = sorted(zip(important_features.values(), important_features.keys()), reverse=True) print "Features by importance:\n", important_features ax = plt.axes() for i in range(num_columns): # Use an arrow to project each original feature as a # labeled vector on your principal component axes plt.arrow(0, 0, xvector[i], yvector[i], color='b', width=0.0005, head_width=0.02, alpha=0.75) plt.text(xvector[i]*1.2, yvector[i]*1.2, list(columns)[i], color='b', alpha=0.75) return ax
def make_intens_all(w1, w2): fig = plt.figure(figsize=(6., 6.)) gs = gridspec.GridSpec(1,1) gs.update(left=0.13, right=0.985, bottom = 0.13, top=0.988) ax = plt.subplot(gs[0]) plt.minorticks_on() make_contours() labels = ["A", "B", "C", "D"] for i, field in enumerate(fields): os.chdir(os.path.join(data_dir, "combined_{0}".format(field))) image = "collapsed_w{0}_{1}.fits".format(w1, w2) intens = pf.getdata(image, verify=False) extent = calc_extent(image) extent = offset_extent(extent, field) plt.imshow(intens, cmap="bone", origin="bottom", extent=extent, vmin=-20, vmax=80) verts = calc_verts(intens, extent) path = Path(verts, [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY,]) patch = patches.PathPatch(path, facecolor='none', lw=2, edgecolor="r") ax.add_patch(patch) xtext, ytext = np.mean(verts[:-1], axis=0) plt.text(xtext-8, ytext+8, labels[i], color="r", fontsize=35, fontweight='bold', va='top') plt.hold(True) plt.xlim(26, -38) plt.ylim(-32, 32) plt.xlabel("X [kpc]") plt.ylabel("Y [kpc]") # plt.show() plt.savefig(os.path.join(plots_dir, "muse_fields.eps"), dpi=60, format="eps") plt.savefig(os.path.join(plots_dir, "muse_fields.png"), dpi=200) return
def export(self, query, n_topics, n_words, title="PCA Export", fname="PCAExport"): vec = DictVectorizer() rows = topics_to_vectorspace(self.model, n_topics, n_words) X = vec.fit_transform(rows) pca = skPCA(n_components=2) X_pca = pca.fit(X.toarray()).transform(X.toarray()) match = [] for i in range(n_topics): topic = [t[1] for t in self.model.show_topic(i, len(self.dictionary.keys()))] m = None for word in topic: if word in query: match.append(word) break pyplot.figure() for i in range(X_pca.shape[0]): pyplot.scatter(X_pca[i, 0], X_pca[i, 1], alpha=.5) pyplot.text(X_pca[i, 0], X_pca[i, 1], s=' '.join([str(i), match[i]])) pyplot.title(title) pyplot.savefig(fname) pyplot.close()
def plot_embedding(features, classes, labels, title=None): x_min, x_max = np.min(features, 0), np.max(features, 0) features = (features - x_min) / (x_max - x_min) plt.figure() ax = plt.subplot(111) for i in range(features.shape[0]): plt.text(features[i, 0], features[i, 1], str(labels[i]), color=plt.cm.Set1(float(classes[i]) / 10), fontdict={'weight': 'bold', 'size': 9}) if hasattr(offsetbox, 'AnnotationBbox'): # only print thumbnails with matplotlib > 1.0 shown_images = np.array([[1., 1.]]) # just something big for i in range(features.shape[0]): dist = np.sum((features[i] - shown_images) ** 2, 1) if np.min(dist) < 4e-3: # don't show points that are too close continue shown_images = np.r_[shown_images, [features[i]]] """imagebox = offsetbox.AnnotationBbox( offsetbox.OffsetImage(digits.images[i], cmap=plt.cm.gray_r), X[i]) ax.add_artist(imagebox)""" plt.xticks([]), plt.yticks([]) if title is not None: plt.title(title)
def plotFrame(lines, chop_times,dist,samDist,DetDist,fracEi,Eis): modSamDist=dist[-1]+samDist totDist=modSamDist+DetDist for i in range(len(dist)): plt.plot([-20000,120000],[dist[i],dist[i]],c='k',linewidth=1.) for j in range(len(chop_times[i][:])): plt.plot(chop_times[i][j],[dist[i],dist[i]],c='white',linewidth=1.) plt.plot([-20000,120000],[totDist,totDist],c='k',linewidth=2.) for i in range(len(lines)): x0=-lines[i][0][1]/lines[i][0][0] x1=(modSamDist-lines[i][0][1])/lines[i][0][0] plt.plot([x0,x1],[0,modSamDist],c='b') x2=(totDist-lines[i][0][1])/lines[i][0][0] plt.plot([x1,x2],[modSamDist,totDist],c='b') newline=[lines[i][0][0]*np.sqrt(1+fracEi),modSamDist-lines[i][0][0]*np.sqrt(1+fracEi)*x1] x3=(totDist-newline[1])/(newline[0]) plt.plot([x1,x3],[modSamDist,totDist],c='r') newline=[lines[i][0][0]*np.sqrt(1-fracEi),modSamDist-lines[i][0][0]*np.sqrt(1-fracEi)*x1] x4=(totDist-newline[1])/(newline[0]) plt.plot([x1,x4],[modSamDist,totDist],c='r') plt.text(x2,totDist+0.2,"{:3.1f}".format(Eis[i])) plt.xlabel('TOF ($\mu$sec)') plt.ylabel('Distance (m)') plt.xlim(0,100000) plt.show()
def plot_data(years, stat, win_type): data = get_data('data/%s_top_%s.dat' % (years, stat)) plt.figure(1) min_x, max_x = sys.maxint, 0 min_y, max_y = sys.maxint, 0 for datum in data: name, value, passes_filter, home_wins, total_wins = datum if win_type == 'home': wins = home_wins elif win_type == 'total': wins = total_wins else: print "Warning: invalid win_type. Defaulted to 'home'." wins = home_wins if passes_filter: color = 'k' else: color = 'r' plt.text(wins, value, name, size='large', color=color) min_x, max_x = min(min_x, wins), max(max_x, wins) min_y, max_y = min(min_y, value), max(max_y, value) plt.xlim(min_x - max_x/10.0, max_x + max_x/10.0) plt.ylim(min_y - max_y/10.0, max_y + max_y/10.0) # X and Y labels plt.ylabel(stat, size='large') plt.xlabel('# of %s wins' % win_type, size='large') plt.show()
def createResponsePlot(dataframe,plotdir): mag = dataframe['MAGPDE'].as_matrix() response = (dataframe['TFIRSTPUB'].as_matrix())/60.0 response[response > 60] = 60 #anything over 60 minutes capped at 6 minutes imag5 = (mag >= 5.0).nonzero()[0] imag55 = (mag >= 5.5).nonzero()[0] fig = plt.figure(figsize=(8,6)) n,bins,patches = plt.hist(response[imag5],color='g',bins=60,range=(0,60)) plt.hold(True) plt.hist(response[imag55],color='b',bins=60,range=(0,60)) plt.xlabel('Response Time (min)') plt.ylabel('Number of earthquakes') plt.xticks(np.arange(0,65,5)) ymax = text.ceilToNearest(max(n),10) yinc = ymax/10 plt.yticks(np.arange(0,ymax+yinc,yinc)) plt.grid(True,which='both') plt.hold(True) x = [20,20] y = [0,ymax] plt.plot(x,y,'r',linewidth=2,zorder=10) s1 = 'Magnitude 5.0, Events = %i' % (len(imag5)) s2 = 'Magnitude 5.5, Events = %i' % (len(imag55)) plt.text(35,.85*ymax,s1,color='g') plt.text(35,.75*ymax,s2,color='b') plt.savefig(os.path.join(plotdir,'response.pdf')) plt.savefig(os.path.join(plotdir,'response.png')) plt.close() print 'Saving response.pdf'
def polim(axe, polluant='O3'): """ Raccourcis pour tracer les lignes horizontales décrivant les seuils réglementaires pour les polluants """ seuils = {'O3': (150, 180, 240), 'NO2': (135, 200, 400), 'SO2': (200, 300, 500)} labs = ['MVR', 'IR', 'A'] if polluant not in seuils.keys(): return axe colors = ('green', 'orange', 'red') xlim = axe.get_xlim() for i in range(len(seuils[polluant])): seuil = seuils[polluant][i] color = colors[i] x0 = xlim[0] x1 = xlim[1] axe.hlines(seuil, x0, x1, color, label='_nolegend_') for i in range(len(seuils[polluant])): # Deuxième passe pour inscrire les # valeurs, sinon le texte de la première ligne est décalée (bug MPL??) seuil = seuils[polluant][i] color = colors[i] x0 = xlim[0] t = "%s (%s)" % (labs[i], seuil) plt.text(x0, seuil, t, color=color, ha='left', va='bottom', weight='bold') plt.ylim(0, seuils[polluant][-1] + 100) plt.draw() return axe
def autolabel(rects,labels): # attach some text labels for i,(rect,label) in enumerate(zip(rects,labels)): height = rect.get_height() plt.text(rect.get_x() + rect.get_width()/2., 1.05*height, label, ha='left', va='bottom',fontsize=8,rotation=45)
def saveDebug_pyresample(weight_sum, filename): """ Save image for debugging onto map with grid and coastlines using pyresample """ import matplotlib matplotlib.use('AGG', warn=False) from pyresample import plot import matplotlib.pyplot as plt import matplotlib.cm as cm new_image = np.array(255.0 * weight_sum / np.max(weight_sum), 'uint8') bmap = plot.area_def2basemap(pc(Satellites.outwidth, Satellites.outheight), resolution='c') bmap.drawcoastlines(linewidth=0.2, color='red') bmap.drawmeridians(np.arange(-180, 180, 10), linewidth=0.2, color='red') bmap.drawparallels(np.arange(-90, 90, 10), linewidth=0.2, color='red') bmap.imshow(new_image, origin='upper', vmin=0, vmax=255, cmap=cm.Greys_r) # @UndefinedVariable # plt.show() i = datetime.datetime.utcnow() plt.text(0, -75, "%s UTC" % i.isoformat(), color='green', size=15, horizontalalignment='center') plt.savefig(filename, bbox_inches='tight', pad_inches=0, dpi=400) plt.close()
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=None, zmin=1): """This function prints and plots the confusion matrix for the intent classification. Normalization can be applied by setting `normalize=True`.""" import numpy as np zmax = cm.max() plt.imshow(cm, interpolation='nearest', cmap=cmap if cmap else plt.cm.Blues, aspect='auto', norm=LogNorm(vmin=zmin, vmax=zmax)) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=90) plt.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] logger.info("Normalized confusion matrix: \n{}".format(cm)) else: logger.info("Confusion matrix, without normalization: \n{}".format(cm)) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.ylabel('True label') plt.xlabel('Predicted label')
def plot2(self,oligodata,name): for level in oligodata: valcheck=['z(a)','z(c)','z(g)','z(t)','z(t+a)/z(c+g)'] x=[] y=[] maxval=0 for val in oligodata[level]: if (val not in valcheck) and (oligodata[level][val]>maxval): maxval-=maxval maxval+=oligodata[level][val] if (val not in valcheck): cval=self.get_C(val[0:int(name[3])-1]) cval=cval+'a+'+cval+'c+'+cval+'g+'+cval+'t' if (cval not in valcheck): valcheck.append(val) valcheck.append(cval) x.append(oligodata[level][val]) y.append(oligodata[level][cval]) maxval=maxval+0.05*maxval ## print 'maxval=',maxval plt.plot(x,y,'k+') plt.plot([-1,maxval],[-1,maxval],'k') ## plt.xlabel('x') ## plt.ylabel('y') plt.text(maxval/2,maxval-2*float(maxval)/100,r'$S_{'+ level+'}^{'+self.sotype+'}$',va='top',ha='center',fontsize=20) ## plt.legend() resname=self.inpufile.path+name+'_'+level.rpartition('/')[0]+'-'+level.rpartition('/')[2]+'.pdf' plt.savefig(resname) plt.cla() plt.close()
def draw_path(order, boundaries=None): if boundaries: (p1, p2, p3), (p1a, p2a, p3a) = boundaries b_dict = {p1: "p1", p1a: "p1a", p2: "p2", p2a: "p2a", p3: "p3", p3a: "p3a"} else: b_dict = {} locations = hack_locations locations2 = np.zeros(locations.shape) for i in order: locations2[i, :] = locations[order[i], :] print locations print order print locations2 x1 = [] y1 = [] delta = 0.0 for i in order: x1.append(locations[i, 0] - delta) y1.append(locations[i, 1] - delta) delta -= 0.005 x1.append(locations[order[0], 0]) y1.append(locations[order[0], 1]) plt.xlim((-0.1, 1.2)) plt.ylim((-0.1, 1.2)) plt.plot(x1, y1, marker="x", linestyle="-", color="b") for i, o in enumerate(order): plt.text(x1[i], y1[i] + 0.01, "%d %s" % (i, b_dict.get(i, "")))
def draw_path(order, boundaries=None): if boundaries: (p1, p2, p3), (p1a, p2a, p3a) = boundaries b_dict = { p1:'p1', p1a:'p1a', p2:'p2', p2a:'p2a', p3:'p3', p3a:'p3a'} else: b_dict = {} locations = hack_locations locations2 = np.zeros(locations.shape) for i in order: locations2[i,:] = locations[order[i],:] print locations print order print locations2 x1 = [] y1 = [] delta = 0.0 for i in order: x1.append(locations[i,0] - delta) y1.append(locations[i,1] - delta) delta -= 0.005 x1.append(locations[order[0], 0]) y1.append(locations[order[0], 1]) plt.xlim((-.1, 1.2)) plt.ylim((-.1, 1.2)) plt.plot(x1, y1, marker='x', linestyle = '-', color = 'b') for i, o in enumerate(order): plt.text(x1[i], y1[i] + 0.01, '%d %s' % (i, b_dict.get(i, '')))
def plot1(self,oligodata,name): for level in oligodata: valcheck=[] x=[] y=[] maxval=0 for val in oligodata[level]: if oligodata[level][val]>maxval: maxval-=maxval maxval+=oligodata[level][val] crval=self.get_CR(val) if val!=crval and (val not in valcheck) and (crval not in valcheck): valcheck.append(val) valcheck.append(crval) x.append(oligodata[level][val]) y.append(oligodata[level][crval]) maxval=maxval+0.05*maxval ## print 'maxval=',maxval plt.plot(x,y,'k+') plt.plot([-1,maxval],[-1,maxval],'k') ## plt.xlabel('x') ## plt.ylabel('y') plt.text(maxval/2,maxval-2*float(maxval)/100,r'$S_{'+ level+'}^{'+self.sotype+'}$',va='top',ha='center',fontsize=20) ## plt.legend() resname=self.inpufile.path+name+'_'+level.rpartition('/')[0]+'-'+level.rpartition('/')[2]+'.pdf' plt.savefig(resname) plt.cla() plt.close()
def plotOEMSEarlyLateReplicatedOriginsWTandMUT(oemfilestrain1,oemfilestrain2,oemfilestrain3,oemfilestrain4,reptime): # Grab OEMS for early and late replicated origins for strain 1 oemsEarlyLateRO_strain1 = rt.getOEMSForEarlyvsLateReplicatedOriginsAllChromosomes(reptime,oemfilestrain1) # Grab OEMS for early and late replicated origins for strain 2 oemsEarlyLateRO_strain2 = rt.getOEMSForEarlyvsLateReplicatedOriginsAllChromosomes(reptime,oemfilestrain2) # Grab OEMS for early and late replicated origins for strain 3 oemsEarlyLateRO_strain3 = rt.getOEMSForEarlyvsLateReplicatedOriginsAllChromosomes(reptime,oemfilestrain3) # Grab OEMS for early and late replicated origin for strain 4 oemsEarlyLateRO_strain4 = rt.getOEMSForEarlyvsLateReplicatedOriginsAllChromosomes(reptime,oemfilestrain4) bp = plt.boxplot([oemsEarlyLateRO_strain1['early'],oemsEarlyLateRO_strain1['late'],oemsEarlyLateRO_strain2['early'],oemsEarlyLateRO_strain2['late'],oemsEarlyLateRO_strain3['early'],oemsEarlyLateRO_strain3['late'],oemsEarlyLateRO_strain4['early'],oemsEarlyLateRO_strain4['late']],1) for line in bp['medians']: x1, y = line.get_xydata()[0] x2 = line.get_xydata()[1][0] x = (x1+x2)/2 plt.text(x, y, '%.3f' % y,horizontalalignment='center') plt.xticks(range(1,9),('Early-WT','Late-WT','Early-pif1m2KO','Late-pif1m2KO','Early-rrm3dKO','Late-rrm3dKO','Early-rrm3d_pif1m2KO','Late-rrm3d_pif1m2KO')) plt.ylabel('OEM') plt.title('OEMs of Early and Late Replicating Origins') plt.show()
def plot_factor_vs_nhi(data): fact = [] lognhi = [] nhi = data['nhi_thin'] # Optically-thin assumption nhi_heiles = data['nhi_hl'] # N(HI) from Carl Heiles Paper for i in range(0, len(nhi)): temp = round(nhi_heiles[i]/nhi[i], 3) lognhi.append(np.log10(nhi[i])) fact.append(temp) # Fit and Plot # a, b = np.polyfit(lognhi,fact,1) plt.plot(lognhi, fact, 'r.', label='Factor = N(HI)/N(HI)_thin') plt.plot(lognhi, a*np.array(lognhi) + b, 'k-', label='') a = round(a, 2) b = round(b, 2) plt.xlabel('Factor') plt.ylabel('log(N(HI)_opt.thin).1e20') plt.title('Correlation between N(HI)_Heiles and Optically thin N(HI)') plt.grid(True) plt.text(0.21, 1.31, 'Arecibo beam at 1.4GHz = 3.5\'', color='blue', fontsize=12) plt.text(0.21, 0.92, 'a = '+str(a)+' b = '+str(b), color='blue', fontsize=12) plt.legend(loc='upper right') plt.show()
def _autolabel(self, plt, rects): """a method to label the height of the bar in the bar chart""" for rect in rects: height = int(rect.get_height()) if (height > 0): plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % int(height))
def plot_confusion_matrix(y_true, y_pred, thresh, classes): """ This function plots the (normalized) confusion matrix. """ # obtain class predictions from probabilities y_pred = (y_pred>=thresh)*1 # obtain (unnormalized) confusion matrix cm = confusion_matrix(y_true, y_pred) # normalize confusion matrix cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.figure() plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues) plt.title('Confusion matrix') plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], '.2f'), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') plt.show()
def main(args): histogram = args.histogram min_limit = args.min_limit max_limit = args.max_limit kmer = args.kmer output_name = args.output_name Kmer_histogram = pd.io.parsers.read_csv(histogram, sep="\t", header=None) Kmer_coverage = Kmer_histogram[Kmer_histogram.columns[0]].tolist() Kmer_count = Kmer_histogram[Kmer_histogram.columns[1]].tolist() Kmer_freq = [Kmer_coverage[i] * Kmer_count[i] for i in range(len(Kmer_coverage))] # coverage peak, disregarding initial peak kmer_freq_peak = Kmer_freq.index(max(Kmer_freq[min_limit:max_limit])) kmer_freq_peak_value = max(Kmer_freq[min_limit:max_limit]) xmax = max_limit ymax = kmer_freq_peak_value + (kmer_freq_peak_value * 0.30) plt.plot(Kmer_coverage, Kmer_freq) plt.title("K-mer length = {}".format(kmer)) plt.xlim((0, xmax)) plt.ylim((0, ymax)) plt.vlines(kmer_freq_peak, 0, kmer_freq_peak_value, colors="r", linestyles="--") plt.text(kmer_freq_peak, kmer_freq_peak_value + 2000, str(kmer_freq_peak)) plotname = "{}".format(output_name) plt.savefig(plotname) plt.clf() return 0
def pylot_show(): sql = 'select * from douban;' cur.execute(sql) rows = cur.fetchall() # 把表中所有字段读取出来 count = [] # 每个分类的数量 category = [] # 分类 for row in rows: count.append(int(row[2])) category.append(row[1]) y_pos = np.arange(len(category)) # 定义y轴坐标数 #color = cm.jet(np.array(2)/max(count)) plt.barh(y_pos, count, color='y', align='center', alpha=0.4) # alpha图表的填充不透明度(0~1)之间 plt.yticks(y_pos, category) # 在y轴上做分类名的标记 plt.grid(axis = 'x') for count, y_pos in zip(count, y_pos): # 分类个数在图中显示的位置,就是那些数字在柱状图尾部显示的数字 plt.text(count+3, y_pos, count, horizontalalignment='center', verticalalignment='center', weight='bold') plt.ylim(+28.0, -2.0) # 可视化范围,相当于规定y轴范围 plt.title('douban_top250') # 图表的标题 fontproperties='simhei' plt.ylabel('movie category') # 图表y轴的标记 plt.subplots_adjust(bottom = 0.15) plt.xlabel('count') # 图表x轴的标记 #plt.savefig('douban.png') # 保存图片 plt.show()
def labelgridcells(shapefile): labelcount=len(shapef.shapes()) for shape in shapef.shapes(): x,y= gMap(shape.points[0][0],shape.points[0][1]) plt.text(x,y,labelcount,color='w') #print str(labelcount)+' '+str(shape.points[0][0])+' '+str(shape.points[0][1]) labelcount-=1
def histogram(arrayGenes): arrayG = np.array(arrayGenes) mean = arrayG.mean() std = arrayG.std() print 'mean = ', mean print 'std = ', std # mu, sigma = 100, 15 # x = mu + sigma * np.random.randn(100) # # print np.random.randn(2) # # for each in x: # print each # the histogram of the data n, bins, patches = plt.hist(arrayG, 60, normed=1, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([0, 1000, 0, 1000]) plt.grid(True) plt.show()
def make_equil_plts_2(phi, tad, xeq, gas): """ make_equil_plts_2 make_equil_plts_2 makes plots from Python function equil; it's the same as make_equil_plts, but with 2 separate figures """ plt.figure(1) phitadplt = plt.plot(phi,tad) plt.xlabel('Equivalence Ratio') plt.ylabel('Temperature (K)') plt.title('Adiabatic Flame Temperature') plt.figure(2) plt.ylim(10.**(-14),1) plt.xlim(phi[0],phi[49]) phixeqsemilogyplt = plt.semilogy(phi,xeq) # matplotlib semilogy takes in the correct dimensions! What I mean is that say phi is a list of length 50. Suppose xeq is a numpy array of shape (50,53). This will result in 53 different (line) graphs on the same plot, the correct number of line graphs for (50) (x,y) data points/dots! plt.xlabel('Equivalence Ratio') plt.ylabel('Mole Fraction') plt.title('Equilibrium Composition') j = 10 for k in range(gas.n_species): plt.text(phi[j],1.5*xeq[j,k],gas.species_name(k)) j = j + 2 if j > 46: j = 10 plt.show() return phitadplt, phixeqsemilogyplt
def plot_traj(self,ap=True,ag=True,fig=[],cps=20,ans=40,**kwargs): if fig ==[]: fig = plt.figure('trajectory', figsize=(20, 5), dpi=100) fig, ax = self.layout.showG('s',fig=fig,nodes=False,**kwargs) if ag: for i,n in enumerate(self.agents): cp=0 for j,p in enumerate(self.data[n]['p']): if j == 0: ax.plot(p[0],p[1],'o',color=self.colors[n],label='agent #'+n,**kwargs) else: # to avoid multiple label in legend ax.plot(p[0],p[1],'o',color=self.colors[n],**kwargs) if j%self.checkpoint==0: ax.plot(p[0],p[1],'o',color=self.colors[n],ms=cps) plt.text(p[0]-0.2,p[1]-0.2, str(cp), fontsize=10) cp = cp+1 if ap: for i,n in enumerate(self.ap): if n =='6' or n == '9': color='r' else : color='g' ax.plot(self.data[n]['p'][0,0],self.data[n]['p'][0,1] ,'^',color=color,label='AP #'+n,ms=ans) ax.grid('off') ax.legend(numpoints=1) return fig,ax
def execute(bins=10, ylim=False): data = pandas.merge(load_terms(), load_search_results().rename(columns={'identifier': 'term_id'}), on=['term_id'], how='inner') data = data[data['term_name'].apply(lambda x: len(x.split(';')[0]) > 5)] data = data[data['term_id'].apply(lambda x: x.startswith('A'))] data = pandas.merge(data, load_radiopaedia_terms(), on=['term_id', 'term_name'], how='inner') # load_radiopaedia_terms() # g = sns.pairplot(data, vars=['search_results_log', 'pagerank', 'difficulty_prob']) # for ax in g.axes.flat: # if ax.get_xlabel() in ['difficulty_prob', 'pagerank']: # ax.set_xlim(0, 1) # if ax.get_ylabel() in ['difficulty_prob', 'pagerank']: # ax.set_ylim(0, 1) # if min(ax.get_xticks()) < 0: # ax.set_xlim(0, max(ax.get_xticks())) # if min(ax.get_yticks()) < 0: # ax.set_ylim(0, max(ax.get_yticks())) # output.savefig('importance_pair', tight_layout=False) rcParams['figure.figsize'] = 30, 20 for term_name, difficulty_prob, pagerank in data[['term_name', 'difficulty_prob', 'pagerank']].values: plt.plot(1 - difficulty_prob, pagerank, color='red', marker='s', markersize=10) plt.text(1 - difficulty_prob, pagerank, term_name) if ylim: plt.ylim(0, 0.5) plt.xlabel('Predicted error rate') plt.ylabel('Pagerank') output.savefig('importance_pagerank')
b = matxMultiply(XT, Y) ret = gj_Solve(A, b) return ret[0][0], ret[1][0] m, b = linearRegression(X, Y) print(m, b) # 你求得的回归结果是什么? # 请使用运行以下代码将它画出来。 # In[41]: # 请不要修改下面的代码 x1, x2 = -5, 5 y1, y2 = x1 * m + b, x2 * m + b plt.xlim((-5, 5)) plt.xlabel('x', fontsize=18) plt.ylabel('y', fontsize=18) plt.scatter(X, Y, c='b') plt.plot((x1, x2), (y1, y2), 'r') plt.text(1, 2, 'y = {m}x + {b}'.format(m=m, b=b)) plt.show() # 你求得的回归结果对当前数据集的MSE是多少? # In[42]: print(calculateMSE(X, Y, m, b))
# names inside the quotes in the two lines below. Do not # modify anything else in this file your_first_name = 'Dana' your_last_name = 'Smith' # If you are an instructor, modify the next 3 lines. # You do not need to modify anything else in this file. classname = 'Intro Exp Phys I' term = 'Fall_2012' # must contain no spaces email = '*****@*****.**' plt.plot([0,1], 'r', [1,0], 'b') plt.text( 0.5, 0.9, '{0:s} {1:s}\n{2:s}\n{3:s}' .format(your_first_name, your_last_name, classname, term), horizontalalignment='center', verticalalignment='top', size = 'x-large', bbox=dict(facecolor='purple', alpha=0.4)) plt.text( 0.5, 0.1, 'scipy {0:s}\nnumpy {1:s}\nmatplotlib {2:s}\non {3:s}\n{4:s}' .format(scipy.__version__, numpy.__version__, matplotlib.__version__, platform.platform(), socket.gethostname() ) , horizontalalignment='center', verticalalignment='bottom') filename = your_last_name + '_' + your_first_name + '_' + \ term + '.png' plt.title('{0:s} "{1:s}"\n E-mail this file to "{2:s}"' .format('This plot has been saved on your computer as', filename, email), fontsize=12) plt.savefig(filename) plt.show()
file = open("DataLog1.txt", "w") # set up the figure, the axis, and the plot element we want to animate fig = plt.figure(figsize=(7, 4), dpi=120) fig.suptitle("Backster" + u"\N{TRADE MARK SIGN}") ax = p3.Axes3D(fig, [0, 0.05, 0.7, 0.95]) # declare text line ax1 = fig.add_axes([0.75, 0.4, 0.2, 0.2]) ax1.axis('off') # create box for text rect = patches.Rectangle((0, 0), 1, 1, fc=[1.0, 1.0, 1.0], linewidth=2.0, edgecolor=[0.0, 0.0, 0.0]) ax1.add_patch(rect) textbox = plt.text(0.5, 0.5, "Initial", horizontalalignment='center', verticalalignment='center') def quatRotate(q, va0q): vb0q = qmultiply(q, qmultiply(va0q, qconjugate(q))); return vb0q def qconjugate(q): # Pull out the scalar part of our input quaternion. q0 = q[0] # Pull out the vector part of our input quaternions. # qv = q[1:4] # means you want the 1-3 python indexes # Calculate the conjugate of this input quaternion for output.
import numpy as np import matplotlib.pyplot as plt import csv ar = np.random.poisson(30, 1000) bins = plt.hist(ar, 14, normed=True) plt.xlabel("k") plt.ylabel("P(X=k)") plt.grid(axis='x', alpha=0.75) plt.grid(axis='y', alpha=0.75) plt.title("Poisson Distribution") plt.text(41, 0.06, r'$lamda=30$') plt.show() with open('poisson.csv', 'w') as csvfile: fieldnames = ['points'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for i in ar: writer.writerow({'points': i})
mbk_means_cluster_centers) X = TruncatedSVD_X[:,0:2] # KMeans ax = fig.add_subplot(1, 3, 1) for k, col in zip(range(n_clusters), colors): my_members = k_means_labels == k cluster_center = k_means_cluster_centers[k] ax.plot(X[my_members, 0], X[my_members, 1], 'w', markerfacecolor=col, marker='.') ax.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col, markeredgecolor='k', markersize=6) ax.set_title('KMeans') ax.set_xticks(()) ax.set_yticks(()) plt.text(-3.5, 1.8, 'train time: %.2fs\ninertia: %f' % ( t_batch, k_means.inertia_)) # MiniBatchKMeans ax = fig.add_subplot(1, 3, 2) for k, col in zip(range(n_clusters), colors): my_members = mbk_means_labels == k cluster_center = mbk_means_cluster_centers[order[k]] ax.plot(X[my_members, 0], X[my_members, 1], 'w', markerfacecolor=col, marker='.') ax.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col, markeredgecolor='k', markersize=6) ax.set_title('MiniBatchKMeans') ax.set_xticks(()) ax.set_yticks(()) plt.text(-3.5, 1.8, 'train time: %.2fs\ninertia: %f' % (t_mini_batch, mbk.inertia_))
def plot_spectra(freqs, fluxes, errs, models, names, params, param_errs, rcs, BICs, colours, labels, figname, annotate=True, model_selection='better'): """Plot a figure of the radio spectra of an individual source, according to the input data and models. Arguments: ---------- freqs : list A list of frequencies in MHz. fluxes : list A list of fluxes in Jy. errs : list A list of flux uncertainties in Jy. models : list A list of functions corresponding to models of the radio spectrum. names : 2D list A list of fitted parameter names corresponding to each model above. params : 2D list A list of fitted parameter values corresponding to each model above. param_errs : 2D list A list of uncertainties on the fitted parameters corresponding to each model above. rcs : list A list of reduced chi squared values corresponding to each model above. BICs : list A list of Bayesian Information Criteria (BIC) values corresponding to each model above. colours : list A list of colours corresponding to each model above. labels : list A list of labels corresponding to each model above. figname : string The filename to give the figure when writing to file. Keyword arguments: ------------------ annotate : bool Annotate fit info onto figure. model_selection : string How to select models for plotting, based on the BIC values. Options are: 'best' - only plot the best model. 'all' - plot all models. 'better' - plot each model better than the previous, chronologically.""" #create SEDs directory if doesn't already exist if not os.path.exists('SEDs'): os.mkdir('SEDs') fig = plt.figure() ax = plt.subplot() #plot frequency axis 20% beyond range of values xlin = np.linspace(min(freqs) * 0.8, max(freqs) * 1.2, num=5000) plt.ylabel(r'Flux Density $S$ (mJy)') plt.xlabel(r'Frequency $\nu$ (GHz)') plt.xscale('log') plt.yscale('log') #adjust the tick values and add grid lines at minor tick locations subs = [1.0, 2.0, 5.0] ax.xaxis.set_major_locator(ticker.LogLocator(subs=subs)) ax.yaxis.set_major_locator(ticker.LogLocator(subs=subs)) ax.xaxis.set_minor_formatter(ticker.NullFormatter()) ax.yaxis.set_minor_formatter(ticker.NullFormatter()) ax.xaxis.set_major_formatter(ticker.FuncFormatter(ticks_format_freq)) ax.yaxis.set_major_formatter(ticker.FuncFormatter(ticks_format_flux)) ax.grid(b=True, which='minor', color='w', linewidth=0.5) #plot flux measurements plt.errorbar(freqs, fluxes, yerr=errs, linestyle='none', marker='.', c='r', zorder=15) best_bic = 0 dBIC = 3 offset = 0 plotted_models = 0 #plot each model for i in range(len(models)): ylin = models[i](xlin, *params[i]) txt = "{0}:\n {1}".format(labels[i], r'$\chi^2_{\rm red} = %.1f$' % rcs[i]) #compare BIC values bic = BICs[i] if i > 0: dBIC = best_bic - bic if model_selection != 'best': txt += ', {0}'.format(r'$\Delta{\rm BIC} = %.1f$' % (dBIC)) if dBIC >= 3: best_bic = bic #plot model if selected according to input if model_selection == 'all' or (model_selection == 'better' and dBIC >= 3) or (model_selection == 'best' and BICs[i] == min(BICs)): plotted_models += 1 plt.plot(xlin, ylin, c=colours[i], linestyle='--', zorder=i + 1, label=labels[i]) plt.legend(scatterpoints=1, fancybox=True, frameon=True, shadow=True) txt += '\n' #add each fitted parameter to string (in LaTeX format) for j, param in enumerate(names[i]): units = '' tokens = param.split('_') if len(tokens[0]) > 1: tokens[0] = "\\" + tokens[0] if len(tokens) > 1: param = r'%s_{\rm %s}' % (tokens[0], tokens[1]) else: param = tokens[0] val = params[i][j] err = param_errs[i][j] if param.startswith('S'): units = 'Jy' if val < 0.01: val = val * 1e3 err = err * 1e3 units = 'mJy' elif 'nu' in param: units = 'MHz' if val > 100: val = val / 1e3 err = err / 1e3 units = 'GHz' val = sig_figs(val) err = sig_figs(err) txt += ' ' + r'${0}$ = {1} $\pm$ {2} {3}'.format( param, val, err, units) + '\n' #annotate all fit info if it will fit on figure if annotate and plotted_models <= 3: plt.text(offset, 0, txt, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes) offset += 0.33 #write figure and close plt.savefig('SEDs/{0}'.format(figname)) plt.close()
def canvas(self, title=None, subtitle=None, infotext=None, figscaling=1.0): """Prepare the canvas to plot on, with title and subtitle. Args: title (str, optional): Title of plot. subtitle (str, optional): Sub title of plot. infotext (str, optional): Text to be written as info string. figscaling (str, optional): Figure scaling, default is 1.0 """ # overriding the base class canvas plt.rcParams["axes.xmargin"] = 0 # fill the plot margins # self._fig, (ax1, ax2) = plt.subplots(2, figsize=(11.69, 8.27)) self._fig, __ = plt.subplots(figsize=(11.69 * figscaling, 8.27 * figscaling)) ax1 = OrderedDict() ax1["main"] = plt.subplot2grid((20, 28), (0, 0), rowspan=20, colspan=23) ax2 = plt.subplot2grid((20, 28), (10, 23), rowspan=5, colspan=5) ax3 = plt.subplot2grid((20, 28), (15, 23), rowspan=5, colspan=5) # indicate A to B plt.text( 0.02, 0.98, "A", ha="left", va="top", transform=ax1["main"].transAxes, fontsize=8, ) plt.text( 0.98, 0.98, "B", ha="right", va="top", transform=ax1["main"].transAxes, fontsize=8, ) # title here: if title is not None: plt.text( 0.5, 1.09, title, ha="center", va="center", transform=ax1["main"].transAxes, fontsize=18, ) if subtitle is not None: ax1["main"].set_title(subtitle, size=14) if infotext is not None: plt.text( -0.11, -0.11, infotext, ha="left", va="center", transform=ax1["main"].transAxes, fontsize=6, ) ax1["main"].set_ylabel("Depth", fontsize=12.0) ax1["main"].set_xlabel("Length along well", fontsize=12) ax2.tick_params( axis="both", which="both", bottom=False, top=False, right=False, left=False, labelbottom=False, labeltop=False, labelright=False, labelleft=False, ) ax3.tick_params( axis="both", which="both", bottom=False, top=False, right=False, left=False, labelbottom=False, labeltop=False, labelright=False, labelleft=False, ) # need these also, a bug in functions above? ax2.xaxis.set_major_formatter(plt.NullFormatter()) ax2.yaxis.set_major_formatter(plt.NullFormatter()) ax3.xaxis.set_major_formatter(plt.NullFormatter()) ax3.yaxis.set_major_formatter(plt.NullFormatter()) self._ax1 = ax1 self._ax2 = ax2 self._ax3 = ax3
def __repr__(self): # TODO: Currently plotting the wrong number of facets. if self.facet_type=="grid": fig, axs = plt.subplots(self.n_high, self.n_wide, sharex=True, sharey=True) plt.subplots_adjust(wspace=.05, hspace=.05) elif self.facet_type=="wrap": subplots_available = self.n_wide * self.n_high extra_subplots = subplots_available - self.n_dim_x fig, axs = plt.subplots(self.n_high, self.n_wide) for extra_plot in axs.flatten()[-extra_subplots:]: extra_plot.axis('off') # TODO: This isn't working plots = [None for i in range(self.n_dim_x)] for i in range(self.n_dim_x): idx = (i % self.n_high) * self.n_wide + (i % self.n_wide) plots[idx] = (i % self.n_wide, i % self.n_high) plots = [plot for plot in plots if plot is not None] plots = sorted(plots, key=lambda x: x[1] + x[0] * self.n_high + 1) else: fig, axs = plt.subplots(self.n_high, self.n_wide) plt.subplot(self.n_wide, self.n_high, 1) # Faceting just means doing an additional groupby. The # dimensions of the plot remain the same if self.facets: cntr = 0 if len(self.facets)==2: for facets, frame in self.data.groupby(self.facets): pos = self.facet_pairs.index(facets) + 1 plt.subplot(self.n_wide, self.n_high, pos) for layer in self._get_layers(frame): for geom in self.geoms: callbacks = geom.plot_layer(layer) # This needs to enumerate all possibilities for pos, facets in enumerate(self.facet_pairs): pos += 1 if pos <= self.n_high: plt.subplot(self.n_wide, self.n_high, pos) plt.table(cellText=[[facets[1]]], loc='top', cellLoc='center', cellColours=[['lightgrey']]) if (pos % self.n_high)==0: plt.subplot(self.n_wide, self.n_high, pos) x = max(plt.xticks()[0]) y = max(plt.yticks()[0]) ax = axs[pos % self.n_high][pos % self.n_wide] plt.text(x*1.025, y/2., facets[0], bbox=dict(facecolor='lightgrey', color='black'), fontdict=dict(rotation=-90, verticalalignment="center") ) plt.subplot(self.n_wide, self.n_high, pos) # Handle the different scale types here (free|free_y|free_x|None) and # also make sure that only the left column gets y scales and the bottom # row gets x scales scale_facet(self.n_wide, self.n_high, self.facet_pairs, self.facet_scales) else: for facet, frame in self.data.groupby(self.facets): for layer in self._get_layers(frame): for geom in self.geoms: if self.facet_type=="wrap": if cntr+1 > len(plots): continue pos = plots[cntr] if pos is None: continue y_i, x_i = pos pos = x_i + y_i * self.n_high + 1 plt.subplot(self.n_wide, self.n_high, pos) else: plt.subplot(self.n_wide, self.n_high, cntr) # TODO: this needs some work if (cntr % self.n_high)==-1: plt.tick_params(axis='y', which='both', bottom='off', top='off', labelbottom='off') callbacks = geom.plot_layer(layer) if callbacks: for callback in callbacks: fn = getattr(axs[cntr], callback['function']) fn(*callback['args']) #TODO: selective titles plt.title(facet) cntr += 1 else: for layer in self._get_layers(self.data): for geom in self.geoms: plt.subplot(1, 1, 1) callbacks = geom.plot_layer(layer) if callbacks: for callback in callbacks: fn = getattr(axs, callback['function']) fn(*callback['args']) # Handling the details of the chart here; probably be a better # way to do this... if self.title: plt.title(self.title) if self.xlab: if self.facet_type=="grid": fig.text(0.5, 0.025, self.xlab) else: plt.xlabel(self.xlab) if self.ylab: if self.facet_type=="grid": fig.text(0.025, 0.5, self.ylab, rotation='vertical') else: plt.ylabel(self.ylab) if self.xmajor_locator: plt.gca().xaxis.set_major_locator(self.xmajor_locator) if self.xtick_formatter: plt.gca().xaxis.set_major_formatter(self.xtick_formatter) fig.autofmt_xdate() if self.xbreaks: # xbreaks is a list manually provided plt.gca().xaxis.set_ticks(self.xbreaks) if self.xtick_labels: plt.gca().xaxis.set_ticklabels(self.xtick_labels) if self.ytick_formatter: plt.gca().yaxis.set_major_formatter(self.ytick_formatter) if self.xlimits: plt.xlim(self.xlimits) if self.ylimits: plt.ylim(self.ylimits) if self.scale_y_reverse: plt.gca().invert_yaxis() if self.scale_x_reverse: plt.gca().invert_xaxis() # TODO: Having some issues here with things that shouldn't have a legend # or at least shouldn't get shrunk to accomodate one. Need some sort of # test in place to prevent this OR prevent legend getting set to True. if self.legend: if self.facets: if 1==2: ax = axs[0][self.n_wide] box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) cntr = 0 for ltype, legend in self.legend.items(): lname = self.aesthetics.get(ltype, ltype) ax.add_artist(draw_legend(ax, legend, ltype, lname, cntr)) cntr += 1 else: box = axs.get_position() axs.set_position([box.x0, box.y0, box.width * 0.8, box.height]) cntr = 0 for ltype, legend in self.legend.items(): if legend: lname = self.aesthetics.get(ltype, ltype) axs.add_artist(draw_legend(axs, legend, ltype, lname, cntr)) cntr += 1 # TODO: We can probably get more sugary with this return "<ggplot: (%d)>" % self.__hash__()
import matplotlib.pyplot as plt import numpy as np x = np.arange(-10, 10, 0.02) y = np.sin(x) # sin图像 plt.figure('sin') plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 plt.xlim(-10, 10) plt.ylim(-1, 1) # y轴0点 plt.figure('y axis') line_x = x line_y = 0 * line_x # x轴0点 plt.figure('x axis') line_x1 = 0 * x line_y1 = np.sin(line_x) # 作图 plt.plot(line_x, line_y, 'r-', x, y, 'g-', line_x1, line_y1, 'y-') plt.title('sin图像') plt.text(0, 0, 'sin0°') plt.xlabel('x/°') plt.ylabel('value') plt.legend(['x轴', 'sinx', 'y轴'], loc='upper left') plt.savefig('sin.png') plt.show()
import tensorflow as tf import pandas as pd import numpy as np import matplotlib.pyplot as plt TRAIN_URL="http://download.tensorflow.org/data/iris_training.csv" train_path=tf.keras.utils.get_file(TRAIN_URL.split('/')[-1],TRAIN_URL) column_names=['SepalLength','SepalWidth','PetalLength','PetalWidth','Species'] df_iris=pd.read_csv(train_path,header=0,names=column_names) iris=np.array(df_iris) fig=plt.figure('Iris Data',figsize=(15,15)) plt.suptitle("Andreson's Iris Dara Set\n(Blue->Setosa|Red->Versicolor|Green->Virginical)") for i in range(4): for j in range(4): plt.subplot(4,4,4*i+(j+1)) if(i==j): plt.text(0.3,0.4,column_names[i],fontsize=15) else: plt.scatter(iris[:,j],iris[:,i],c=iris[:,4],cmap='brg') if(i==0): plt.title(column_names[j]) if(j==0): plt.ylabel(column_names[i]) plt.show()
alpha, ap, am = c2 beta, bp, bm = m2 sigma, sp, sm = sig2 ys = m2[0] * xs + c2[0] resids = f - m2[0] * r + c2[0] plt.fill_between(xs, ys-2*np.std(resids)-2*sig2[0], ys+2*np.std(resids)+2*sig2[0], color=pink, alpha=.1) plt.fill_between(xs, ys-np.std(resids)-sig2[0], ys+np.std(resids)+sig2[0], color=pink, alpha=.3) plt.errorbar(r, f, xerr=rerr, yerr=ferr, fmt="k.", capsize=0, ecolor=".5", mec=".2", alpha=.5) plt.plot(xs, ys, "k") plt.plot(xs, ys-np.std(resids), color=pink) plt.plot(xs, ys+np.std(resids), color=pink) plt.ylabel("$\log_{10}(F_8)$") plt.xlabel("$\log_{10}(\\rho_{\star}[\mathrm{g~cm}^{-3}])$") #plt.text(-1.15, 2.25, "$\log_{10} (\\rho_{\star}) \sim \mathcal{N} \left(\\alpha_\\rho + \\beta_\\rho \log_{10}(F_8), \ # \\sigma=\\sigma_{\\rho}\\right)$") plt.text(-1.15, 2.25, "$\log_{10} (F_8) \sim \mathcal{N} \left(\\alpha_\\rho + \\beta_\\rho \log_{10}(\\rho_{\star}), \\sigma=\\sigma_{\\rho}\\right)$") plt.text(-1.45, 1.2, "$\\alpha_\\rho = %.2f \pm %.2f$" % (np.round(alpha, 2), np.round(ap, 2))) plt.text(-1.45, 1., "$\\beta_\\rho = %.2f \pm %.2f $" % (np.round(beta, 2), np.round(bp, 2))) plt.text(-1.45, .8, "$\\sigma_{\\rho} = %.3f \pm %.3f $" % (np.round(sigma, 2), np.round(sp, 3))) plt.xlim(min(r), max(r)) plt.ylim(.4, 2.5) plt.savefig("../version1.0/flicker_vs_rho.pdf") # load data f, ferr, l, lerr, _, _ = np.genfromtxt("../data/log.dat").T alpha, ap, am = c2 beta, bp, bm = m2 sigma, sp, sm = sig2 ys = m2[0] * xs + c2[0] resids = f - m2[0] * l + c2[0]
plt.ylabel('State-action value') plt.subplot(312) if len(episode_sizes) >= 1000: episode_sizes = running_mean(episode_sizes, 1000) plt.plot(range(len(episode_sizes)), episode_sizes) plt.ylabel('Episode length') xmax = 0.8 * len(episode_sizes) ymax = 0.7 * max(episode_sizes) _lengths_ = [ env.current_values[key]['episode_lengths'][-1] for key in env.current_values if len(env.current_values[key]['episode_lengths']) ] _mean_ = float("%0.3f" % np.mean(_lengths_)) plt.text(xmax, ymax, 'Mean: {}\nMax: {}'.format(_mean_, max(_lengths_))) plt.subplot(313) plt.plot(range(len(env.epsilon_decay)), env.epsilon_decay) plt.xlabel('Episode') plt.ylabel('Epsilon') plt.tight_layout() F = 'images/' + 'L_{}_{}'.format(length, seq) pylab.savefig(F + '.pdf', pad_inches=0, transparent=False) os.system('pdf-crop-margins -v -s -u %s.pdf 1> /dev/null 2> /dev/null' % F) os.system('mv %s_cropped.pdf %s.pdf' % ('L_{}_{}'.format(length, seq), F)) # Make movies of 50 longest trajectories total = 0
# print "t_cf", type(t_cf), type(t_cf[0]), t_cf[0], t_cf[0][0].pos # See if this case if case0 or case1 if len(t_cf[0]) >= len(env.ore_order): t_b = [] for i in range(1): in_obs_pos = copy.deepcopy(env.obs_pos) for ore_i in range(i+1): in_obs_pos.remove(env.obs_pos[env.ore_order[ore_i]]) t_b.append(env.get_cf_b(t_cf[i], in_obs_pos)) print "\n step", i, " has cf b:", t_b[i] draw_figs = 1 if draw_figs == 1: for c_i in range(len(can_info)): plt.text(can_info[c_i].grid[0], can_info[c_i].grid[1], 'Can' + str(c_i), fontsize=20, ha='center', bbox=dict(facecolor='pink', alpha=0.8)) for o_i in range(len(env.obs_grid)): plt.text(env.obs_grid[o_i][0], env.obs_grid[o_i][1], 'Obs' + str(o_i), fontsize=20, ha='center', bbox=dict(facecolor='red', alpha=0.8)) for step_i in range(1): step_grid = copy.deepcopy(env.grid_act) step_obs_grid = copy.deepcopy(env.obs_grid) for ore_i in range(step_i + 1): step_obs_grid.remove(env.obs_grid[env.ore_order[ore_i]]) for i in range(len(step_obs_grid)): step_grid = CUF.obstacle_circle(step_grid, [round(step_obs_grid[i][0], 2), round(step_obs_grid[i][1], 2), env.obs_r[i]], 2) for ci in range(len(can_info)): xi, yi = can_info[ci].grid step_grid = CUF.obstacle_circle(step_grid, [xi, yi, 0.04], 30) step_grid = CUF.obstacle_circle(step_grid, [env.tar_grid[0], env.tar_grid[1], tar_r], 4) # target
def main(args): torch.manual_seed(args.seed) if torch.cuda.is_available(): torch.cuda.manual_seed(args.seed) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') dataset = MNIST(root=args.root, train=True, transform=transforms.ToTensor(), download=True) data_loader = DataLoader(dataset=dataset, batch_size=args.batch_size, shuffle=True) if args.loss == 'bce': def loss_fn(recon_x, x, mean, log_var, invSigma=None): BCE = torch.nn.functional.binary_cross_entropy(recon_x.view( -1, 28 * 28), x.view(-1, 28 * 28), reduction='sum') KLD = -0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp()) return (BCE + KLD) / x.size(0) elif args.loss == 'mse': def loss_fn(recon_x, x, mean, log_var, invSigma=None): xdiff = x.view(-1, 28 * 28) - recon_x.view(-1, 28 * 28) MSE = 0.5 * torch.trace(invSigma.mm(torch.t(xdiff).mm(xdiff))) KLD = -0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp()) return (MSE + KLD) / x.size(0) else: raise NameError('Wrong loss name. Choose either bce or mse.') vae = VAE(encoder_layer_sizes=args.encoder_layer_sizes, latent_size=args.latent_size, decoder_layer_sizes=args.decoder_layer_sizes, device=device, conditional=args.conditional, num_labels=10 if args.conditional else 0).to(device) optimizer = torch.optim.Adam(vae.parameters(), lr=args.learning_rate) logs = defaultdict(list) invSigma = torch.eye(28 * 28).to(device) for epoch in range(args.epochs): tracker_epoch = defaultdict(lambda: defaultdict(dict)) vae.train() for iteration, (x, y) in enumerate(data_loader): x, y = x.to(device), y.to(device) if args.conditional: recon_x, mean, log_var, z = vae(x, y) else: recon_x, mean, log_var, z = vae(x) for i, yi in enumerate(y): id = len(tracker_epoch) tracker_epoch[id]['x'] = z[i, 0].item() tracker_epoch[id]['y'] = z[i, 1].item() tracker_epoch[id]['label'] = yi.item() loss = loss_fn(recon_x, x, mean, log_var, invSigma) optimizer.zero_grad() loss.backward() optimizer.step() logs['loss'].append(loss.item()) if iteration % args.print_every == 0 or iteration == len( data_loader) - 1: print("Epoch {:02d}/{:02d} Batch {:04d}/{:d}, Loss {:9.4f}". format(epoch, args.epochs, iteration, len(data_loader) - 1, loss.item())) if args.conditional: c = torch.arange(0, 10).long().unsqueeze(1) x = vae.inference(n=c.size(0), c=c) else: x = vae.inference(n=10) plt.figure() plt.figure(figsize=(5, 10)) for p in range(10): plt.subplot(5, 2, p + 1) if args.conditional: plt.text(0, 0, "c={:d}".format(c[p].item()), color='black', backgroundcolor='white', fontsize=8) plt.imshow(x[p].view(28, 28).cpu().data.numpy()) plt.axis('off') if not os.path.exists( os.path.join(args.fig_root, args.vae_name)): if not (os.path.exists(os.path.join(args.fig_root))): os.mkdir(os.path.join(args.fig_root)) os.mkdir(os.path.join(args.fig_root, args.vae_name)) plt.savefig(os.path.join( args.fig_root, args.vae_name, "E{:d}I{:d}.png".format(epoch, iteration)), dpi=300) plt.clf() plt.close('all') df = pd.DataFrame.from_dict(tracker_epoch, orient='index') g = sns.lmplot(x='x', y='y', hue='label', data=df.groupby('label').head(100), fit_reg=False, legend=True) g.savefig(os.path.join(args.fig_root, args.vae_name, "E{:d}-Dist.png".format(epoch)), dpi=300) # Update the (inverse) covariance matrix: if args.loss == 'mse': with torch.no_grad(): Sigma = torch.zeros(28 * 28, 28 * 28).to(device) vae.eval() for iteration, (x, y) in enumerate(data_loader): x, y = x.to(device), y.to(device) if args.conditional: recon_x, mean, log_var, z = vae(x, y) else: recon_x, mean, log_var, z = vae(x) xdiff = x.view(-1, 28 * 28) - recon_x.view(-1, 28 * 28) Sigma += torch.t(xdiff).mm(xdiff) Sigma = Sigma / len(data_loader.dataset) + 1e-3 * torch.eye( 28 * 28).to(device) invSigma = torch.inverse(Sigma) if args.loss == 'mse': plt.figure() plt.imshow(Sigma.cpu().data.numpy()) plt.axis('off') plt.savefig(os.path.join(args.fig_root, args.vae_name, "Sigma{:d}.png".format(epoch)), dpi=300) plt.clf() plt.close('all') Sigma = None # save invSigma: np.save(os.path.join(args.fig_root, args.vae_name, "inSigma.npy"), invSigma.cpu().data.numpy()) # save final model save_file = os.path.join(args.fig_root, args.vae_name, 'final_model.pt') torch.save(vae.state_dict(), save_file)
plt.yticks(np.arange(0, np.max(y2d[:,0]),30)) ax.set_yticklabels(np.arange(0,np.max(y2d[:,0])/30,1).astype(int), fontsize = 13, zorder = 6) #x-axis plt.xlim([0,xlen]) plt.xticks(np.arange(0,xlen,100)) ax.set_xticklabels(np.arange(xmin*0.2,xmax*0.2,100*0.2).astype(int), fontsize = 13) #axis labels plt.ylabel('Height (km)', fontsize = 15, labelpad = 8) if j == 3: plt.xlabel('Distance within Domain (km)', fontsize = 15, labelpad = 7) #Colorbar cbaxes = fig.add_axes([0.92, 0.2, 0.035, 0.55]) cbar = plt.colorbar(ref_plot, cax = cbaxes, ticks = levels_ticks) cbar.ax.tick_params(labelsize=13) plt.text(-.1, -0.12, 'm/s', fontsize = 19) path = '/uufs/chpc.utah.edu/common/home/u1013082/public_html/phd_plots/cm1/' plt.savefig('%s' % path + 'png_for_gifs/3panel_cross_section_30ms_u_%03d.png' % i, dpi = 100) plt.close(fig) ##Build GIF #os.system('convert -delay 12 -quality 100 /uufs/chpc.utah.edu/common/home/u1013082/public_html/phd_plots/cm1/png_for_gifs/3panel_cross_section_30ms_u_*.png /uufs/chpc.utah.edu/common/home/u1013082/public_html/phd_plots/cm1/gifs/3panel_cross_section_30ms_u.gif') ###Delete PNGs
def hasilsejarah(): try: qty = request.form['qty'] mean = request.form['mean'] if qty == '' or mean == '': return render_template('history.html', b='kosong') else: randomname = random.randint(10000, 9999999) listplot = os.listdir('./storagemobil') aa = 'qty' + str(len(listplot) + 1) + '_' + str(randomname) + '.jpg' bb = 'mean' + str(len(listplot) + 1) + '_' + str(randomname) + '.jpg' if qty != '': print(qty) print(mean) # Qty plot Figure ----------------------- x = df[qty].value_counts().index.astype('str') y = df[qty].value_counts() plt.clf() plt.figure(figsize=(10, 5)) plt.bar(x, y, width=0.3, label='Quantity') plt.xticks(x, rotation='vertical') plt.ylabel('Quantity') plt.xlabel(f'{qty}') plt.legend() if qty != 'car': for i in range(len(x)): plt.text(i, y.iloc[i], y.iloc[i], fontsize=8, horizontalalignment='center', verticalalignment='bottom') plt.grid() plt.tight_layout() plt.savefig('storagemobil/%s' % aa) else: pass if mean != '': # Mean Figure Plot ----------------------- ft = df[mean].value_counts().index dictmean = {} for i in ft: dictmean[i] = df['price'][df[mean] == i].mean() c = dict(Counter(dictmean).most_common()) x1 = list(c.keys()) y1 = list(c.values()) plt.figure(figsize=(10, 5)) plt.bar(np.arange(len(x1)), y1, width=0.3, color='y', tick_label=x1, label='Average Price', alpha=1) plt.xticks(rotation='vertical') plt.ylabel('Average Price USD') plt.xlabel(f'{mean}') plt.legend() if mean != 'car': for i in range(len(x1)): plt.text(i, y1[i], round(y1[i]), fontsize=8, horizontalalignment='center', verticalalignment='bottom') plt.tight_layout() plt.grid() plt.savefig('storagemobil/%s' % bb) else: pass return render_template('history.html', b='isi', aa=aa, bb=bb) except: return redirect(url_for('error'))
box_h = ((y2 - y1) / unpad_h) * img.shape[0] box_w = ((x2 - x1) / unpad_w) * img.shape[1] y1 = ((y1 - pad_y // 2) / unpad_h) * img.shape[0] x1 = ((x1 - pad_x // 2) / unpad_w) * img.shape[1] color = bbox_colors[int( np.where(unique_labels == int(cls_pred))[0])] bbox = patches.Rectangle((x1, y1), box_w, box_h, linewidth=2, edgecolor=color, facecolor='none') ax.add_patch(bbox) plt.text(x1, y1, s=classes[int(cls_pred)], color='white', verticalalignment='top', bbox={ 'color': color, 'pad': 0 }) plt.axis('off') # save image plt.savefig(img_path.replace(".jpg", "-det.jpg"), bbox_inches='tight', pad_inches=0.0) plt.show() # In[ ]:
def Shadow(a, b): ix = (x>a) & (x<b) plt.fill_between(x,y,0,where=ix,facecolor='grey', alpha=0.25) plt.text(0.5 * (a + b), 0.2, "$\int_a^b f(x)\mathrm{d}x$", \ horizontalalignment='center')
def sample_test_data(index): num_px = train_set_x_orig.shape[1] c = classes[d["Y_prediction_test"][0,index]].decode("utf-8") plt.imshow(test_set_x[:,index].reshape((num_px, num_px, 3))) plt.text(2,10, c, fontsize=24)
def equation(ax2, bx, c, flag=False): ''' * equation: Calculates the roots of the second degree equation. * Parameters: ax2 = It is any number and are called the coefficients of the equation. (Type: Integer) bx = It is any number and are called the coefficients of the equation. (Type: Integer) c = It is any number and are called the coefficients of the equation. (Type: Integer) Returns: n: Returns a vector with the information, always returning: info: Results information. (Type: String) delta: Delta, which is the Discriminant value. (Type: Integer or Float) x1: Real root 1. (Type: Integer or Float) x2: Real root 2. (Type: Integer or Float) In that order, if you have those variables. (Type: List) Example: How to read the results: res = equation(1, -2, -1) output = ['INFO','Delta', 'X1', 'X2'] for i in range (len(res)): print('{}: {}'.format(output[i], res[i])) Result: INFO: The delta is greater than zero, the equation has two real and distinct values. Delta: 8 X1: 2.414213562373095 X2: -0.41421356237309515 ''' print('\nTyped function: {}x^2 {}x {} = 0\n'.format(ax2, bx, c)) if flag == True: import matplotlib.pyplot as plt import numpy as np result = [] delta = (bx**2) - (4 * ax2 * c) if delta > 0: info = 'The delta is greater than zero, the equation has two real and distinct values.' x1 = (-bx + delta**(0.5)) / (2.0 * ax2) x2 = (-bx - delta**(0.5)) / (2.0 * ax2) result = info, delta, x1, x2 if flag == True: axis_x = [] axis_y = [] zeros = [] axis_xn = [] axis_yn = [] variacao = abs(x1 - x2) if variacao < 3: variacao = 3 for x in np.arange(x1 - variacao, x2 + variacao, variacao / 100): y = ax2 * (x**2) + bx * (x) + c axis_x.append(x) axis_y.append(y) zeros.append(0.0) axis_xn.append(-x) axis_yn.append(-y) plt.title('Graph of the Quadratic Function') plt.xlabel('X') plt.ylabel('Y') plt.plot(axis_x, axis_y, color="red") plt.plot(axis_x, zeros, color="black") plt.plot(zeros, axis_y, color="black") plt.plot(zeros, axis_yn, color="black") plt.scatter(x1, 0, marker='x', color='blue') plt.scatter(x2, 0, marker='x', color='blue') plt.text(x1, 1.5, ('' + str(x1))) plt.text(x2, 1.5, ('' + str(x2))) plt.show() return result elif delta == 0: info = 'The delta is equal to zero, the equation has only one real value or two equal results.' x1 = (-bx + delta**(0.5)) / (2.0 * ax2) x2 = x1 result = info, delta, x1, x2 if flag == True: axis_x = [] axis_y = [] zeros = [] axis_xn = [] axis_yn = [] variacao = abs(x1 - x2) if variacao < 3: variacao = 3 for x in np.arange(x1 - variacao, x2 + variacao, variacao / 100): y = ax2 * (x**2) + bx * (x) + c axis_x.append(x) axis_y.append(y) zeros.append(0.0) axis_xn.append(-x) axis_yn.append(-y) plt.title('Graph of the Quadratic Function') plt.xlabel('X') plt.ylabel('Y') plt.plot(axis_x, axis_y, color="red") plt.plot(axis_x, zeros, color="black") plt.plot(zeros, axis_y, color="black") plt.plot(zeros, axis_yn, color="black") plt.scatter(x1, 0, marker='x', color='blue') plt.scatter(x2, 0, marker='x', color='blue') plt.text(x1, 1.5, ('' + str(x1))) plt.text(x2, 1.5, ('' + str(x2))) plt.show() return result else: info = 'The delta value is less than zero, the equation has no real values.' result = info, delta x1 = x2 = 0 if flag == True: axis_x = [] axis_y = [] zeros = [] axis_xn = [] axis_yn = [] variacao = abs(x1 - x2) if variacao < 3: variacao = 3 for x in np.arange(x1 - variacao, x2 + variacao, variacao / 100): y = ax2 * (x**2) + bx * (x) + c axis_x.append(x) axis_y.append(y) zeros.append(0.0) axis_xn.append(-x) axis_yn.append(-y) plt.title('Graph of the Quadratic Function') plt.xlabel('X') plt.ylabel('Y') plt.plot(axis_x, axis_y, color="red") plt.plot(axis_x, zeros, color="black") plt.plot(zeros, axis_y, color="black") plt.plot(zeros, axis_yn, color="black") plt.scatter(x1, 0, marker='x', color='blue') plt.scatter(x2, 0, marker='x', color='blue') plt.text(x1, 1.5, ('' + str(x1))) plt.text(x2, 1.5, ('' + str(x2))) plt.show() return result
def load_and_plot_experiments( experiments, process_results, yaxis, xaxis, nsamples, color, marker, section="val", min_epoch=0, max_epoch=None, hollow_marker=False, show_labels=True, plot_fit=False, ): """ Load results and plot list of experiments. Args: experiments: Dictionary {name: path}, see example above. yaxis: The metric to use as yaxis. xaxis: The metric to use as xaxis. nsamples: Number of samples to use for computing mean+-std. section: The section of the results to use. min_epoch, max_epoch: The min and max epochs to perform the fit. """ x_all = [] y_all = [] # remove alpha for marker labels if len(color) == 4: text_color = color[:-1] else: text_color = color # In these cases the polynomial fails to capture the loss behaviour over the full range of epochs, so we harcode the range were the optimum is. for path, label in experiments: if path == "experiments/softlabels_inaturalist19_beta15": min_epoch = 0 results, epochs, start, end = get_results(path, nsamples, min_epoch, max_epoch, plot_fit=plot_fit) # write list of epochs around minimum on dictionary if path not in experiment_to_best_epoch: experiment_to_best_epoch[path] = [ epochs[e] for e in range(start, end + 1) ] else: assert experiment_to_best_epoch[path] == [ epochs[e] for e in range(start, end + 1) ], "Found two different best epoch for run '{}'".format(path) x_values = [ process_results["x"](results[epochs[i]][xaxis]) for i in range(start, end + 1) ] y_values = [ process_results["y"](results[epochs[i]][yaxis]) for i in range(start, end + 1) ] x_m = np.median(x_values) # x_e = np.std(x_values, ddof=1) y_m = np.median(y_values) # y_e = np.std(y_values, ddof=1) x_all.append(x_m) y_all.append(y_m) if not hollow_marker: plt.plot(x_m, y_m, color=color, marker=marker, zorder=100) else: plt.plot(x_m, y_m, color=color, marker=marker, zorder=100, markerfacecolor="w") if show_labels: plt.text(x_m * (1 - 0.01), y_m * (1 - 0.01), label, color=text_color, fontsize=8) plt.plot(x_all, y_all, "--", color="k", alpha=0.4, zorder=0, linewidth=1)
#parameters = ["total_cases1", "new_cases1", "serious_critical1"] #counts = [total_cases, new_cases, serious_critical] #df2.plot.bar(x="parameters", y="counts", rot=70, title="Number of tourist visits - Year 2018"); #plot.show(block=True); #print(df2.columns.tolist()) #df2 = df2.drop(df2.columns[[2:9:3]], axis=1, inplace=True) #print(df2.columns.tolist()) #df3 = df2.delete(['id', 'record_date']) #print(df2) #df2 = df1.reset_index() #print(df3) #df2.dtypes #print(y) data = y labels = ['Total Cases', 'Total Deaths', 'New cases', 'Active Cases'] my_colors = 'grbykmc' plt.figure(figsize=(10, 8)) plt.yticks(range(len(data)), labels) plt.barh(range(len(data)), data, color=my_colors) for i, v in enumerate(y): plt.text(v, i, v, color='blue', va='center', fontweight='bold') plt.grid(color='#95a5a6', linestyle='--', linewidth=2, axis='y', alpha=0.3) plt.xlabel('Covid Statistics', fontsize=20) plt.ylabel('Count', fontsize=20) plt.title('Most recent Covid 19 statistics from ' + entry + " updated at \n" + record_date, fontsize=20) plt.show()
def plot_waterfall(data, start, source_name, duration, dm,ofile, integrate_ts=False, integrate_spec=False, show_cb=False, cmap_str="gist_yarg", sweep_dms=[], sweep_posns=[], ax_im=None, ax_ts=None, ax_spec=None, interactive=True, downsamp=1,nsub=None,subdm=None,width=None, snr=None, csv_file=None,prob=None): """ I want a docstring too! """ if source_name is None: source_name="Unknown" #Output file if ofile is "unknown_cand": title = "%s_" + ofile + "_%.3f_%s" % (source_name,start,str(dm)) else: title=source_name + "_" + ofile # Set up axes fig = plt.figure(figsize=(10,14)) #fig.canvas.set_window_title("Frequency vs. Time") ''' im_width = 0.6 if integrate_spec else 0.8 im_height = 0.6 if integrate_ts else 0.8 if not ax_im: ax_im = plt.axes((0.15, 0.15, im_width, im_height)) if integrate_ts and not ax_ts: ax_ts = plt.axes((0.15, 0.75, im_width, 0.2),sharex=ax_im) if integrate_spec and not ax_spec: ax_spec = plt.axes((0.75, 0.15, 0.2, im_height),sharey=ax_im) ''' ax_ts = plt.axes((0.1, 0.835, 0.71, 0.145)) ax_im = plt.axes((0.1, 0.59, 0.71, 0.24), sharex=ax_ts) ax_dmvstm = plt.axes((0.1, 0.345, 0.71, 0.24), sharex=ax_ts) ax_spec = plt.axes((0.815, 0.59, 0.16, 0.24),sharey=ax_im) ax_dmsnr = plt.axes((0.815,0.345,0.16,0.24),sharey=ax_dmvstm) ax_orig = plt.axes((0.1, 0.1,0.71, 0.21)) data.downsample(downsamp) nbinlim = np.int(duration/data.dt) #Get window spectrum_window = 0.02*duration window_width = int(spectrum_window/data.dt) # bins burst_bin = nbinlim/2 #Zap all channels which have more than half bins zero (drop packets) zerochan=1 arrmedian=np.ones(data.numchans) if zerochan: for ii in range(data.numchans): chan = data.get_chan(ii) #if 50% are zero if len(chan)-np.count_nonzero(chan)>0.5*len(chan): arrmedian[ii]=0.0 #arrmedian=np.array(arrmedian) #Additional zapping from off-pulse spectra extrazap=1 zapthresh=4 if extrazap: off_spec1 = np.array(data.data[..., 0:burst_bin-window_width]) # off-pulse from left off_spec2 = np.array(data.data[...,burst_bin+window_width:nbinlim]) # off-pulse from right off_spec = (np.mean(off_spec1,axis=1) + np.mean(off_spec2,axis=1))/2.0 # Total off-pulse mask = np.zeros(data.data.shape,dtype=bool) masked_val = np.ones(data.data.shape[1],dtype=bool) masked_chan = np.array(np.where(off_spec>zapthresh*np.std(off_spec)))[0] mask[masked_chan] = masked_val masked_chan1 = np.array(np.where(arrmedian==0))[0] mask[masked_chan1] = masked_val data=data.masked(mask,maskval=0) for i in masked_chan: ax_spec.axhline(data.freqs[i],alpha=0.4,color='grey') for i in masked_chan1: ax_spec.axhline(data.freqs[i],alpha=0.4,color='grey') # DM-vs-time plot dmvstm_array = [] #Old way ''' lodm = int(dm-(dm*0.15)) if lodm < 0: lodm = 0 hidm = int(dm+(dm*0.15)) ''' band = (data.freqs.max()-data.freqs.min()) centFreq = (data.freqs.min()+band/2.0)/(10**3) # To get it in GHz print width,centFreq,band #This comes from Cordes and McLaughlin (2003) Equation 13. FWHM_DM = 506*float(width)*pow(centFreq,3)/band #The candidate DM might not be exact so using a longer range FWHM_DM = 3.0*FWHM_DM lodm = dm-FWHM_DM if lodm < 0: lodm = 0 hidm = 2*dm # If low DM is zero then range should be 0 to 2*DM else: hidm= dm+FWHM_DM print FWHM_DM,dm,lodm,hidm dmstep = (hidm-lodm)/48.0 datacopy = copy.deepcopy(data) #print lodm,hidm for ii in np.arange(lodm,hidm,dmstep): #for ii in range(400,600,10): #Without this, dispersion delay with smaller DM step does not produce delay close to bin width data.dedisperse(0,padval='rotate') data.dedisperse(ii,padval='rotate') Data = np.array(data.data[..., :nbinlim]) Dedisp_ts = Data.sum(axis=0) dmvstm_array.append(Dedisp_ts) dmvstm_array=np.array(dmvstm_array) #print np.shape(dmvstm_array) #np.save('dmvstm_1step.npz',dmvstm_array) ax_dmvstm.set_xlabel("Time (sec) ") ax_dmvstm.set_ylabel("DM") #ax_dmvstm.imshow(dmvstm_array, aspect='auto', cmap=matplotlib.cm.cmap_d[cmap_str], origin='lower',extent=(data.starttime, data.starttime+ nbinlim*data.dt, lodm, hidm)) ax_dmvstm.imshow(dmvstm_array, aspect='auto', origin='lower',extent=(data.starttime, data.starttime+ nbinlim*data.dt, lodm, hidm)) #cmap=matplotlib.cm.cmap_d[cmap_str]) #interpolation='nearest', origin='upper') plt.setp(ax_im.get_xticklabels(), visible = False) plt.setp(ax_ts.get_xticklabels(), visible = False) ax_dmvstm.set_ylim(lodm,hidm) #extent=(data.starttime, data.starttime+ nbinlim*data.dt, 500, 700) #plt.show() #fig2 = plt.figure(2) #plt.imshow(dmvstm_array,aspect='auto') #Plot Freq-vs-time data = copy.deepcopy(datacopy) #data.downsample(downsamp) data.dedisperse(dm,padval='rotate') nbinlim = np.int(duration/data.dt) #orig img = ax_im.imshow(data.data[..., :nbinlim], aspect='auto', \ cmap=matplotlib.cm.cmap_d[cmap_str], \ interpolation='nearest', origin='upper', \ extent=(data.starttime, data.starttime+ nbinlim*data.dt, \ data.freqs.min(), data.freqs.max())) #ax_im.axvline(x=(data.starttime + nbinlim*data.dt)/2.0,ymin=data.freqs.min(),ymax=data.freqs.max(),lw=3,color='b') if show_cb: cb = ax_im.get_figure().colorbar(img) cb.set_label("Scaled signal intensity (arbitrary units)") # Dressing it up ax_im.xaxis.get_major_formatter().set_useOffset(False) #ax_im.set_xlabel("Time") ax_im.set_ylabel("Frequency (MHz)") # Plot Time series #Data = np.array(data.data[..., :nbinlim]) Data = np.array(data.data[..., :nbinlim]) Dedisp_ts = Data.sum(axis=0) times = (np.arange(data.numspectra)*data.dt + start)[..., :nbinlim] ax_ts.plot(times, Dedisp_ts,"k") ax_ts.set_xlim([times.min(),times.max()]) text1 = "DM: " + "%.2f" % float(data.dm) plt.text(1.1,0.9,text1,fontsize=15,ha='center', va='center', transform=ax_ts.transAxes) text2 = "Width: " + "%.2f" % float(width) plt.text(1.1,0.75,text2,fontsize=15,ha='center', va='center', transform=ax_ts.transAxes) text3 = "SNR: " + "%.2f" % float(snr) plt.text(1.1,0.6,text3,fontsize=15,ha='center', va='center', transform=ax_ts.transAxes) ax_ts.set_title(title,fontsize=14) plt.setp(ax_ts.get_xticklabels(), visible = False) plt.setp(ax_ts.get_yticklabels(), visible = False) #Spectrum and DM-vs-SNR plot ax_ts.axvline(times[burst_bin]-spectrum_window,ls="--",c="grey") ax_ts.axvline(times[burst_bin]+spectrum_window,ls="--",c="grey") #Get spectrum and DM-vs-SNR for the on-pulse window on_spec = np.array(data.data[..., burst_bin-window_width:burst_bin+window_width]) on_dmsnr = np.array(dmvstm_array[..., burst_bin-window_width:burst_bin+window_width]) Dedisp_spec = np.mean(on_spec,axis=1) Dedisp_dmsnr = np.mean(on_dmsnr, axis=1) #Get off-pulse and DM-vs-SNR for range outside on-pulse window off_spec1 = np.array(data.data[..., 0:burst_bin-window_width]) # off-pulse from left off_spec2 = np.array(data.data[...,burst_bin+window_width:nbinlim]) # off-pulse from right off_spec = (np.mean(off_spec1,axis=1) + np.mean(off_spec2,axis=1))/2.0 # Total off-pulse off_dmsnr1 = np.array(dmvstm_array[...,0:burst_bin-window_width]) off_dmsnr2 = np.array(dmvstm_array[...,burst_bin+window_width:nbinlim]) off_dmsnr = (np.mean(off_dmsnr1,axis=1) + np.mean(off_dmsnr2,axis=1))/2.0 #Get Y-axis for both plots dms = np.linspace(lodm, hidm, len(Dedisp_dmsnr)) freqs = np.linspace(data.freqs.max(), data.freqs.min(), len(Dedisp_spec)) #Spectrum plot ax_spec.plot(Dedisp_spec,freqs,color="red",lw=2) ax_spec.plot(off_spec,freqs,color="blue",alpha=0.5,lw=1) ttest=float(stats.ttest_ind(Dedisp_spec,off_spec)[0].tolist()) ttestprob = float(stats.ttest_ind(Dedisp_spec,off_spec)[1].tolist()) text3 = "t-test" plt.text(1.1,0.45,text3,fontsize=12,ha='center', va='center', transform=ax_ts.transAxes) text4 = " %.2f" % (ttest) + "(%.2f" % ((1-ttestprob)*100) + "%)" plt.text(1.1,0.3,text4,fontsize=12,ha='center', va='center', transform=ax_ts.transAxes) if prob: text5 = "ML prob: " + "%.2f" % (float(prob)) print text5 plt.text(1.1,0.1,text5,fontsize=12,ha='center', va='center', transform=ax_ts.transAxes) #DMvsSNR plot ax_dmsnr.plot(Dedisp_dmsnr,dms,color="red",lw=2) ax_dmsnr.plot(off_dmsnr,dms,color="grey",alpha=0.5,lw=1) Dedisp_dmsnr_split = np.array_split(Dedisp_dmsnr,5) #Sub-array could be different sizes that's why Dedisp_dmsnr_split[0]=Dedisp_dmsnr_split[0].sum() Dedisp_dmsnr_split[1]=Dedisp_dmsnr_split[1].sum() Dedisp_dmsnr_split[2]=Dedisp_dmsnr_split[2].sum() Dedisp_dmsnr_split[3]=Dedisp_dmsnr_split[3].sum() Dedisp_dmsnr_split[4]=Dedisp_dmsnr_split[4].sum() #Plot settings plt.setp(ax_spec.get_xticklabels(), visible = True) plt.setp(ax_dmsnr.get_xticklabels(), visible = False) plt.setp(ax_spec.get_yticklabels(), visible = False) plt.setp(ax_dmsnr.get_yticklabels(), visible = False) ax_spec.set_ylim([data.freqs.min(),data.freqs.max()]) ax_dmsnr.set_ylim(lodm,hidm) #Plot original data data.dedisperse(0,padval='rotate') ax_im.set_ylabel("Frequency (MHz)") ax_orig.set_ylabel("Frequency (MHz)") ax_orig.set_xlabel("Time (sec)") FTdirection = source_name.split("_")[0] if FTdirection == 'nT': ndata = data.data[...,::-1] print "Will be flipped in Time" elif FTdirection == 'nF': ndata = data.data[::-1,...] print "Will be flipped in freq" elif FTdirection == 'nTnF': ndata = data.data[::-1,::-1] print "Will be flipped in time and freq" else: ndata = data.data print "No flip" # Sweeping it up for ii, sweep_dm in enumerate(sweep_dms): ddm = sweep_dm-data.dm #delays = psr_utils.delay_from_DM(ddm, data.freqs) #delays -= delays.min() if sweep_posns is None: sweep_posn = 0.0 elif len(sweep_posns) == 1: sweep_posn = sweep_posns[0] else: sweep_posn = sweep_posns[ii] sweepstart = data.dt*data.numspectra*sweep_posn+data.starttime #sweepstart = data.dt*data.numspectra + data.starttime sty="b-" if FTdirection == 'nT': ddm = (-1)*ddm # Negative DM nfreqs = data.freqs elif FTdirection == 'nF': nfreqs = data.freqs[::-1] elif FTdirection == 'nTnF': ddm = (-1)*ddm # Negative DM nfreqs = data.freqs[::-1] else: nfreqs = data.freqs delays = psr_utils.delay_from_DM(ddm, data.freqs) delays -= delays.min() ndelay = (delays+sweepstart) ndelay2 = (delays+sweepstart+duration) ax_orig.set_xlim(data.starttime, data.starttime + len(data.data[0])*data.dt) ax_orig.set_ylim(data.freqs.min(),data.freqs.max()) ax_orig.plot(ndelay, nfreqs, "b-", lw=2, alpha=0.7) ax_orig.plot(ndelay2, nfreqs, "b-", lw=2, alpha=0.7) #Orig ax_orig.imshow(ndata, aspect='auto', \ cmap=matplotlib.cm.cmap_d[cmap_str], \ interpolation='nearest', origin='upper', \ extent=(data.starttime, data.starttime + len(data.data[0])*data.dt, \ data.freqs.min(), data.freqs.max())) ''' ax_orig.imshow(ndata, aspect='auto', \ cmap=matplotlib.cm.cmap_d[cmap_str], \ interpolation='nearest', origin='lower', \ extent=(data.starttime, data.starttime + len(data.data[0])*data.dt, \ data.freqs.min(), data.freqs.max())) ''' #if interactive: # fig.suptitle("Frequency vs. Time") # fig.canvas.mpl_connect('key_press_event', \ # lambda ev: (ev.key in ('q','Q') and plt.close(fig))) #oname = "%.3f_%s.png" % (start,str(dm)) if ofile is "unknown_cand": ofile = ofile + "_%.3f_%s.png" % (start,str(dm)) ttestTrsh1=3 ttestTrsh2=1 probTrsh1=0.5 probTrsh2=0.05 DMleft = Dedisp_dmsnr_split[0] DMcent = Dedisp_dmsnr_split[2] DMright = Dedisp_dmsnr_split[4] if prob >= probTrsh2: ofile = "A_" + ofile plt.text(1.1,0.2,"cat: A",fontsize=12,ha='center', va='center', transform=ax_ts.transAxes) elif ttest > ttestTrsh1 and DMcent > DMleft and DMcent > DMright: ofile = "B_" + ofile plt.text(1.1,0.2,"cat: B",fontsize=12,ha='center', va='center', transform=ax_ts.transAxes) elif ttest > ttestTrsh2 and DMcent > DMleft and DMcent > DMright: plt.text(1.1,0.2,"cat: C",fontsize=12,ha='center', va='center', transform=ax_ts.transAxes) ofile = "C_" + ofile plt.savefig(ofile) #plt.show() return ofile,ttest,ttestprob
def sample_training_data(index): c = classes[np.squeeze(train_set_y[:, index])].decode("utf-8") plt.imshow(train_set_x_orig[index]) plt.text(2,10, c, fontsize=24)
data_types.append(inp_name) assert len(ind) == len(x_labels) # print(colors) # print(legend_colors) if len(sys.argv) > 2: plt.ylim(0, int(sys.argv[2])) _, top_ylim = plt.ylim() # label UH for r in plts[-1]: h = r.get_height() if h > top_ylim / 2: print("h {} over limit".format(h)) plt.text(r.get_x() + r.get_width() / 2., top_ylim * .8, "{:.2e}".format(h), ha="center", va="center", color="white", fontsize=10, fontweight="bold") plt.xticks(ind, x_labels) plt.legend(data_types) # print(indir) exp_name = indir[:-1].split('/')[-1] # print(exp_name) plt.savefig(exp_name + '.png') # plt.savefig('out.png')
number_id = number_id + 1 draw_x = [build['x'], build['x']] draw_y = [build['y'], build['y'] - 20] ax.add_patch( plt.Rectangle((build['x'] - 30, build['y'] - 80), 60, 60, edgecolor='black', facecolor='none')) plt.plot(draw_x, draw_y, color='blue', linewidth=1, alpha=0.2) plt.text(build['x'], build['y'] - 50, chr(ord('@') + number_id), horizontalalignment='center', verticalalignment='center', fontsize=15, color='blue') node_positions = { node[0]: (node[1]['x'], node[1]['y']) for node in g.nodes(data=True) } label_positions = { node[0]: (node[1]['x'] - 15, node[1]['y'] + 10) for node in g.nodes(data=True) } node_labels = {} for node in g.nodes(data=True):
def train_visualization(net, losses, testX, testY): ''' Generation of training plots 1: training loss over time 2: Correlation matrix (of test predictions) 3: Plot scattering of test predictions Attributes ------- net Trained network losses Collected losses at each epoch testX Test dataset testY Test targets ''' # Plot losses plt.figure() plt.plot(range(1,len(losses)+1), losses, 'k--') plt.xlabel('Epoch') plt.ylabel('Loss') plt.grid() loss_fname = 'fig/loss.png' print(f'Plot of training loss saved under {loss_fname}') plt.savefig(loss_fname) # Get test predictions for future plots pred = net(testX).argmax(1) b00 = (pred == 0) & (testY.argmax(1) == 0) # Predicted as 0 and true class is 0 b01 = (pred == 0) & (testY.argmax(1) == 1) # Predicted as 0 and true class is 1 b10 = (pred == 1) & (testY.argmax(1) == 0) # Predicted as 1 and true class is 0 b11 = (pred == 1) & (testY.argmax(1) == 1) # Predicted as 1 and true class is 1 # Plot confusion matrix plt.figure() conf = torch.tensor([[b00.sum(), b10.sum()], [b01.sum(), b11.sum()]]) plt.matshow(conf, cmap='tab10') for i in range(conf.shape[0]): for j in range(conf.shape[1]): plt.text(i, j, '{:d}'.format(conf[i,j]), ha='center', va='center') plt.xlabel('True Class') plt.ylabel('Predicted class') #plt.show() conf_fname = 'fig/confmat.png' print(f'Plot of confusion matrix saved under {conf_fname}') plt.savefig(conf_fname) # Plotting scattering of correctly (resp. falsely) predicted values plt.figure() correct0 = torch.nonzero(b00==True) correct1 = torch.nonzero(b11==True) plt.scatter(testX[correct0,0], testX[correct0, 1], c='r', alpha=0.2, label='class 0') plt.scatter(testX[correct1,0], testX[correct1, 1], c='b', alpha=0.2, label='class 1') errors = torch.cat((torch.nonzero(b01==True), torch.nonzero(b10 == True)), dim=0) plt.scatter(testX[errors,0], testX[errors, 1], c='k', label='errors') plt.legend() #plt.show() pred_fname = 'fig/predictions.png' print(f'Plot of test predictions saved under {pred_fname}') plt.savefig(pred_fname)
'complete', 'errors', lkl_method] for i, c in enumerate(cols): print("{}: {}, {}".format(c, times_norm_old[i], times_norm_new[i])) fig = plt.figure(figsize=(20, 10)) plt.title("N={}, t={:.2f} -> [{:.0f} m/s]".format( N_tot, t_old, N_tot / t_old), fontsize=18) plt.grid(zorder=0) plt.bar(cols, times_norm_old, zorder=4) # Text annotations x, y = np.arange(len(times_norm_old)), np.round(times_norm_old, 2) up = max(y) * .03 plt.ylim(0, max(y) + 4 * up) for xi, yi, l in zip(*[x, y, list(map(str, y))]): plt.text(xi - len(l) * .02, yi + up, l, fontsize=18, bbox=dict(facecolor='w', edgecolor='w', alpha=.5)) plt.ylabel("% of time used", fontsize=18) plt.xticks(rotation=45, fontsize=18) plt.yticks(fontsize=18) fig.tight_layout() plt.savefig("perf_test_old.png", dpi=150) fig = plt.figure(figsize=(20, 10)) plt.title("N={}, t={:.2f} -> [{:.0f} m/s]".format( N_tot, t_new, N_tot / t_new), fontsize=18) plt.grid(zorder=0) plt.bar(cols, times_norm_new, zorder=4) # Text annotations x, y = np.arange(len(times_norm_new)), np.round(times_norm_new, 2) up = max(y) * .03 plt.ylim(0, max(y) + 4 * up)
def plot_solution(start, end, solution, show_work=3): print(start, end, solution) #plt.legend() plt.clf() plt.axis(x_axis + y_axis) #plt.grid(True, xdata=[5]) radius = solution[0][2] plt.text( -15, -23, 'start: (%d, %d, %d)' % (start[0], start[1], (90 - math.degrees(start[2])) % 360)) plt.text( -15, -24, 'end: (%d, %d, %d)' % (end[0], end[1], (90 - math.degrees(end[2])) % 360)) plt.text(-15, -25, 'radius: %d' % (radius)) plt.text(-5, +23, 'Dubins path', multialignment='center', size=20) if False: plt.text( +5, -23, 'solution: %s %0.1f, %s %0.1f, %s %0.1f' % ( solution[0][0], solution[0][1], solution[1][0], solution[1][1], solution[2][0], solution[2][1], )) else: plt.text( +5, -23, 'solution: %s %s %s (%0.1f, %0.1f, %0.1f)' % ( solution[0][0], solution[1][0], solution[2][0], solution[0][1], solution[1][1], solution[2][1], )) plot_arrow(*start, color='green') plot_arrow(*end, color='red') ax = fig.add_subplot(111) current_position = start #plt.plot((start[0], end[0]), (start[1], end[1])) #plot_arrow(0.0,0.0,0) (sx, sy, syaw) = start (ex, ey, eyaw) = end ex = ex - sx ey = ey - sy lex = math.cos(syaw) * ex + math.sin(syaw) * ey ley = -math.sin(syaw) * ex + math.cos(syaw) * ey leyaw = eyaw - syaw px = [0] py = [0] pyaw = [0] for (mode, length, radius) in solution: print('mode:', mode, 'length:', length, 'radius:', radius) if mode is 'L': print('left') center = ( current_position[0] + math.cos(current_position[2] + math.pi / 2.0) * radius, current_position[1] + math.sin(current_position[2] + math.pi / 2.0) * radius, ) #plt.gca().add_artist(plt.Circle(center, radius, fill=False)) #arcs = [matplotlib.patches.Arc(xy=center, width=radius, height=radius, angle=0, theta1=0, theta2=0)] #arcs = [matplotlib.patches.Arc(xy=center, width=radius * 2, height=radius * 2, angle=0, theta1=0, theta2=360)] circumference = math.pi * radius theta1 = math.degrees(current_position[2]) - 90 theta2 = theta1 + (180 * length / circumference) if show_work == 1: ax.add_artist( matplotlib.patches.Arc( xy=center, width=radius * 2, height=radius * 2, #angle=0, #theta1=theta1, #theta2=theta2, color='purple', linewidth=linewidth, )) elif show_work == 2: ax.add_artist( matplotlib.patches.Arc( xy=center, width=radius * 2, height=radius * 2, angle=0, theta1=theta1, theta2=theta2, color='purple', linewidth=linewidth, )) new_position = (center[0] + math.cos(math.radians(theta2)) * radius, center[1] + math.sin(math.radians(theta2)) * radius, math.radians(theta2 + 90)) elif mode is 'l': print('left, reverse') center = ( current_position[0] + math.cos(current_position[2] + math.pi / 2.0) * radius, current_position[1] + math.sin(current_position[2] + math.pi / 2.0) * radius, ) #plt.gca().add_artist(plt.Circle(center, radius, fill=False)) #arcs = [matplotlib.patches.Arc(xy=center, width=radius, height=radius, angle=0, theta1=0, theta2=0)] #arcs = [matplotlib.patches.Arc(xy=center, width=radius * 2, height=radius * 2, angle=0, theta1=0, theta2=360)] circumference = math.pi * radius theta1 = math.degrees(current_position[2]) - 90 theta2 = theta1 + (180 * length / circumference) if show_work == 1: ax.add_artist( matplotlib.patches.Arc( xy=center, width=radius * 2, height=radius * 2, #angle=0, #theta1=theta1, #theta2=theta2, color='purple', linewidth=linewidth, )) elif show_work == 2: ax.add_artist( matplotlib.patches.Arc( xy=center, width=radius * 2, height=radius * 2, angle=0, theta1=theta1, theta2=theta2, color='purple', linewidth=linewidth, )) new_position = (center[0] + math.cos(math.radians(theta2)) * radius, center[1] + math.sin(math.radians(theta2)) * radius, math.radians(theta2 + 90)) elif mode is 'R': print('right') center = ( current_position[0] + math.cos(current_position[2] - math.pi / 2.0) * radius, current_position[1] + math.sin(current_position[2] - math.pi / 2.0) * radius, ) circumference = math.pi * radius theta2 = math.degrees(current_position[2]) + 90 theta1 = theta2 - (180 * length / circumference) if show_work == 1: ax.add_artist( matplotlib.patches.Arc( xy=center, width=radius * 2, height=radius * 2, #angle=0, #theta1=theta1, #theta2=theta2, color='blue', linewidth=linewidth, )) elif show_work == 2: ax.add_artist( matplotlib.patches.Arc( xy=center, width=radius * 2, height=radius * 2, angle=0, theta1=theta1, theta2=theta2, color='blue', linewidth=linewidth, )) new_position = (center[0] + math.cos(math.radians(theta1)) * radius, center[1] + math.sin(math.radians(theta1)) * radius, math.radians(theta1 - 90)) elif mode is 'r': print('right, reverse') center = ( current_position[0] + math.cos(current_position[2] - math.pi / 2.0) * radius, current_position[1] + math.sin(current_position[2] - math.pi / 2.0) * radius, ) circumference = math.pi * radius #theta2 = math.degrees(current_position[2])+90 #theta1 = theta2 - (180 *length/circumference) theta1 = math.degrees(current_position[2]) + 90 theta2 = theta1 + (180 * length / circumference) #theta1 = math.degrees(current_position[2])+90 #theta2 = theta1 - (180 * length/circumference) if show_work == 1: ax.add_artist( matplotlib.patches.Arc( xy=center, width=radius * 2, height=radius * 2, #angle=0, #theta1=theta1, #theta2=theta2, color='orange', linewidth=linewidth, )) elif show_work == 2: ax.add_artist( matplotlib.patches.Arc( xy=center, width=radius * 2, height=radius * 2, angle=0, theta1=theta1, theta2=theta2, color='orange', linewidth=linewidth, )) new_position = (center[0] + math.cos(math.radians(theta1)) * radius, center[1] + math.sin(math.radians(theta1)) * radius, math.radians(theta1 - 90)) elif mode is 'S': print('straight') new_position = ( current_position[0] + math.cos(current_position[2]) * length, current_position[1] + math.sin(current_position[2]) * length, current_position[2], ) if show_work == 2: plt.plot( (current_position[0], new_position[0]), (current_position[1], new_position[1]), color='yellow', linewidth=linewidth, ) else: raise Exception, 'unknown mode: %s' % (mode) current_position = new_position plt.ion() plt.draw() plt.pause(0.001)
def evaluate(model, val_loader): model.eval() outputs = [step(model, batch) for batch in val_loader] Preds = [x['preds'] for x in outputs] Labels = [x['labels'] for x in outputs] Outs = [x['out'] for x in outputs] Preds = torch.cat(Preds, dim=0).cpu() Labels = torch.cat(Labels, dim=0).cpu() Outs = torch.cat(Outs, dim=0).cpu() Scores = F.softmax(Outs, dim=1) print(Preds, Labels, Scores) print(Preds.size(), len(Labels), Scores.size()) print('General Evaluation') # Precision | Recall | F1 - score | AUC acc_all = accuracy_score(Labels, Preds) ap_all = precision_score(Labels, Preds, average='macro') ar_all = recall_score(Labels, Preds, average='macro') f1_all = f1_score(Labels, Preds, average='macro') print(acc_all, ap_all, ar_all, f1_all) y_pred = [] y_true = [] for i in range(1900): if Preds[i] == 0: y_pred.append('AMD') elif Preds[i] == 1: y_pred.append('DME') elif Preds[i] == 2: y_pred.append('NM') elif Preds[i] == 3: y_pred.append('PCV') elif Preds[i] == 4: y_pred.append('PM') if Labels[i] == 0: y_true.append('AMD') elif Labels[i] == 1: y_true.append('DME') elif Labels[i] == 2: y_true.append('NM') elif Labels[i] == 3: y_true.append('PCV') elif Labels[i] == 4: y_true.append('PM') t1 = classification_report(y_true, y_pred, target_names=['AMD', 'DME', 'NM', 'PCV', 'PM']) t2 = classification_report(y_true, y_pred, output_dict=True, target_names=['AMD', 'DME', 'NM', 'PCV', 'PM']) print(t1) print(t2) # draw confuse matrix classes = ['AMD', 'DME', 'NM', 'PCV', 'PM'] cm = confusion_matrix(y_true, y_pred) fig, ax = plt.subplots() plt.imshow(cm, cmap=plt.cm.Greens) indices = range(len(cm)) plt.xticks(indices, classes) plt.yticks(indices, classes) plt.colorbar() plt.xlabel('Pred') plt.ylabel('True') for first_index in range(len(cm)): for second_index in range(len(cm[first_index])): plt.text(first_index, second_index, cm[first_index][second_index]) fig.savefig("./img/{}/Best-cm-img{}.png".format(args.model, args.bsize), dpi=320, format='png') # cal auc roc_ovr = roc_auc_score(Labels, Scores, multi_class='ovr') print('--roc-ovr:', roc_ovr) roc_ovo = roc_auc_score(Labels, Scores, multi_class='ovo') print('--roc-ovo:', roc_ovo)