def plot_pulsar_obliquity(): from numpy import linspace, array, arange from pylab import rc, plot, xlim, xticks, xlabel, ylabel import matplotlib.pyplot as plt import mpmath rc('text', usetex=True) fig = plt.figure() # Initial params. rot1 = 1.76e-4 rot2 = 1256. params = {'m2': 1.4 * 1.98892e30,'r2':20000.0,'rot2':rot2,\ 'r1':70000.e3/2,'rot1':rot1,'i_a':.5,'ht':0.,\ 'a':600000.e3,'e':.1,'i':0.8,'h':2} # Set parameters. sp.parameters = params # Period. period = sp.wp_period ob = sp.obliquity_time lspace = linspace(0,5*period,250) xlim(0,float(lspace[-1])) xlabel(r'$\textnormal{Time (years)}$') ylabel(r'$\textnormal{Obliquity }\left( ^\circ \right)$') ob_series = array([ob(t).real*(360/(2*mpmath.pi())) for t in lspace]) plot(lspace,ob_series,'k-',linewidth=2) xticks(arange(xlim()[0],xlim()[1],365*24*3600*20),[r'$'+str(int(_)*20)+r'$' for _ in [t[0] for t in enumerate(arange(xlim()[0],xlim()[1],365*24*3600*20))]]) return params = {'m2': 1.4 * 1.98892e30,'r2':20000.0,'rot2':rot2,\ 'r1':15000.e3/2,'rot1':rot1,'i_a':.5,'ht':0.,\ 'a':600000.e3,'e':.1,'i':0.8,'h':2} # Set parameters. sp.parameters = params # Period. ob = sp.obliquity_time ob_series2 = array([ob(t).real*(360/(2*mpmath.pi())) for t in lspace]) plot(lspace,ob_series-ob_series2,'k-',linewidth=2)
def plot_embedding(X, title=None): x_min, x_max = np.min(X, 0), np.max(X, 0) X = (X - x_min) / (x_max - x_min) pl.figure() ax = pl.subplot(111) for i in range(digits.data.shape[0]): pl.text( X[i, 0], X[i, 1], str(digits.target[i]), color=pl.cm.Set1(digits.target[i] / 10.0), fontdict={"weight": "bold", "size": 9}, ) if hasattr(offsetbox, "AnnotationBbox"): # only print thumbnails with matplotlib > 1.0 shown_images = np.array([[1.0, 1.0]]) # just something big for i in range(digits.data.shape[0]): dist = np.sum((X[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, [X[i]]] imagebox = offsetbox.AnnotationBbox(offsetbox.OffsetImage(digits.images[i], cmap=pl.cm.gray_r), X[i]) ax.add_artist(imagebox) pl.xticks([]), pl.yticks([]) if title is not None: pl.title(title)
def plot_Barycenter(dataset_name, feat, unfeat, repo): if dataset_name==MNIST: _, _, test=get_data(dataset_name, repo, labels=True) xtest1,_,_, labels,_=test else: _, _, test=get_data(dataset_name, repo, labels=False) xtest1,_,_ =test labels=np.zeros((len(xtest1),)) # get labels def bary_wdl2(index): return _bary_wdl2(index, xtest1, feat, unfeat) n=xtest1.shape[-1] num_class = (int)(max(labels)+1) barys=[bary_wdl2(np.where(labels==i)) for i in range(num_class)] pl.figure(1, (num_class, 1)) for i in range(num_class): pl.subplot(1,10,1+i) pl.imshow(barys[i][0,0,:,:],cmap='Blues',interpolation='nearest') pl.xticks(()) pl.yticks(()) if i==0: pl.ylabel('DWE Bary.') if num_class >1: pl.title('{}'.format(i)) pl.tight_layout(pad=0,h_pad=-2,w_pad=-2) pl.savefig("imgs/{}_dwe_bary.pdf".format(dataset_name))
def plot_earth_moon_obliquity(): from numpy import linspace, array, arange from pylab import rc, plot, xlim, xticks, xlabel, ylabel import matplotlib.pyplot as plt import mpmath rc('text', usetex=True) fig = plt.figure() # Initial params. #params = {'m2':1.9e27,'r2':70000.e3,'rot2':0.00017585125448028,\ # 'r1':1,'rot1':6.28*100,'i_a':0.5,'ht': 0,\ # 'a':421700E3 / 4,'e':0.01,'i':0.8,'h':2} rot1 = 7.978282018665196e-08 rot2 = 2.972e-06 params = {'m2':1.98892e30,'r2':695500000.0,'rot2':rot2,\ 'r1':384748.e3,'rot1':rot1,'i_a':10. * 2*mpmath.pi()/360,'ht': 0,\ 'a':149597870700. * 0.1,'e':0.017,'i':7. * 2*mpmath.pi()/360,'h':mpmath.pi()/2} # Set parameters. sp.parameters = params # Period. period = sp.wp_period ob = sp.obliquity_time lspace = linspace(0,5*period,250) xlim(0,float(lspace[-1])) xlabel(r'$\textnormal{Time (Ma)}$') ylabel(r'$\textnormal{Obliquity }\left( ^\circ \right)$') ob_series = array([ob(t).real*(360/(2*mpmath.pi())) for t in lspace]) plot(lspace,ob_series,'k-',linewidth=2) xticks(arange(xlim()[0],xlim()[1],365*24*3600*100000000),[r'$'+str(int(_)*100)+r'$' for _ in [t[0] for t in enumerate(arange(xlim()[0],xlim()[1],365*24*3600*100000000))]])
def createPlot(dataY, dataX, ticksX, annotations, axisY, axisX, dostep, doannotate): if not ticksX: ticksX = dataX if dostep: py.step(dataX, dataY, where='post', linestyle='-', label=axisY) # where=post steps after point else: py.plot(dataX, dataY, marker='o', ms=5.0, linestyle='-', label=axisY) if annotations and doannotate: for note, x, y in zip(annotations, dataX, dataY): py.annotate(note, (x, y), xytext=(2,2), xycoords='data', textcoords='offset points') py.xticks(np.arange(1, len(dataX)+1), ticksX, horizontalalignment='left', rotation=30) leg = py.legend() leg.draggable() py.xlabel(axisX) py.ylabel('time (s)') # Set X axis tick labels as rungs #print zip(dataX, dataY) py.draw() py.show() return
def fixup_gauge(current_data): import pylab size = 34 pylab.title("Pressure at Gauge 0", fontsize=size) pylab.xticks([1.0, 1.5, 2.0, 2.5, 3.0], fontsize=size) pylab.yticks([-0.3, -0.1, 0.1, 0.3, 0.5], fontsize=size)
def getOptCandGamma(cv_train, cv_label): print "Finding optimal C and gamma for SVM with RBF Kernel" C_range = 10.0 ** np.arange(-2, 9) gamma_range = 10.0 ** np.arange(-5, 4) param_grid = dict(gamma=gamma_range, C=C_range) cv = StratifiedKFold(y=cv_label, n_folds=40) # Use the svm.SVC() as the cost function to evaluate parameter choices # NOTE: Perhaps we should run computations in parallel if needed. Does it # do that already within the class? grid = GridSearchCV(svm.SVC(), param_grid=param_grid, cv=cv) grid.fit(cv_train, cv_label) score_dict = grid.grid_scores_ scores = [x[1] for x in score_dict] scores = np.array(scores).reshape(len(C_range), len(gamma_range)) pl.figure(figsize=(8,6)) pl.subplots_adjust(left=0.05, right=0.95, bottom=0.15, top=0.95) pl.imshow(scores, interpolation='nearest', cmap=pl.cm.spectral) pl.xlabel('gamma') pl.ylabel('C') pl.colorbar() pl.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45) pl.yticks(np.arange(len(C_range)), C_range) pl.show() print "The best classifier is: ", grid.best_estimator_
def rmsdSpreadSubplot(multiplier=1.0, layout=(-1, -1)): rmsd_data = dict( (e, rad_data[e]['innov'][quant]) for e in rad_data.iterkeys() ) spread_data = dict( (e, rad_data[e]['spread'][quant]) for e in rad_data.iterkeys() ) times = temp.getTimes() n_t = len(times) for exp, exp_name in exp_names.iteritems(): pylab.plot(sawtooth(times, times)[:(n_t + 1)], rmsd_data[exp][:(n_t + 1)], color=colors[exp], linestyle='-') pylab.plot(times[(n_t / 2):], rmsd_data[exp][n_t::2], color=colors[exp], linestyle='-') for exp, exp_name in exp_names.iteritems(): pylab.plot(sawtooth(times, times)[:(n_t + 1)], spread_data[exp][:(n_t + 1)], color=colors[exp], linestyle='--') pylab.plot(times[(n_t / 2):], spread_data[exp][n_t::2], color=colors[exp], linestyle='--') ylim = pylab.ylim() pylab.plot(times, -1 * np.ones((len(times),)), color='#999999', linestyle='-', label="RMS Innovation") pylab.plot(times, -1 * np.ones((len(times),)), color='#999999', linestyle='--', label="Spread") pylab.axhline(y=7, color='k', linestyle=':') pylab.axvline(x=14400, color='k', linestyle=':') pylab.ylabel("RMS Innovation/Spread (dBZ)", size='large') pylab.xlim(times[0], times[-1]) pylab.ylim(ylim) pylab.legend(loc=4) pylab.xticks(times[::2], [ "" for t in times[::2] ]) pylab.yticks(size='x-large') return
def plot_cost(self): if self.show_cost not in self.train_outputs[0][0]: raise ShowNetError("Cost function with name '%s' not defined by given convnet." % self.show_cost) train_errors = [o[0][self.show_cost][self.cost_idx] for o in self.train_outputs] test_errors = [o[0][self.show_cost][self.cost_idx] for o in self.test_outputs] numbatches = len(self.train_batch_range) test_errors = numpy.row_stack(test_errors) test_errors = numpy.tile(test_errors, (1, self.testing_freq)) test_errors = list(test_errors.flatten()) test_errors += [test_errors[-1]] * max(0,len(train_errors) - len(test_errors)) test_errors = test_errors[:len(train_errors)] numepochs = len(train_errors) / float(numbatches) pl.figure(1) x = range(0, len(train_errors)) pl.plot(x, train_errors, 'k-', label='Training set') pl.plot(x, test_errors, 'r-', label='Test set') pl.legend() ticklocs = range(numbatches, len(train_errors) - len(train_errors) % numbatches + 1, numbatches) epoch_label_gran = int(ceil(numepochs / 20.)) # aim for about 20 labels epoch_label_gran = int(ceil(float(epoch_label_gran) / 10) * 10) # but round to nearest 10 ticklabels = map(lambda x: str((x[1] / numbatches)) if x[0] % epoch_label_gran == epoch_label_gran-1 else '', enumerate(ticklocs)) pl.xticks(ticklocs, ticklabels) pl.xlabel('Epoch') # pl.ylabel(self.show_cost) pl.title(self.show_cost)
def prob_dispersion(target, result, prob, nclasses=5, save_as=''): classprob = [[] for i in 2 * range(nclasses)] for i in xrange(len(result)): p = prob[i][result[i]] if result[i] == target[i]: classprob[2*result[i]].append(p) else: classprob[2*result[i] + 1].append(p) xlabels = [[str(i+1) + "-Good", str(i+1) + "-Bad"] for i in range(nclasses)] xlabels = reduce(list.__add__, xlabels, []) # Plotting the result fig = plt.figure() fig.suptitle('Probability distribution' , fontsize=20) plot = fig.add_subplot(111) pylab.boxplot(classprob) pylab.xticks(range(1, 2 * nclasses + 1), xlabels) plot.set_xlabel('Predicted' , fontsize = 16) plot.set_ylabel('Probabilities' , fontsize = 16) plot.tick_params(axis='both', which='major', labelsize=14) plot.tick_params(axis='both', which='minor', labelsize=8) # Save options if save_as =='': plt.show() else : fig.savefig(save_as)
def plotMatrix(matrix, color_scheme, row_headers, col_headers, vmin, vmax, options): pylab.imshow(matrix, cmap=color_scheme, origin='lower', vmax=vmax, vmin=vmin, interpolation='nearest') # offset=0: x=center,y=center # offset=0.5: y=top/x=right offset = 0.0 if options.xticks: pylab.xticks([offset + x for x in range(len(options.xticks))], options.xticks, rotation="vertical", fontsize="8") else: if col_headers and len(col_headers) < 100: pylab.xticks([offset + x for x in range(len(col_headers))], col_headers, rotation="vertical", fontsize="8") if options.yticks: pylab.yticks([offset + y for y in range(len(options.yticks))], options.yticks, fontsize="8") else: if row_headers and len(row_headers) < 100: pylab.yticks([offset + y for y in range(len(row_headers))], row_headers, fontsize="8")
def plotCoeff(X, y, obj, featureNames, whichReg): """ Plot Regression's Coeff """ clf = classifiers[whichReg] clf,_,_ = fitAlgo(clf, X,y, opt= True, param_dict = param_dist_dict[whichReg]) if whichReg == "LogisticRegression": coeff = np.absolute(clf.coef_[0]) else: coeff = np.absolute(clf.coef_) print coeff indices = np.argsort(coeff)[::-1] print indices print featureNames featureList = [] # num_features = len(featureNames) print("Feature ranking:") for f in range(num_features): featureList.append(featureNames[indices[f]]) print("%d. feature %s (%.2f)" % (f, featureNames[indices[f]], coeff[indices[f]])) fig = pl.figure(figsize=(8,6),dpi=150) pl.title("Feature importances",fontsize=30) # pl.bar(range(num_features), coeff[indices], # yerr = std_importance[indices], color=paired[0], align="center", # edgecolor=paired[0],ecolor=paired[1]) pl.bar(range(num_features), coeff[indices], color=paired[0], align="center", edgecolor=paired[0],ecolor=paired[1]) pl.xticks(range(num_features), featureList, size=15,rotation=90) pl.ylabel("Importance",size=30) pl.yticks(size=20) pl.xlim([-1, num_features]) # fix_axes() pl.tight_layout() save_path = 'plots/'+obj+'/'+whichReg+'_feature_importances.pdf' fig.savefig(save_path)
def plot(self, filesuffix=('.png',)): pylab.figure() kPluses = 10**numpy.linspace(0, 3, 100) kMinuses = 10**numpy.linspace(6, 9, 100) figureOfMerits = numpy.zeros((len(kPluses), len(kMinuses), 4), 'd') for i, kPlus in enumerate(kPluses): for j, kMinus in enumerate(kMinuses): figureOfMerits[i, j, :] = self.figureOfMerit(self.generateData({'kPlus' : kPlus, 'kMinus' : kMinus})) data = self.generateData({'kPlus' : kPluses[0], 'kMinus' : kMinuses[0]}) self._contourf(kMinuses, kPluses, figureOfMerits, data) pylab.xticks((10**6, 10**7, 10**8, 10**9), fontsize=self.fontsize) pylab.yticks((10**0, 10**1, 10**2, 10**3), fontsize=self.fontsize) pylab.xlabel(r'$k^-$ $\left(1\per\metre\right)$', fontsize=self.fontsize) pylab.ylabel(r'$k^+$ $\left(\power{\metre}{3}\per\mole\cdot\second\right)$', fontsize=self.fontsize) pylab.text(2 * 10**6, 7 * 10**2, r'I', fontsize=self.fontsize) pylab.text(3 * 10**7, 7 * 10**2, r'II', fontsize=self.fontsize) pylab.text(6 * 10**8, 7 * 10**2, r'III', fontsize=self.fontsize) pylab.text(6 * 10**8, 7 * 10**1, r'IV', fontsize=self.fontsize) for fP, kPlus, paxeslabel in ((1.143,3.51e+00, False), (0.975, 9.33e+00, False), (0.916, 3.51e+01, False), (0.89, 9.33e+01, False), (0.87, 3.51e+02, True)): for fM, kMinus, maxeslabel in ((1.4, 2.48e+06, False), (1.07, 7.05e+6, False), (0.96, 2.48e+07, False), (0.91, 7.05e+7, False), (0.88, 2.48e+08, True)): xpos = (numpy.log10(kMinus) - 6.) / 3. * fM ypos = numpy.log10(kPlus) / 3. * fP self.makeBackGroundPlot({'kPlus' : kPlus, 'kMinus' : kMinus}, xpos, ypos, axeslabel=paxeslabel and maxeslabel) for fs in filesuffix: pylab.savefig('kPlusVkMinus' + fs) pylab.close('all')
def plot_importances(imp, clfName, obj): imp=np.vstack(imp) print imp mean_importance = np.mean(imp,axis=0) std_importance = np.std(imp,axis=0) indices = np.argsort(mean_importance)[::-1] print indices print featureNames featureList = [] # num_features = len(featureNames) print("Feature ranking:") for f in range(num_features): featureList.append(featureNames[indices[f]]) print("%d. feature %s (%.2f)" % (f, featureNames[indices[f]], mean_importance[indices[f]])) fig = pl.figure(figsize=(8,6),dpi=150) pl.title("Feature importances",fontsize=30) pl.bar(range(num_features), mean_importance[indices], yerr = std_importance[indices], color=paired[0], align="center", edgecolor=paired[0],ecolor=paired[1]) pl.xticks(range(num_features), featureList, size=15,rotation=90) pl.ylabel("Importance",size=30) pl.yticks(size=20) pl.xlim([-1, num_features]) # fix_axes() pl.tight_layout() save_path = 'plots/'+obj+'/'+clfName+'_feature_importances.pdf' fig.savefig(save_path)
def param_sweeping(clf, obj, X, y, param_dist, metric, param, clfName): '''Plot a parameter sweeping (ps) curve with the param_dist as a axis, and the scoring based on metric as y axis. Keyword arguments: clf - - classifier X - - feature matrix y - - target array param - - a parameter of the classifier param_dist - - the parameter distribution of param clfName - - the name of the classifier metric - - the metric we use to evaluate the performance of the classifiers obj - - the name of the dataset we are using''' scores = [] for i in param_dist: y_true = [] y_pred = [] # new classifer each iteration newclf = eval("clf.set_params("+ param + "= i)") y_pred, y_true, gs_score_list, amp = testAlgo(newclf, X, y, clfName) mean_fpr, mean_tpr, mean_auc = plot_unit_prep(y_pred, y_true, metric) scores.append(mean_auc) print("Area under the ROC curve : %f" % mean_auc) fig = pl.figure(figsize=(8,6),dpi=150) paramdist_len = len(param_dist) pl.plot(range(paramdist_len), scores, label = 'Parameter Sweeping Curve') pl.xticks(range(paramdist_len), param_dist, size = 15, rotation = 45) pl.xlabel(param.upper(),fontsize=30) pl.ylabel(metric.upper(),fontsize=30) pl.title('Parameter Sweeping Curve',fontsize=25) pl.legend(loc='lower right') pl.tight_layout() fig.savefig('plots/'+obj+'/'+ clfName +'_' + param +'_'+'ps.pdf') pl.show()
def task1(): ''' Task 1 Generate, transform and plot gaussian data ''' X = generate_data(100) X2 = scale_data(X) X3 = standardise_data(X) # Plot data # Your code here # Hint: Use the functions pl.scatter(x[0,:],[1,:],c='r'), pl.hold(True), # pl.legend, pl.title, pl.xlabel, pl.ylabel fig = pl.figure() ax1 = fig.add_subplot(111) print ax1.scatter(X[0], X[1], c='y', label='Raw data') ax1.scatter(X2[0], X2[1], c='r', label='Scaled data') ax1.scatter(X3[0], X3[1], c='b', label='Standardised data') pl.title('Simple transformations of Gaussian Data') pl.xlabel('x') pl.ylabel('y') pl.xticks(range(-6,10,2)) pl.yticks(range(-4,5,1)) # ax1.title('Simple transformations of Gaussian Data') ???? -> does not work ax1.legend(scatterpoints=1) pl.savefig('task_1_340528_341455.pdf')
def makeboxplot(filteredclusts, dblibrary, figname, pool=False): '''takes a filtered dict of clusts worth keeping and creates a boxplot of either by lane (default) or pool''' indiv_cluster_count = defaultdict(int) for clust, inddict in filteredclusts.items(): for ind, reads in inddict.items(): if ind in indiv_cluster_count.keys(): indiv_cluster_count[ind]+=1 else: indiv_cluster_count[ind]+=1 t = gdata_tools.get_table_as_dict(dblibrary) db_ind_countd = Util.countdict([d['sampleid'] for d in t if d['sampleid'] in indiv_cluster_count.keys()[3]]) #creates a table of individual dicts from google spreadsheet indiv_by_group = defaultdict(list) for d in t: if 'pool' in d: indkey = (d.get('flowcell',None),d.get('lane',None),d.get('index',None),d.get('sampleid',None)) if indkey in indiv_cluster_count: if pool == True: indiv_by_group[(d['flowcell'],d['lane'],d.get('index',None),d['pool'])].append(indiv_cluster_count[indkey]) else: indiv_by_group[(d['flowcell'],d['lane'],d.get('index',None))].append(indiv_cluster_count[indkey]) boxes = [] labels = [] for group,indcounts in indiv_by_group.items(): boxes.append(indcounts) labels.append(group) boxplt = pylab.figure(1) pylab.boxplot(boxes) pylab.xticks(arange(1,(len(labels)+1)),labels,fontsize='small') #legend with best location (0) if pools boxplt.savefig(figname)
def plot_file_color(base, thin=True, start=0, size=14, save=False): conf, track, pegs = load(base) fig = pl.figure(figsize=(size,size*conf['top']/conf['wall'])) track = track[start:] x = track[:,0]; y = track[:,1] t = np.linspace(0,1,x.shape[0]) points = np.array([x,y]).transpose().reshape(-1,1,2) segs = np.concatenate([points[:-1],points[1:]],axis=1) lc = LineCollection(segs, linewidths=0.25, cmap=pl.cm.coolwarm) lc.set_array(t) pl.gca().add_collection(lc) #pl.scatter(x, y, c=np.arange(len(x)),linestyle='-',cmap=pl.cm.coolwarm) #pl.plot(track[-1000000:,0], track[-1000000:,1], '-', linewidth=0.0125, alpha=0.8) for peg in pegs: pl.gca().add_artist(pl.Circle(peg, conf['radius'], color='k', alpha=0.3)) pl.xlim(0, conf['wall']) pl.ylim(0, conf['top']) pl.xticks([]) pl.yticks([]) pl.tight_layout() pl.show() if save: pl.savefig(base+".png", dpi=200)
def tracks_movie(base, skip=1, frames=500, size=10): """ A movie of each particle as a point """ conf, track, pegs = load(base) fig = pl.figure(figsize=(size,size*conf['top']/conf['wall'])) plot = None for t in xrange(1,max(frames, track.shape[1]/skip)): tmp = track[:,t*skip,:] if not ((tmp[:,0] > 0) & (tmp[:,1] > 0) & (tmp[:,0] < conf['wall']) & (tmp[:,1] < conf['top'])).any(): continue if plot is None: plot = pl.plot(tmp[:,0], tmp[:,1], 'k,', alpha=1.0, ms=0.1)[0] pl.xticks([]) pl.yticks([]) pl.xlim(0,conf['wall']) pl.ylim(0,conf['top']) pl.tight_layout() else: plot.set_xdata(tmp[:,0]) plot.set_ydata(tmp[:,1]) pl.draw() pl.savefig(base+'-movie-%05d.png' % (t-1))
def display_coeff(data=None): betaAll,betaErrAll, R2adjAll = measure_stamp_coeff(data = data, zernike_max_order=20) ind = np.arange(len(betaAll[0])) momname = ('M20','M22.Real','M22.imag','M31.real','M31.imag','M33.real','M33.imag') fmtarr = ['bo-','ro-','go-','co-','mo-','yo-','ko-'] pl.figure(figsize=(17,13)) for i in range(7): pl.subplot(7,1,i+1) pl.errorbar(ind,betaAll[i],yerr = betaErrAll[i],fmt=fmtarr[i]) pl.grid() pl.xlim(-1,21) if i ==0: pl.ylim(-10,65) elif i ==1: pl.ylim(-5,6) elif i ==2: pl.ylim(-5,6) elif i == 3: pl.ylim(-0.1,0.1) elif i == 4: pl.ylim(-0.1,0.1) elif i ==5: pl.ylim(-100,100) elif i == 6: pl.ylim(-100,100) pl.xticks(ind,('','','','','','','','','','','','','','','','','','','','')) pl.ylabel(momname[i]) pl.xticks(ind,('0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19')) pl.xlabel('Zernike Coefficients') return '--- done ! ----'
def plot_question(fname, question_text, data): import pylab import numpy as np from matplotlib.font_manager import FontProperties from matplotlib.text import Text pylab.figure().clear() pylab.title(question_text) #pylab.xlabel("Verteilung") #pylab.subplot(101) if True or len(data) < 3: width = 0.95 pylab.bar(range(len(data)), [max(y, 0.01) for x, y in data], 0.95, color="g") pylab.xticks([i+0.5*width for i in range(len(data))], [x for x, y in data]) pylab.yticks([0, 10, 20, 30, 40, 50]) #ind = np.arange(len(data)) #pylab.bar(ind, [y for x, y in data], 0.95, color="g") #pylab.ylabel("#") #pylab.ylim(ymax=45) #pylab.ylabel("Antworten") #pylab.xticks(ind+0.5, histo.get_ticks()) #pylab.legend(loc=3, prop=FontProperties(size="smaller")) ##pylab.grid(True) else: pylab.pie([max(y, 0.1) for x, y in data], labels=[x for x, y in data], autopct="%.0f%%") pylab.savefig(fname, format="png", dpi=75)
def align_subplots(N,M,xlim=None, ylim=None): """make all of the subplots have the same limits, turn off unnecessary ticks""" #find sensible xlim,ylim if xlim is None: xlim = [np.inf,-np.inf] for i in range(N*M): pb.subplot(N,M,i+1) xlim[0] = min(xlim[0],pb.xlim()[0]) xlim[1] = max(xlim[1],pb.xlim()[1]) if ylim is None: ylim = [np.inf,-np.inf] for i in range(N*M): pb.subplot(N,M,i+1) ylim[0] = min(ylim[0],pb.ylim()[0]) ylim[1] = max(ylim[1],pb.ylim()[1]) for i in range(N*M): pb.subplot(N,M,i+1) pb.xlim(xlim) pb.ylim(ylim) if (i)%M: pb.yticks([]) else: removeRightTicks() if i<(M*(N-1)): pb.xticks([]) else: removeUpperTicks()
def plot_predictions(self): data = self.get_next_batch(train=False)[2] # get a test batch num_classes = self.test_data_provider.get_num_classes() NUM_ROWS = 2 NUM_COLS = 4 NUM_IMGS = NUM_ROWS * NUM_COLS NUM_TOP_CLASSES = min(num_classes, 4) # show this many top labels label_names = self.test_data_provider.batch_meta['label_names'] if self.only_errors: preds = n.zeros((data[0].shape[1], num_classes), dtype=n.single) else: preds = n.zeros((NUM_IMGS, num_classes), dtype=n.single) rand_idx = nr.randint(0, data[0].shape[1], NUM_IMGS) print rand_idx data[0] = n.require(data[0][:,rand_idx], requirements='C') data[1] = n.require(data[1][:,rand_idx], requirements='C') data += [preds] temp = data[0] print data print temp.ndim,temp.shape,temp.size # Run the model self.libmodel.startFeatureWriter(data, self.sotmax_idx) self.finish_batch() fig = pl.figure(3) fig.text(.4, .95, '%s test case predictions' % ('Mistaken' if self.only_errors else 'Random')) if self.only_errors: err_idx = nr.permutation(n.where(preds.argmax(axis=1) != data[1][0,:])[0])[:NUM_IMGS] # what the net got wrong data[0], data[1], preds = data[0][:,err_idx], data[1][:,err_idx], preds[err_idx,:] data[0] = self.test_data_provider.get_plottable_data(data[0]) for r in xrange(NUM_ROWS): for c in xrange(NUM_COLS): img_idx = r * NUM_COLS + c if data[0].shape[0] <= img_idx: break pl.subplot(NUM_ROWS*2, NUM_COLS, r * 2 * NUM_COLS + c + 1) pl.xticks([]) pl.yticks([]) try: img = data[0][img_idx,:,:,:] except IndexError: # maybe greyscale? img = data[0][img_idx,:,:] pl.imshow(img, interpolation='nearest') true_label = int(data[1][0,img_idx]) img_labels = sorted(zip(preds[img_idx,:], label_names), key=lambda x: x[0])[-NUM_TOP_CLASSES:] pl.subplot(NUM_ROWS*2, NUM_COLS, (r * 2 + 1) * NUM_COLS + c + 1, aspect='equal') ylocs = n.array(range(NUM_TOP_CLASSES)) + 0.5 height = 0.5 width = max(ylocs) pl.barh(ylocs, [l[0]*width for l in img_labels], height=height, \ color=['r' if l[1] == label_names[true_label] else 'b' for l in img_labels]) pl.title(label_names[true_label]) pl.yticks(ylocs + height/2, [l[1] for l in img_labels]) pl.xticks([width/2.0, width], ['50%', '']) pl.ylim(0, ylocs[-1] + height*2)
def paint1(data): order_data = sorted(data.items(), key=lambda x: x[0]) print order_data x = [i[0] for i in order_data] y0 = [i[1][0] for i in order_data] y1 = [i[1][1] for i in order_data] y2 = [i[1][2] for i in order_data] y3 = [i[1][3] for i in order_data] #print "x", x #print "y0", y0 #print "y1", y1 pl.plot(x, y0, 'r', label="NONE") pl.plot(x, y1, 'b', label="MPI") pl.plot(x, y2, 'g', label="OMP") pl.plot(x, y3, 'y', label="OMP_MPI") pl.legend(loc="upper left") pl.xticks(list(xrange(3, 11)), [str(i) for i in xrange(3, 11)]) pl.xlabel('String Length') pl.ylabel('Time') pl.show()
def paint2(data): order_data = sorted(data.items(), key=lambda x: x[0]) print order_data dic = ['', "NONE", "MPI", "OMP", "OMP_MPI", ''] x = [i for i in xrange(4)] y0 = order_data[0][1] y1 = order_data[1][1] y2 = order_data[2][1] y3 = order_data[3][1] y4 = order_data[4][1] #y4 = order_data[4][1] #print "x", x #print "y0", y0 #print "y1", y1 pl.plot(x, y0, 'r', label="length=4") pl.plot(x, y1, 'b', label="length=5") pl.plot(x, y2, 'g', label="length=6") pl.plot(x, y3, 'y', label="length=7") pl.plot(x, y4, 'c', label="length=8") #pl.plot(x, y4, 'k', label="length=9") pl.xticks(list(xrange(-1, 5)), list(dic)) pl.legend(loc="upper right") pl.xlabel('Compiling Modes') pl.ylabel('Time') pl.show()
def postProcess(self): coreName = self.study.cacheDir + os.sep + "hz-" + self.name f = open(coreName) vals = {} for l in f: toks = l.split("\t") vals[toks[0], toks[1]] = toks[2:] f.close() for pop in self.pops: pylab.clf() pylab.title(pop) labels = [] ehzs = [] ohzs = [] ns = [] for indiv in self.study.pops.getIndivs(pop): labels.append("\n".join(list(indiv))) o, e, n, f = vals[indiv] ehz = 1 - float(e) / int(n) ohz = 1 - float(o) / int(n) ehzs.append(ehz) ohzs.append(ohz) ns.append(int(n)) pylab.xticks(rotation=90, fontsize="x-small") pylab.legend() pylab.plot(ehzs, "+", label="ExpHe") pylab.plot(ohzs, "+", label="ObsHe") a2 = pylab.twinx() a2.plot(ns, ".") pylab.xticks(list(range(len(labels))), labels) xmin, xmax = pylab.xlim() pylab.xlim(xmin - 1, xmax + 1) pylab.savefig(coreName + "-" + pop + ".png")
def plot_bases(self, autoscale=True, stampsize=None): import pylab as plt N = len(self.psfbases) cols = int(np.ceil(np.sqrt(N))) rows = int(np.ceil(N / float(cols))) plt.clf() plt.subplots_adjust(hspace=0, wspace=0) cut = 0 if stampsize is not None: H, W = self.shape assert H == W cut = max(0, (H - stampsize) / 2) ima = dict(interpolation="nearest", origin="lower") if autoscale: mx = self.psfbases.max() ima.update(vmin=-mx, vmax=mx) nil, xpows, ypows = self.polynomials(0.0, 0.0, powers=True) for i, (xp, yp, b) in enumerate(zip(xpows, ypows, self.psfbases)): plt.subplot(rows, cols, i + 1) if cut > 0: b = b[cut:-cut, cut:-cut] if autoscale: plt.imshow(b, **ima) else: mx = np.abs(b).max() plt.imshow(b, vmin=-mx, vmax=mx, **ima) plt.xticks([]) plt.yticks([]) plt.title("x^%i y^%i" % (xp, yp)) plt.suptitle("PsfEx eigen-bases")
def plot_C_gamma_grid_search(grid, C_range, gamma_range, score): ''' Plots the scores computed on a grid. Arguments: grid - the grid search object created using GridSearchCV() C_range - the C parameter range gamma_range - the gamma parameter range score - the scoring function ''' # grid_scores_ contains parameter settings and scores # We extract just the scores scores = [x[1] for x in grid.grid_scores_] scores = np.array(scores).reshape(len(C_range), len(gamma_range)) # draw heatmap of accuracy as a function of gamma and C pl.figure(figsize=(8, 6)) pl.subplots_adjust(left=0.05, right=0.95, bottom=0.15, top=0.95) pl.imshow(scores, interpolation='nearest', cmap=pl.cm.spectral) pl.title("Grid search on C and gamma for best %s" % score) pl.xlabel('gamma') pl.ylabel('C') pl.colorbar() pl.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45) pl.yticks(np.arange(len(C_range)), C_range) pl.show()
def plot_io_obliquity_2(): from numpy import linspace, array, arange from pylab import rc, plot, xlim, xticks, xlabel, ylabel import matplotlib.pyplot as plt import mpmath rc('text', usetex=True) fig = plt.figure() # Initial params. #params = {'m2':1.9e27,'r2':70000.e3,'rot2':0.00017585125448028,\ # 'r1':1,'rot1':6.28*100,'i_a':0.5,'ht': 0,\ # 'a':421700E3 / 4,'e':0.01,'i':0.8,'h':2} params = {'m2':1.9e27,'r2':70000.e3,'rot2':0.00017585125448028,\ 'r1':1821E3,'rot1':6.28/(21 * 3600),'i_a':0.17453,'ht': 0,\ 'a':421700E3,'e':0.05,'i':0.349,'h':0.35} # Set parameters. sp.parameters = params # Period. period = sp.wp_period ob = sp.obliquity_time lspace = linspace(0,5*period,250) xlim(0,float(lspace[-1])) xlabel(r'$\textnormal{Time (Ma)}$') ylabel(r'$\textnormal{Obliquity }\left( ^\circ \right)$') ob_series = array([ob(t).real*(360/(2*mpmath.pi())) for t in lspace]) plot(lspace,ob_series,'k-',linewidth=2) xticks(arange(xlim()[0],xlim()[1],365*24*3600*1000000),[r'$'+str(int(_)*1)+r'$' for _ in [t[0] for t in enumerate(arange(xlim()[0],xlim()[1],365*24*3600*1000000))]])
def plot_genome(out_file, data_file, samples, dpi=300, screen=False): if screen: PL.rcParams.update(PLOT_PARAMS_SCREEN) LOG.info("plot_genome - out_file=%s, data_file=%s, samples=%s, dpi=%d"%(out_file, data_file, str(samples), dpi)) colors = 'bgryckbgryck' data = read_posterior(data_file) if samples is None or len(samples) == 0: samples = data.keys() if len(samples) == 0: return PL.figure(None, [14, 4]) right_end = 0 # rightmost plotted base pair for chrm in sort_chrms(data.values()[0]): # for chromosomes in ascending order max_site = max(data[samples[0]][chrm]['L']) # length of chromosome for s, sample in enumerate(samples): # plot all samples I = SP.where(SP.array(data[sample][chrm]['SD']) < 0.3)[0] # at sites that have confident posteriors PL.plot(SP.array(data[sample][chrm]['L'])[I] + right_end, SP.array(data[sample][chrm]['AF'])[I], alpha=0.4, color=colors[s], lw=2) # offset by the end of last chromosome if right_end > 0: PL.plot([right_end, right_end], [0,1], 'k--', lw=0.4, alpha=0.2) # plot separators between chromosomes new_right = right_end + max(data[sample][chrm]['L']) PL.text(right_end + 0.5*(new_right - right_end), 0.9, str(chrm), horizontalalignment='center') right_end = new_right # update rightmost end PL.plot([0,right_end], [0.5,0.5], 'k--', alpha=0.3) PL.xlim(0,right_end) xrange = SP.arange(0,right_end, 1000000) PL.xticks(xrange, ["%d"%(int(x/1000000)) for x in xrange]) PL.xlabel("Genome (Mb)"), PL.ylabel("Reference allele frequency") PL.savefig(out_file, dpi=dpi)
self.pseudo_regret.append(self.agent.pseudo_regret) agents: List[AgentMetrics] = [] def register_agent(a: Agent): agents.append(AgentMetrics(a)) rounds = 1000 n_actions = 5 rewards = NormallyDistributedRandomRewards(n_actions, mu_max=5, stddev=2, seed=42) for i in range(1, 100): register_agent(Agent(rewards, EpsilonGreedyBandit(i / 100))) for _ in range(rounds): for m in agents: m.agent.play_round() m.record_metrics() x = list(map(lambda x: x / 100, range(1, 100))) y = [a.pseudo_regret[-1] for a in agents] pylab.plot(x, y) pylab.xticks(list(map(lambda x: (x / 100) * 10, range(11)))) pylab.show()
def applyPlotOptions(fig, ax, options): if options.xlim: xlim(float(options.xlim.split(",")[0]), float(options.xlim.split(",")[1])) if options.yticksorientation: ax.tick_params(axis='y', which='major', tickdir=options.xticksorientation) if options.xticks: vals = options.xticks.split(",") if len(vals) == 1: ax.tick_params(axis='x', which='major', labelsize=float(vals[0])) elif len(vals) == 4: xticks(arange(float(vals[0]), float(vals[1]), float(vals[2])), size=float(vals[3])) else: print( "Error: ticks must be given as one float (<SIZE>) or four floats (<MIN>,<MAX>,<STEP>,<SIZE>)" ) sys.exit() if options.xtime1: ax.xaxis.set_major_formatter(ff(m2hm1)) if options.xtime2: ax.xaxis.set_major_formatter(ff(m2hm2)) if options.xgrid: ax.xaxis.grid(True) if options.xlabel: xlabel(options.xlabel, size=options.xlabelsize) if options.xticksorientation: labels = ax.get_xticklabels() for label in labels: label.set_rotation(options.xticksorientation) if options.ylim: ylim(float(options.ylim.split(",")[0]), float(options.ylim.split(",")[1])) if options.yticks: vals = options.yticks.split(",") if len(vals) == 1: ax.tick_params(axis='y', which='major', labelsize=float(vals[0])) elif len(vals) == 4: yticks(arange(float(vals[0]), float(vals[1]), float(vals[2])), size=float(vals[3])) else: print( "Error: ticks must be given as one float (<SIZE>) or four floats (<MIN>,<MAX>,<STEP>,<SIZE>)" ) sys.exit() if options.ytime1: ax.yaxis.set_major_formatter(ff(m2hm1)) if options.ytime2: ax.yaxis.set_major_formatter(ff(m2hm2)) if options.ygrid: ax.yaxis.grid(True) if options.ylabel: ylabel(options.ylabel, size=options.ylabelsize) if options.yticksorientation: labels = ax.get_yticklabels() for label in labels: label.set_rotation(options.yticksorientation) if options.title: title(options.title, size=options.titlesize) if options.adjust: vals = options.adjust.split(",") if len(vals) == 2: fig.subplots_adjust(left=float(vals[0]), bottom=float(vals[1])) elif len(vals) == 4: fig.subplots_adjust(left=float(vals[0]), bottom=float(vals[1]), right=float(vals[2]), top=float(vals[3])) else: print( "Error: adjust must be given as two floats (<LEFT>,<BOTTOM>) or four floats (<LEFT>,<BOTTOM>,<RIGHT>,<TOP>)" ) sys.exit()
# Predict anomalies sequentially (assume known transition matrix and # initial probabilities) A = np.array([[.999, .001], [.01, .99]]) pi = np.array([.5, .5]) y_pred_dynamic = anomalymodel.predict_sequence(Y_test, A, pi) # In[162]: plt.clf() plt.figure(figsize=(10, 6)) plt.subplot(4, 1, 1) plt.plot(X_test[:, 0]) plt.ylabel('ECG 1') #,rotation='horizontal') plt.grid(which='major', axis='x') plt.xticks(plt.xticks()[0], '') plt.title('Detection of cardiac arrhythmia from ECG sequence') plt.subplot(4, 1, 2) plt.plot(X_test[:, 1]) plt.grid(which='major', axis='x') plt.xticks(plt.xticks()[0], '') plt.ylabel('ECG 2') #,rotation='horizontal') plt.subplot(4, 1, 3) plt.plot(y_pred_static[:, 1], 'r') plt.xticks(plt.xticks()[0], '') plt.grid(which='major', axis='both') plt.ylim([-.05, 1.05]) plt.ylabel('Anomaly score\n(static)') #,rotation='horizontal') plt.subplot(4, 1, 4) plt.plot(y_pred_dynamic[:, 1], 'r') plt.grid(which='major', axis='both')
def allele_plot(filename, normalize=False, alleles=None, generations=None): """Plot the alleles from each generation from the individuals file. This function creates a plot of the individual allele values as they change through the generations. It creates three subplots, one for each of the best, median, and average individual. The best and median individuals are chosen using the fitness data for each generation. The average individual, on the other hand, is actually an individual created by averaging the alleles within a generation. This function requires the pylab library. .. note:: This function only works for single-objective problems. .. figure:: _static/allele_plot.png :alt: Example allele plot :align: center An example image saved from the ``allele_plot`` function. Arguments: - *filename* -- the name of the individuals file produced by the file_observer - *normalize* -- Boolean value stating whether allele values should be normalized before plotting (default False) - *alleles* -- a list of allele index values that should be plotted (default None) - *generations* -- a list of generation numbers that should be plotted (default None) If *alleles* is ``None``, then all alleles are plotted. Similarly, if *generations* is ``None``, then all generations are plotted. """ import pylab generation_data = [] reader = csv.reader(open(filename)) for row in reader: g = int(row[0]) row[3] = row[3].replace('[', '') row[-1] = row[-1].replace(']', '') individual = [float(r) for r in row[3:]] individual.append(float(row[2])) try: generation_data[g] except IndexError: generation_data.append([]) generation_data[g].append(individual) for gen in generation_data: gen.sort(key=lambda x: x[-1]) for j, g in enumerate(gen): gen[j] = g[:-1] best = [] median = [] average = [] for gen in generation_data: best.append(gen[0]) plen = len(gen) if plen % 2 == 1: med = gen[(plen - 1) // 2] else: med = [] for a, b in zip(gen[plen // 2 - 1], gen[plen // 2]): med.append(float(a + b) / 2) median.append(med) avg = [0] * len(gen[0]) for individual in gen: for i, allele in enumerate(individual): avg[i] += allele for i, a in enumerate(avg): avg[i] /= float(len(gen)) average.append(avg) for plot_num, (data, title) in enumerate( zip([best, median, average], ["Best", "Median", "Average"])): if alleles is None: alleles = list(range(len(data[0]))) if generations is None: generations = list(range(len(data))) if normalize: columns = list(zip(*data)) max_col = [max(c) for c in columns] min_col = [min(c) for c in columns] for dat in data: for i, d in enumerate(dat): dat[i] = (d - min_col[i]) / float(max_col[i] - min_col[i]) plot_data = [] for g in generations: plot_data.append([data[g][a] for a in alleles]) sub = pylab.subplot(3, 1, plot_num + 1) pylab.pcolor(pylab.array(plot_data)) pylab.colorbar() step_size = max(len(generations) // 7, 1) ytick_locs = list(range(step_size, len(generations), step_size)) ytick_labs = generations[step_size::step_size] pylab.yticks(ytick_locs, ytick_labs) pylab.ylabel('Generation') if plot_num == 2: xtick_locs = list(range(len(alleles))) xtick_labs = alleles pylab.xticks(xtick_locs, xtick_labs) pylab.xlabel('Allele') else: pylab.setp(sub.get_xticklabels(), visible=False) pylab.title(title) pylab.show()
def run(params, out=sys.stdout): cm = CellGraph(tol_length=params.tol_length, tol_angle=params.tol_angle) if not params.xdsdir and params.topdir: params.xdsdir = map( lambda x: x[0], filter( lambda x: any( map(lambda y: y.startswith("XDS_ASCII.HKL"), x[2])) or "DIALS.HKL" in x[2], os.walk(params.topdir))) for i, xdsdir in enumerate(params.xdsdir): cm.add_proc_result(i, xdsdir) cm.group_xds_results(out) ret = cm.grouped_dirs if len(ret) == 0: return cm print >> out print >> out, "About the largest group:" for idx, wd in enumerate(ret[0]): xac_hkl = os.path.join(wd, "XDS_ASCII.HKL") correct_lp = os.path.join(wd, "CORRECT.LP") print >> out, "%.3d %s" % (idx, os.path.relpath(wd, params.topdir) if params.topdir is not None else wd), if not os.path.isfile(xac_hkl): print >> out, "Unsuccessful" continue sg = XDS_ASCII(xac_hkl, read_data=False).symm.space_group_info() clp = correctlp.CorrectLp(correct_lp) if "all" in clp.table: cmpl = clp.table["all"]["cmpl"][-1] else: cmpl = float("nan") ISa = clp.a_b_ISa[-1] print >> out, "%10s ISa=%5.2f Cmpl=%5.1f " % (sg, ISa, cmpl) if params.do_pointless: worker = pointless.Pointless() files = map(lambda x: os.path.join(x, "INTEGRATE.HKL"), ret[0]) #print files files = filter(lambda x: os.path.isfile(x), files) print >> out, "\nRunning pointless for the largest member." result = worker.run_for_symm(xdsin=files, logout="pointless.log", tolerance=10, d_min=5) if "symm" in result: print >> out, " pointless suggested", result[ "symm"].space_group_info() if 0: import pylab pos = nx.spring_layout(G) #pos = nx.spectral_layout(G) #pos = nx.circular_layout(G) #nx.draw_networkx_nodes(G, pos, node_size = 100, nodelist=others, node_color = 'w') nx.draw_networkx_nodes(G, pos, node_size=100, node_color='w') nx.draw_networkx_edges(G, pos, width=1) nx.draw_networkx_labels(G, pos, font_size=12, font_family='sans-serif', font_color='r') pylab.xticks([]) pylab.yticks([]) pylab.savefig("network.png") pylab.show() return cm
model2 = ols(y=df['minProfit'], x=df.ix[:, 'rangeWidth':'type2']) model3 = ols(y=df['safeMinProfit'], x=df.ix[:, 'rangeWidth':'type2']) #df.ix[:,'maxProfit':'minProfit'].plot() grouped = df.groupby('type2') #long short max profit lsMaxProfitDict = [0, 0] for name, group in grouped: lsMaxProfitDict[name] = group['maxProfit'].values pylab.boxplot(lsMaxProfitDict) pylab.xticks([1, 2], ['short', 'long']) #long short min profit lsMaxProfitDict = [0, 0] for name, group in grouped: lsMaxProfitDict[name] = group['minProfit'].values pylab.boxplot(lsMaxProfitDict) pylab.xticks([1, 2], ['short', 'long']) #histograms of profit vs. rangeWidth, signalCount #first group by signalCount (discrete) signalGrouped = df.groupby('signalCountGroup') signalCount_meanRets = {} for name, group in signalGrouped:
graph = nx.Graph() for i in range(nodeSize): graph.add_node(str(i)) for i in range(nodeSize): graph.add_edge(str(nodes[i][0]), str(nodes[i][1])) pylab.figure(figsize=(10, 10)) # pos = graphviz_layout(graph, prog='dot') # pos = graphviz_layout(graph, prog='dot') # pos = graphviz_layout(graph, prog='twopi') pos = graphviz_layout(graph, prog='dot') nx.draw_networkx_nodes(graph, pos, node_size=1, node_color="black") nx.draw_networkx_edges(graph, pos, width=1) # nx.draw_networkx_edge_labels(graph, pos)#, edge_labels=edge_labels) # nx.draw_networkx_labels(graph, pos)#, font_size=16, font_color="r") pylab.xticks([]) pylab.yticks([]) # pylab.show() os.chdir( '/Users/Aoyama/Documents/B4/Memorability_data/tulip/csv/300nodes/originData' ) f = open('' + str(j) + '.csv', 'w') writer = csv.writer(f) for i in nodes: writer.writerow(i) f.close()
import pylab as pl import numpy as np def f(x, y): return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 - y**2) n = 10 x = np.linspace(-3, 3, 3.5 * n) y = np.linspace(-3, 3, 3.0 * n) X, Y = np.meshgrid(x, y) Z = f(X, Y) pl.axes([0.025, 0.025, 0.95, 0.95]) pl.imshow(Z, interpolation='nearest', cmap='bone', origin='lower') pl.colorbar(shrink=.92) pl.xticks(()) pl.yticks(()) pl.show()
def TemperatureHumidityGraph(source,days,delay): print("TemperatureHumidityGraph source:%s days:%s" % (source,days)) print("sleeping seconds:", delay) time.sleep(delay) print("TemperatureHumidityGraph running now") # blink GPIO LED when it's run GPIO.setup(18, GPIO.OUT) GPIO.output(18, True) time.sleep(0.2) GPIO.output(18, False) # now we have get the data, stuff it in the graph try: print("trying database") db = mdb.connect('localhost', 'root', config.MySQL_Password, 'GroveWeatherPi'); cursor = db.cursor() query = "SELECT TimeStamp, bmp180Temperature, outsideTemperature, outsideHumidity, insideHumidity FROM WeatherData where now() - interval %i hour < TimeStamp" % (days*24) print "query=", query cursor.execute(query) result = cursor.fetchall() t = [] u = [] v = [] x = [] z = [] fig = pyplot.figure() for record in result: t.append(record[0]) u.append(record[1]) v.append(record[2]) x.append(record[3]) z.append(record[4]) print ("count of t=",len(t)) if (len(t) == 0): return #dts = map(datetime.datetime.fromtimestamp, s) #fds = dates.date2num(dts) # converted # matplotlib date format object hfmt = dates.DateFormatter('%m/%d-%H') ax = fig.add_subplot(111) ax.xaxis.set_major_locator(dates.HourLocator(interval=6)) ax.xaxis.set_major_formatter(hfmt) pylab.xticks(rotation='vertical') pyplot.subplots_adjust(bottom=.3) pylab.plot(t, v, color='g',label="Outside Temp (C)",linestyle="-",marker=".") pylab.plot(t, u, color='r',label="Inside Temp (C)",linestyle="-",marker=".") pylab.xlabel("Hours") pylab.ylabel("degrees C") pylab.legend(loc='upper left') pylab.axis([min(t), max(t), 0, 40]) ax2 = pylab.twinx() pylab.ylabel("% ") pylab.plot(t, x, color='y',label="Outside Hum %",linestyle="-",marker=".") pylab.plot(t, z, color='b',label="Inside Hum %",linestyle="-",marker=".") pylab.axis([min(t), max(t), 0, 100]) pylab.legend(loc='lower left') pylab.figtext(.5, .05, ("Environmental Statistics Last %i Days" % days),fontsize=18,ha='center') #pylab.grid(True) pyplot.setp( ax.xaxis.get_majorticklabels(), rotation=70) ax.xaxis.set_major_formatter(dates.DateFormatter('%m/%d-%H')) pyplot.show() try: pyplot.savefig("/home/pi/RasPiConnectServer/static/TemperatureHumidityGraph.png") except: pyplot.savefig("/home/pi/SDL_Pi_GroveWeatherPi/static/TemperatureHumidityGraph.png") except mdb.Error, e: print "Error %d: %s" % (e.args[0],e.args[1])
cnt += 1 # now do the barplot pl.subplot(nrows, ncols, cnt) ybars = [np.mean(data[cl][i]) for i in range(len(my_groups))] y_sd = [ np.std(data[cl][i]) / np.sqrt(len(sx[i])) for i in range(len(my_groups)) ] pl.bar(np.arange(len(ybars)), ybars, 0.35, color=['g', 'r', 'b'], ecolor='k', yerr=y_sd) pl.xticks(range(len(my_groups)), ['NV', 'persistent', 'remission']) # pl.title(res_fname.split('/')[-1]) # pl.title() pl.xlim([-.2, 2.5]) cnt += 1 # do some ttests out_str = [] t, p = stats.ttest_ind(data[cl][0], data[cl][1]) if t > 0: out_str.append('>') else: out_str.append('<') if p < .05: out_str[-1] += 's' if not quiet:
# calculate ctr and find outliers ctr = pd.read_csv('agg_ctr.csv') ctr['ctr'] = ctr['clicks'] / ctr['impressions'] ctr['date'] = pd.to_datetime(ctr['hour'], format="%y%m%d%H") ctr['mean'] = ctr['ctr'].rolling(WINDOW, center=True).mean() ctr['std'] = ctr['ctr'].rolling(WINDOW, center=True).std() out = (ctr['ctr'] > ctr['mean'] + DEV * ctr['std']) | (ctr['ctr'] < ctr['mean'] -DEV * ctr['std']) # plot pl.figure(1, figsize=(10, 5)) pl.plot(ctr['date'], ctr['ctr']) pl.xticks(rotation=-45) pl.xlabel('Date') pl.ylabel('CTR') pl.title('Raw CTR') pl.figure(2, figsize=(10, 5)) pl.plot(ctr['date'], ctr['ctr'], '-C0', label='CTR') pl.plot(ctr['date'], ctr['mean'], '-C1', label='mean') minc, maxc = ctr['mean']-DEV*ctr['std'], ctr['mean']+DEV*ctr['std'] s = ~ctr['mean'].isna() pl.fill_between(ctr['date'][s], minc[s], maxc[s], color='C1', alpha=0.2, label='{} std'.format(DEV)) pl.plot(ctr['date'][out], ctr['ctr'][out], 'or', label='outliers') pl.xticks(rotation=-45)
def plot_image(x): pl.imshow(x.reshape((28, 28)), cmap='gray') pl.xticks(()) pl.yticks(())
ms=pl.sqrt(v) * 5 + 2, mec='k', mew=1, label='%d Observation' % v) else: pl.plot([-100], [1], 'o', color='none', ms=pl.sqrt(v) * 5 + 2, mec='k', mew=1, label='%d Observations' % v) pl.legend(loc='lower left', fancybox=True, shadow=True, numpoints=1) pl.xticks() pl.yticks() pl.xlabel('Mean of age group (years)') pl.ylabel('Width of age group (years)') pl.axis([-5, 110., .6, 500.]) pl.subplots_adjust(left=.1, right=.99, bottom=.15, top=.95) pl.savefig('book/graphics/af_age_groups_scatter.pdf') ### @export 'save-results' book_graphics.save_json('af_age_groups.json', { 'most_freq_cnt': most_freq_cnt, 'rows_total': rows_total })
spikes = session.get_spike_times(*datum) pl.subplot(subplotsHeight, subplotsWidth, \ subplotsWidth * y + x + 1) physio.plotting.psth.plot(trials, spikes, options.before, \ options.after, options.nbins) pl.axvline(0., color='k') pl.axvspan(0., 0.5, color='k', alpha=0.1) if x == 0: pl.ylabel('Cluster: %i\nRate(Hz)' % datum[1]) else: pl.yticks([]) if y == 0: pl.title('%s' % str(condition), rotation=45) if y < len(data) - 1: pl.xticks([]) else: pl.xticks([0., .5]) pl.xlabel("Seconds") ymaxs[y] = max(ymaxs[y], pl.ylim()[1]) session.close() for y in xrange(subplotsHeight): for x in xrange(subplotsWidth): pl.subplot(subplotsHeight, subplotsWidth, \ subplotsWidth * y + x + 1) pl.ylim(0, ymaxs[y]) if not os.path.exists(options.outdir): os.makedirs(options.outdir) # TODO move this down
bars = Bars() for clonal_group in dr_tools.loadlist(o.clonal_groups): samples = [ s for s in exprt.samples if any( s.startswith(clonal_group_start) or s.startswith('pool.' + clonal_group_start) for clonal_group_start in clonal_group.split('\t')) ] for min_cell_fraction in o.min_cell_fraction: merged_output = count_mono(samples, False, None, min_cell_fraction) use_ti = merged_output[-1] for ri in range(o.random_bars): bars.addbase( '', *count_mono(samples, True, use_ti, min_cell_fraction)) bars.addbar('', *merged_output) bars.x_pos += 1 pylab.bar(bars.xarr, [-y for y in bars.c57], facecolor='#888800', linewidth=0, width=0.9) pylab.bar(bars.xarr, bars.cast, facecolor='#aa8800', linewidth=0, width=0.9) pylab.xticks(bars.labels_x, bars.labels, rotation=90) pylab.subplots_adjust(bottom=0.3) pylab.savefig(o.figure)
from sklearn.model_selection import train_test_split from keras.datasets import mnist """## One Neuron""" # functions for neuron construction def sigmoid(x): return 1.0/(1+np.exp(-x)) def sigmoid_derivation(x): return x*(1.0-x) # artificial data for the experiment X=np.array([[.11,.05,.95],[.09,.03,.08],[.01,.09,.91], [.04,.92,.07],[.05,.02,.04],[.07,.97,.05], [.06,.02,.98],[.02,.06,.03],[.01,.09,.03], [.02,.94,.01],[.06,.03,.95],[.04,.91,.09]]) Y=np.array([[1,0,1,2,0,2,1,0,1,2,1,2]]).T pl.imshow(X.T,cmap=pl.cm.cool); pl.xticks([]) pl.text(-.3,1,str(Y.T)[2:-2],c='white',fontsize=29); # the start point for iterations synapse0=np.random.randn(3,1); layer0=X # the steps of iteration for iter in range(100): layer1=sigmoid(np.dot(layer0,synapse0)) # finding errors and directions for correction layer1_error=layer1-Y layer1_delta=layer1_error*sigmoid_derivation(layer1) # correction values & coefficients synapse0_derivative=np.dot(layer0.T,layer1_delta) synapse0-=synapse0_derivative pl.scatter(list(range(1,13)),layer1,c=Y,cmap=pl.cm.cool);
plt.ylabel('test accuracy') plt.xlabel('epochs') plt.legend(batch_size_choices, loc='upper right') plt.savefig('figures/q2_cvtestaccuracy') # mean over epochs plt.figure(2) average_train_accuracy_per_epoch = np.mean(average_trainaccuracy, axis=1) print("Average_train_accuracy_per_epoch {}".format( average_train_accuracy_per_epoch)) average_test_accuracy_per_epoch = np.mean(average_testaccuracy, axis=1) print("Average_test_accuracy_per_epoch {}".format( average_test_accuracy_per_epoch)) plt.plot(batch_size_choices, average_train_accuracy_per_epoch) plt.plot(batch_size_choices, average_test_accuracy_per_epoch) plt.xticks(batch_size_choices) plt.title('average train and test accuracies against batch size') plt.ylabel('average accuracy per epoch') plt.xlabel('batch size') plt.legend(['train', 'test'], loc='upper right') plt.savefig('figures/q2_averageaccuracyperepoch') #plot the time taken to train the network for one epoch against different batch sizes #mean over 5 folds plt.figure(3) plt.plot(batch_size_choices, np.mean(timelog, axis=0)) plt.xticks(batch_size_choices) plt.title('time taken to train network') plt.ylabel('mean time per epoch (s)') plt.xlabel('batch size') plt.savefig('figures/q2_timeperepoch')
#p.axis([0, data[:,0].max(), -1, 1]) #p.ylabel('environmental conditions', fontsize=X_Label) #p.xticks(size=X_ticks) #p.yticks(size=Y_ticks) #p.grid(True) p.subplot(313) p.plot(data[:, 0], data[:, 4], 'k-') p.annotate('$h_{c} = %1.4f$' % (The_big_value, ), xy=(XX, YY), xycoords='data', fontsize=AnnotateFontSize) p.axis([0, data[:, 0].max(), 0, data[:, 4].max()]) p.xlabel('time (steps)', fontsize=X_Label) p.ylabel('number of cells', fontsize=X_Label) p.xticks(size=X_ticks) p.yticks(size=Y_ticks) p.grid(True) #p.subplot(313) #p.plot(data[:, 0], data[:, 2],'k-', linewidth=1) #p.fill_between(data[:, 0],data[:, 2] + data[:, 3],data[:, 2] - data[:, 3], # color=(0.75,0.75,0.75,0.75)) #p.axis([0, data[:,0].max(), 0, (data[:, 2] + data[:, 3]).max()]) #p.xlabel('time (steps)', fontsize=X_Label) #p.ylabel('mean number of genes', fontsize=Y_Label) #p.xticks(size=X_ticks) #p.yticks(size=Y_ticks) #p.grid(True) #---- end of first plot------- p.show()
def CIE_1976_UCS_chromaticity_diagram_plot( cmfs='CIE 1931 2 Degree Standard Observer', **kwargs): """ Plots the *CIE 1976 UCS Chromaticity Diagram*. Parameters ---------- cmfs : unicode, optional Standard observer colour matching functions used for diagram bounds. \**kwargs : dict, optional Keywords arguments. Returns ------- bool Definition success. Examples -------- >>> CIE_1976_UCS_chromaticity_diagram_plot() # doctest: +SKIP True """ settings = {'figure_size': (DEFAULT_FIGURE_WIDTH, DEFAULT_FIGURE_WIDTH)} settings.update(kwargs) canvas(**settings) cmfs = get_cmfs(cmfs) illuminant = DEFAULT_PLOTTING_ILLUMINANT image = matplotlib.image.imread( os.path.join( PLOTTING_RESOURCES_DIRECTORY, 'CIE_1976_UCS_Chromaticity_Diagram_{0}.png'.format( cmfs.name.replace(' ', '_')))) pylab.imshow(image, interpolation=None, extent=(0, 1, 0, 1)) labels = (420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 680) wavelengths = cmfs.wavelengths equal_energy = np.array([1 / 3] * 2) uv = Luv_to_uv(XYZ_to_Luv(cmfs.values, illuminant), illuminant) wavelengths_chromaticity_coordinates = dict(zip(wavelengths, uv)) pylab.plot(uv[..., 0], uv[..., 1], color='black', linewidth=2) pylab.plot((uv[-1][0], uv[0][0]), (uv[-1][1], uv[0][1]), color='black', linewidth=2) for label in labels: u, v = wavelengths_chromaticity_coordinates.get(label) pylab.plot(u, v, 'o', color='black', linewidth=2) index = bisect.bisect(wavelengths, label) left = wavelengths[index - 1] if index >= 0 else wavelengths[index] right = (wavelengths[index] if index < len(wavelengths) else wavelengths[-1]) dx = (wavelengths_chromaticity_coordinates.get(right)[0] - wavelengths_chromaticity_coordinates.get(left)[0]) dy = (wavelengths_chromaticity_coordinates.get(right)[1] - wavelengths_chromaticity_coordinates.get(left)[1]) norme = lambda x: x / np.linalg.norm(x) uv = np.array([u, v]) direction = np.array([-dy, dx]) normal = (np.array([-dy, dx]) if np.dot(norme(uv - equal_energy), norme(direction)) > 0 else np.array([dy, -dx])) normal = norme(normal) normal /= 25 pylab.plot((u, u + normal[0] * 0.75), (v, v + normal[1] * 0.75), color='black', linewidth=1.5) pylab.text(u + normal[0], v + normal[1], label, clip_on=True, ha='left' if normal[0] >= 0 else 'right', va='center', fontdict={'size': 'small'}) ticks = np.arange(-10, 10, 0.1) pylab.xticks(ticks) pylab.yticks(ticks) settings.update({ 'title': 'CIE 1976 UCS Chromaticity Diagram - {0}'.format(cmfs.title), 'x_label': 'CIE u\'', 'y_label': 'CIE v\'', 'grid': True, 'bounding_box': (0, 1, 0, 1), 'bbox_inches': 'tight', 'pad_inches': 0 }) settings.update(kwargs) boundaries(**settings) decorate(**settings) return display(**settings)
def display_filename(pickle_filename, name=None): print "Loading data to display..." data = pickle.load(open(pickle_filename, 'rb')) coll, xlabel, ylabel = data if not name: name = pickle_filename.split('/')[3] def normalize(val, low, high): return (val - low) / (high - low) num_features = actual.NUM_FEATURES ms = [] ss = [] for i in range(num_features): ms.append([]) ss.append([]) # find ranges of values for value in coll: (bp, force, i, j, means, stds) = value #(bp, force, i, j, sfmm, sfmstd) = value for i in range(num_features): ms[i].append(means[i]) ss[i].append(stds[i]) #ms[0].append(sfmm) #ss[0].append(sfmstd) print "Generating plots..." #m = 0 ### for schelleng force ri = 7 gi = 0 bi = 7 ### for accel? #ri = 0 #gi = 1 #bi = 7 power = 0.5 invert = False #invert = True if PLOT_PNG_BARE: fig = pylab.figure() fig.set_size_inches(4, 3) ax = pylab.Axes(fig, [ 0., 0., 1., 1., ]) else: fig = pylab.figure() pylab.title("%s" % (name)) fig.set_size_inches(16, 12) numx = xlabel.num numy = ylabel.num img = numpy.zeros((numy, numx, 3), dtype=numpy.float32) min_ms_ri = min(ms[ri]) min_ms_gi = min(ms[gi]) min_ms_bi = min(ms[bi]) max_ms_ri = max(ms[ri]) max_ms_gi = max(ms[gi]) max_ms_bi = max(ms[bi]) min_ss_ri = min(ms[ri]) max_ss_ri = max(ms[ri]) print "got min/max" for value in coll: (bp, force, i, j, means, stds) = value #(bp, force, i, j, sfmm, sfmstd) = value x = bp y = force #r = normalize(means[ri], min(ms[ri]), max(ms[ri])) g = normalize(means[gi], min_ms_gi, max_ms_gi) b = normalize(means[bi], min_ms_bi, max_ms_bi) r = normalize(stds[ri], min_ss_ri, max_ss_ri) if invert: r = 1. - r**power g = 1. - g**power b = 1. - b**power else: r = r**power g = g**power b = b**power #text = "%.3f\t%.3f\t%.3f\t%.3f\t%.3f" % (x,y,r,g,b) #out.write(text + "\n") #pylab.plot(x,y, '.', color=(r,g,b), markersize=10) #pylab.semilogy(x,y, '.', color=(r,g,b), markersize=10) img[i][j][0] = r img[i][j][1] = g img[i][j][2] = b print "start imshow" if PLOT_PNG_BARE: ax.imshow( img, #interpolation='bilinear', aspect="auto", origin="lower", ) ax.set_xlim(0, numx - 1) ax.set_ylim(0, numy - 1) else: pylab.imshow( img, #interpolation='bilinear', aspect="auto", origin="lower", ) pylab.xlim(0, numx - 1) pylab.ylim(0, numy - 1) print "end imshow" for m in range(5, 10): modal = 1.0 / m relpos = 1.0 - (xlabel.high - modal) / (xlabel.high - xlabel.low) abspos = relpos * (numx - 1) print abspos, if xlabel.log: abspos = numpy.log(abspos) print abspos #print m, modal, relpos, abspos if PLOT_PNG_BARE: pass #ax.axvline(abspos, color="yellow") else: #pylab.axvline(abspos, color="yellow") pass xlocs = numpy.linspace(0, numx - 1, 11) ylocs = numpy.linspace(0, numy - 1, 11) if xlabel.log: xticks = map( lambda d: str("%.3f" % d), numpy.exp( numpy.linspace(numpy.log(xlabel.low), numpy.log(xlabel.high), len(xlocs)))) else: xticks = map(lambda d: str("%.3f" % d), numpy.linspace(xlabel.low, xlabel.high, len(xlocs))) if ylabel.log: yticks = map( lambda d: str("%.2f" % d), numpy.exp( numpy.linspace(numpy.log(ylabel.low), numpy.log(ylabel.high), len(ylocs)))) else: yticks = map(lambda d: str("%.2f" % d), numpy.linspace(ylabel.low, ylabel.high, len(ylocs))) if True: pylab.xticks(xlocs, xticks) pylab.xlabel(xlabel.title) pylab.yticks(ylocs, yticks) pylab.ylabel(ylabel.title) if PLOT_STD: pylab.figure() pylab.title("Standard deviations for %s" % (name)) pylab.xlim(XB_MIN, XB_MAX) power = 0.5 for value in coll: (bp, force, i, j, means, stds) = value x = bp y = force r = normalize(stds[ri], min(ss[ri]), max(ss[ri])) g = normalize(stds[gi], min(ss[gi]), max(ss[gi])) b = normalize(stds[bi], min(ss[bi]), max(ss[bi])) r = r**power g = g**power b = b**power pylab.plot(x, y, '.', color=(r, g, b), markersize=10) if PLOT_COMBO: pylab.figure() pylab.title("Combo for %s" % (name)) pylab.xlim(XB_MIN, XB_MAX) power = 0.5 ri = 0 gi = 3 bi = 3 for value in coll: (bp, force, i, j, means, stds) = value x = bp y = force r = normalize(means[ri], min(ms[ri]), max(ms[ri])) g = normalize(stds[gi], min(ss[gi]), max(ss[gi])) b = normalize(means[bi], min(ms[bi]), max(ms[bi])) r = r**power g = g**power b = b**power pylab.plot(x, y, '.', color=(r, g, b), markersize=10) #pylab.axis('off') #pylab.yscale('log') filename = name + ".png" if PLOT_PNG_BARE: ax.set_axis_off() fig.add_axes(ax) #pylab.title('') #pylab.savefig(name+".png", bbox_inches='tight', pad_inches=0) extent = ax.get_window_extent().transformed( fig.dpi_scale_trans.inverted()) pylab.savefig(filename, bbox_inches=extent) print "filename:\tX:", xlabel.title, xlabel.low, xlabel.high print "\t\tY:", ylabel.title, ylabel.low, ylabel.high else: pylab.savefig(filename) print "... done"
xs = sorted(d.keys()) ys = [None] * len(xs) for i in range(len(xs)): ys[i] = d[xs[i]] with open("out.csv", "w") as csv_stream: for tick in sorted(d.keys()): csv_stream.write(str(tick) + "," + str(d[tick])) for gen in info: if info[gen] == tick: csv_stream.write("," + gen) csv_stream.write("\n") X = np.arange(len(xs)) pl.bar(X, ys, align='edge', width=1) pl.xticks(X, xs, rotation='vertical') try: pl.ylim(float(sys.argv[7]), float(sys.argv[8])) except ValueError: print "Improper limit" except IndexError: try: pl.ylim(0, float(sys.argv[7])) except IndexError: pl.ylim(0, 1) pl.xlim(xleft * subdivide, xright * subdivide) fig = pl.gcf() #axis.set_title( "" )
imgRGB = cv2.cvtColor( img_C, cv2.COLOR_BGR2RGB ) # Passage BGR --> RGB via cv2.cvtColor (non utilisé par la suite) # 2.1.1 Affichage image via opencv --> affichage format BGR cv2.namedWindow("mon image BGR", cv2.WINDOW_NORMAL) # Pour dimensionner la fenetre d'affichage cv2.imshow("mon image BGR", img_C) cv2.namedWindow("mon image RGB", cv2.WINDOW_NORMAL) cv2.imshow("mon image RGB", img_C2) # Affichage image via plt --> affichage format RGB plt.figure() plt.subplot(121) plt.imshow(img_C) plt.xticks([]), plt.yticks([]) plt.title(" Mon image BGR") plt.subplot(122) plt.imshow(img_C2) plt.xticks([]), plt.yticks([]) plt.title(" Mon image RGB") plt.show() # 2.1.2 Affichage des trois canaux séparéments plt.figure() plt.subplot(131) plt.imshow(b, cmap='gray') plt.xticks([]), plt.yticks([]) plt.title(" Mon image B") plt.subplot(132) plt.imshow(g, cmap='gray')
width=ASLD_hist_ranges_bin_width, color=(0.75, 0.75, 0.75, 0.75), linewidth=0) p.bar(ASLD_hist_ranges, ASLD_to_hist_mean, width=ASLD_hist_ranges_bin_width, color='k', linewidth=0) p.axis([ 0, ASLD_hist_ranges.max(), 0, (ASLD_to_hist_mean + ASLD_to_hist_std).max() ]) p.xlabel('time passed between reproductions (steps)', fontsize=FontSize) #p.ylabel('Frequency of occurrence', fontsize = FontSize) p.ylabel('number of cells', fontsize=FontSize) p.xticks(size=TickSize) p.yticks(size=TickSize) p.grid(True) p.figure(2, figsize=(12, 6)) p.title('Turbulence level = %(turb)1.3f ; Death rate = %(death)1.3f' % { "turb": tubulence, "death": death_rate }) p.plot(x, y, 'ko-', markersize=4) p.axvspan(repr_mean_number_of_genes - repr_std_number_of_genes, repr_mean_number_of_genes + repr_std_number_of_genes, color=(0.75, 0.75, 0.75, 0.75), hatch='/') p.axvspan(real_mean_number_of_genes - real_std_number_of_genes, real_mean_number_of_genes + real_std_number_of_genes,
levels2 = [0.005, 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] tfig = [0, 10] tfig2 = [20, 30] nplot = -1 for k in range(len(tfig)): nplot = nplot + 1 alphat = np.transpose(alphap[:, :, 0, tfig[k]]) alphat2 = np.transpose(alphap[:, :, 0, tfig2[k]]) ax1 = fig.add_subplot(gs2[nplot, 0]) title('t=' + str(tfig[nplot] * Dt) + ' s') CS_colors = plt.contourf(Xp, Yp, alphat, nlevels, cmap=plt.cm.coolwarm) l0 = ax1.plot(Xp, ybed[:, tfig[k]], '--k', linewidth=1) axis([0, 0.5, -0.03, 0.03]) xticks([], []) props = dict(boxstyle='round', facecolor='none', alpha=1.0) xticks([], []) if nplot == 1: xticks([0, 0.1, 0.2, 0.3, 0.4, 0.5], ['0', '0.1', '0.2', '0.3', '0.4', '0.5']) xlabel('x (m)') yticks([-0.025, 0, 0.025], ['-0.025', '0', '0.025']) ax2 = fig.add_subplot(gs2[nplot, 1]) title('t=' + str(tfig2[nplot] * Dt) + ' s') CS_colors = plt.contourf(Xp, Yp, alphat2, nlevels, cmap=plt.cm.coolwarm) l0 = ax2.plot(Xp, ybed[:, tfig2[k]], '--k', linewidth=1) axis([0, 0.5, -0.03, 0.03])
pl.figure(**book_graphics.quarter_page_params) for ii, grid in enumerate('abcd'): dm = results[grid] pl.subplot(1, 4, ii + 1) dismod3.plotting.plot_intervals(dm, [ d for d in dm.data if dm.relevant_to(d, 'prevalence', region, year, sex) ], color='black', print_sample_size=False, alpha=1., plot_error_bars=False, linewidth=2) book_graphics.plot_rate(dm, dismod3.utils.gbd_key_for('prevalence', region, year, sex), linestyle='-') pl.axis([10, 60, -.05, .8]) pl.xlabel('Age (Years)') pl.xticks([15, 35, 55]) if ii == 0: pl.yticks([0, .2, .4, .6], [0, 20, 40, 60]) pl.ylabel('Prevalence (per 100)') else: pl.yticks([0, .2, .4, .6], ['', '', '', '']) pl.text(12, .75, '(%s)' % grid, va='top', ha='left') pl.subplots_adjust(wspace=0, bottom=.15, left=.1, right=.99, top=.97) pl.savefig('pms_grids.pdf') book_graphics.save_json('pms_grids.json', vars())
def myimshow(*args, **kwargs): plt.imshow(*args, **kwargs) plt.xticks([]) plt.yticks([]) plt.axis(imExt(afwimg))
def extrapolate(x, y, n=-1.5, plot=0, reg=0, txt=None): '''Extrapolation tool. Mainly intended for RPA correlation energies, but could be useful for other purposes. Fits a straight line to an expression of the form: y=b + alpha*x**n and extrapolates the result to infinite x. reg=N gives linear regression using the last N points in x. reg should be larger than 2''' if txt is None: f = sys.stdout elif isinstance(txt, basestring): f = paropen(txt, 'a') else: f = txt assert len(x) == len(y) ext = [] print('Two-point extrapolation:', file=f) for i in range(len(x) - 1): alpha = (y[i] - y[i + 1]) / (x[i]**n - x[i + 1]**n) ext.append(y[i + 1] - alpha * x[i + 1]**n) print(' ', x[i], '-', x[i + 1], ':', ext[-1], file=f) print(file=f) if plot: import pylab as pl #pl.subplot(211) pl.plot(x**n, y, 'o-', label='Data') pl.xticks(x**n, [int(e) for e in x]) pl.axis([0, None, None, None]) if reg > 2: a = x[-reg:]**n b = y[-reg:] N = reg delta = N * np.sum(a**2) - (np.sum(a))**2 A = (np.sum(a**2) * np.sum(b) - np.sum(a) * np.sum(a * b)) / delta B = (N * np.sum(a * b) - np.sum(a) * np.sum(b)) / delta sigma_y = (1. / (N - 2.) * np.sum((b - A - B * a)**2))**0.5 sigma_A = sigma_y * (np.sum(a**2) / delta)**0.5 print('Linear regression using last %s points:' % N, file=f) print(' Extrapolated result:', A, file=f) print(' Uncertainty:', sigma_A, file=f) print(file=f) if plot: print([a[0], 0], [A + B * a[0], A]) pl.plot([a[0], 0], [A + B * a[0], A], '--', label='Regression') pl.legend(loc='upper left') else: A = 0 B = 0 sigma_A = 0 if plot: pl.show() #pl.subplot(212) pl.plot(x[1:], ext, 'o-', label='Two-point extrapolation') if reg > 2: pl.plot([x[-reg], x[-1]], [A, A], '--', label='Regression') pl.errorbar(x[-2], A, yerr=sigma_A, elinewidth=2.0, capsize=5, c='g') pl.legend(loc='lower right') pl.show() if not txt is None: f.close() return ext, A, B, sigma_A
pl.figure(figsize=(8, 6), dpi=80) # Crear una nueva subgráfica en una rejilla de 1x1 pl.subplot(1, 1, 1) X = np.linspace(-np.2*pi, np.2*pi, 256, endpoint=True) C, S = np.cos(X), np.sin(X) # Graficar la función coseno con una línea continua azul de 1 pixel de grosor pl.plot(X, C, color="blue", linewidth=1.0, linestyle="-") # Graficar la función seno con una línea continua verde de 1 pixel de grosor pl.plot(X, S, color="green", linewidth=1.0, linestyle="-") # Establecer límites del eje x pl.xlim(-4.0, 4.0) # Ticks en x pl.xticks(np.linspace(-4, 4, 9, endpoint=True)) # Establecer límites del eje y pl.ylim(-1.0, 1.0) # Ticks en y pl.yticks(np.linspace(-1, 1, 5, endpoint=True)) # Guardar la figura usando 72 puntos por pulgada # savefig("exercice_2.png", dpi=72) # Mostrar resultado en pantalla pl.show()
def plotMixtureStructure(mix,headers,transpose=1): plot = np.zeros(( mix.G,mix.dist_nr ) ) for i in range(mix.dist_nr): #print "-------------\n",headers[i] #print mix.leaders[i] #print mix.groups[i] # check for noise variables if len(mix.leaders[i]) == 1: l = mix.leaders[i][0] for g in range(mix.G): plot[g,i] = 1 else: for l in mix.leaders[i]: #if len(mix.groups[i][l]) == 0: # plot[l,i] = 2 # else: plot[l,i] = l+3 for g in mix.groups[i][l]: plot[g,i] = l+3 for j in range(mix.dist_nr): t = {} t[2] = 2 t[1] = 1 index = 3 v_list = [] for v in plot[:,j]: if v == 2: continue elif v == 1: break else: if v not in v_list: v_list.append(v) v_list.sort() #print plot[:,j] #print v_list for v in v_list: t[v] = index index +=1 for i in range(mix.G): plot[i,j] = t[plot[i,j]] #for p in plot: # print p.tolist() #print plot #pylab.subplot(1,2,1) #x = np.array([0.0,5.0],dtype='Float64') #y = np.array([0.0,5.0],dtype='Float64') #pylab.plot(x,y,'o') #pylab.subplot(1,2,2) z = np.array(plot) if transpose: #print z.shape z = z.transpose() #print z.shape mat = pylab.matshow(z) # pylab.setp(mat) # print mat.axes # print mat.figure pylab.grid(True,linestyle='-',linewidth=0.5) # set xticks #pylab.xticks( np.arange(len(headers))+0.5,['']*len(headers), size=12) xtickpos = np.arange(0.0, mix.G+1,0.5) temp = zip(range(mix.G), ['']*mix.G) xticklabels = [] for tt in temp: xticklabels += list(tt) pylab.xticks( xtickpos,xticklabels) # rotation='vertical',size=12 #print xtickpos #print xticklabels # remove grid lines for tick lines at label positions xgridlines = pylab.getp(pylab.gca(), 'xgridlines') #print xgridlines for j in range(len(xticklabels)): if xticklabels[j] != '': xgridlines[j].set_linestyle('None') # set yticks ytickpos = np.arange(0.0, len(headers),0.5) temp = zip(headers, ['']*len(headers)) yticklabels = [] for tt in temp: yticklabels += list(tt) pylab.yticks( ytickpos, yticklabels, size=8) # remove grid lines for tick lines at label positions ygridlines = pylab.getp(pylab.gca(), 'ygridlines') #print len(ygridlines), len(ytickpos), len(yticklabels) for j,yl in enumerate(ygridlines): if yticklabels[j] != '': yl.set_linestyle('None') #pylab.setp(ygridlines, 'linestyle', 'None') #pylab.yticks( np.arange(len(headers))+0.5) # loc, ll = pylab.yticks() # for lll in ll: # print lll,lll.get_dashlength() # #lll.set_dashlength(-1) #plotMixture.pylab.figtext(0.15,0.5,'T1',size=20, weight='bold') #plotNormalMixtureDensitture.pylab.figtext(0.925,0.5,'T1',size=20, weight='bold') #start = 0.15 # start = 0.18 # end = 0.925 # # space = end - start # step = space / mix.dist_nr #for i in range(0,mix.dist_nr): # XXX HACK for two components #pylab.figtext(0.12,0.63,'$C_1$',size=20, weight='bold') #pylab.figtext(0.12,0.26,'$C_2$',size=20, weight='bold') else: fig = pylab.matshow(z) pylab.grid(True,linestyle='-',linewidth=0.5) #pylab.yticks(np.arange(mix.G+1) ) #pylab.xticks( np.arange(len(plot[0])+1)+0.5,headers,rotation=90,size=12) #fig.set_size_inches(20,20) # set xticks #pylab.xticks( np.arange(len(headers))+0.5,['']*len(headers), size=12) xtickpos = np.arange(0.0, len(headers),0.5) temp = zip(headers, ['']*len(headers)) xticklabels = [] for tt in temp: xticklabels += list(tt) pylab.xticks( xtickpos,xticklabels) # rotation='vertical',size=12 print len(xtickpos), xtickpos.tolist() print len(xticklabels), xticklabels # remove grid lines for tick lines at label positions xgridlines = pylab.getp(pylab.gca(), 'xgridlines') print xgridlines for j in range(len(xticklabels)): if xticklabels[j] != '': xgridlines[j].set_linestyle('None') # set yticks ytickpos = np.arange(0.0, mix.G,0.5) temp = zip(range(mix.G), ['']*mix.G) yticklabels = [] for tt in temp: yticklabels += list(tt) pylab.yticks( ytickpos, yticklabels, size=12) print len(ytickpos), ytickpos.tolist() print len(yticklabels), yticklabels # remove grid lines for tick lines at label positions ygridlines = pylab.getp(pylab.gca(), 'ygridlines') #print len(ygridlines), len(ytickpos), len(yticklabels) for j,yl in enumerate(ygridlines): if yticklabels[j] != '': yl.set_linestyle('None')
import pandas as pd import numpy as np import matplotlib.pyplot as plt import pylab as pl ##构造函数生成topol不同类型的频数汇总DataFrame表 ##函数说明:输入文件路径生成topol频数表 def topol_bar(a): #a:文件路径,注意:'\'都要使用‘\\’ df = pd.read_excel(a,sheet_name='Sheet1')#读取整张表格 dft = df.pivot_table(values = ['phase'],index = ['topol'],aggfunc = 'count')#Pivot.table dft.rename(columns={'phase':'Fred'}, inplace = True) return dft ##使用topol_bar函数将两个表格中topol例数据分类汇总 df1 = topol_bar('C:\\Users\\Administrator\\Desktop\\Excel_Pandas\\1\\3.xls') df2 = topol_bar('C:\\Users\\Administrator\\Desktop\\Excel_Pandas\\1\\4.xls') ##绘制直方图并显示 df1.plot(kind='bar',width = 0.35).get_figure()#直方图,宽度0.35 plt.title('3.xls Topol-Frequency')#设置标题 pl.xticks(rotation=360)#旋转xlable到水平位置 plt.ylabel('Frequency')#设置y轴lable plt.yticks(np.arange(-0, 60, 5))#设置y轴范围,下同 df2.plot(kind='bar',width = 0.23).get_figure() plt.title('4.xls Topol-Frequency') pl.xticks(rotation=360) plt.ylabel('Frequency') plt.yticks(np.arange(-0, 60, 5)) plt.show()#显示图表