def make_bb_plot(data, p0, save_dir, range=None,title='Plot of thing vs thing', xlabel='X axis', ylabel='Y axis',save_name='plot', overlay_reg_bins = True, edges=None,scale=None, bins=80): normed=False if scale=='normed': normed=True scale=None if edges != None: bb_edges=edges else: bb_edges = bayesian_blocks(data,p0=p0) plt.figure() #bin_content = np.histogram(data,bb_edges,density=True)[0] #plt.yscale('log', nonposy='clip') hist(data,bins=bins,range=range,histtype='stepfilled',alpha=0.2,label='{} bins'.format(bins),normed=normed,scale=scale) #hist(data,bins=100,histtype='stepfilled',alpha=0.2,label='100 bins',normed=False) bb_content, bb_edges,_ = hist(data,bins=bb_edges,range=range,histtype='step',linewidth=2.0,color='crimson',label='b blocks',normed=normed,scale=scale) #fill_between_steps(plt.gca(), bb_edges, bin_content*len(data),bin_content*len(data)/2, alpha=0.5, step_where='pre',linewidth=2,label='norm attempt') plt.legend() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.savefig(save_dir+save_name+'_bb.pdf') return bb_content,bb_edges
ST_dict_data = pkl.load(open("files/BHTree.p", "rb")) print "pickle loaded" ST_dict_data[mult] = [i for i in ST_dict_data[mult] if i > 2300] nentries = len(ST_dict_data[mult]) print nentries my_pdf = TF1("my_pdf", "2.3949e6*3.74/(0.67472+x)**10.1809", 2300, 6000) # my_pdf = TF1("my_pdf","2.3949e6/(0.67472+x)**10.1809",1900,5500) my_rands = [] # for i in xrange(nentries): for i in xrange(nentries): my_rands.append(my_pdf.GetRandom()) normed_counts_mc, bb_edges = np.histogram(my_rands, bayesian_blocks(my_rands), density=True) normed_counts_data, _ = np.histogram(ST_dict_data[mult], bb_edges, density=True) counts_mc, _ = np.histogram(my_rands, bb_edges) counts_data, _ = np.histogram(ST_dict_data[mult], bb_edges) rescaled_counts_mc = normed_counts_mc * nentries rescaled_counts_data = normed_counts_data * nentries bin_centres = (bb_edges[:-1] + bb_edges[1:]) / 2.0 rescaled_err = np.sqrt(counts_data) / (bb_edges[1:] - bb_edges[:-1]) err = np.sqrt(counts_data) fig = plt.figure() ax = plt.gca() ax.set_yscale("log", nonposy="clip") fill_between_steps(ax, bb_edges, rescaled_counts_mc, 1e-3, alpha=0.2, step_where="pre", linewidth=0, label="fit MC")
def make_comp_plots(data, p0, save_dir,title='Plot of thing vs thing', xlabel='X axis', ylabel='Y axis',save_name='plot'): bb_edges = bayesian_blocks(data,p0=p0) plt.figure() plt.yscale('log', nonposy='clip') hist(data,bins=100,histtype='stepfilled',alpha=0.2,label='100 bins',normed=True) hist(data,bins=bb_edges,histtype='step',linewidth=2.0,color='crimson',label='b blocks',normed=True) plt.legend() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.savefig(save_dir+save_name+'_binsVbb.pdf') plt.figure() plt.yscale('log', nonposy='clip') hist(data,'knuth',histtype='stepfilled',alpha=0.2,label='knuth',normed=True) hist(data,bins=bb_edges,histtype='step',linewidth=2.0,color='crimson',label='b blocks',normed=True) plt.legend() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.savefig(save_dir+save_name+'_knuthVbb.pdf') plt.figure() plt.yscale('log', nonposy='clip') hist(data,'scott',histtype='stepfilled',alpha=0.2,label='scott',normed=True) hist(data,bins=bb_edges,histtype='step',linewidth=2.0,color='crimson',label='b blocks',normed=True) plt.legend() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.savefig(save_dir+save_name+'_scottVbb.pdf') plt.figure() plt.yscale('log', nonposy='clip') hist(data,'freedman',histtype='stepfilled',alpha=0.2,label='freedman',normed=True) hist(data,bins=bb_edges,histtype='step',linewidth=2.0,color='crimson',label='b blocks',normed=True) plt.legend() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.savefig(save_dir+save_name+'_freedmanVbb.pdf') plt.figure() plt.yscale('log', nonposy='clip') hist(data,bins=bb_edges,histtype='stepfilled',alpha=0.4,label='b blocks',normed=True) plt.legend() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.savefig(save_dir+save_name+'_bb.pdf') plt.figure() plt.yscale('log', nonposy='clip') hist(data,bins=100,histtype='stepfilled',alpha=0.4,label='100 bins',normed=True) plt.legend() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.savefig(save_dir+save_name+'_bins.pdf') plt.figure() plt.yscale('log', nonposy='clip') hist(data,bins='knuth',histtype='stepfilled',alpha=0.4,label='knuth',normed=True) plt.legend() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.savefig(save_dir+save_name+'_knuth.pdf') plt.figure() plt.yscale('log', nonposy='clip') hist(data,bins='scott',histtype='stepfilled',alpha=0.4,label='scott',normed=True) plt.legend() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.savefig(save_dir+save_name+'_scott.pdf') plt.figure() plt.yscale('log', nonposy='clip') hist(data,bins='freedman',histtype='stepfilled',alpha=0.4,label='freedman',normed=True) plt.legend() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.savefig(save_dir+save_name+'_freedman.pdf')
ST_dict_data = pkl.load(open( "files/BHTree.p", "rb" )) print('pickle loaded') ST_dict_data[mult]=[i for i in ST_dict_data[mult] if i>2300] nentries = len(ST_dict_data[mult]) print(nentries) my_pdf = TF1("my_pdf","2.3949e6*3.74/(0.67472+x)**10.1809",2300,6000) #my_pdf = TF1("my_pdf","2.3949e6/(0.67472+x)**10.1809",1900,5500) my_rands = [] #for i in xrange(nentries): for i in range(nentries): my_rands.append(my_pdf.GetRandom()) normed_counts_mc, bb_edges = np.histogram(my_rands,bayesian_blocks(my_rands), density=True) normed_counts_data, _= np.histogram(ST_dict_data[mult],bb_edges, density=True) counts_mc, _= np.histogram(my_rands,bb_edges) counts_data, _= np.histogram(ST_dict_data[mult],bb_edges) rescaled_counts_mc = normed_counts_mc*nentries rescaled_counts_data = normed_counts_data*nentries bin_centres = (bb_edges[:-1] + bb_edges[1:])/2. rescaled_err = np.sqrt(counts_data)/(bb_edges[1:]-bb_edges[:-1]) err = np.sqrt(counts_data) fig = plt.figure() ax = plt.gca() ax.set_yscale("log", nonposy='clip') fill_between_steps(ax, bb_edges, rescaled_counts_mc,1e-3, alpha=0.2, step_where='pre',linewidth=0,label='fit MC')