def plot_ave(results_list, title): """ show average with error bars""" pylab.clf() pylab.figure().autofmt_xdate() x_range = range(len(results_list[0])) err_x, err_y, std_list = [], [], [] for i in x_range: if i % 10 == 0: #get average for each generation column = [] for result in results_list: column.append(result[i]) average = np.average(column) std_dev = np.std(column) err_x.append(i) err_y.append(average) std_list.append(std_dev) pylab.errorbar(err_x, err_y, yerr=std_list) title += '_average' pylab.title(title) if not os.path.exists('./graphs'): os.makedirs('./graphs') filename = 'graphs/' + title + FILETYPE pylab.savefig(filename)
def plot_heatingrate(data_dict, filename, do_show=True): pl.figure(201) color_list = ['b','r','g','k','y','r','g','b','k','y','r',] fmtlist = ['s','d','o','s','d','o','s','d','o','s','d','o'] result_dict = {} for key in data_dict.keys(): x = data_dict[key][0] y = data_dict[key][1][:,0] y_err = data_dict[key][1][:,1] p0 = np.polyfit(x,y,1) fit = LinFit(np.array([x,y,y_err]).transpose(), show_graph=False) p1 = [0,0] p1[0] = fit.param_dict[0]['Slope'][0] p1[1] = fit.param_dict[0]['Offset'][0] print fit x0 = np.linspace(0,max(x)) cstr = color_list.pop(0) fstr = fmtlist.pop(0) lstr = key + " heating: {0:.2f} ph/ms".format((p1[0]*1e3)) pl.errorbar(x/1e3,y,y_err,fmt=fstr + cstr,label=lstr) pl.plot(x0/1e3,np.polyval(p0,x0),cstr) pl.plot(x0/1e3,np.polyval(p1,x0),cstr) result_dict[key] = 1e3*np.array(fit.param_dict[0]['Slope']) pl.xlabel('Heating time (ms)') pl.ylabel('nbar') if do_show: pl.legend() pl.show() if filename != None: pl.savefig(filename) return result_dict
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 demo(): import pylab # The module normalize is not part of the osrefl code base. from reflectometry.reduction import normalize from .examples import ng7 as dataset spec = dataset.spec()[0] water = WaterIntensity(D2O=20,probe=spec.probe) spec.apply(normalize()) theory = water.model(spec.Qz,spec.detector.wavelength) pylab.subplot(211) pylab.title('Data normalized to water scattering (%g%% D2O)'%water.D2O) pylab.xlabel('Qz (inv Ang)') pylab.ylabel('Reflectivity') pylab.semilogy(spec.Qz,theory,'-',label='expected') scale = theory[0]/spec.R[0] pylab.errorbar(spec.Qz,scale*spec.R,scale*spec.dR,fmt='.',label='measured') spec.apply(water) pylab.subplot(212) #pylab.title('Intensity correction factor') pylab.xlabel('Slit 1 opening (mm)') pylab.ylabel('Incident intensity') pylab.yscale('log') pylab.errorbar(spec.slit1.x,spec.R,spec.dR,fmt='.',label='correction') pylab.show()
def plot_sed(fluxes, backgrounds, errors, **kwargs): """ Trivial SED plotting """ pl.errorbar(band_waves.values(),fluxes-backgrounds,yerr=errors,marker='s', **kwargs) pl.xlabel('$\lambda$ (mm)') pl.ylabel('mJy/beam')
def romanzuniga07(wavelength, AKs, makePlot=False): filters = ['J', 'H', 'Ks', '[3.6]', '[4.5]', '[5.8]', '[8.0]'] wave = np.array([1.240, 1.664, 2.164, 3.545, 4.442, 5.675, 7.760]) A_AKs = np.array([2.299, 1.550, 1.000, 0.618, 0.525, 0.462, 0.455]) A_AKs_err = np.array([0.530, 0.080, 0.000, 0.077, 0.063, 0.055, 0.059]) # Interpolate over the curve spline_interp = interpolate.splrep(wave, A_AKs, k=3, s=0) A_AKs_at_wave = interpolate.splev(wavelength, spline_interp) A_at_wave = AKs * A_AKs_at_wave if makePlot: py.clf() py.errorbar(wave, A_AKs, yerr=A_AKs_err, fmt='bo', markerfacecolor='none', markeredgecolor='blue', markeredgewidth=2) # Make an interpolated curve. wavePlot = np.arange(wave.min(), wave.max(), 0.1) extPlot = interpolate.splev(wavePlot, spline_interp) py.loglog(wavePlot, extPlot, 'k-') # Plot a marker for the computed value. py.plot(wavelength, A_AKs_at_wave, 'rs', markerfacecolor='none', markeredgecolor='red', markeredgewidth=2) py.xlabel('Wavelength (microns)') py.ylabel('Extinction (magnitudes)') py.title('Roman Zuniga et al. 2007') return A_at_wave
def NormDeltaRvT(folder,keys): if folder[0]['IVtemp']<250 and folder[0]['IVtemp']>5: APiterator = [5,10] AP = Analysis.AnalyseFile() P = Analysis.AnalyseFile() tsum = 0.0 for f in folder: if f['iterator'] in APiterator: AP.add_column(f.column('Voltage'),str(f['iterator'])) else: P.add_column(f.column('Voltage'),str(f['iterator'])) tsum = tsum + f['Sample Temp'] AP.apply(func,0,replace=False,header='Mean NLV') AP.add_column(f.Current,column_header = 'Current') P.apply(func,0,replace=False,header='Mean NLV') P.add_column(f.Current,column_header = 'Current') APfit= AP.curve_fit(quad,'Current','Mean NLV',bounds=lambda x,y:x,result=True,header='Fit',asrow=True) Pfit = P.curve_fit(quad,'Current','Mean NLV',bounds=lambda x,y:x,result=True,header='Fit',asrow=True) DeltaR = Pfit[2] - APfit[2] ErrDeltaR = numpy.sqrt((Pfit[3]**2)+(APfit[3]**2)) Spinsig.append(DeltaR/Res_Cu(tsum/10)) Spinsig_error.append(ErrDeltaR) Temp.append(tsum/10) plt.hold(True) plt.title('$\Delta$R$_s$ vs T from linear coef of\nNLIV fit for '+f['Sample ID'],verticalalignment='bottom') plt.xlabel('Temperture (K)') plt.ylabel(r'$\Delta$R$_s$/$\rho$') plt.errorbar(f['IVtemp'],1e3*DeltaR,1e3*ErrDeltaR,ecolor='k',marker='o',mfc='r', mec='k') #plt.plot(f['IVtemp'],ErrDeltaR,'ok') return Temp, Spinsig
def plotting_cc_group(outdir, outnpz, mfrac, Fruns, pixel_scale, size, \ figure, label, color, ms=8): xi_a = [] for fi in range(Fruns): ofile = os.path.join(outdir, outnpz%(fi, mfrac)) if not os.path.exists(ofile): continue f = np.load(ofile) theta = f['theta'] if len(xi_a) < 1: xi_a = f['yprof'] else: xi_a = np.row_stack((xi_a, f['yprof'])) if len(xi_a.shape) > 1: xi = xi_a.mean(axis=0) xie = xi_a.std(axis=0) N = xi_a.shape[0] xie /= np.sqrt(N) else: xi = xi_a.copy() xie = 0.0 N = 1.0 pl.figure(figure) ax = pl.subplot(111) pl.errorbar(theta, xi, xie, c=color, marker='o', ms=ms, label=label) return ax, theta, N
def plot(self, params, errors=None,label=''): params=[max(1e-100,p) for p in params] E=np.concatenate(([self._ERange[0]],self._splitE,[self._ERange[1]])) pl.plot(reduce(lambda a,b:a+b,[[e,e] for e in E]),[1e-10]+reduce(lambda a,b:a+b,[[p,p] for p in params])+[1e-10],label=label) if errors!=None: for i in range(len(E)-1): pl.errorbar([np.sqrt(E[i]*E[i+1])],[params[i]],yerr=[errors[i]],fmt='r')
def _show_plots(target, fitted, wt, wo, corrected): tau_NP, tau_P, attenuator, rate = target tau_NP_f, tau_P_f, attenuation_f, rate_f = fitted # Plot the results sim_pars = (r'Sim $\tau_{NP}=%g\,{\rm %s}$, $\tau_{P}=%g\,{\rm %s}$, ${\rm attenuator}=%g$' )%(tau_NP, DEADTIME_UNITS, tau_P, DEADTIME_UNITS, attenuator) fit_pars = (r'Fit $\tau_{NP}=%s$, $\tau_P=%s$, ${\rm attenuator}=%.2f$' )%( ("%.2f"%tau_NP_f[0] if np.inf > tau_NP_f[1] > 0 else "-"), ("%.2f"%tau_P_f[0] if np.inf > tau_P_f[1] > 0 else "-"), 1./attenuation_f[0], ) title = '\n'.join((sim_pars, fit_pars)) import pylab pylab.subplot(211) #pylab.errorbar(rate, rate_f[0], yerr=rate_f[1], fmt='c.', label='fitted rate') #mincident = np.linspace(rate[0], rate[-1], 400) #munattenuated = expected_rate(mincident, tau_NP_f[0], tau_P_f[0]) #mattenuated = expected_rate(mincident/attenuator, tau_NP_f[0], tau_P_f[0]) #minc = np.hstack((mincident, 0., mincident)) #mobs = np.hstack((munattenuated, np.NaN, mattenuated)) #pylab.plot(minc, mobs, 'c-', label='expected rate') pylab.errorbar(rate, uval(corrected), yerr=udev(corrected), fmt='r.', label='corrected rate') _show_rates(rate, wo, wt, attenuator, tau_NP_f[0], tau_P_f[0]) pylab.subplot(212) _show_droop(rate, wo, wt, attenuator) pylab.suptitle(title) #pylab.figure(); _show_inversion(wo, tau_P_f, tau_NP_f) pylab.show()
def _show_rates(rate, wo, wt, attenuator, tau_NP, tau_P): import pylab #pylab.figure() pylab.errorbar(rate, wt[0], yerr=wt[1], fmt='g.', label='attenuated') pylab.errorbar(rate, wo[0], yerr=wo[1], fmt='b.', label='unattenuated') pylab.xscale('log') pylab.yscale('log') pylab.xlabel('incident rate (counts/second)') pylab.ylabel('observed rate (counts/second)') pylab.legend(loc='best') pylab.grid(True) pylab.plot(rate, rate/attenuator, 'g-', label='target') pylab.plot(rate, rate, 'b-', label='target') Ipeak, Rpeak = peak_rate(tau_NP=tau_NP, tau_P=tau_P) if rate[0] <= Ipeak <= rate[-1]: pylab.axvline(x=Ipeak, ls='--', c='b') pylab.text(x=Ipeak, y=0.05, s=' %g'%Ipeak, ha='left', va='bottom', transform=pylab.gca().get_xaxis_transform()) if False: pylab.axhline(y=Rpeak, ls='--', c='b') pylab.text(y=Rpeak, x=0.05, s=' %g\n'%Rpeak, ha='left', va='bottom', transform=pylab.gca().get_yaxis_transform())
def plotCorr(self,pars=None,SHOW=True,SAVE=True): skipfits=True pb.clf() for irn in range(len(self.ranges)): for ist in range(len(self.params)/3): pb.errorbar(np.arange(len(self.Pcorr[irn,ist,:]))/self.Fs,self.Pcorr[irn,ist,:],yerr=self.PcorrERR[irn,ist,:],color=colours[ist]) pb.ylim([0,1]) pb.grid(True) pb.xlabel('Time (s)',fontsize=20) pb.ylabel('Probabilities',fontsize=20) pb.xticks(fontsize=16) pb.yticks(fontsize=16) if SAVE: fn=os.path.basename(self.filename) pb.savefig(self.figdir+'corr_'+'range_'+str(irn)+fn[:-4]+'.eps') f=open(self.datdir+fn[:-4]+'.dat','a') f.seek(0,2) f.write('#corr_range_'+str(irn)+'\n') #f.write('BinCentre(pA)\tBinMin(pA)\tBinMax(pA)\tCounts\n') #for i in range(len(self.n)): # f.write(str(self.x[i])+'\t'+str(self.bins[i])+'\t'+str(self.bins[i+1])+'\t'+str(self.n[i])+'\n') f.close() if SHOW:pb.show() pb.clf() return
def plot_data(yRange=None): ''' Plots and saves the cell measurement data. Returns nothing. ''' fig = plt.figure(figsize=(18,12)) ax = plt.subplot(111) plt.errorbar(range(len(avgCells.index)), avgCells[column], yerr=stdCells[column], fmt='o') ax = plt.gca() ax.set(xticks=range(len(avgCells.index)), xticklabels=avgCells.index) xlims = ax.get_xlim() ax.set_xlim([lim-1 for lim in xlims]) # adjust yRange if it was specified if yRange!=None: ax.set_ylim(yRange) fileName = column + ' exlcuding outliers' else: fileName = column plt.subplots_adjust(bottom=0.2, right=0.98, left=0.05) plt.title(column) plt.ylabel('mm') locs, labels = plt.xticks() plt.setp(labels, rotation=90) mng = plt.get_current_fig_manager() mng.window.state('zoomed') #plt.show() path1 = 'Y:/Test data/ACT02/vision inspection/plot_100_cells/' path2 = 'Y:/Nate/git/nuvosun-python-lib/vision system/plot_100_cells/' fig.savefig(path1 + fileName, bbox_inches = 'tight') fig.savefig(path2 + fileName, bbox_inches = 'tight') plt.close()
def compare_models(db, stoch="itn coverage", stat_func=None, plot_type="", **kwargs): if stat_func == None: stat_func = lambda x: x X = {} for k in sorted(db.keys()): c = k.split("_")[2] X[c] = [] for k in sorted(db.keys()): c = k.split("_")[2] X[c].append([stat_func(x_ki) for x_ki in db[k].__getattribute__(stoch).gettrace()]) x = pl.array([pl.mean(xc[0]) for xc in X.values()]) xerr = pl.array([pl.std(xc[0]) for xc in X.values()]) y = pl.array([pl.mean(xc[1]) for xc in X.values()]) yerr = pl.array([pl.std(xc[1]) for xc in X.values()]) if plot_type == "scatter": default_args = {"fmt": "o", "ms": 10} default_args.update(kwargs) for c in X.keys(): pl.text(pl.mean(X[c][0]), pl.mean(X[c][1]), " %s" % c, fontsize=8, alpha=0.4, zorder=-1) pl.errorbar(x, y, xerr=xerr, yerr=yerr, **default_args) pl.xlabel("First Model") pl.ylabel("Second Model") pl.plot([0, 1], [0, 1], alpha=0.5, linestyle="--", color="k", linewidth=2) elif plot_type == "rel_diff": d1 = sorted(100 * (x - y) / x) d2 = sorted(100 * (xerr - yerr) / xerr) pl.subplot(2, 1, 1) pl.title("Percent Model 2 deviates from Model 1") pl.plot(d1, "o") pl.xlabel("Countries sorted by deviation in mean") pl.ylabel("deviation in mean (%)") pl.subplot(2, 1, 2) pl.plot(d2, "o") pl.xlabel("Countries sorted by deviation in std err") pl.ylabel("deviation in std err (%)") elif plot_type == "abs_diff": d1 = sorted(x - y) d2 = sorted(xerr - yerr) pl.subplot(2, 1, 1) pl.title("Percent Model 2 deviates from Model 1") pl.plot(d1, "o") pl.xlabel("Countries sorted by deviation in mean") pl.ylabel("deviation in mean") pl.subplot(2, 1, 2) pl.plot(d2, "o") pl.xlabel("Countries sorted by deviation in std err") pl.ylabel("deviation in std err") else: assert 0, "plot_type must be abs_diff, rel_diff, or scatter" return pl.array([x, y, xerr, yerr])
def show_table(table_name,ls="none", fmt="o", legend=False, name="m", do_half=0): bt = fi.FITS(table_name)[1].read() rgpp = (np.unique(bt["rgp_lower"])+np.unique(bt["rgp_upper"]))/2 nbins = rgpp.size plt.xscale("log") colours=["purple", "forestgreen", "steelblue", "pink", "darkred", "midnightblue", "gray", "sienna", "olive", "darkviolet"] pts = ["o", "D", "x", "^", ">", "<", "1", "s", "*", "+", "."] for i,r in enumerate(rgpp): sel = (bt["i"]==i) snr = 10** ((np.log10(bt["snr_lower"][sel]) + np.log10(bt["snr_upper"][sel]))/2) if do_half==1 and i>nbins/2: continue elif do_half==2 and i<nbins/2: continue if legend: plt.errorbar(snr, bt["%s"%name][i*snr.size:(i*snr.size)+snr.size], bt["err_%s"%name][i*snr.size:(i*snr.size)+snr.size], color=colours[i], ls=ls, fmt=pts[i], lw=2.5, label="$R_{gpp}/R_p = %1.2f-%1.2f$"%(np.unique(bt["rgp_lower"])[i],np.unique(bt["rgp_upper"])[i])) else: plt.errorbar(snr, bt["%s"%name][i*snr.size:(i*snr.size)+snr.size], bt["err_%s"%name][i*snr.size:(i*snr.size)+snr.size], color=colours[i], ls=ls, fmt=pts[i], lw=2.5) plt.xlim(10,300) plt.axhline(0, lw=2, color="k") plt.xlabel("Signal-to-Noise $SNR_w$") if name=="m": plt.ylim(-0.85,0.05) plt.ylabel("Multiplicative Bias $m \equiv (m_1 + m_2)/2$") elif name=="alpha": plt.ylabel(r"PSF Leakage $\alpha \equiv (\alpha _1 + \alpha _2)/2$") plt.ylim(-0.5,2) plt.legend(loc="lower right")
def RJmcmc_LRG_check(indir,bins=1): 'looks at LRG output from fit_all.py Looks at all .pik files in dir and plots them' if not indir[-1]=='/': indir+='/' #load file names files=os.listdir(indir) print 'Plotting from %i bin' %bins age,norm,metal,Z=[],[],[],[] for i in files: try: temp=pik.load(open(indir+i)) lab.figure() mc.plot_model(temp[0][str(bins)][temp[1][str(bins)].min()== temp[1][str(bins)]][0], temp[3][i[:-4]],bins) lab.title(i) #get median,lower, upper bound of parameters age.append(nu.percentile(sigmaclip(10**temp[0][str(bins)][:,1])[0],[50,15.9,84.1])) metal.append(nu.percentile(sigmaclip(temp[0][str(bins)][:,0])[0],[50,15.9,84.1])) norm.append(nu.percentile(sigmaclip(temp[0][str(bins)][:,2])[0],[50,15.9,84.1])) Z.append(float(i[:-4])) except: continue age,metal,norm,Z=nu.array(age),nu.array(metal),nu.array(norm),nu.array(Z) #make uncertanties relitive age[:,1],age[:,2]=nu.abs(age[:,0]-age[:,1]),nu.abs(age[:,0]-age[:,2]) age=age/10**9. lab.figure() lab.errorbar(Z,age[:,0],yerr=age[:,1:].T,fmt='.') lab.xlabel('Redshift') lab.ylabel('Age (Gyr)') lab.show()
def scatter_stats(db, s1, s2, f1=None, f2=None, **kwargs): if f1 == None: f1 = lambda x: x # constant function if f2 == None: f2 = f1 x = [] xerr = [] y = [] yerr = [] for k in db: x_k = [f1(x_ki) for x_ki in db[k].__getattribute__(s1).gettrace()] y_k = [f2(y_ki) for y_ki in db[k].__getattribute__(s2).gettrace()] x.append(pl.mean(x_k)) xerr.append(pl.std(x_k)) y.append(pl.mean(y_k)) yerr.append(pl.std(y_k)) pl.text(x[-1], y[-1], " %s" % k, fontsize=8, alpha=0.4, zorder=-1) default_args = {"fmt": "o", "ms": 10} default_args.update(kwargs) pl.errorbar(x, y, xerr=xerr, yerr=yerr, **default_args) pl.xlabel(s1) pl.ylabel(s2)
def condense_node(self,index): qnode=self.qlist[index] print qnode.q #print qnode.th a3=[] counts=[] counts_err=[] monlist=[] for mydataitem in qnode.th: mydata=mydataitem.data monlist.append(mydata.metadata['count_info']['monitor']) counts_err.append(N.array(mydata.data['counts_err'])) counts.append(N.array(mydata.data['counts'])) a3.append(N.array(mydata.data['a3'])) a3_out,counts_out,counts_err_out=simple_combine(a3,counts,counts_err,monlist) #print a3_out.shape #print counts_out.shape #print counts_err_out.shape qnode.th_condensed={} qnode.th_condensed['a3']=a3_out qnode.th_condensed['counts']=counts_out qnode.th_condensed['counts_err']=counts_err_out print qnode.th_condensed['counts'].std() print qnode.th_condensed['counts'].mean() print qnode.th_condensed['counts'].max() print qnode.th_condensed['counts'].min() if 0: pylab.errorbar(a3_out,counts_out,counts_err_out,marker='s',linestyle='None',mfc='black',mec='black',ecolor='black') pylab.show() return
def plot_masses(ps, rs, scs, save=False, name=''): p.figure p.rc('text', usetex=True) p.rc('font', size=18) p.xlabel('$am_1 + am_2$') p.ylabel('$aM_{vs}$') legend = () # Pions. xs, ys, es = zip(*[q.pt for q in ps]) # Unpack data. legend += p.errorbar(xs, ys, es, fmt='o')[0], # Rhoxs. #xs, ys, es = zip(*[r.pt for r in rxs]) # Unpack data. #legend += p.errorbar(xs, ys, es, fmt='o')[0], # Rhos. xs, ys, es = zip(*[r.pt for r in rs]) # Unpack data. legend += p.errorbar(xs, ys, es, fmt='o')[0], # Scalars. xs, ys, es = zip(*[r.pt for r in scs]) # Unpack data. legend += p.errorbar(xs, ys, es, fmt='o')[0], p.legend(legend, ('$\pi$', r'$\rho$', '$a_0$'), 'best') if save: p.savefig(name) else: p.show()
def blocks_per_trial(experiment, neutral_blocks = False, clf = True, fig_no = 1, last_n = 6): days = set([s.day for s in experiment.get_sessions('all', 'all')]) residual_trials = np.zeros(len(experiment.subject_IDs)) # Number of trials in last (uncompleted) block of session. mean_bpt, sd_bpt = ([],[]) # Lists to hold mean and standard deviation of blocks per trial for each day. for day in days: day_blocks_per_trial = [] sessions = experiment.get_sessions('all', day) for session in sessions: assert hasattr(session,'blocks'), 'Session does not have block info.' ax = experiment.subject_IDs.index(session.subject_ID) # Index used for residual trials array. blocks_per_trial, residual_trials[ax] = _session_blocks_per_trial(session, residual_trials[ax], neutral_blocks) day_blocks_per_trial.append(blocks_per_trial) mean_bpt.append(ut.nanmean(day_blocks_per_trial)) sd_bpt.append (np.sqrt(ut.nanvar(np.array(day_blocks_per_trial)))) days = np.array(list(days))-min(days) + 1 p.figure(fig_no) if clf: p.clf() p.subplot(2,1,1) p.errorbar(days, mean_bpt, sd_bpt/np.sqrt(len(experiment.subject_IDs))) p.xlim(0.5, max(days) + 0.5) p.ylim(ymin = 0) p.ylabel('Blocks per trial') p.subplot(2,1,2) p.plot(days,1 / np.array(mean_bpt)) p.xlabel('Day') p.ylabel('Trials per block')
def create_report_var(name, mean, cov, inf, minimum=None, maximum=None): report = Node(id=name) report.data("covariance", cov) report.data("information", inf) node_mean = report.data("mean", mean) with node_mean.data_file("bounds", "image/png") as filename: e = 3 * sqrt(cov.diagonal()) pylab.ioff() f = pylab.figure() x = range(len(mean)) pylab.errorbar(x, mean, yerr=e) if minimum is not None: pylab.plot(x, minimum, "b-") if maximum is not None: pylab.plot(x, maximum, "b-") pylab.savefig(filename) pylab.close(f) f = report.figure(name) f.sub("mean/bounds", caption="Mean") f.sub("covariance", caption="Covariance", display="posneg") f.sub("information", caption="Information", display="posneg") return report
def plot_one_ppc(model, t): """ plot data and posterior predictive check :Parameters: - `model` : data.ModelData - `t` : str, data type of 'i', 'r', 'f', 'p', 'rr', 'm', 'X', 'pf', 'csmr' """ stats = model.vars[t]['p_pred'].stats() if stats == None: return pl.figure() pl.title(t) x = model.vars[t]['p_obs'].value.__array__() y = x - stats['quantiles'][50] yerr = [stats['quantiles'][50] - pl.atleast_2d(stats['95% HPD interval'])[:,0], pl.atleast_2d(stats['95% HPD interval'])[:,1] - stats['quantiles'][50]] pl.errorbar(x, y, yerr=yerr, fmt='ko', mec='w', capsize=0, label='Obs vs Residual (Obs - Pred)') pl.xlabel('Observation') pl.ylabel('Residual (observation-prediction)') pl.grid() l,r,b,t = pl.axis() pl.hlines([0], l, r) pl.axis([l, r, y.min()*1.1 - y.max()*.1, -y.min()*.1 + y.max()*1.1])
def p2dscatter(self, log=False, color=None, label=None, orientation='horizontal', **kwargs): """ use pylab.errorplot to visualize these scatter points Parameters: log : if true create logartihmic plot (all other kwargs will be passed to pylab.errobar) """ if len(self.x) == 0: return ax = p.gca() if color is None: color = next(ax._get_lines.color_cycle) kw = {"xerr" : self.xerr, "yerr" : self.yerr, "fmt" : "k", "capsize" : 0., "linestyle" : 'None', "color" : color} kw.update(kwargs) if orientation == 'vertical': x, y = self.y, self.x kw["xerr"], kw["yerr"] = kw["yerr"], kw["xerr"] axis_name = 'x' else: x, y = self.x, self.y axis_name = 'y' _set_logscale(ax, log, axis=axis_name) p.errorbar(x, y, **kw) if not hasattr(ax, "_legend_proxy"): ax._legend_proxy = LegendProxy(ax) ax._legend_proxy.add_scatter(label=label, color=color) _h2label(self, orientation)
def plot_dmsq2(HOpions, OOpions, title=None, save=False, name=''): "Plot m^2_{vs} - m^2_{vv}/2." # Set up figure. fig = p.figure() p.rc('text', usetex=True) p.rc('font', size=16) p.rc('axes', linewidth=0.5) p.xlabel('$am_{s}$') p.ylabel('$m^2_{vs} - m^2_{vv}/2$') legend = () xr = np.linspace(0.0,0.06) # First data set. hopions = [HOpions[1], HOpions[5]] r = OOpions[3] xs = [q.m1 for q in hopions] ys = [(q.msq - r.msq/2) for q in hopions] es = [nerror(q.sig_msq, r.sig_msq) for q in hopions] fit = line_fit2(zip(xs,ys,es)) legend += p.errorbar(xs, ys, fmt='bo')[0], # Fit results p.errorbar(xr, fit.a+fit.b*xr, fmt='b-') print fit.a, fit.sig_a if save: p.savefig(name) else: p.show()
def plot_results(self, results, xloc, color, ls, label): iter_counts = sorted(set([it for it, av in results.keys() if av == self.average])) sorted_results = [results[it, self.average] for it in iter_counts] avg = np.array([r.train_logprob() for r in sorted_results]) if hasattr(r, 'train_logprob_interval'): lower = np.array([r.train_logprob_interval()[0] for r in sorted_results]) upper = np.array([r.train_logprob_interval()[1] for r in sorted_results]) if self.logscale: plot_cmd = pylab.semilogx else: plot_cmd = pylab.plot xloc = xloc[:len(avg)] lw = 2. if label not in self.labels: plot_cmd(xloc, avg, color=color, ls=ls, lw=lw, label=label) else: plot_cmd(xloc, avg, color=color, ls=ls, lw=lw) self.labels.add(label) pylab.xticks(fontsize='xx-large') pylab.yticks(fontsize='xx-large') try: pylab.errorbar(xloc, (lower+upper)/2., yerr=(upper-lower)/2., fmt='', ls='None', ecolor=color) except: pass
def plot_fitness(self, show=True, save=False): df = pd.DataFrame([res['t1opt']['results']['Best_score'].values for res in self.results.allRes]) df = df.astype(float) pylab.clf() for res in self.results.allRes: pylab.plot(res['t1opt']['results']['Best_score'], '--', color='grey') pylab.grid() pylab.xlabel("Generation") pylab.ylabel("Score") #pylab.plot(df.mean().values, 'kx--', lw=3, label='Mean Score') y = df.mean().values x = range(0, len(y)) yerr = df.std().values pylab.errorbar(x, y, yerr=yerr, xerr=None, fmt='-', label='Mean Score', color='k', lw=3) pylab.legend() if save is True: self._report.savefig("fitness.png") if show is False: pylab.close()
def __primativePlotTGNs__(self,bare=bool(False)): """ Is a macro of plotting commands that takes a list of TGNs that plots each of these individually as a collection of points. Creates a figure plotting the thread of list of TGNs using the centroid and an X,Y error bars. Take a optional boolean to make the plot not include a title and legend """ #Determine the index that corresponds to X and Y quantities xIndex=0 yIndex=0 xLabel="NULL" yLabel="NULL" (xIndex,xLabel,yIndex,yLabel)=self.__getIndexAndLabels__() plotValues=list() gpsTimesInList=list() for thisTGN in self.tgnList: label=str(thisTGN.getID()) #Get the X,Y property (xC,xE)=thisTGN.getCentroidErrorViaIndex(xIndex) (yC,yE)=thisTGN.getCentroidErrorViaIndex(yIndex) plotValues.append([xC,yC,xE,yE,label]) gpsTimesInList.append(thisTGN.getGPS()) for x,y,ex,ey,txtLabel in plotValues: pylab.errorbar(x,y,xerr=ex,yerr=ey,label=txtLabel,marker='o') pylab.xlabel(str(xLabel)) pylab.ylabel(str(yLabel)) if not bare: pylab.title("TGNs: %i"%(min(gpsTimesInList))) pylab.legend()
def _plot_aggr_random(self, span, Nmax, marker='o', color='r', markersize=6): # those are the best submitter. Nothing to recompute, can be extracted # from the df itself. iauc = [self.df.ix[x].mean_auc for x in range(0, Nmax)] pylab.clf() pylab.plot([x for x in span], iauc, marker+color, markersize=markersize, label="AUC (individual submissions)".format(self.mode)) pylab.grid(True) #pylab.plot() pylab.xlabel("N", fontsize=20) pylab.ylabel("AUROC", fontsize=20) pylab.title("Aggregated AUROC (random case)", fontsize=20) pylab.errorbar(span, self.results.mean(axis=0), self.results.std(axis=0), label="{} aggregation (over N submissions)".format(self.mode)) pylab.legend(loc="lower left") self._random_results = {} self._random_results['x'] = span self._random_results['individual'] = iauc self._random_results['aggregation_mean'] = list(self.results.mean(axis=0)) self._random_results['aggregation_std'] = list(self.results.std(axis=0)) self._random_results['aggregation_all'] = [list(x) for x in self.results] xmax = pylab.xlim()[1] pylab.ylim([0.35, 0.86]) pylab.xlim(0.5, xmax)
def nishiyama09(wavelength, AKs, makePlot=False): # Data pulled from Nishiyama et al. 2009, Table 1 filters = ['V', 'J', 'H', 'Ks', '[3.6]', '[4.5]', '[5.8]', '[8.0]'] wave = np.array([0.551, 1.25, 1.63, 2.14, 3.545, 4.442, 5.675, 7.760]) A_AKs = np.array([16.13, 3.02, 1.73, 1.00, 0.500, 0.390, 0.360, 0.430]) A_AKs_err = np.array([0.04, 0.04, 0.03, 0.00, 0.010, 0.010, 0.010, 0.010]) # Interpolate over the curve spline_interp = interpolate.splrep(wave, A_AKs, k=3, s=0) A_AKs_at_wave = interpolate.splev(wavelength, spline_interp) A_at_wave = AKs * A_AKs_at_wave if makePlot: py.clf() py.errorbar(wave, A_AKs, yerr=A_AKs_err, fmt='bo', markerfacecolor='none', markeredgecolor='blue', markeredgewidth=2) # Make an interpolated curve. wavePlot = np.arange(wave.min(), wave.max(), 0.1) extPlot = interpolate.splev(wavePlot, spline_interp) py.loglog(wavePlot, extPlot, 'k-') # Plot a marker for the computed value. py.plot(wavelength, A_AKs_at_wave, 'rs', markerfacecolor='none', markeredgecolor='red', markeredgewidth=2) py.xlabel('Wavelength (microns)') py.ylabel('Extinction (magnitudes)') py.title('Nishiyama et al. 2009') return A_at_wave
def plotRes_varyingTrees( data_dict, dataset_name, max_correct=3000 , show=True): ''' Plots the results of a varyingNumTrees() experiment, using a dictionary structure to hold the data. See the loadRes_varyingTrees() comments on the dictionary layout. ''' xvals = data_dict['NumTrees'] #prox forest trials pf_avg = data_dict['PF'].mean(axis=0) pf_std = data_dict['PF'].std(axis=0) pf_95_conf = 1.96 * pf_std / math.sqrt(data_dict['PF'].shape[0]) #kdt forest trials kdt_avg = data_dict['KDT'].mean(axis=0) kdt_std = data_dict['KDT'].std(axis=0) kdt_95_conf = 1.96 * kdt_std / math.sqrt(data_dict['KDT'].shape[0]) #plot average results of each, bounded by lower and upper bounds of 95% conf intv pl.hold(True) pl.errorbar(xvals, pf_avg/max_correct, yerr=pf_95_conf/max_correct, fmt='-r', label="PF") pl.errorbar(xvals, kdt_avg/max_correct, yerr=kdt_95_conf/max_correct, fmt='-.b', label="KDT") pl.ylim([0,1.05]) pl.title(dataset_name) pl.xlabel("Number of Trees in Forest") pl.ylabel("Percent Correct") pl.legend(loc='lower right') if show: pl.show()
def run_test(args): rho = args.rho num_times = args.num_times min_num_rows = args.min_num_rows max_num_rows = args.max_num_rows n_grid = args.n_grid filename = args.filename discrete = args.discrete num_samples = [] for ns in log_linspace(min_num_rows, max_num_rows, n_grid).tolist(): num_samples.append(int(ns)) variances = [] burn_in = 200 MIs = numpy.zeros((num_times, len(num_samples))) mi_diff = numpy.zeros((len(num_samples), num_times)) if not discrete: T, true_mi, external_mi = gen_correlated_data(num_samples[-1], rho) cctypes = ['continuous'] * 2 else: T, true_mi, external_mi = gen_correlated_data_discrete( num_samples[-1], rho) cctypes = ['multinomial'] * 2 data_subs = [] n_index = 0 for n in num_samples: T_sub = numpy.copy(T[0:n - 1, :]) data = [] data_subs.append(T_sub) print("%i: " % n) for t in range(num_times): M_c = du.gen_M_c_from_T(T_sub, cctypes) state = State.p_State(M_c, T_sub) state.transition(n_steps=burn_in) X_D = state.get_X_D() X_L = state.get_X_L() MI, Linfoot = iu.mutual_information(M_c, [X_L], [X_D], [(0, 1)], n_samples=5000) mi_diff[n_index, t] = true_mi - MI[0][0] print("\t%i TRUE: %e, EST: %e " % (t, true_mi, MI[0][0])) MIs[t, n_index] = MI[0][0] n_index += 1 if discrete: dtype_str = "discrete" else: dtype_str = "continuous" basefilename = filename + str(int(time.time())) figname = basefilename + ".png" datname = basefilename + "_DATA.png" pl.figure # plot data # pl.subplot(1,2,1) pl.figure(tight_layout=True, figsize=(len(data_subs) * 4, 4)) i = 0 for T_s in data_subs: pl.subplot(1, len(data_subs), i + 1) num_rows = num_samples[i] if discrete: heatmap, xedges, yedges = numpy.histogram2d(T_s[:, 0], T_s[:, 1], bins=10) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] pl.imshow(heatmap, extent=extent, interpolation="nearest") else: pl.scatter(T_s[:, 0], T_s[:, 1], alpha=.3, s=81) pl.title('#r: ' + str(num_rows)) i += 1 pl.suptitle("data for rho: %1.2f (%s)" % (rho, dtype_str)) pl.savefig(datname) pl.clf() pl.figure(tight_layout=True, figsize=(5, 4)) # plot convergence # pl.subplot(1,2,2) # standard deviation stderr = numpy.std(MIs, axis=0) #/(float(num_times)**.5) mean = numpy.mean(MIs, axis=0) pl.errorbar(num_samples, mean, yerr=stderr, c='blue') pl.plot(num_samples, mean, c="blue", alpha=.8, label='mean MI') pl.plot(num_samples, [true_mi] * len(num_samples), color='red', alpha=.8, label='true MI') pl.plot(num_samples, [external_mi] * len(num_samples), color=(0, .5, .5), alpha=.8, label='external MI') pl.title('convergence') pl.xlabel('#rows in X (log)') pl.ylabel('CrossCat MI - true MI') pl.legend(loc=0, prop={'size': 8}) pl.gca().set_xscale('log') # save output pl.title("convergence rho: %1.2f (%s)" % (rho, dtype_str)) pl.savefig(figname)
def plotPhotometry(imgsources, refsources, matches, prefix, band=None, zp=None, delta=False, referrs=None, refstargal=None, title=None, saveplot=True, format='png'): print('%i ref sources' % len(refsources)) print('%i image sources' % len(imgsources)) print('%i matches' % len(matches)) # In the "matches" list: # m.first is catalog # m.second is image # In this function, the "m" prefix stands for "matched", # "u" stands for "unmatched". # *sigh*, turn these into Python lists, so we have the "index" function. refsources = [s for s in refsources] imgsources = [s for s in imgsources] # Now we build numpy int arrays for indexing into the "refsources" and # "imgsources" arrays. MR = [] MI = [] for m in matches: try: i = refsources.index(m.first) except ValueError: print('Match list reference source ID', m.first.getSourceId(), 'was not in the list of reference stars') continue try: j = imgsources.index(m.second) except ValueError: print('Match list source ID', m.second.getSourceId(), 'was not in the list of image sources') continue MR.append(i) MI.append(j) MR = np.array(MR) MI = np.array(MI) # Build numpy boolean arrays for indexing the unmatched stars. UR = np.ones(len(refsources), bool) UR[MR] = False UI = np.ones(len(imgsources), bool) UI[MI] = False def flux2mag(f): return -2.5 * np.log10(f) refmag = np.array([flux2mag(s.getPsfFlux()) for s in refsources]) imgflux = np.array([s.getPsfFlux() for s in imgsources]) imgfluxerr = np.array([s.getPsfFluxErr() for s in imgsources]) # Cut to fluxes that aren't silly and get mags of matched sources. okflux = (imgflux[MI] > 1) MI = MI[okflux] MR = MR[okflux] mimgflux = imgflux[MI] mimgmag = flux2mag(mimgflux) mimgmagerr = abs(2.5 / np.log(10.) * imgfluxerr[MI] / mimgflux) mrefmag = refmag[MR] # Get mags of unmatched sources. uimgflux = imgflux[UI] okflux = (uimgflux > 1) uimgmag = flux2mag(uimgflux[okflux]) urefmag = refmag[UR] # Legend entries: pp = [] pl = [] plt.clf() imag = np.append(mimgmag, uimgmag) mrefmagerr = None if referrs is not None: referrs = np.array(referrs) mrefmagerr = referrs[MR] if refstargal: assert (len(refstargal) == len(refsources)) refstargal = np.array(refstargal).astype(bool) ptsets = [(np.logical_not(refstargal[MR]), 'g', 'Matched galaxies', 10), (refstargal[MR], 'b', 'Matched stars', 12)] else: ptsets = [(np.ones_like(mrefmag).astype(bool), 'b', 'Matched sources', 10)] for I, c, leg, zo in ptsets: if delta: dm = mimgmag[I] - mrefmag[I] + zp xi = mrefmag[I] yi = dm dx = mrefmagerr dy = mimgmagerr else: xi = mimgmag[I] yi = mrefmag[I] dx = mimgmagerr dy = mrefmagerr p1 = plt.plot(xi, yi, '.', color=c, mfc=c, mec=c, alpha=0.5, zorder=zo) if dx is None or dy is None: # errorbars xerr, yerr = None, None if dx is not None: xerr = dx[I] if dy is not None: yerr = dy[I] plt.errorbar(xi, yi, xerr=xerr, yerr=yerr, ecolor=c, fmt=None, zorder=zo) else: # get the current axis ca = plt.gca() # add error ellipses for j, i in enumerate(np.flatnonzero(I)): a = Ellipse(xy=np.array([xi[j], yi[j]]), width=dx[i] / 2., height=dy[i] / 2., alpha=0.5, fill=True, ec=c, fc=c, zorder=zo) ca.add_artist(a) pp.append(p1) pl.append(leg) if delta: m = max(abs(dm)) plt.axis([np.floor(min(refmag)) - 0.5, np.ceil(max(refmag)), -m, m]) else: plt.axis([ np.floor(min(imag)) - 0.5, np.ceil(max(imag)), np.floor(min(refmag)) - 0.5, np.ceil(max(refmag)) ]) ax = plt.axis() if not delta: # Red tick marks show unmatched img sources dy = (ax[3] - ax[2]) * 0.05 y1 = np.ones_like(uimgmag) * ax[3] p2 = plt.plot(np.vstack((uimgmag, uimgmag)), np.vstack((y1, y1 - dy)), 'r-', alpha=0.5) p2 = p2[0] # Blue tick marks show matched img sources y1 = np.ones_like(mimgmag) * ax[3] p3 = plt.plot(np.vstack((mimgmag, mimgmag)), np.vstack((y1 - (0.25 * dy), y1 - (1.25 * dy))), 'b-', alpha=0.5) p3 = p3[0] # Red ticks for unmatched ref sources dx = (ax[1] - ax[0]) * 0.05 x1 = np.ones_like(urefmag) * ax[1] p4 = plt.plot(np.vstack((x1, x1 - dx)), np.vstack((urefmag, urefmag)), 'r-', alpha=0.5) p4 = p4[0] # Blue ticks for matched ref sources x1 = np.ones_like(mrefmag) * ax[1] p5 = plt.plot(np.vstack((x1 - (0.25 * dx), x1 - (1.25 * dx))), np.vstack((mrefmag, mrefmag)), 'b-', alpha=0.5) p5 = p5[0] if zp is not None: if delta: pzp = plt.axhline(0, linestyle='--', color='b') else: X = np.array([ax[0], ax[1]]) pzp = plt.plot(X, X + zp, 'b--') pp.append(pzp) pl.append('Zeropoint') # reverse axis directions. if delta: plt.axis([ax[1], ax[0], ax[2], ax[3]]) else: plt.axis([ax[1], ax[0], ax[3], ax[2]]) if band is not None: reflabel = 'Reference catalog: %s band (mag)' % band else: reflabel = 'Reference catalog mag' if delta: plt.xlabel(reflabel) plt.ylabel('Instrumental - Reference (mag)') fn = prefix + '-dphotom.' + format if zp is not None: # Make the plot area smaller to fit the twin axis pos = plt.gca().get_position() ll = pos.min sz = pos.size plt.gca().set_position(pos=[ll[0], ll[1], sz[0], sz[1] - 0.05]) # Put the title up top (otherwise it follows the axis) if title is not None: plt.figtext(0.5, 0.96, title, ha='center', va='top', fontsize='large') title = None plt.twiny() # Red tick marks show unmatched img sources if zp is not None: dy = (ax[3] - ax[2]) * 0.05 y1 = np.ones_like(uimgmag) * ax[3] p2 = plt.plot(np.vstack((uimgmag, uimgmag)) + zp, np.vstack((y1, y1 - dy)), 'r-', alpha=0.5) p2 = p2[0] # Blue tick marks show matched img sources y1 = np.ones_like(mimgmag) * ax[3] p3 = plt.plot(np.vstack((mimgmag, mimgmag)) + zp, np.vstack((y1 - (0.25 * dy), y1 - (1.25 * dy))), 'b-', alpha=0.5) p3 = p3[0] # Red ticks for unmatched ref sources y1 = np.ones_like(urefmag) * ax[2] p4 = plt.plot(np.vstack((urefmag, urefmag)), np.vstack((y1, y1 + dy)), 'r-', alpha=0.5) p4 = p4[0] # Blue ticks for matched ref sources y1 = np.ones_like(mrefmag) * ax[2] p5 = plt.plot(np.vstack((mrefmag, mrefmag)), np.vstack((y1 + (0.25 * dy), y1 + (1.25 * dy))), 'b-', alpha=0.5) p5 = p5[0] plt.xlim(ax[1] - zp, ax[0] - zp) plt.xlabel('Instrumental mag') legloc = 'lower right' else: plt.ylabel(reflabel) plt.xlabel('Image instrumental mag') fn = prefix + '-photom.' + format legloc = 'center right' if title is not None: plt.title(title) pp += [p3, p2] pl += ['Matched sources', 'Unmatched sources'] plt.figlegend(pp, pl, legloc, numpoints=1, prop=FontProperties(size='small')) P1 = _output(fn, format, saveplot) if delta: plt.ylim(-0.5, 0.5) fn = prefix + '-dphotom2.' + format P2 = _output(fn, format, saveplot) if not saveplot: P1.update(P2) return P1
def get_performance(file_name, name): a, b = os.path.splitext(os.path.basename(file_name)) file_name, ext = os.path.splitext(file_name) file_out = os.path.join(os.path.abspath(file_name), a) result_name = os.path.join(file_name, 'injected') pic_name = file_name + '.pic' data = pickle.load(open(pic_name)) n_cells = data['cells'] nb_insert = len(n_cells) amplitude = data['amplitudes'] sampling = data['sampling'] thresh = int(sampling * 2 * 1e-3) truncate = True result = h5py.File(file_out + '.result.hdf5') fitted_spikes = {} fitted_amps = {} for key in list(result.get('spiketimes').keys()): fitted_spikes[key] = result.get('spiketimes/%s' % key)[:] for key in list(result.get('amplitudes').keys()): fitted_amps[key] = result.get('amplitudes/%s' % key)[:] templates = h5py.File(file_out + '.templates.hdf5').get('temp_shape')[:] spikes = {} real_amps = {} result = h5py.File(os.path.join(result_name, '%s.result.hdf5' % a)) for key in list(result.get('spiketimes').keys()): spikes[key] = result.get('spiketimes/%s' % key)[:] for key in list(result.get('real_amps').keys()): real_amps[key] = result.get('real_amps/%s' % key)[:] n_tm = templates[2] // 2 res = numpy.zeros((len(n_cells), 2)) res2 = numpy.zeros((len(n_cells), 2)) real_amplitudes = [] p_deltas = {} n_deltas = {} p_amps = {} n_amps = {} if truncate: max_spike = 0 for temp_id in range(n_tm - len(n_cells), n_tm): key = 'temp_' + str(temp_id) if len(fitted_spikes[key] > 0): max_spike = max(max_spike, fitted_spikes[key].max()) for temp_id in range(n_tm - len(n_cells), n_tm): key = 'temp_' + str(temp_id) spikes[key] = spikes[key][spikes[key] < max_spike] for gcount, temp_id in enumerate(range(n_tm - len(n_cells), n_tm)): key = 'temp_' + str(temp_id) count = 0 p_deltas[key] = [] n_deltas[key] = [] p_amps[key] = [] n_amps[key] = [] for spike in spikes[key]: idx = numpy.where( numpy.abs(fitted_spikes[key] - spike) < thresh)[0] if len(idx) > 0: count += 1 p_deltas[key] += [ numpy.abs(fitted_spikes[key][idx] - spike)[0] ] p_amps[key] += [fitted_amps[key][idx][:, 0][0]] if len(spikes[key]) > 0: res[gcount, 0] = count / float(len(spikes[key])) count = 0 for lcount, spike in enumerate(fitted_spikes[key]): idx = numpy.where(numpy.abs(spikes[key] - spike) < thresh)[0] if len(idx) > 0: count += 1 n_deltas[key] += [numpy.abs(spikes[key][idx] - spike)[0]] n_amps[key] += [fitted_amps[key][lcount]] if len(fitted_spikes[key]) > 0: res[gcount, 1] = count / (float(len(fitted_spikes[key]))) res2[gcount, 0] = numpy.mean(fitted_amps[key][:, 0]) res2[gcount, 1] = numpy.var(fitted_amps[key][:, 0]) real_amplitudes += [numpy.mean(real_amps[key])] pylab.figure() ax = pylab.subplot(211) pylab.plot(amplitude, 100 * (1 - res[:, 0])) pylab.plot(amplitude, 100 * (1 - res[:, 1])) ax.set_yscale('log') pylab.xlim(amplitude[0], amplitude[-1]) pylab.setp(pylab.gca(), xticks=[]) pylab.legend(('False negative', 'False positive')) pylab.ylabel('Errors [%]') pylab.subplot(212) pylab.errorbar(amplitude * real_amplitudes, amplitude * res2[:, 0], yerr=res2[:, 1]) pylab.xlabel('Relative Amplitude') pylab.ylabel('Fitted amplitude') pylab.xlim(amplitude[0], amplitude[-1]) pylab.show() pylab.tight_layout() plot_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '.')) plot_path = os.path.join(plot_path, 'plots') plot_path = os.path.join(plot_path, 'fitting') if not os.path.exists(plot_path): os.makedirs(plot_path) output = os.path.join(plot_path, '%s.pdf' % name) pylab.savefig(output) return res
fig_size = plt.rcParams["figure.figsize"] fig_size[0] = 12 fig_size[1] = 6 plt.rcParams["figure.figsize"] = fig_size font_size = plt.rcParams["font.size"] font_size = 15 plt.rcParams["font.size"] = font_size # Error bar cap size in points cpsize = 1 plt.errorbar(F_fast[0], F_fast[1], yerr=F_fast[1] * F_fast[2], c='b', label="Flux Rapide", capsize=cpsize) plt.errorbar(F_therm[0], F_therm[1], yerr=F_therm[1] * F_therm[2], c='orange', label="Flux Thermique", capsize=cpsize) #plt.errorbar(F_tot[0], F_tot[1], yerr=F_tot[1]*F_tot[2], c='r', label="Total") plt.xlabel("Position [cm]") plt.ylabel("Flux") plt.legend() plt.tight_layout() plt.show()
import uncertainties def linear(x, a, b): return a*x+b Vl, Vce, dVl, dVce = pylab.loadtxt('/home/federico/Laboratorio3/relazione3/dati2.txt', unpack=True) Rb = 46700 dRb = 400 Rl = 977 dRl = 8 Ic = (Vl)/Rl dIc = Ic*((dVl/Vl)**2+(dRl/Rl)**2)**0.5 pylab.figure(2) pylab.title('I_c vs V_be') pylab.xlabel('V_ce (V)') pylab.ylabel('I_c (mA)') pylab.grid(color = "gray") Ic = 1000*Ic dIc = 1000*dIc pylab.errorbar(Vce, Ic, dIc, dVce, "o", color="black") Ic = Ic/1000 dIc = dIc/1000 pylab.show()
plt.yticks(visible=False) plt.xlim(0,5) plt.ylim(ymin=0) plt.title('$z=0.0$', fontsize=18) plt.savefig('wpp_sat_cent_like_z0.png') plt.savefig('wpp_sat_cent_like_z0.pdf') plt.close() Lcc_99 = np.copy(Lcc) Lss_99 = np.copy(Lss) Lsc_99 = np.copy(Lsc) plt.errorbar(x,x*wcc,yerr=x*Ecc,color='darkred', marker='.', linestyle='none', label='$cc$') plt.errorbar(x,x*wss,yerr=x*Ess,color='midnightblue', marker='.', linestyle='none', label='$ss$') plt.errorbar(x,x*wsc,yerr=x*Esc,color='purple', marker='.', linestyle='none', label='$sc$') plt.ylabel('$w_{++}$') plt.xlabel('$r_p$') plt.xscale('log') plt.savefig('sat_cent_wpp.png') plt.savefig('sat_cent_wpp.pdf') plt.close()
n_samples = 100 n_clusters_range = np.linspace(2, n_samples, 10).astype(np.int) pl.figure(1) plots = [] names = [] for score_func in score_funcs: print "Computing %s for %d values of n_clusters and n_samples=%d" % ( score_func.__name__, len(n_clusters_range), n_samples) t0 = time() scores = uniform_labelings_scores(score_func, n_samples, n_clusters_range) print "done in %0.3fs" % (time() - t0) plots.append(pl.errorbar( n_clusters_range, np.median(scores, axis=1), scores.std(axis=1))[0]) names.append(score_func.__name__) pl.title("Clustering measures for 2 random uniform labelings\n" "with equal number of clusters") pl.xlabel('Number of clusters (Number of samples is fixed to %d)' % n_samples) pl.ylabel('Score value') pl.legend(plots, names) pl.ylim(ymin=-0.05, ymax=1.05) # Random labeling with varying n_clusters against ground class labels # with fixed number of clusters n_samples = 1000 n_clusters_range = np.linspace(2, 100, 10).astype(np.int)
## Calculate median and 1/2-sigma bounds #print "%3.3e %3.3e %3.3e %3.3e %3.3e" % (p_cdf(0.5), p_cdf(0.16), p_cdf(0.84), # p_cdf(0.5)- p_cdf(0.16), p_cdf(0.84)- p_cdf(0.5)) ####################### #data = np.array([dat_u, dat_g, dat_r, dat_i, dat_z]).T #print data.shape P.subplot(111) #P.plot(l, data[0], 'kx', ms=8., mew=1.5) P.errorbar(l, median, yerr=(errm, errp), color='#0B73CE', ms=8., ls='none', marker='.', capsize=4., elinewidth=1.5, mew=1.5) # Plot best-fit powerlaw ll = np.linspace(2700., 9900., 100) #P.plot(ll, 3e6*np.exp(-16.*ll/5000.), 'k--', lw=2., alpha=0.5) #P.plot(ll, 0.5*np.exp(-0.003*(ll - 5000.)), 'k--', lw=2., alpha=0.5) P.plot(ll, extinct_amp(ll, pnew), color='#E72327', lw=2., dashes=[4, 3]) # Place band names close to markers for i in range(len(BANDS)): P.annotate(BANDS[i],
def onclick(event): P.figure(fh.number) P.clf() #ax = P.gca() #inv = ax.transData.inverted() #A=inv.transform((event.x, event.y)) #A[1]=np.int(np.round((1-A[1])*array2d.shape[1])) #A[0]=np.int(np.round((A[0])*array2d.shape[0])) try: y = np.round(event.xdata) except: return x = np.round(event.ydata) #ARRAY MAPPING IS first axis y(rows) and second axis is cols (x) if all(np.isnan(array3d[x, y, :])): #if there are no points to plot (all nan) then return return #Plot second scatter data. if array3d2 is not None: if isinstance(array3d2, list): if yerror3d is None: w = np.ones(array3d[x, y, :].shape) else: w = basic.rescale(1. / yerror3d[x, y, :], [1, 2]) markers = ['*', '+', 's', 'd', 'x', 'v', '<', '>', '^'] m = 0 for arr in array3d2: print("%d, %d, %d" % (x, y, m)) P.scatter(xScat, arr[x, y, :], marker=markers[m]) idx = ~(np.isnan(arr[x, y, :]) | np.isnan(array3d[x, y, :])) #c=insar.crosscorrelate(basic.nonan(w[idx]*arr[x, y,idx]),basic.nonan(w[idx]*array3d[x, y,idx])) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress( basic.nonan(w[idx] * arr[x, y, idx]), basic.nonan(w[idx] * array3d[x, y, idx])) P.annotate(str("r2[%s]: %0.2f" % (markers[m], r_value)), (0, 0.9 - m * 0.05), xycoords='axes fraction') m = m + 1 else: if xerror3d2 is None: xerr = None else: xerr = xerror3d2[x, y, :] if yerror3d2 is None: yerr = None else: yerr = yerror3d2[x, y, :] P.errorbar(xScat, array3d2[x, y, :], xerr=xerr, yerr=yerr, marker='*', fmt='o') #Plot function result as scatter data. p = None if fn is not None: if fn == 'linear_amplitude_annual': dataMask = ~np.isnan(array3d[x, y, :]) p0 = np.array([1, 0, 0, basic.nonan(array3d[x, y, :]).mean()]) fitfun = lambda p: (p[0] + p[1] * xScat[dataMask] / 365. ) * np.cos(2 * np.pi * xScat[dataMask] / 365. + p[2]) + p[3] xScat2 = np.linspace(xScat.min(), xScat.max()) fitfun2 = lambda p: (p[0] + p[1] * xScat2 / 365.) * np.cos( 2 * np.pi * xScat2 / 365. + p[2]) + p[3] #errfun=lambda p: sum(abs(basic.nonan(array3d[x, y,:])-fitfun(p))); if yerror3d is None: w = np.ones(array3d[x, y, :].shape) else: w = basic.rescale(1. / yerror3d[x, y, :], [1, 2]) errfun = lambda p: basic.nonan(w * array3d[x, y, :]) - w[ dataMask] * fitfun(p) #p=scipy.optimize.fmin_powell(errfun, p0) p = scipy.optimize.leastsq(errfun, p0) p = p[0] P.scatter(xScat[dataMask], fitfun(p), marker='^') sortedxy = np.squeeze(np.dstack([xScat2, fitfun2(p)])) sortedxy = sortedxy[sortedxy[:, 0].argsort(), :] P.plot(sortedxy[:, 0], sortedxy[:, 1]) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress( basic.nonan(w * array3d[x, y, :]), w[dataMask] * fitfun(p)) P.annotate( str("a0:%0.2f\na1:%0.2f\npha:%0.2f\nbias:%0.2f\nr2:%0.2f" % (p[0], p[1], p[2], p[3], r_value**2.)), (0.8, 0.8), xycoords='axes fraction') elif fn == 'quadratic_amplitude_annual': dataMask = ~np.isnan(array3d[x, y, :]) p0 = np.array( [1, 0, 0, 0, basic.nonan(array3d[x, y, :]).mean()]) fitfun = lambda p: (p[0] + p[1] * xScat[dataMask] / 365. + p[ 2] * (xScat[dataMask] / 365.)**2.) * np.cos( 2 * np.pi * xScat[dataMask] / 365. + p[3]) + p[4] xScat2 = np.linspace(xScat.min(), xScat.max()) fitfun2 = lambda p: (p[0] + p[1] * xScat2 / 365. + p[2] * ( xScat2 / 365.)**2.) * np.cos(2 * np.pi * xScat2 / 365. + p[ 3]) + p[4] #errfun=lambda p: sum(abs(basic.nonan(array3d[x, y,:])-fitfun(p))); if yerror3d is None: w = np.ones(array3d[x, y, :].shape) else: w = basic.rescale(1. / yerror3d[x, y, :], [1, 2]) errfun = lambda p: basic.nonan(w * array3d[x, y, :]) - w[ dataMask] * fitfun(p) #p=scipy.optimize.fmin_powell(errfun, p0) p = scipy.optimize.leastsq(errfun, p0) p = p[0] P.scatter(xScat[dataMask], fitfun(p), marker='^') sortedxy = np.squeeze(np.dstack([xScat2, fitfun2(p)])) sortedxy = sortedxy[sortedxy[:, 0].argsort(), :] P.plot(sortedxy[:, 0], sortedxy[:, 1]) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress( basic.nonan(w * array3d[x, y, :]), w[dataMask] * fitfun(p)) P.annotate(str( "a0:%0.2f\na1:%0.2f\na2:%0.2f\npha:%0.2f\nbias:%0.2f\nr2:%0.2f" % (p[0], p[1], p[2], p[3], p[4], r_value**2.)), (0.8, 0.8), xycoords='axes fraction') elif fn == 'annual': dataMask = ~np.isnan(array3d[x, y, :]) p0 = np.array([1, 1, basic.nonan(array3d[x, y, :]).mean()]) fitfun = lambda p: p[0] * np.cos(2 * np.pi * xScat[dataMask] / 365. + p[1]) + p[2] xScat2 = np.linspace(xScat.min(), xScat.max()) fitfun2 = lambda p: p[0] * np.cos(2 * np.pi * xScat2 / 365. + p[1]) + p[2] #errfun=lambda p: sum(abs(basic.nonan(array3d[x, y,:])-fitfun(p))); if yerror3d is None: w = np.ones(array3d[x, y, :].shape) else: w = basic.rescale(1. / yerror3d[x, y, :], [1, 2]) errfun = lambda p: basic.nonan(w * array3d[x, y, :]) - w[ dataMask] * fitfun(p) #p=scipy.optimize.fmin_powell(errfun, p0) p = scipy.optimize.leastsq(errfun, p0) p = p[0] P.scatter(xScat[dataMask], fitfun(p), marker='^') sortedxy = np.squeeze(np.dstack([xScat2, fitfun2(p)])) sortedxy = sortedxy[sortedxy[:, 0].argsort(), :] P.plot(sortedxy[:, 0], sortedxy[:, 1]) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress( basic.nonan(w * array3d[x, y, :]), w[dataMask] * fitfun(p)) P.annotate(str("amp:%0.2f\npha:%0.2f\nbias:%0.2f\nr2:%0.2f" % (p[0], p[1], p[2], r_value**2.)), (0.8, 0.8), xycoords='axes fraction') else: p = None P.scatter(xScat, fn(xScat), marker='^') #convert axis to date... if dateaxis: try: P.figure(fh.number).axes[0].xaxis_date(tz=None) P.figure(fh.number).autofmt_xdate() except: pass #change x y to xMap, yMap if yMap is not None: xM = ya * x + yb else: xM = x if xMap is not None: yM = xa * (y) + xb else: yM = y #x and y are flipped in the try/except block above. So Flip again. #if p is not None: # P.title("x,y,[]: " + str(yM) + ", " + str(xM) + ', ' + str(p) ) #else: P.title("x,y,z,z.std: " + str(yM) + ", " + str(xM) + ', ' + str(array2d[x, y]) + ', ' + str(np.std(basic.nonan(array3d[x, y, :])))) # rotate and align the tick labels so they look better #P.figure(fh.number).autofmt_xdate() # use a more precise date string for the x axis locations in the # toolbar #P.gca().fmt_xdata = mdates.DateFormatter('%Y-%m-%d') if xerror3d is None: xerr = None else: xerr = xerror3d[x, y, :] if yerror3d is None: yerr = None else: yerr = yerror3d[x, y, :] if modelError: yerr = yerror3d[x, y, :] yerr[dataMask] = errfun(p) P.errorbar(xScat, array3d[x, y, :], xerr=xerr, yerr=yerr, fmt='ro') if ylimScat is not None: P.ylim(ylimScat)
for win in predWindows])[np.newaxis, :]), return_std=True) pastPred = gpr.predict(scaler.transform(clip[predColumns[:-1]].values), return_std=True) return gpr, pred, pastPred scaler = preprocessing.StandardScaler().fit(clip[predColumns[:-1]].values) kernel = gp.kernels.ConstantKernel(1.0, (1e-1, 1e3)) * gp.kernels.RBF( 10.0, (1e-3, 1e3)) + gp.kernels.WhiteKernel(1) gpSimple = fitGp(clip, scaler, kernel, predColumns, 5000) # show the past pastPred = gpSimple[2] plt.figure() plt.errorbar(clip.date, pastPred[0], yerr=pastPred[1], label='pred') plt.plot(clip.date, clip.y, 'o', alpha=0.5, label='actual', linewidth=2) plt.grid() plt.legend() # interestingly, while stdev does vary between 0 and 2~, in out-of-training, it does spike. # Does this mean it hasn't seen that particular sample before? plt.figure() plt.plot(clip.date, pastPred[1]) # Try more complex kernels. k1 = gp.kernels.ConstantKernel(10) * gp.kernels.RBF(10.0) # k2 = gp.kernels.ConstantKernel(.1) * gp.kernels.RBF() * gp.kernels.ExpSineSquared() k3 = gp.kernels.ConstantKernel( .1) * gp.kernels.RBF() * gp.kernels.RationalQuadratic() kernel2 = k1 + k3 + gp.kernels.WhiteKernel(1)
def plot_compare_methods(offsets, eoffsets, dx=None, dy=None, fig1=1, fig2=2, legend=True): """ plot wrapper """ import pylab pylab.figure(fig1) pylab.clf() if dx is not None and dy is not None: pylab.plot([dx], [dy], 'kx', markersize=30, zorder=50, markeredgewidth=3) pylab.errorbar(offsets[:, 0], offsets[:, 1], xerr=eoffsets[:, 0], yerr=eoffsets[:, 1], linestyle='none', label='DFT') pylab.errorbar(offsets[:, 2], offsets[:, 3], xerr=eoffsets[:, 2], yerr=eoffsets[:, 3], linestyle='none', label='Taylor') pylab.errorbar(offsets[:, 4], offsets[:, 5], xerr=eoffsets[:, 4], yerr=eoffsets[:, 5], linestyle='none', label='Gaussian') pylab.errorbar(offsets[:, 6], offsets[:, 7], xerr=eoffsets[:, 6], yerr=eoffsets[:, 7], linestyle='none', label='$\\chi^2$') if legend: pylab.legend(loc='best') means = offsets.mean(axis=0) stds = offsets.std(axis=0) emeans = eoffsets.mean(axis=0) estds = eoffsets.std(axis=0) print("Standard Deviations: ", stds) print("Error Means: ", emeans) print("emeans/stds: ", emeans / stds) pylab.figure(fig2) pylab.clf() if dx is not None and dy is not None: pylab.plot([dx], [dy], 'kx', markersize=30, zorder=50, markeredgewidth=3) pylab.errorbar(means[0], means[1], xerr=emeans[0], yerr=emeans[1], capsize=20, color='r', dash_capstyle='round', solid_capstyle='round', label='DFT') pylab.errorbar(means[2], means[3], xerr=emeans[2], yerr=emeans[3], capsize=20, color='g', dash_capstyle='round', solid_capstyle='round', label='Taylor') pylab.errorbar(means[4], means[5], xerr=emeans[4], yerr=emeans[5], capsize=20, color='b', dash_capstyle='round', solid_capstyle='round', label='Gaussian') pylab.errorbar(means[6], means[7], xerr=emeans[6], yerr=emeans[7], capsize=20, color='m', dash_capstyle='round', solid_capstyle='round', label='$\\chi^2$') pylab.errorbar(means[0], means[1], xerr=stds[0], yerr=stds[1], capsize=10, color='r', linestyle='--', linewidth=5) pylab.errorbar(means[2], means[3], xerr=stds[2], yerr=stds[3], capsize=10, color='g', linestyle='--', linewidth=5) pylab.errorbar(means[4], means[5], xerr=stds[4], yerr=stds[5], capsize=10, color='b', linestyle='--', linewidth=5) pylab.errorbar(means[6], means[7], xerr=stds[6], yerr=stds[7], capsize=10, color='m', linestyle='--', linewidth=5) if legend: pylab.legend(loc='best')
def cornerplot(theory, data, show_cuts=False): plt.switch_backend("pdf") plt.style.use("y1a1") matplotlib.rcParams["ytick.minor.visible"] = False matplotlib.rcParams["xtick.minor.visible"] = False matplotlib.rcParams["ytick.minor.width"] = 0.1 matplotlib.rcParams["ytick.major.size"] = 2.5 matplotlib.rcParams["xtick.major.size"] = 2.5 matplotlib.rcParams["xtick.minor.size"] = 1.8 ni, nj = 4, 4 #ni,nj=np.genfromtxt("%s/shear_xi_plus/values.txt"%theory1[0]).T[2] ni = int(ni) nj = int(nj) if data is not None: data = fi.FITS(data) rows, cols = ni + 1, nj + 2 count = 0 for i in range(ni): for j in range(nj): count += 1 if j > i: continue print(i, j) (xp, xip, dxip), (xm, xim, dxim) = get_real_spectra(i, j, data, error=True) posp = positions[(i + 1, j + 1, "+")] ax = plt.subplot(rows, cols, posp) ax.annotate( "(%d, %d)" % (i + 1, j + 1), (3., yticks[-2]), textcoords='data', fontsize=9, ) ax.yaxis.set_tick_params(which='minor', left='off', right='off') plt.ylim(ymin, ymax) plt.xscale("log") if (posp == 19) or (posp == 1) or (posp == 7) or (posp == 13): #plt.yticks(visible=True) plt.yticks(yticks[:-1], ytick_labels[:-1], fontsize=ytickfontsize, visible=True) else: plt.yticks(visible=False) if (posp == 19): plt.ylabel(r"$\theta \xi_+ \times 10^{4}$", fontsize=11) plt.xlabel(r"$\theta \;\; [ \mathrm{arcmin} ]$", fontsize=10) else: pass if posp in [19, 14, 9, 4]: plt.xlabel(r"$\theta \;\; [ \mathrm{arcmin} ]$ ", fontsize=10) plt.xlim(2.2, 270) plt.xticks([10, 100], ["10", "100"], fontsize=9) plt.axhline(0, color='k', ls=':') if show_cuts: xlower, xupper = lims['+'][(i + 1, j + 1)] plt.axvspan(1e-6, xlower, color='gray', alpha=0.2) plt.axvspan(xupper, 500, color='gray', alpha=0.2) xlower_opt, xupper_opt = lims_opt['+'][(i + 1, j + 1)] plt.axvspan(1e-6, xlower_opt, color='gray', alpha=0.2) linestyles = ['-', ':', '--', '-'] xta, xip_theory, xim_theory, xip_theory_gi, xim_theory_gi, xip_theory_ii, xim_theory_ii = get_theory_spectra( i, j, theory) plt.errorbar(xp, xp * xip * 1e4, yerr=xp * dxip * 1e4, marker='.', linestyle='none', markerfacecolor='k', markeredgecolor='k', ecolor='k', markersize=markersize) p1 = plt.plot(xta, xta * xip_theory * 1e4, color='darkmagenta', lw=1.5, label='GG+GI+II') plt.plot(xta, F * xta * (xip_theory_gi + xip_theory_ii) * 1e4, color='steelblue', lw=1., ls='-', label='GI') p2 = plt.plot(xta, F * xta * xip_theory_gi * 1e4, color='pink', lw=1.5, ls='--', label='GI') p3 = plt.plot(xta, F * xta * xip_theory_ii * 1e4, color='midnightblue', lw=1.5, ls=':', label='II') posm = positions[(i + 1, j + 1, "-")] ax = plt.subplot(rows, cols, posm) ax.annotate("(%d, %d)" % (i + 1, j + 1), (3, yticks[-2]), textcoords='data', fontsize=9) ax.yaxis.set_tick_params(which='minor', left='off', right='off') plt.ylim(ymin, ymax) #ax.yaxis.set_ticks_position("right") #ax.yaxis.set_label_position("right") if (posm == 30) or (posm == 12) or (posm == 18) or (posm == 24): #plt.yticks(visible=True) ax.yaxis.set_label_position("right") plt.yticks(yticks, ytick_labels, fontsize=ytickfontsize, visible=True) else: plt.yticks(visible=False) if (posm == 30): plt.ylabel(r"$\theta \xi_-\times 10^{4}$", fontsize=11) ax.yaxis.set_label_position("right") plt.yticks(yticks[:-1], ytick_labels[:-1], fontsize=ytickfontsize) else: pass if posm in [30, 29, 28, 27]: plt.xlabel(r"$\theta \;\; [ \mathrm{arcmin} ]$ ", fontsize=10) plt.xscale("log") plt.xlim(2.2, 270) plt.xticks([10, 100], ["10", "100"], fontsize=9) plt.yticks(yticks[:-1], ytick_labels[:-1], fontsize=ytickfontsize) plt.axhline(0, color='k', ls=':') if show_cuts: xlower, xupper = lims['-'][(i + 1, j + 1)] plt.axvspan(1e-6, xlower, color='gray', alpha=0.2) plt.axvspan(xupper, 500, color='gray', alpha=0.2) xlower_opt, xupper_opt = lims_opt['-'][(i + 1, j + 1)] plt.axvspan(1e-6, xlower_opt, color='gray', alpha=0.2) plt.errorbar(xm, xm * xim * 1e4, yerr=xm * dxim * 1e4, marker='.', linestyle='none', markerfacecolor='k', markeredgecolor='k', ecolor='k', markersize=markersize) plt.plot(xta, xta * xim_theory * 1e4, color='darkmagenta', lw=1.5) plt.plot(xta, F * xta * (xim_theory_gi + xim_theory_ii) * 1e4, color='steelblue', lw=1., ls='-', label='GI') plt.plot(xta, F * xta * xim_theory_gi * 1e4, color='pink', lw=1.5, ls='--') plt.plot(xta, F * xta * xim_theory_ii * 1e4, color='midnightblue', lw=1.5, ls=':') plt.legend([p1, p2, p3], title='title', bbox_to_anchor=(1.05, 1), loc='upper right', fontsize=12) # ax = plt.subplot(111) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.65, box.height]) legend_x = 4.2 legend_y = 5.2 proxies = [ plt.Line2D([0, 2.5], [0, 0], linestyle=ls, linewidth=1.5, color=lc) for ls, lc in [('-', 'darkmagenta'), ('-', 'steelblue'), ( '--', 'pink'), (':', 'midnightblue')] ] plt.legend(proxies, [ "GG+GI+II", r"$10 \times$ GI+II", r"$10 \times$ GI", r'$10 \times$ II' ], loc='upper right', bbox_to_anchor=(legend_x, legend_y), fontsize=9) plt.subplots_adjust(hspace=0, wspace=0, bottom=0.14, left=0.14, right=0.88) plt.savefig("plots/unblinded_datavector_xipm_maglimbf.pdf") plt.savefig("plots/unblinded_datavector_xipm_maglimbf.png")
# ATTENZIONE ALLE UNITA' DI MIAURA dx = 4 / 1000 dy = np.array([1] * len(y)) x = x / 1000 #Funzione carica def carica(x, q, t): return q * (1 - np.exp(-x / t)) init = (1023, 15) #q è il max valore in digit di delta V (1023), t è tau=RC #UNITA' DI MISURAAAAA pylab.figure(0) #Serve per vedere se gli init sono giusti pylab.errorbar(x, y, dy, dx, linestyle='', color='black', marker='.') xx = np.linspace(min(x), max(x), 2000) pylab.plot(xx, carica(xx, *init), color='red') #FIT sigma = dy w = 1 / sigma**2 pars, covm = curve_fit(carica, x, y, init, sigma) chi2 = ((w * (y - carica(x, *pars))**2)).sum() ndof = len(x) - len(init) print('pars:', pars) print('covm:\n', covm) dq, dt = np.sqrt(covm.diagonal()) print('\ndq=%f , dt=%f' % (dq, dt))
T = [12, 25, 43] c_fit = ['r', 'g', 'b'] c_dati = ['y', 'c', 'm'] for i in range(len(files)): filename = files[i] I, P = py.loadtxt(filename, unpack=True) dI = py.ones(len(I)) * 0.1 dP = py.ones(len(P)) for k in range(len(P)): dP[k] = P[k] * 0.05 ##faccio il grafico P-I py.figure(1) py.errorbar(I, P, dP, dI, '.', color=c_dati[i], markersize='3') ##estraggo i dati da fittare j = 0 while P[j] > 300: j = j + 1 ##funzione di fit def retta(x, a, b): return a * x + b ##trovo I_th con un fit m = [] dm = [] q = [] dq = []
def plot_corr_w2_col(theta, w2, xlabel, ylabel, label, title='', SNR=False, scales_SNR=None, with0=False, ylog=False): """Plot columns theta versus w2, containing data and error column(s). Called by plot_corr_w2_internal. Parameters ---------- theta: array of length N of float x-axis data w2: array NxNcol of float y-axis data, sets of data and error columns xlabel: string x-axis label ylabel: string y-axis label label: array of length 2 of string labels for the two data sets (w2) title: string title string (default: empty) SNR: boolean If True, calculates and adds as text to plot signa-to-noise ratio. scales_SNR: string, two numbers with separator '_' or ' ' Minimum and maximum scale for SNR calculation. If None (default) use full range. with0: bool If True, adds the y=0 line. Default: False ylog: bool If True, plot log of y-axis, default: False Returns ------- None """ color = ['b', 'g'] msize = [5, 4] sign = [-1, 1] f_err, f_lim, log_x = prepare_plot(theta, title, xlabel, ylabel) if len(w2) == 4: yerr = [w2[2], w2[3]] Ndata = 2 elif len(w2) == 2: yerr = [w2[1]] Ndata = 1 else: mkstuff.error('Wrong number {} of columns in w2'.format(len(w2))) if ylog is True: for i in range(0, Ndata): # dlogy = dy/y yerr[i] = yerr[i] / w2[i] id_pos = np.where(w2[i]>0) id_neg = np.where(w2[i]<=0) y_pos = np.log10(w2[i][id_pos]) y_neg = np.log10(-w2[i][id_neg]) if i == 0: mkplot.make_log_ticks('y', min(y_pos)-2, max(y_pos)+1) plt.plot(log_x[id_pos] + sign[i]*f_err, y_pos, '{}.'.format(color[i]), marker='o', markersize=msize[i], label=label[i]) plt.plot(log_x[id_neg] + sign[i]*f_err, y_neg, '{}o'.format(color[i]), marker='o', mfc='none', markersize=msize[i], label='') plt.errorbar(log_x[id_pos] + sign[i]*f_err, y_pos, yerr=yerr[i][id_pos], fmt='none', ecolor='{}'.format(color[i]), elinewidth=1, label='') plt.errorbar(log_x[id_neg] + sign[i]*f_err, y_neg, yerr=yerr[i][id_neg], fmt='none', ecolor='{}'.format(color[i]), elinewidth=0.5, label='') else: for i in [0, Ndata-1]: plt.plot(log_x + sign[i]*f_err, w2[i], '{}'.format(color[i]), linewidth=1, label=label[i]) plt.errorbar(log_x + sign[i]*f_err, w2[i], yerr=yerr[i], fmt='none', ecolor='{}'.format(color[i]), label='') if with0 is True: plt.plot([min(log_x), max(log_x)], [0, 0], 'k', linewidth=1, label=None) plt.legend(loc='upper right', numpoints=1, frameon=False) if SNR is True: imin, imax = get_index_range(theta, scales_SNR) ax = plt.subplot(1, 1, 1) if len(w2) == 4: snr = signal_to_noise_w2_var(w2[0][imin:imax+1], w2[2][imin:imax+1]) snr_x = signal_to_noise_w2_var(w2[1][imin:imax+1], w2[3][imin:imax+1]) plt.text(0.95, 0.7, 'SNR_x = {}'.format('%.2f' % snr_x), ha='right', transform=ax.transAxes) elif len(w2) == 2: snr = signal_to_noise_w2_var(w2[0][imin:imax+1], w2[1][imin:imax+1]) plt.text(0.95, 0.75, 'SNR_t = {}'.format('%.2f' % snr), ha='right', transform=ax.transAxes) ymin, ymax = plt.ylim() dx2 = (log_x[1] - log_x[0])/2 plt.plot([log_x[imin]-dx2, log_x[imin]-dx2], [ymin, ymax], linestyle='dashed', color='black') plt.plot([log_x[imax]+dx2, log_x[imax]+dx2], [ymin, ymax], linestyle='dashed', color='black') # From plot_wgl.py:plot_stacked_wgl mkplot.update_lim('x', max_new = np.log10(max(theta) * f_lim))
rdev = simpleRouteDev demand.append(int(line.split()[-1])) if "agent" in line: mean = agentWaitMean dev = agentWaitDev rmean = agentRouteMean rdev = agentRouteDev if "waiting" in line: mean.append(float(line.split()[-2])) dev.append(float(line.split()[-1])) if line.startswith("('footmain0to1'"): rmean.append(float(line.split()[-2])) rdev.append(float(line.split()[-1])) stats.close() figure() errorbar(demand, simpleWaitMean, simpleWaitDev, lw=2, ms=10, fmt='o', label='standard bus scenario') errorbar(demand, agentWaitMean, agentWaitDev, lw=2, ms=10, color="red", fmt='o', label='agent controlled cyber cars') xlim(0, 50) ylim(0, 3300) xlabel('Repeater interval (s)') ylabel('Waiting time (s)') title('Mean and standard deviation of waiting time') legend(numpoints=1) savefig("waitingtime.png") figure() errorbar(demand, simpleRouteMean, simpleRouteDev, lw=2, ms=10, fmt='o', label='standard bus scenario') errorbar(demand, agentRouteMean, agentRouteDev, lw=2, ms=10, color="red", fmt='o', label='agent controlled cyber cars') xlim(0, 50)
colours = ['#7223AD', '#A4CD64', '#DD9EE8', '#3775A1'] #plt.subplot(121,aspect=0.11594202898550725) plt.subplot(211) plt.minorticks_on() plt.xlim(0.2, 1.05) plt.ylim(1.3, 2.5) plt.axhline(1, color='k', ls=':') plt.ylabel('Galaxy bias $b_g$', fontsize=fontsize) plt.xticks([0.2, 0.4, 0.6, 0.8, 1.], visible=False) plt.errorbar(x_lens_ml, b_ml_mid, yerr=[b_ml_mid - b_ml_lower, b_ml_upper - b_ml_mid], label=r'Maglim ($b_g$)', linestyle='none', marker='o', markeredgecolor=colours[0], markerfacecolor=colours[0], ecolor=colours[0], markersize=3.5) plt.errorbar(x_lens_rm, b_rm_mid, yerr=[b_rm_mid - b_rm_lower, b_rm_upper - b_rm_mid], label=r'redMaGiC ($b_g$)', linestyle='none', marker='o', markerfacecolor=colours[2], markeredgecolor=colours[2], ecolor=colours[2], markersize=3.5) plt.legend(loc='upper left', fontsize=8)
# Power spectra sig_k, sig_pk, sig_stddev = box.binned_power_spectrum(delta_x=signal_cube) proc4_k, proc4_pk, proc4_stddev = box.binned_power_spectrum( delta_x=cleaned_cube4) proc8_k, proc8_pk, proc8_stddev = box.binned_power_spectrum( delta_x=cleaned_cube8) proc4hp_k, proc4hp_pk, proc4hp_stddev = box.binned_power_spectrum( delta_x=cleaned_cube4_hp) th_k, th_pk = box.theoretical_power_spectrum() amp_fac = (tracer.signal_amplitude() * tracer.bias_HI())**2. plt.figure() plt.subplot(111) plt.plot(th_k, th_pk * amp_fac, 'k-') plt.errorbar(sig_k, sig_pk, yerr=sig_stddev, color='r', marker='.') plt.errorbar(proc4_k, proc4_pk, yerr=proc4_stddev, color='b', marker='x') plt.errorbar(proc4hp_k, proc4hp_pk, yerr=proc4hp_stddev, color='y', marker='s') plt.errorbar(proc8_k, proc8_pk, yerr=proc8_stddev, color='g', marker='+') plt.xscale('log') plt.yscale('log') #plt.show() #sys.exit(0) # Plot correlation functions and vanilla theoretical prediction plt.figure() plt.subplot(111) r = corr_true['r'] h = box.cosmo['h']
ax.yaxis.grid(True, linestyle='-', which='major', color='#dddddd', alpha=0.5, zorder=1) box_plots = plt.bar(ind, means[:, 0], width, color='#cccccc', alpha=0.35, zorder=2) bars = errorbar(ind + (width / 2 - 0.0), means[:, 0], yerr=stds, color='k', ecolor='r', elinewidth='4', capsize=0, linestyle='None', zorder=3) ax.set_xlim(0, ind.max()) ax.set_ylabel('Semi-quantitative Un-sharpness Score') ax.set_xlabel('Aperture') ax.set_xticks(ind + width / 2) ax.set_title('Unsharpness of ' + camera_model + ' with ' + lens_name + ' lens') ax.set_xticklabels(means[:, 1]) for tick in ax.axes.get_xticklines(): tick.set_visible(False) axis.Axis.zoom(ax.xaxis, -0.3) show()
top=.95, bottom=.15, wspace=.35, hspace=.15, right=0.95) #Plot 2 p.figure(1) pklo, pkhi = 1e-6, 1e18 #1e2,1e14 ax2 = p.subplot(gs[4]) #used to be 2 #p.loglog(pIs, pCs, 'k.') p.setp(ax2.get_yticklabels(), visible=False) #uncomment if no left-hand P(k) plot p.errorbar(pIs, n.abs(pCs), xerr=2 * pIs_err, yerr=2 * pCs_err, capsize=0, fmt='k.') p.loglog([pklo, pkhi], [pklo, pkhi], 'k-') p.xlim(pklo, pkhi) p.ylim(pklo, pkhi) p.xlabel(r'$P_{\rm in}(k)\ [{\rm mK}^2\ (h^{-1}\ {\rm Mpc})^3]$', fontsize=14) p.ylabel(r'$P_{\rm out}(k)\ [{\rm mK}^2\ (h^{-1}\ {\rm Mpc})^3]$', fontsize=14) p.grid() pkup = max(n.abs(pCvs)) pkdn = min(n.abs(pCvs)) p.fill_between([pklo, pkhi], [pkdn, pkdn], [pkup, pkup], facecolor='gray', edgecolor='gray') """ for kpl,pk,err in zip(kpls,pks,errs):
# for redshift bins # z1: 2.15 < z < 2.5 # z2: 2.5 < z < 5 # z3: 2.5 < z < 5 # z4: 1 < z < 2.15 # z1: 0.1 < z < 5 # *ALL wavelengths are in micron* # *ALL flux densities are in *mJy* # lambda[micron] frequency [GHz] mean_flux_z1 [mJy] mean_flux_z2 [mJy] mean_flux_z3 [mJy] mean_flux_z4 [mJy] mean_flux_z5 [mJy] 1sigma_err_z1 [mJy] 1sigma_err_z2 [mJy] 1sigma_err_z3 [mJy] 1sigma_err_z4 [mJy] 1sigma_err_z5 [mJy] ''' np.savetxt(outfile, data_out, header=header, fmt='%.5e') data = np.loadtxt(outfile, unpack=True) nus = data[1] pl.errorbar(nus, data[6], yerr=2 * data[11], marker='o', label='0.1 < z < 5') pl.errorbar(nus, data[5], yerr=2 * data[10], marker='o', label='1 < z < 5', linestyle='') pl.axhline(0, 0, 3400, linestyle='--', color='black') pl.xscale('log') pl.xlabel('Frequency [GHz]') pl.ylabel('Flux [mJy]') pl.legend(loc='best') pl.grid() pl.ylim(-2, 16) pl.savefig('one_bin_quasar_stack_%s.png' % run_tag) #pl.show()
def Aeronet_measures(site, date, time, plot_all=0, plot_date=0, plot_ang=0, root = None): ''' A function for the retrieval of aeronet measurements at specific date and time from an Aeroent site linear interpolation used for the interpolation over time of the aot and corresponding Triple values A fitting to Angstrom is used for the AOT 550 instead of 1st or 2nd order polynominal fitting args: site -- Aeronet site names date -- date in the format of 'yyyy-mm-dd' time -- float vlaue of time in a day, like '23.56' plot_all -- plot both the Aeronet measurements of the day and Angstrom fitting root -- directory to the aeronet measurements files downloaded from 'https://aeronet.gsfc.nasa.gov/new_web/index.html' return: aero_date -- aeronet measurements of the day err -- Triple error aot_550 -- aot_550 at the time (ang, off) -- Angstrom and fitting off set ''' fname = glob(root+'*%s*'%site)[0] aero = read_aeronet(fname) keys = aero[date].keys() aero_date = aero[date].loc[:,keys[0]:'AOT_340'].dropna(axis=1, how='all').dropna(axis=0, how='all') err = aero[date].loc[:,'%TripletVar_'+keys[0].split('_')[-1]:'%TripletVar_340'].dropna(axis=1, how='all').dropna(axis=0, how='all') #Angstrom_440_870 = aero[date].loc[:,'440-870Angstrom'] hours = np.array([i.hour+ i.minute/60. + i.second/3600. for i in aero_date.index]) f_aot = interp1d(hours, aero_date.T, bounds_error=0) f_err = interp1d(hours, err.T, bounds_error=0) #f_ang = interp1d(hours, Angstrom_440_870.T, bounds_error=0) wv = np.array([int(i.split('_')[-1]) for i in aero_date.keys()]) ynew = f_aot(time) enew = f_err(time) aot_550_1, p1, error_1 = inter_aot(wv, ynew, full=1, Second=0) aot_550_2, p2, error_2 = inter_aot(wv, ynew, full=1, Second=1) if plot_all | plot_date: plt.figure(figsize=(12,6)) for i,_ in enumerate(aero_date.keys()): plt.errorbar(aero_date.index, aero_date[aero_date.keys()[i]],ls='--',marker='o', ms=1, yerr= 0.01*err[err.keys()[i]],capsize=3, elinewidth=0.5,lw=0.5, markeredgewidth=0.5, label=aero_date.keys()[i]) tickes = pd.DatetimeIndex([date+' %02d:00:00'%i for i in np.arange(0,24.,3)]) ticks = ['%02d:00:00'%i for i in np.arange(0,24., 3)] plt.xticks(tickes, ticks, rotation=45) plt.xlim(tickes[0], pd.DatetimeIndex([date+' 23:59:59'])) plt.legend() plt.title('%s %s Level 2 AOT'%(site, date)) plt.ylabel('AOT') if plot_all | plot_ang: fig = plt.figure(figsize=(12,6)) ax = fig.add_subplot(111) ax.errorbar(wv, ynew,enew*0.01, label='Aeronets', ls='--',marker='o', ms=2, capsize=2, elinewidth=0.5,ecolor='r',c='k',lw=0.5, markeredgewidth=0.5) ang_aot_1 = np.exp(-1*p1(np.log(np.arange(wv.min(), wv.max()+1, 1)))) ang_aot_2 = np.exp(-1*p2(np.log(np.arange(wv.min(), wv.max()+1, 1)))) ax.plot(np.arange(wv.min(), wv.max()+1, 1), ang_aot_1, '--', label='1st order Fiiting') ax.plot(np.arange(wv.min(), wv.max()+1, 1), ang_aot_2, '--', label='2nd order Fiiting') ax.plot(550, aot_550_1 , 'o', label='1st order AOT 550') ax.plot(550, aot_550_2 , 'o', label='2nd order AOT 550') ax.annotate('1st oder fitting AOT 550 = %.03f'%aot_550_1, xy=(550, aot_550_1), xycoords='data', xytext=(0.5, 0.75), textcoords='axes fraction', arrowprops=dict(facecolor='black', shrink=0.05, width=1, headwidth=5), horizontalalignment='left', verticalalignment='top', ) ax.annotate('2nd oder fitting AOT 550 = %.03f'%aot_550_2, xy=(550, aot_550_2), xycoords='data', xytext=(0.5, 0.55), textcoords='axes fraction', arrowprops=dict(facecolor='black', shrink=0.05, width=1, headwidth=5), horizontalalignment='left', verticalalignment='top', ) plt.legend() plt.ylabel('AOT') plt.xlabel('Wavelength ($nm$)') plt.title('AOT at %02d:%02d:%02d'%(int(time), (time-int(time))*60, ((time - int(time))*60.-int((time-int(time))*60))*60)) return [aot_550_2, p2, error_2], [aot_550_1, p1, error_1]
#----------------------------------------------------------------------------- N = 40 x = numpy.linspace(0.0, 100.0, N) #----------------------------------------------------------------------------- # test 1 -- linear data print("Linear fit to linear data\n") # make up the experimental data with errors sigma = 25.0 * numpy.ones(N) y = yExperiment(10.0, 3.0, sigma, x) pylab.scatter(x, y) pylab.errorbar(x, y, yerr=sigma, fmt=None) # do the linear regression a1, a2, chisq, sigmaFit = linearRegression(x, y, sigma, sigmaa=1) pylab.plot(x, a1 + a2 * x) print("reduced chisq = ", chisq) print(" a1 = %f +/- %f\n a2 = %f +/- %f\n" % (a1, sigmaFit[0], a2, sigmaFit[1])) pylab.savefig("linear-regression.png") #----------------------------------------------------------------------------- # test 2 -- quadratic data
# note: if we wanted to deal with error bars, we would weight each # residual accordingly return y - a0 * numpy.exp(a1 * x) # make up some experimental data a0 = 2.5 a1 = 2. / 3. sigma = 2.0 x = numpy.linspace(0.0, 4.0, 25) y = a0 * numpy.exp(a1 * x) + sigma * numpy.random.randn(len(x)) pylab.scatter(x, y) pylab.errorbar(x, y, yerr=sigma, fmt=None, label="_nolegend_") # initial guesses a0 = 1 a1 = 1 # fit -- here the args is a tuple of objects that will be added to the # argument lists for the function to be minimized (resid in our case) afit, flag = optimize.leastsq(resid, [a0, a1], args=(x, y)) print flag print afit p = pylab.plot(x, afit[0] * numpy.exp(afit[1] * x), label=r"$a_0 = $ %f; $a_1 = $ %f" % (afit[0], afit[1]))
def ReadAndRemoveEM(filename, readsecond=False, doplot=False, dellast=True, ePhi=0.5, ePerc=1., lam=2000.): """ Read res1file and remove EM effects using a double-Cole-Cole model fr,rhoa,phi,dphi = ReadAndRemoveEM(filename, readsecond/doplot bools) """ fr, rhoa, phi, drhoa, dphi = read1resfile(filename, readsecond, dellast=dellast) # forward problem mesh = pg.createMesh1D(1, 6) # 6 independent parameters f = DoubleColeColeModelling(mesh, pg.asvector(fr), phi[2] / abs(phi[2])) f.regionManager().loadMap("region.control") model = f.createStartVector() # inversion inv = pg.RInversion(phi, f, True, False) inv.setAbsoluteError(phi * ePerc * 0.01 + ePhi / 1000.) inv.setRobustData(True) # inv.setCWeight(pg.RVector(6, 1.0)) # wozu war das denn gut? inv.setMarquardtScheme(0.8) inv.setLambda(lam) inv.setModel(model) erg = inv.run() inv.echoStatus() chi2 = inv.chi2() mod0 = pg.RVector(erg) mod0[0] = 0.0 # set IP term to zero to obtain pure EM term emphi = f(mod0) resid = (phi - emphi) * 1000. if doplot: s = "IP: m= " + str(rndig(erg[0])) + " t=" + str(rndig(erg[1])) + \ " c =" + str(rndig(erg[2])) s = s + " EM: m= " + str(rndig(erg[3])) + " t=" + str(rndig(erg[4])) +\ " c =" + str(rndig(erg[5])) fig = P.figure(1) fig.clf() ax = P.subplot(111) P.errorbar(fr, phi * 1000., yerr=dphi * 1000., fmt='x-', label='measured') ax.set_xscale('log') P.semilogx(fr, f(mod0) * 1000., label='EM term (CC)') P.errorbar(fr, resid, yerr=dphi * 1000., label='IP term') ax.set_yscale('log') P.xlim((min(fr), max(fr))) P.ylim((0.1, max(phi) * 1000.)) P.xlabel('f in Hz') P.ylabel(r'-$\phi$ in mrad') P.grid(True) P.title(s) P.legend(loc=2) # ('measured','2-cole-cole','residual')) fig.show() return N.array(fr), N.array(rhoa), N.array( resid), N.array(phi) * 1e3, dphi, chi2, N.array(emphi) * 1e3
#!/usr/bin/env python3.3 import pylab as plt import numpy as np x = np.arange(40) y = np.cos(x) yerr = np.random.uniform(0, 1, 40) plt.figure() #plt.plot(x,y) #plt.plot([10,20],[10]*2,'b-',linewidth=10) plt.errorbar(x, y, yerr=yerr, fmt='bo', capsize=5) #plt.show() plt.savefig('/tmp/testing_led.png')
def aspcap_residue_plot(self, test_predictions, test_labels, test_pred_error=None, test_labels_err=None): """ NAME: aspcap_residue_plot PURPOSE: plot aspcap residue INPUT: test_predictions (ndarray): Test result from neural network test_labels (ndarray): Gound truth for tests result test_pred_error (ndarray): (Optional) 1-sigma error for tests result from Baysian neural network. test_labels_err (ndarray): (Optional) Gound truth for tests result OUTPUT: None, just plots to be saved HISTORY: 2018-Jan-28 - Written - Henry Leung (University of Toronto) """ import pylab as plt import numpy as np import seaborn as sns print("Start plotting residues") resid = test_predictions - test_labels # Some plotting variables for asthetics plt.rcParams['axes.facecolor'] = 'white' sns.set_style("ticks") plt.rcParams['axes.grid'] = True plt.rcParams['grid.color'] = 'gray' plt.rcParams['grid.alpha'] = '0.4' x_lab = 'ASPCAP' y_lab = 'astroNN' fullname = self.targetname aspcap_residue_path = os.path.join(self.fullfilepath, 'ASPCAP_residue') if not os.path.exists(aspcap_residue_path): os.makedirs(aspcap_residue_path) mad_labels = np.zeros(test_labels.shape[1]) for i in range(test_labels.shape[1]): not9999_index = np.where(test_labels[:, i] != MAGIC_NUMBER) mad_labels[i] = mad((test_labels[:, i])[not9999_index], axis=0) if test_pred_error is None: # To deal with prediction from non-Bayesian Neural Network test_pred_error = np.zeros(test_predictions.shape) for i in range(self.labels_shape): plt.figure(figsize=(15, 11), dpi=200) plt.axhline(0, ls='--', c='k', lw=2) not9999 = np.where(test_labels[:, i] != -9999.)[0] plt.errorbar((test_labels[:, i])[not9999], (resid[:, i])[not9999], yerr=(test_pred_error[:, i])[not9999], markersize=2, fmt='o', ecolor='g', capthick=2, elinewidth=0.5) plt.xlabel('ASPCAP ' + target_name_conversion(fullname[i]), fontsize=25) plt.ylabel('$\Delta$ ' + target_name_conversion(fullname[i]) + '\n(' + y_lab + ' - ' + x_lab + ')', fontsize=25) plt.tick_params(labelsize=20, width=1, length=10) if self.labels_shape == 1: plt.xlim([ np.min((test_labels[:])[not9999]), np.max((test_labels[:])[not9999]) ]) else: plt.xlim([ np.min((test_labels[:, i])[not9999]), np.max((test_labels[:, i])[not9999]) ]) ranges = (np.max((test_labels[:, i])[not9999]) - np.min( (test_labels[:, i])[not9999])) / 2 plt.ylim([-ranges, ranges]) bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=2) bias = np.median((resid[:, i])[not9999], axis=0) scatter = mad((resid[:, i])[not9999], axis=0) plt.figtext(0.6, 0.75, '$\widetilde{m}$=' + '{0:.3f}'.format(bias) + ' $\widetilde{s}$=' + '{0:.3f}'.format(scatter / float(mad_labels[i])) + ' s=' + '{0:.3f}'.format(scatter), size=25, bbox=bbox_props) plt.tight_layout() plt.savefig(aspcap_residue_path + f'/{fullname[i]}_test.png') plt.close('all') plt.clf() if test_labels_err is not None: for i in range(self.labels_shape): plt.figure(figsize=(15, 11), dpi=200) plt.axhline(0, ls='--', c='k', lw=2) not9999 = np.where(test_labels[:, i] != -9999.)[0] plt.scatter((test_labels_err[:, i])[not9999], (resid[:, i])[not9999], s=0.7) plt.xlabel('ASPCAP Error of ' + target_name_conversion(fullname[i]), fontsize=25) plt.ylabel('$\Delta$ ' + target_name_conversion(fullname[i]) + '\n(' + y_lab + ' - ' + x_lab + ')', fontsize=25) plt.tick_params(labelsize=20, width=1, length=10) if self.labels_shape == 1: plt.xlim([ np.percentile((test_labels_err[:])[not9999], 5), np.percentile((test_labels_err[:])[not9999], 95) ]) else: plt.xlim([ np.min((test_labels_err[:, i])[not9999]), np.percentile((test_labels_err[:, i])[not9999], 90) ]) ranges = (np.percentile( (resid[:, i])[not9999], 5) - np.percentile( (resid[:, i])[not9999], 95)) plt.ylim([-ranges, ranges]) plt.tight_layout() plt.savefig(aspcap_residue_path + f'/{fullname[i]}_test_err.png') plt.close('all') plt.clf() print("Finished plotting residues")
pylab.plot(t, f[:, i], label=r'$sigma$=%.3f' % sigma[i]) pylab.plot(t, func(t, 1.05, -0.105), '-k', label='True curve') # Ploting y vs x As Lines And Markers pylab.legend( loc='upper right') # Placing A Legend On The Top Right Corner Of The Graph pylab.xlabel(r't$\rightarrow$', fontsize=15) # Setting The Label For The x-axis pylab.ylabel(r'f(t)+noise$\rightarrow$', fontsize=15) # Setting The Label For The y-axis pylab.grid(True) # Displaying The Grid pylab.show() # Displaying The Figure data = f[:, 0] pylab.figure(1) # Creating A New Figure pylab.errorbar( t[::5], data[::5], sigma[0], fmt='ro', label='Errorbar' ) # Ploting y vs x As Lines And Markers With Attached Error Bars pylab.plot(t, func(t, 1.05, -0.105), 'b', label='f(t)') # Ploting y vs x As Lines And Markers pylab.legend( loc='upper right') # Placing A Legend On The Top Right Corner Of The Graph pylab.xlabel(r't$\rightarrow$', fontsize=15) # Setting The Label For The x-axis pylab.grid(True) # Displaying The Grid pylab.show() # Displaying The Figure x = sp.jv(2, t) M = pylab.c_[x, t] b = np.array([1.05, -0.105]) Lhs = np.matmul(M, b) # Matrix Multiplication Rhs = np.array(func(t, 1.05, -0.105))
Add error bars to a scatterplot or bar chart. ----------------------------------------------------------- (c) 2013 Allegra Via and Kristian Rother Licensed under the conditions of the Python License This code appears in section 16.4.3 of the book "Managing Biological Data with Python". ----------------------------------------------------------- ''' from pylab import figure, errorbar, bar, savefig figure() # scatterplot with error bars x1 = [0.1, 0.3, 0.5, 0.6, 0.7] y1 = [1, 5, 5, 10, 20] err1 = [3, 3, 3, 10, 12] errorbar(x1, y1, err1 , fmt='ro') # barplot with error bars x2 = [1.1, 1.2, 1.3, 1.4, 1.5] y2 = [10, 15, 10, 15, 17] err2 = (2, 3, 4, 1, 2) width = 0.05 bar(x2, y2, width, color='r', yerr=err2, ecolor="black") savefig('errorbars.png')
def clt(): for sampleSize in sampleSizes: sampleMeans = [] for t in range(20): sample = flipCoin(sampleSize) ## FILL THIS IN sampleMeans.append(getMeanAndStd(sample)[0]) ## FILL IN TWO LINES ## WHAT TO DO WITH THE SAMPLE MEANS? #the sameple means is normally distributed #if we take the mean of the sample mean #it is the mean of the normal distribution meanOfMeans.append(getMeanAndStd(sampleMeans)[0]) stdOfMeans.append(getMeanAndStd(sampleMeans)[1]) clt() pylab.figure(1) pylab.errorbar(sampleSizes, meanOfMeans, yerr=1.96 * pylab.array(stdOfMeans), label='Est. mean and 95% confidence interval') pylab.xlim(0, max(sampleSizes) + 50) pylab.axhline(0.65, linestyle='--', label='True probability of Heads') pylab.title('Estimates of Probability of Heads') pylab.xlabel('Sample Size') pylab.ylabel('Fraction of Heads (minutes)') pylab.legend(loc='best') pylab.show()