def plot_timeseries(x, y, window, bins): pl.xscale('linear') pl.hist2d(x, y, bins=bins, alpha=0.5) plot_smoothed(x, y, window, c='k') pl.axhline(np.mean(y), c='k') pl.xlabel('time (s)') pl.ylabel('extension (nm)')
def simulateParticles_loop( n=50, n_gen=100, showPlots=True): ''' simulate gaussian motion of n particles for n_gen steps each ''' # store the value of each particle at every step f_all = np.zeros((n,n_gen)) for i in range(n): y = 0 f_all[i,0] = y for j in range(1,n_gen): y += np.random.randn() f_all[i,j] = y x_grid, y_grid = np.mgrid[0:n, 0:n_gen] if showPlots: plt.figure() plt.plot(y_grid.T, f_all.T, alpha=.2) plt.figure() plt.hist2d(y_grid.ravel(), f_all.ravel(), bins=(n_gen, 50)) plt.show() return y_grid, f_all
def run_synthetic_SGLD(): theta1 = 0 theta2 = 1 sigma1 = numpy.sqrt(10) sigma2 = 1 sigmax = numpy.sqrt(2) X = load_synthetic(theta1=theta1, theta2=theta2, sigmax=sigmax, num=100) minibatch_size = 1 total_iter_num = 1000000 lr_scheduler = SGLDScheduler(begin_rate=0.01, end_rate=0.0001, total_iter_num=total_iter_num, factor=0.55) optimizer = mx.optimizer.create('sgld', learning_rate=None, rescale_grad=1.0, lr_scheduler=lr_scheduler, wd=0) updater = mx.optimizer.get_updater(optimizer) theta = mx.random.normal(0, 1, (2,), mx.cpu()) grad = nd.empty((2,), mx.cpu()) samples = numpy.zeros((2, total_iter_num)) start = time.time() for i in xrange(total_iter_num): if (i + 1) % 100000 == 0: end = time.time() print("Iter:%d, Time spent: %f" % (i + 1, end - start)) start = time.time() ind = numpy.random.randint(0, X.shape[0]) synthetic_grad(X[ind], theta, sigma1, sigma2, sigmax, rescale_grad= X.shape[0] / float(minibatch_size), grad=grad) updater('theta', grad, theta) samples[:, i] = theta.asnumpy() plt.hist2d(samples[0, :], samples[1, :], (200, 200), cmap=plt.cm.jet) plt.colorbar() plt.show()
def skymap (tipo1, tipo2="none"): if tipo2 == "none": if tipo1 == "casa": pl.figure(2, figsize=(6, 5), facecolor='w', edgecolor='k') pl.hist2d(ra_casa, dec_casa, bins=51, norm=LogNorm()) pl.colorbar() pl.xlabel('Ascencion Recta [grados]') pl.ylabel('Declinacion [grados]') pl.show() elif tipo1 == "off": pl.figure(2, figsize=(6, 5), facecolor='w', edgecolor='k') pl.hist2d(ra_off, dec_off, bins=51, norm=LogNorm()) pl.colorbar() pl.xlabel('Ascencion Recta [grados]') pl.ylabel('Declinacion [grados]') pl.show() else: print "no tenemos esos datos" elif tipo1 == "casa" and tipo2 == "off": np.seterr(divide='ignore', invalid='ignore') pl.figure(3, figsize=(5, 5), facecolor='w', edgecolor='k') hist_casa, xedge, yedge = np.histogram2d(ra_casa, dec_casa, bins=15) hist_off, xedge, yedge = np.histogram2d(ra_off, dec_off, bins=15) hist_excess = np.subtract(hist_casa, hist_off) hist_excess = np.divide(hist_excess, hist_off) pl.imshow(hist_excess, interpolation='gaussian') pl.xlabel('Ascencion Recta [u.a.]') pl.ylabel('Declinacion [u.a.]') pl.show() else: print "no tenemos esos datos"
def _fill_hist(self, x, y, mapsize, data_coords, what='train'): x = np.arange(.5, mapsize[1]+.5, 1) y = np.arange(.5, mapsize[0]+.5, 1) X, Y = np.meshgrid(x, y) if what == 'train': a = plt.hist2d(x, y, bins=(mapsize[1], mapsize[0]), alpha=.0, cmap=cm.jet) area = a[0].T * 12 plt.scatter(data_coords[:, 1], mapsize[0] - .5 - data_coords[:, 0], s=area.flatten(), alpha=.9, c='None', marker='o', cmap='jet', linewidths=3, edgecolor='r') else: a = plt.hist2d(x, y, bins=(mapsize[1], mapsize[0]), alpha=.0, cmap=cm.jet, norm=LogNorm()) area = a[0].T*50 plt.scatter(data_coords[:, 1] + .5, mapsize[0] - .5 - data_coords[:, 0], s=area, alpha=0.9, c='None', marker='o', cmap='jet', linewidths=3, edgecolor='r') plt.scatter(data_coords[:, 1]+.5, mapsize[0]-.5-data_coords[:, 0], s=area, alpha=0.2, c='b', marker='o', cmap='jet', linewidths=3, edgecolor='r') plt.xlim(0, mapsize[1]) plt.ylim(0, mapsize[0])
def plotMAtreeFINALhist( scale1, mass1, Vmaxarray, plotfunc_count): print("Entered Final Hist Plot Function") plot_title="Mass Accretion History Histogram" #Can code the number in with treemax x_axis="scale time" y_axis="total mass" figure_name=os.path.expanduser('~/Mar12HISTMassAccrfig_num' + str(plotfunc_count) +'.png') plotfunc_count += 1 #Choose which type of plot you would like: Commented out. #yminv = 1000*2.57*10**8. yminv = 0 ymaxv = 4000*2.57*10**8. #The following code is to make the histogram appear much neater by eliminating values which are greatly out of range. plt.hist2d(scale1, mass1, (100, 500), cmap=plt.cm.jet, norm=matplotlib.colors.LogNorm() ) plt.ylim([yminv, ymaxv]) plt.title(plot_title) plt.xlabel(x_axis) plt.ylabel(y_axis) #plt.yscale("log") plt.savefig(figure_name) print("Saving plot: %s" % figure_name) #In order to Plot only a single tree on a plot must clear lists before loop. #Comment out to over plot curves. plt.clf() clearmass = [] clearscale = [] clearVmax = [] plotfunc_count += 1 #return clearmass, clearscale, clearVmax, plotfunc_count return plotfunc_count
def histogram(self, index1, index2, **options): data = self.datafile.data() # param 1 parameter1 = self.datafile.parameters[index1] data1 = data[:, index1] parameter2 = self.datafile.parameters[index2] data2 = data[:, index2] plot.clf() plot.figure(figsize=(10, 10), dpi=80) # x axis plot.xlabel(parameter1[0]) xmin = options['xmin'] if options['xmin'] != None else parameter1[1] xmax = options['xmax'] if options['xmax'] != None else parameter1[2] plot.xlim(xmin, xmax) # y axis plot.ylabel(parameter2[0]) ymin = options['ymin'] if options['ymin'] != None else parameter2[1] ymax = options['ymax'] if options['ymax'] != None else parameter2[2] plot.ylim(ymin, ymax) # plot plot.hist2d(data1, data2, bins=100) plot.tight_layout() plot.savefig(self.pdffile)
def elaborate_bivariate_stats(crStats , getter1 , getter2 , further_conditioning=None , hist_output=True , **kw ): out1,bins1,u_out1 = elaborate_univariate_stats(crStats ,getter1 ,further_conditioning ,hist_output=False) out2,bins2,u_out2 = elaborate_univariate_stats(crStats ,getter2 ,further_conditioning ,hist_output=False) final_hist_param = dict( hist2d_param.items() + kw.items() ) if hist_output: plt.hist2d(out1,out2,**final_hist_param) return out1,out2,u_out1,u_out2
def velocity_density_field(P, Rbox, x, y, z, vx, vy, vz, foutname, focusshell=2): print 'lets graph! ',foutname xmin = x - Rbox ymin = y - Rbox zmin = z - Rbox if (xmin < 0): xmin =0 if (ymin < 0): ymin = 0 if (zmin < 0): zmin = 0 xmax = x + Rbox ymax = y + Rbox zmax = z + Rbox tempx = np.linspace(xmin, xmax, 50) tempy = np.linspace(ymin, ymax, 50) velx = P['v'][:,0] - vx vely = P['v'][:,1] - vy X,Y = meshgrid(tempx, tempy) U =theplt.hist2d(P['p'][:,0], P['p'][:,1], range=[[xmin,xmax],[ymin,ymax]], bins=50, norm=LogNorm(), vmin=1, weights=velx) V = theplt.hist2d(P['p'][:,0], P['p'][:,1], range=[[xmin,xmax],[ymin,ymax]], bins=50, norm=LogNorm(), vmin=1, weights=vely) M = theplt.hist2d(P['p'][:,0], P['p'][:,1], range=[[xmin,xmax],[ymin,ymax]], bins=50, norm=LogNorm(), vmin=1) Q = theplt.quiver( X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3], M[::3, ::3], units='x', pivot='tip', linewidths=(2,), edgecolors=('k'), headaxislength=5, cmap=cm.get_cmap('hot')) theplt.colorbar() theplt.savefig(foutname) theplt.clf()
def plot_histogram(values, ground_truth, max_dist): import matplotlib.pyplot as plt plt.hist2d(ground_truth, values, (max_dist+1, max_dist+1), cmap=plt.cm.jet, norm=mpl.colors.LogNorm()) plt.xlabel('ground truth') plt.ylabel('prediction') plt.title('2d histogram of distance to membrane results') plt.show()
def plot(self): """ Gibt die räumliche Verteilung sowie die Energiespektren der Bereiche innerhalb eines 4 cm Radius um den Nullpunkt und außerhalb dessen in Konsole und Datei aus. """ plt.figure() plt.hist2d(self.particles.coords[:, 1], self.particles.coords[:, 2], bins=100) plt.colorbar() plt.title("Verteilung auf Detektor") plt.xlabel("y-Position") plt.ylabel("z-Position") plt.savefig("distribution.png") self.inner = (np.sqrt(np.sum(self.particles.coords[:, 1::] ** 2, 1)) < 40) * self.survivors plt.figure() plt.hist(self.particles.energy[self.inner] * 1e3, bins=50) plt.title("Spektrum in 4 cm Radius") plt.xlabel("E / keV") plt.ylabel("Anzahl") plt.savefig("inner.png") plt.figure() plt.hist(self.particles.energy[np.logical_not(self.inner)] * 1e3, bins=50) plt.title("Spektrum ausserhalb") plt.xlabel("E / keV") plt.ylabel("Anzahl") plt.savefig("outer.png")
def survey_overlap(xs, ys, xs1, ys1, c_graph = True, radius = -0.1, nbins = 1000): """ Takes two sets of points and sees if one is inside the other Might need nbins and radius tweaking xs, ys are the points to draw the contours off xs1, ys1 are the points to check if in contour """ import numpy as np import matplotlib.pyplot as plt xwalls = np.linspace( min(xs) - 5.0, max(xs) + 5.0, nbins + 1 ) ywalls = np.linspace( min(ys) - 5.0, max(ys) + 5.0, nbins + 1 ) im, xs_bin, ys_bin, ax = plt.hist2d(xs, ys, bins = (xwalls, ywalls) ) xs_mids = 0.5*(xs_bin[:-1] + xs_bin[1:]) ys_mids = 0.5*(ys_bin[:-1] + ys_bin[1:]) plt.close() im[im>0] = 1 conts = plt.contour(xs_mids, ys_mids, im.T, 1) xwalls1 = np.linspace( min(xs1) - 5.0, max(xs1) + 5.0, nbins + 1) ywalls1 = np.linspace( min(ys1) - 5.0, max(ys1) + 5.0, nbins + 1) im1, xs_bin1, ys_bin1, ax1 = plt.hist2d(xs1, ys1, bins = (xwalls1, ywalls1)) xs_mids1 = 0.5*(xs_bin1[:-1] + xs_bin1[1:]) ys_mids1 = 0.5*(ys_bin1[:-1] + ys_bin1[1:]) plt.close() im1[im1>0] = 1 conts = plt.contour(xs_mids, ys_mids, im.T, 1) conts1 = plt.contour(xs_mids1, ys_mids1, im1.T, 1) plt.show()
def partD(): ls = np.linspace(-math.pi/2, math.pi/2, DATA_DIM*2) rs = np.linspace(0.001, 25, DATA_DIM) l_list = [] vel_list = [] for i in range(len(ls)): for j in range(len(rs)): R = math.sqrt(rs[j]**2 + 10**2 - 2 * 10 * rs[j] * math.cos(ls[i])) if (R < 15): if (R > 4 and R < 6): vel_add = 50*math.cos( math.asin( 10*(math.sin(abs(ls[i]))/R )) ) #vel_add = 0 #subbing out radial expansion vel = vel4(rs[j], ls[i], R) - vel_add*(ls[i]/abs(ls[i])) l_list.append(ls[i]) l_list.append(ls[i]) vel_list.append(vel) vel_list.append(vel) else: l_list.append(ls[i]) vel_list.append(vel4(rs[j], ls[i], R)) plt.hist2d(l_list, vel_list, PLOT_DIM, cmin=1) plt.savefig("partd.png")
def generate_beam(): plt.clf() #grab data x = data_io.wireX y = data_io.wireY if len(x) == 0 or len(y) == 0: return None, None #make plot ax1 = plt.subplot2grid((3,3),(0,0), colspan=2) plt.hist(x, bins=50, range=[-5,5]) plt.xlim(-5,5) ax1.axes.get_xaxis().set_visible(False) ax1.axes.get_yaxis().set_visible(False) ax2 = plt.subplot2grid((3,3),(1,0), colspan=2, rowspan=2) plt.hist2d(x, y, bins=50, range=np.array([(-5,5),(-5,5)])) plt.xlim(-5,5) plt.ylim(-5,5) plt.xlabel('x position') plt.ylabel('y position') ax3 = plt.subplot2grid((3,3),(1,2), rowspan=2) plt.hist(y, bins=50, orientation='horizontal', range=[-5,5]) plt.ylim(-5,5) ax3.axes.get_xaxis().set_visible(False) ax3.axes.get_yaxis().set_visible(False) for temp_file in glob.glob(app.config['UPLOAD_FOLDER'] + '/temp_beam*'): os.remove(temp_file) filename = unique_filename('temp_beam.png') filepath = upload_path(filename) plt.savefig(filepath) return filename, filepath
def run_diagnostics(samples, function=None, plots=True): if plots: xlim = (-0.5, 1.5) ylim = (-1.5, 1.) # plot the sample distribution f = plt.gcf() f.set_size_inches(8, 8) plt.hist2d(samples[:,1], samples[:,0], bins=50, cmap=reds, zorder=100) plt.xlabel("Longitude") plt.ylabel("Latitude") # overlay the true function if function: plot_true_function(function, xlim, ylim) plt.show() plot_diagnostics(samples) gelman_rubin(samples) # gewecke #geweke_val = pymc.diagnostics.geweke(samples, intervals=1)[0][0][1] Geweke(samples)
def mark_pos(filename): x = [] y = [] with open(filename,"r") as f: line = f.readline().split() size = line[1] line = f.readline().split() T = line[1] line = f.readline().split() J = line[1] line = f.readline().split() H = line[1] tail = "_"+size+"_T"+T+"_J"+J+"_H"+H line = f.readline().split() while line: line = f.readline().split() if len(line)==2: x.append(int(line[0])) y.append(int(line[1])) fig = plt.figure(figsize = (5,5)) plt.title("mark_pos") plt.xlabel("x") plt.ylabel("y") plt.hist2d(x,y,bins = int(size)) plt.xlim(0,int(size)-1) plt.ylim(0,int(size)-1) #plt.savefig(filename[:-15]+tail+".png") plt.savefig(filename[:-4]+".png") plt.close()
def group_mark_pos(step, per_step): i = 0 while i<=step: x = [] y = [] filename = "./data/gif_dat/mark_pos_metropolis_"+str(i)+".dat" with open(filename,"r") as f: line = f.readline().split() size = line[1] line = f.readline().split() T = line[1] line = f.readline().split() J = line[1] line = f.readline().split() H = line[1] tail = "_"+size+"_T"+T+"_J"+J+"_H"+H line = f.readline().split() while line: line = f.readline().split() if len(line)==2: x.append(int(line[0])) y.append(int(line[1])) fig = plt.figure(figsize = (5,5)) plt.title("mark_pos") plt.xlabel("x") plt.ylabel("y") plt.hist2d(x,y,bins = int(size)) plt.xlim(0,int(size)-1) plt.ylim(0,int(size)-1) #plt.savefig(filename[:-15]+tail+".png") plt.savefig(filename[:-4]+".png") plt.close() i += per_step if (step//i != step//(i+1)): print(step//i)
def hist2d_plots(df, var1="diameter", var2="calcium"): plt.figure() plt.hist2d(df[var1], df[var2], bins=[20, 20], normed=True) plt.xlabel(var1) plt.ylabel(var2) plt.colorbar(label="Joint Probability")
def PlotHistogram(self,fname_extension=""): """ Plots the histogram of the accumulated data. """ data=[[] for i in range(self.dimensionality)] for window in self.windows: if not os.path.isfile(window.path_to_datafile):continue f=open(window.path_to_datafile,"r") for l in f: s=l.split() for i in range(self.dimensionality):data[i].append(float(s[i+1])) f.close() filename="histogram_{0}{1}".format(len(self.windows),fname_extension) hist_range=[(cv.min_value,cv.max_value) for cv in self.cv_list] plt.figure() if self.dimensionality==2: bins=[self.cv_list[0].num_bins,self.cv_list[1].num_bins] hist_range=[(self.cv_list[0].min_value,self.cv_list[0].max_value),(self.cv_list[1].min_value,self.cv_list[1].max_value)] plt.hist2d(data[0],data[1],range=hist_range,bins=bins) if self.cv_list[0].units:plt.xlabel("{0} [{1}]".format(self.cv_list[0].name,self.cv_list[0].units)) else:plt.xlabel("{0}".format(self.cv_list[0].name)) if self.cv_list[1].units:plt.ylabel("{0} [{1}]".format(self.cv_list[1].name,self.cv_list[1].units)) else:plt.ylabel("{0}".format(self.cv_list[1].name)) plt.colorbar() elif self.dimensionality==1: bins=self.cv_list[0].num_bins plt.hist(data[0],bins=bins,range=hist_range[0]) if self.cv_list[0].units:plt.xlabel("{0} [{1}]".format(self.cv_list[0].name,self.cv_list[0].units)) else:plt.xlabel("{0}".format(self.cv_list[0].name)) plt.ylabel("Count") plt.savefig(os.path.join(self.hist_dir,filename)) plt.close()
def hist2d(data, bins): data = np.array(data) x = data[:,0] y = data[:,1] plt.hist2d(x, y, bins) plt.colorbar()
def multi_scatter(self, y_axis_Col_index = 0, x_axis_Col_indexes = [0], Number_of_Figures = 1): ''' Import modules''' import matplotlib.pyplot as plt import numpy as np '''Set the number of rows and cols per Figure''' plots_per_figure = int(np.ceil(len(x_axis_Col_indexes)/float(Number_of_Figures))) num_cols_per_subplot = int(plots_per_figure**0.5) num_rows_per_subplot = int(plots_per_figure**0.5) while (num_rows_per_subplot*num_cols_per_subplot < plots_per_figure): num_rows_per_subplot = num_rows_per_subplot + 1 ''' Plot Data''' i = 0 for fig_index in range(1, Number_of_Figures + 1): plt.figure('multi_scatter' + ' ' + str(fig_index), facecolor = 'white').suptitle('Scatter plot with respect to ' + self.fields[y_axis_Col_index], fontsize = self.textfontsize + 2) plt.subplots_adjust(hspace = self.hspace, wspace = self.wspace) for subplot_int in range(1, plots_per_figure + 1): plt.subplot(num_rows_per_subplot, num_cols_per_subplot, subplot_int) correlation = np.corrcoef(self.data[:, x_axis_Col_indexes[i]], self.data[:, y_axis_Col_index])[0,1] if self.scatter_plot_density_status: plt.hist2d(self.data[:, x_axis_Col_indexes[i]], self.data[:, y_axis_Col_index], bins = self.scatter_plot_density_num_bins) plt.annotate('Correlation: ' + str(np.round(correlation,3)), xy=(0.1, 0.9), xycoords='axes fraction', fontsize = self.textfontsize, color = 'white') else: plt.plot(self.data[:, x_axis_Col_indexes[i]], self.data[:, y_axis_Col_index], '.', markersize = self.markersize) plt.annotate('Correlation: ' + str(np.round(correlation,3)), xy=(0.1, 0.9), xycoords='axes fraction', fontsize = self.textfontsize) plt.xlabel(self.fields[x_axis_Col_indexes[i]], fontsize = self.textfontsize) plt.tick_params(axis='both', which='major', labelsize = self.textfontsize - 1) i = i + 1 plt.grid(self.gridstate) if i == len(x_axis_Col_indexes): break
def draw_scatter_2(obj, varname1, varname2, loclist, idlist, stoparray): """ TODO """ varlist1 = np.array([]) varlist2 = np.array([]) cnt = 0 # continuous counter for startarray and stoparray for i in range(len(loclist)): print 'Plotting file', i+1, 'of', len(loclist) rootgrp = nc.Dataset(loclist[i], 'r') var1 = rootgrp.variables[varname1][:, :] var2 = rootgrp.variables[varname2][:, :] p = rootgrp.variables['P'][:, :] for j in idlist[i]: mask = (np.isfinite(p[:, j])) & (p[:, j] != 0) mask &= (np.isfinite(var1[:, j])) & (np.isfinite(var2[:, j])) if not stoparray == None: mask[:stoparray[cnt]] = False varlist1 = np.append(varlist1, var1[:, j][mask]) varlist2 = np.append(varlist2, var2[:, j][mask]) cnt += 1 varlist2 *= 1.e6 #plt.scatter(varlist1, varlist2) x_edges = np.arange(0, 1000 + 20, 20) y_edges = np.arange(-20, 20 + 0.8, 0.8) plt.hist2d(varlist1, varlist2, cmin = 1, norm = clr.LogNorm(1, 10000), bins = [x_edges, y_edges]) plt.colorbar()
def plot_replay_memory_2d_state_histogramm(states): x,v = zip(*states) plt.hist2d(x, v, bins=40, norm=LogNorm()) plt.xlabel("position") plt.ylabel("velocity") plt.colorbar() plt.show()
def plotMAtreeFINALhist( scale1, mass1, Vmaxarray, plotfunc_count): print("Entered Plot Function") plot_title="Mass Accretion History Histogram" #Can code the number in with treemax x_axis="scale time" y_axis="total mass" figure_name=os.path.expanduser('~/Jan14HISTMassAccretionfigure' +'.png') #Choose which type of plot you would like: Commented out. plt.hist2d(scale1, mass1, (100, 100), cmap=plt.cm.jet) #plt.plot(scale1, mass1, linestyle="", marker="o", markersize=3, edgecolor='none') #plt.plot(scale1, Vmaxarray, linestyle="-", marker="o") #plt.scatter(scale1, mass1, label="first tree") plt.title(plot_title) plt.xlabel(x_axis) plt.ylabel(y_axis) #plt.yscale("log") plt.savefig(figure_name) print("Saving plot: %s" % figure_name) print #In order to Plot only a single tree on a plot must clear lists before loop. #Comment out to over plot curves. plt.clf() clearmass = [] clearscale = [] clearVmax = [] plotfunc_count += 1 #return clearmass, clearscale, clearVmax, plotfunc_count return
def PlotHeatMap(Bins,X,Y,ContourLengthNm, LabelColorbar="Data points in bin"): plt.hist2d(X,Y,bins=Bins,cmap='afmhot') plt.axhline(65,color='g',linestyle='--',linewidth=4,alpha=0.3, label="65pN") plt.axvline(ContourLengthNm,color='c',linestyle='--',linewidth=4, alpha=0.3,label=r"L$_0$={:.1f}nm".format(ContourLengthNm))
def main(): # Parse commandline arguments parser = argparse.ArgumentParser(description="Runs benchmark that has been compiled with contech, generating a task graph and optionally running backend tools.") parser.add_argument("inFile", help="Input file, a json file of commRecords") parser.add_argument("outFile", help="Output file, a png of inter-thread communication.") args = parser.parse_args() x, y = [], [] nThreads = 0 print "Loading {}...".format(args.inFile) with open(args.inFile) as file: data = json.load(file) records = data["records"] print "Loaded {} records".format(len(records)) for r in records: src = int(r["src"].split(":")[0]) dst = int(r["dst"].split(":")[0]) nThreads = max([nThreads, src, dst]) x.append(dst) y.append(src) print "Plotting for {} threads, {} communication records...".format(nThreads, len(x)) # plt.title(os.path.basename(args.inFile).replace(".json","")) plt.xlabel("consumer CPU") plt.ylabel("producer CPU") tickPositions = [a + .5 for a in range(nThreads)] plt.xticks(tickPositions, range(nThreads)) plt.yticks(tickPositions, range(nThreads)) plt.hist2d(x, y, bins=nThreads, cmap=matplotlib.cm.Greys) plt.colorbar() plt.savefig(args.outFile)
def plot_impact_map(impactX, impactY, telX, telY, telTypes=None, Outfile="ImpactMap.png"): """ Map of the site with telescopes positions and impact points heatmap Parameters ---------- impactX: `numpy.ndarray` impactY: `numpy.ndarray` telX: `numpy.ndarray` telY: `numpy.ndarray` telTypes: `numpy.ndarray` Outfile: string - name of the output file """ plt.figure(figsize=(12, 12)) plt.hist2d(impactX, impactY, bins=40) plt.colorbar() assert (len(telX) == len(telY)), "telX and telY should have the same length" if telTypes: assert (len(telTypes) == len(telX)), "telTypes and telX should have the same length" plt.scatter(telX, telY, color=telTypes, s=30) else: plt.scatter(telX, telY, color='black', s=30) plt.axis('equal') plt.savefig(Outfile, bbox_inches="tight", format='png', dpi=200) plt.close()
def histo2d(r1, r2): nBin = 8 h1 = r1*nBin/255.0 h2 = r2*nBin/255.0 plt.hist2d(h1, h2, bins=nBin+1, norm=LogNorm()) plt.colorbar() plt.show()
def boundary_sim(xyini, exyini, a, xy_step, dt, tmax, expmt, ephi, vb): """ Run the LE simulation from (x0,y0), stopping if x<xmin. """ me = me0+".boundary_sim: " ## Initialisation x0,y0 = xyini nstp = int(tmax/dt) ## Simulate eta if vb: t0 = time.time() exy = np.vstack([sim_eta(exyini[0], expmt, nstp, a, dt), sim_eta(exyini[1], expmt, nstp, a, dt)]).T if vb: print me+"Simulation of eta",round(time.time()-t0,2),"seconds for",nstp,"steps" ## Spatial variables if vb: t0 = time.time() xy = np.zeros([nstp,2]); xy[0] = [x0,y0] ## Calculate trajectory for i in xrange(0,nstp-1): r = np.sqrt((xy[i]*xy[i]).sum()) xy[i+1] = xy[i] + xy_step(xy[i],r,exy[i]) if vb: print me+"Simulation of x",round(time.time()-t0,2),"seconds for",nstp,"steps" rcoord = np.sqrt((xy*xy).sum(axis=1)) ercoord = np.sqrt((exy*exy).sum(axis=1)) ## -----------------===================----------------- R = 2.0; S = 1.0; lam = 0.5; nu = 1.0 ## Distribution of spatial steps and eta if 0: from LE_RunPlot import plot_step_wall, plot_eta_wall, plot_step_bulk ## In wall regions plot_step_wall(xy,rcoord,R,S,a,dt,vb) plot_eta_wall(xy,rcoord,exy,ercoord,R,S,a,dt,vb) ## In bulk region plot_step_bulk(xy,rcoord,ercoord,R,S,a,dt,vb) exit() ## Trajectory plot with force arrows if 0: from LE_RunPlot import plot_traj plot_traj(xy,rcoord,R,S,lam,nu,force_dnu,a,dt,vb) exit() ## 2D Density plot if 0: R=5.0 plt.hist2d(xy[:,0],xy[:,1],1000,cmap="Blues") ang = np.linspace(0,2*np.pi,60) plt.plot(R*np.cos(ang),R*np.sin(ang),"g--",lw=2) plt.show() exit() ## -----------------===================----------------- if ephi: epcoord = np.arctan2(exy[:,1],exy[:,0]) - np.arctan2(xy[:,1],xy[:,0]) return [rcoord, ercoord, epcoord] else: return [rcoord, ercoord]
def doit(_DATADIR, _HANDLE): fullData = pd.DataFrame({'minutes':[],'fly_x':[],'fly_y':[],'FlyID':[],'GROUP':[]}) for directory in glob.glob(_DATADIR + '*' + _HANDLE + '*' + '*zoom*'): FLY_ID, FMF_TIME, GROUP = parse_fmftime(directory) try: df = pd.read_pickle(directory + '/frame_by_frame_synced.pickle') df['minutes'] = df.synced_time.astype('timedelta64[m]') tempdf = pd.DataFrame({'minutes':df.minutes, 'fly_x':df.fly_x, 'fly_y':df.fly_y, 'FlyID':FLY_ID, 'group':GROUP}) fullData = pd.concat([fullData, tempdf], axis=0) except: pass fullData.to_pickle(_DATADIR + '2D_positions_' + _HANDLE + '.pickle') plt.figure(figsize=(24,2)) for x in np.arange(-1,11): plt.subplot2grid((1,49),(0,4*(x+1)), colspan=4) slyce = fullData[fullData.minutes == x] print x, len(slyce), len(slyce.fly_x.notnull()) plt.hist2d(slyce.fly_x.values, slyce.fly_y.values, bins=50, norm=LogNorm()) plt.xlim(0,659) plt.ylim(0,494) plt.axis('off') if x == 10: axes = plt.subplot2grid((1,49),(0,48), colspan=1) plt.colorbar(cax=axes) plt.savefig(_DATADIR + 'position_plots_' + _HANDLE + '.svg', bbox_inches='tight') return
basin_data = json.load(js) with open('../data/ac_data.json') as js: ac_data = json.load(js) threshold = float(sys.argv[1]) c_vals = [] chan_density = [] for toku_id, values in ac_data.items(): if values[0] > threshold and toku_id in basin_data: basin = basin_data[toku_id] c_vals.append(values[2]) _, _, lengths = read_toku_data('../data/TokunagaData_{}.csv'.format(toku_id)) total_length = np.sum(lengths) area = basin[3] unchan_area = basin[0] chan_density.append(total_length / (area - unchan_area)) # plt.scatter() plt.hist2d(c_vals, np.log10(chan_density), bins=(50, 50), cmap=plt.cm.viridis, density=False) plt.show()
line = slope * df['Frac'] + intercept import sklearn import numpy as np from pandas import DataFrame import seaborn as sns from matplotlib import colors import numpy import math df.to_csv("jacob.csv") x = df['Frac'] y = df['Speed'].apply(numpy.log) plt.subplot(2, 1, 1) plt.scatter(df['Frac'], miles, s=3) plt.plot(df['Frac'], line, color='orange') plt.ylim(0, 100) plt.title("My Rides: Linear Fit") plt.xlabel("Gradient") plt.ylabel("Speed (Kilometers/hour)") plt.subplot(2, 1, 2) h = plt.hist2d(x, miles, bins=150, cmap=plt.cm.BuPu) plt.title("Heatmap of the Same Data") plt.xlim(-0.015, 0.015) plt.ylim(0, 75) plt.xlabel("Gradient") plt.ylabel("Speed (Kilometers)") plt.tight_layout() cbar = plt.colorbar() plt.show()
plt.ylim((0, 0.05)) plt.tight_layout() plt.savefig('%04d.%04d.figures/%04d.%04d.dipole_angle.png' % (i[1], i[2], i[1], i[2]), dpi=600, transparent=True) plt.close() # PLOT 2d histogram of collective variables; NOTE: HIST2D FUNCTION CALCULATES THE PROB DENSITY AS NORMALIZED BY THE TOTAL NUMBER OF DATA POINTS (INSTEAD OF NSTEPS) * DX * DY # atk angle vs atk dist counts, xedges, yedges, image = plt.hist2d( nucleophilic_water_data[first_index:last_index, 2], nucleophilic_water_data[first_index:last_index, 3], bins=[num_bins[0], num_bins[1]], range=[[attack_distance_minimum, attack_distance_maximum], [angle_minimum, angle_maximum]], cmap=prob_density_cmap, normed=True, vmin=0.001, vmax=0.020 ) # 2d histogram (aka heat map) of the geometric collective variables; probability density cb1 = plt.colorbar(extend='max') # extend='max' cb1.set_label('Probability Density', size=20) plt.xlabel(r'P$_{\gamma}$ - O$_{wat}$ Distance ($\AA$)', size=20) plt.ylabel( r'O$_{\beta,\gamma}$-P$_{\gamma}$-O$_{wat}$ Angle (Deg)', size=20) plt.grid(b=True, which='major', axis='both', color='#808080',
def main_plot_latent(name, args): parser = argparse.ArgumentParser() parser.add_argument('seqs') parser.add_argument('--save') args = parser.parse_args(args) seqs = loadSeqs(args.seqs, alpha=ALPHA)[0] # only plot first 10,000 seqs = seqs[:10000] vae = loadVAE(name) latent_dim = vae.latent_dim m, lv = vae.encode(seqs) st = np.exp(lv / 2) if args.save: np.save(args.save, np.dstack([m, lv])) # make 1d distribution plots fig = plt.figure(figsize=(12, 12)) nx = max(latent_dim // 2, 1) ny = (latent_dim - 1) // nx + 1 for z1 in range(latent_dim): fig.add_subplot(nx, ny, z1 + 1) h, b, _ = plt.hist(m[:, z1], bins=100, density=True) wm, ws = m[0][z1], st[0][z1] x = np.linspace(wm - 5 * ws, wm + 5 * ws, 200) y = norm.pdf(x, wm, ws) y = y * np.max(h) / np.max(y) plt.plot(x, y, 'r-') plt.xlim(-5, 5) plt.title('Z{}, <z{}>_std={:.2f}'.format(z1, z1, np.std(m[:, z1]))) plt.savefig('LatentTraining_1d_{}.png'.format(name)) plt.close() # special plot for l=1 case: vary the 1 dimension, make movie of output if vae.latent_dim == 1: z = np.linspace(-4, 4, vae.batch_size) psm = vae.decode_bernoulli(z) import matplotlib.animation as animation fig = plt.figure(figsize=(16, 4)) ax1 = plt.subplot(211) ax2 = plt.subplot(212) h, b, _ = ax1.hist(m[:, 0], bins=100, density=True) ax1.set_xlim(-4, 4) artists = [] for p, zi in zip(psm, z): zpt = ax1.plot([zi], [0], 'r.', ms=20)[0] im = ax2.imshow(p.T, cmap='gray_r', interpolation='nearest', animated=True) artists.append([im, zpt]) #print("".join(ALPHA[c] for c in np.argmax(p, axis=1))) ani = animation.ArtistAnimation(fig, artists, interval=40, blit=True, repeat_delay=1000) #ani.save('vary_z1_{}.mp4'.format(name)) ani.save('vary_z1_{}.gif'.format(name), dpi=80, writer='imagemagick') plt.close() return # make 2d distribution plots r = 4 s = np.linspace(-r, r, 50) X, Y = np.meshgrid(s, s) red = np.broadcast_to(np.array([1., 0, 0, 1]), (len(s), len(s), 4)).copy() fig = plt.figure(figsize=(12, 12)) counter = 0 for z1 in range(latent_dim): print('Var z{}: {}'.format(z1, np.var(m[:, z1]))) for z2 in range(z1 + 1, latent_dim): counter += 1 fig.add_subplot(latent_dim - 1, latent_dim - 1, counter) plt.hist2d(m[:, z2], m[:, z1], bins=np.linspace(-r, r, 50), cmap=DarkBlue, cmin=1) nn = (norm.pdf(X, m[0][z2], st[0][z2]) * norm.pdf(Y, m[0][z1], st[0][z1])) nn = nn / np.max(nn) / 1.5 red[:, :, 3] = nn plt.imshow(red, extent=(-r, r, -r, r), origin='lower', zorder=2) ##wildtype in red #plt.scatter(m[0][z1], m[0][z2],c="r", alpha=1) # make 1std oval for wt #wtv = Ellipse((m[0][z2], m[0][z1]), # width=st[0][z2], height=st[0][z1], # facecolor='none', edgecolor='red', lw=2) #plt.gca().add_patch(wtv) #wtv = Ellipse((m[0][z2], m[0][z1]), # width=2*st[0][z2], height=2*st[0][z1], # facecolor='none', edgecolor='red', lw=1) #plt.gca().add_patch(wtv) plt.xlim(-4, 4) plt.ylim(-4, 4) fs = 26 if latent_dim <= 7: fs *= 2 if z1 == 0: plt.xlabel('$z_{{{}}}$'.format(z2), fontsize=fs, labelpad=fs / 2) plt.gca().xaxis.set_label_position('top') if z2 == latent_dim - 1: plt.ylabel('$z_{{{}}}$'.format(z1), fontsize=fs) plt.gca().yaxis.set_label_position('right') plt.xticks([]) plt.yticks([]) counter += z1 + 1 plt.subplots_adjust(right=0.92, bottom=0.01, left=0.01, top=0.92) plt.savefig('LatentTraining_{}.png'.format(name)) plt.close()
def plot_s1_variables(dst): fig = plt.figure(figsize=(15, 15)) ax = fig.add_subplot(3, 2, 1) (_) = h1(dst.S1e, bins=100, range=(0, 40), histtype='stepfilled', color='steelblue', lbl='') plot_histo(PlotLabels('E (pes)', 'Entries', ''), ax) plt.legend(loc='upper right') ax = fig.add_subplot(3, 2, 2) (_) = h1(dst.S1w, bins=20, range=(0, 500), histtype='stepfilled', color='steelblue', lbl='') plot_histo(PlotLabels('Width (mus)', 'Entries', ''), ax) plt.legend(loc='upper right') ax = fig.add_subplot(3, 2, 3) (_) = h1(dst.S1h, bins=100, range=(0, 10), histtype='stepfilled', color='steelblue', lbl='') plot_histo(PlotLabels('height/s1energy', 'Entries', ''), ax) plt.legend(loc='upper right') ax = fig.add_subplot(3, 2, 4) (_) = h1(dst.S1h / dst.S1e, bins=100, range=(0, 0.6), histtype='stepfilled', color='steelblue', lbl='') plot_histo(PlotLabels('height/s1energy', 'Entries', ''), ax) plt.legend(loc='upper right') ax = fig.add_subplot(3, 2, 5) (_) = h1(dst.S1t / units.mus, bins=20, range=(0, 400), histtype='stepfilled', color='steelblue', lbl='') plot_histo(PlotLabels('S1 time mus', 'Entries', ''), ax) plt.legend(loc='upper right') ax = fig.add_subplot(3, 2, 6) plt.hist2d(dst.S1t / units.mus, dst.S1e, bins=10, range=[[0, 500], [0, 30]]) plt.colorbar() ax.set_xlabel(r'S1 time ($\mu$s) ', fontsize=11) #xlabel ax.set_ylabel('S1 height (pes)', fontsize=11) plt.grid(True)
def plot_2D(x, y, xbins, ybins, figsize=(14, 10)): fig = plt.figure(figsize=figsize) fig.add_subplot(2, 2, 1) nevt, *_ = plt.hist2d(x, y, (xbins, ybins)) plt.colorbar().set_label("Number of events")
Freezes_S_ALL[:, 2], color='steelblue', label="Social") plt.legend(bbox_to_anchor=(0.3, 1.6)) plt.show() #Plot distribution of Bouts // NS vs S fig = plt.figure(figsize=(48, 18), dpi=300) plt.suptitle("Distribution of Bouts" + '\n n=' + format(numFiles), fontweight="bold", fontsize=64, y=1) ax = plt.subplot(221) plt.hist2d(Bouts_NS_ALL[:, 1], Bouts_NS_ALL[:, 2], bins=10, cmap='Blues') plt.title('Bout Starts', fontweight="bold", fontsize=32, y=-0.25) plt.xticks(fontsize=32) plt.yticks(fontsize=32) plt.colorbar() ax = plt.subplot(222) plt.hist2d(Bouts_S_ALL[:, 1], Bouts_S_ALL[:, 2], bins=10, cmap='Blues') plt.title('Bout Starts', fontweight="bold", fontsize=32, y=-0.25) plt.xticks(fontsize=32) plt.yticks(fontsize=32) plt.colorbar() fig.tight_layout(pad=2) #Binned Freezes
def animate(i): hist = plt.hist2d(*x_y_split(frames[i])) time.sleep(0.2) return hist
def plotComplexityData(complexityData): """Plot all data associated with this complexityData - Probe: type and response complexity - Others: just type complexity Saves figure(s) to FIG_PATH/api/<name here>.pdf """ if isProbeData(complexityData.dataFileName): complexitiesToPlot = PROBE_COMPLEXITY_TYPES else: complexitiesToPlot = OTHER_COMPLEXITY_TYPES for complexityType in complexitiesToPlot: xAxLim = COMPLEXITY_TYPE_2_PLOT_MAX_VALUE[ complexityData.api][complexityType] yAxLim = 2 * xAxLim # roundUp(2.25 * xAxLim, 50) #if complexityData.api == 'yelp': # axlabel_fontsize = 18 # ticklabel_fontsize = 15 # inFigureText_fontsize = 15 # nXTicks = -1 # Default # nYTicks = -1 # Default #else: axlabel_fontsize = 20 ticklabel_fontsize = 18 inFigureText_fontsize = 14 nXTicks = 3 nYTicks = 3 # We use our GH data twice, so just generate the charts x2 ?? if complexityData.calcOrigin == 'Our': xlabels = ['Actual response size', 'Actual (response) cxty'] ylabels = ['Pred. response size', 'Pred. (query) cxty'] chartNicknames = ['RQ5', 'Standard'] else: xlabels = ['Actual response size'] ylabels = ['Pred. response size'] chartNicknames = ['RQ5'] if complexityData.calcOrigin == 'Our' and complexityData.api == 'yelp': if complexityType == 'resolve': # Only showing our results for Yelp, and they are tight xAxLim = 100 yAxLim = 2 * xAxLim else: pass #xlabel = '{} response {} complexity'.format(complexityData.calcOrigin, complexityType) #ylabel = '{} query {} complexity'.format(complexityData.calcOrigin, complexityType) # Data sub = pd.DataFrame() sub['actual'] = complexityData.df['response%sComplexity' % complexityType.capitalize()] sub['pred'] = complexityData.df['{}Complexity'.format(complexityType)] # How many of the responses do we show within axLim? nResponsesShown = len(sub[sub['actual'] <= xAxLim]) percResponsesShown = int(100 * nResponsesShown / len(sub)) # For those responses, how many queries are visible? nPredTooBig = len(sub[(sub['actual'] <= xAxLim) & (sub['pred'] > yAxLim)]) percPredTooBig = int(100 * nPredTooBig / nResponsesShown) nPredFit = len(sub) - nPredTooBig percPredFit = 100 - percPredTooBig print("{} - {} - {}: {}% responses fit, {}% of predictions fit".format( complexityData.api, complexityData.calcOrigin, complexityType, percResponsesShown, percPredFit)) for chartNickname, xlabel, ylabel in zip(chartNicknames, xlabels, ylabels): # New figure plt.figure() # Draw diagonal line to demarcate over/under-estimation plt.plot( [0, xAxLim], [0, xAxLim], color='r', linestyle='-', # style='o', linewidth=0.5, label='Upper bound', zorder=-1) # Heatmap norm = matplotlib.colors.Normalize(1e-324, 1) # colors = [[norm(1e-324), "gold"], # [norm(0.25), "orange"], # [norm(0.5), "red"], # [norm(0.75), "darkred"], # [norm(1.0), "black"]] colors = [[norm(1e-324), "lightskyblue"], [norm(0.25), "cornflowerblue"], [norm(0.5), "blue"], [norm(0.75), "darkblue"], [norm(1.0), "black"]] cmap = matplotlib.colors.LinearSegmentedColormap.from_list( "", colors) plt.xlabel(xlabel) plt.ylabel(ylabel) plt.hist2d( sub['actual'], sub['pred'], bins=(100, 100), range=[[1, xAxLim], [1, yAxLim]], norm=matplotlib.colors.LogNorm(), cmap=cmap # plt.get_cmap('viridis') ) # Bound the plot for consistency xTickStep = 50 ax = plt.gca() # Tick marks xticks = np.linspace(0, roundDown(xAxLim, 50), 3) #if nXTicks > 0: # xticks = np.linspace(0, roundDown(xAxLim, 50), nXTicks) #else: # xticks = [0] # xticks.extend(range(xTickStep, xAxLim, xTickStep)) # xticks.append(xAxLim) ax.set_xticks(xticks) if yAxLim > xAxLim: yMult = 100 else: yMult = 50 yticks = np.linspace(0, roundDown(yAxLim, yMult), 3) #if nYTicks > 0: # yticks = np.linspace(0, roundDown(yAxLim, 50), nYTicks) #else: # yticks = [0] # yticks.extend(range(yTickStep, yAxLim, yTickStep)) # yticks.append(roundUp(yAxLim, yTickStep)) ax.set_yticks(yticks) ax.tick_params(axis="x", labelsize=ticklabel_fontsize) ax.tick_params(axis="y", labelsize=ticklabel_fontsize) # Axis labels and legend ax.set_xlabel(xlabel, fontsize=axlabel_fontsize) ax.set_ylabel(ylabel, fontsize=axlabel_fontsize) plt.colorbar() if complexityData.api == 'github' and ( complexityData.calcOrigin == 'libB' or complexityData.calcOrigin == 'libC'): textXPos = 0.05 * xAxLim textYPos = 0.9 * yAxLim else: textXPos = 0.25 * xAxLim textYPos = 0.05 * yAxLim if percResponsesShown < 100 or percPredFit < 100: ax.text(textXPos, textYPos, '{}% resp., {}% of preds.'.format( percResponsesShown, percPredFit), fontsize=inFigureText_fontsize) else: # Show it anyway for consistency ax.text(textXPos, textYPos, '{}% resp., {}% of preds.'.format( percResponsesShown, percPredFit), fontsize=inFigureText_fontsize) ax.set(xlim=(0, xAxLim), ylim=(0, yAxLim)) plt.tight_layout(pad=1) # plt.show() # Save figure figPath = os.path.join( FIG_PATH, complexityData.api, '{}-{}-{}-{}-heatmap.{}'.format(complexityData.api, complexityData.calcOrigin, complexityType, chartNickname, FIG_FILE_FORMAT)) plt.savefig(figPath, bbox_inches='tight', pad_inches=0) print("Plot saved to {}".format(figPath))
def init(): hist = plt.hist2d(*x_y_split(p)) return hist
tbl = Table(res) dustEBV = dust(tbl['l'] * units.deg, tbl['b'] * units.deg) jkc = (tbl['j_m'] - dustCoeff['J'] * dustEBV) - (tbl['k_m'] - dustCoeff['K'] * dustEBV) w12c = (tbl['w1_mag'] - dustCoeff['W1'] * dustEBV) - ( tbl['w2_mag'] - dustCoeff['W2'] * dustEBV) good = ~np.isnan( tbl['w1_mag'] - tbl['w2_mag'] ) #& (np.abs(tbl['w1_mag'] - tbl['w2_mag']) < 0.5) & (tbl['j_m'] - tbl['k_m'] < 2.) plt.hist2d(tbl['w1_mag'][good] - tbl['w2_mag'][good], tbl['j_m'][good] - tbl['k_m'][good], bins=2500, norm=mpl.colors.LogNorm(), cmap='Greys', rasterized=True) plt.xlim(-0.5, 0.5) plt.ylim(0.5, 2) plt.ylabel('tmass j-k') plt.xlabel('wise 1-2') plt.savefig('tmasswiseCC_tgas.png') plt.clf() plt.hist2d(w12c[good], jkc[good], bins=2500, norm=mpl.colors.LogNorm(), cmap='Greys',
x = [i.x for i in p] y = [i.y for i in p] return [x,y] def animate(i): hist = plt.hist2d(*x_y_split(frames[i])) time.sleep(0.2) return hist N_particles = 1000 myrobot = robot() p = [robot() for i in range(N_particles)] frames = [p] fig = plt.figure() hist = plt.hist2d(*x_y_split(p)) def init(): hist = plt.hist2d(*x_y_split(p)) return hist if __name__ == "__main__": for i in range(10): myrobot = myrobot.move(0.1, 5.0) Z = myrobot.sense() p = [bot.set_noise(0.05, 0.05, 5.0).move(0.1,5) for bot in p] weights = [part.measurement_prob(Z) for part in p] p_re,beta = [],0 for i in range(N_particles): beta += 2*max(weights)*random.random() index = 0
def plotTrajectoriesFile(filename, mode='2d', tracerfile=None, tracerfield='P', tracerlon='x', tracerlat='y', recordedvar=None, bins=20, show_plt=True): """Quick and simple plotting of Parcels trajectories :param filename: Name of Parcels-generated NetCDF file with particle positions :param mode: Type of plot to show. Supported are '2d', '3d', 'hist2d', 'movie2d' and 'movie2d_notebook'. The latter two give animations, with 'movie2d_notebook' specifically designed for jupyter notebooks :param tracerfile: Name of NetCDF file to show as background :param tracerfield: Name of variable to show as background :param tracerlon: Name of longitude dimension of variable to show as background :param tracerlat: Name of latitude dimension of variable to show as background :param recordedvar: Name of variable used to color particles in scatter-plot. Only works in 'movie2d' or 'movie2d_notebook' mode. :param bins: Number of bins to use in `hist2d` mode. See also https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist2d.html :param show_plt: Boolean whether plot should directly be show (for py.test) """ if plt is None: print("Visualisation is not possible. Matplotlib not found.") return pfile = Dataset(filename, 'r') lon = np.ma.filled(pfile.variables['lon'], np.nan) lat = np.ma.filled(pfile.variables['lat'], np.nan) time = np.ma.filled(pfile.variables['time'], np.nan) z = np.ma.filled(pfile.variables['z'], np.nan) if (recordedvar is not None): record = pfile.variables[recordedvar] if tracerfile is not None: tfile = Dataset(tracerfile, 'r') X = tfile.variables[tracerlon] Y = tfile.variables[tracerlat] P = tfile.variables[tracerfield] plt.contourf(np.squeeze(X), np.squeeze(Y), np.squeeze(P)) if mode == '3d': from mpl_toolkits.mplot3d import Axes3D # noqa fig = plt.figure(1) ax = fig.gca(projection='3d') for p in range(len(lon)): ax.plot(lon[p, :], lat[p, :], z[p, :], '.-') ax.set_xlabel('Longitude') ax.set_ylabel('Latitude') ax.set_zlabel('Depth') elif mode == '2d': plt.plot(np.transpose(lon), np.transpose(lat), '.-') plt.xlabel('Longitude') plt.ylabel('Latitude') elif mode == 'hist2d': plt.hist2d(lon[~np.isnan(lon)], lat[~np.isnan(lat)], bins=bins) plt.colorbar() plt.xlabel('Longitude') plt.ylabel('Latitude') elif mode in ('movie2d', 'movie2d_notebook'): fig = plt.figure() ax = plt.axes(xlim=(np.nanmin(lon), np.nanmax(lon)), ylim=(np.nanmin(lat), np.nanmax(lat))) plottimes = np.unique(time) plottimes = plottimes[~np.isnan(plottimes)] b = time == plottimes[0] scat = ax.scatter(lon[b], lat[b], s=60, cmap=plt.get_cmap('autumn')) # cmaps not working? ttl = ax.set_title('Particle at time ' + str(plottimes[0])) frames = np.arange(1, len(plottimes)) def animate(t): b = time == plottimes[t] scat.set_offsets(np.matrix((lon[b], lat[b])).transpose()) ttl.set_text('Particle at time ' + str(plottimes[t])) if recordedvar is not None: scat.set_array(record[b]) return scat, rc('animation', html='html5') anim = animation.FuncAnimation(fig, animate, frames=frames, interval=100, blit=False) else: raise RuntimeError('mode %s not known' % mode) if mode == 'movie2d_notebook': plt.close() return anim else: if show_plt: plt.show() return plt
def find_object( ra, dec, pmra, pmdec, epmra, epmdec, corrpm, chi2, nu, g_mag, br_mag, br_excess, sd_model="plummer", min_method="dif", prob_method="complete", prob_limit=0.9, use_hrd=True, nsig=3, bw_hrd=None, r_max=None, err_lim=None, err_handle="relative", object_type="gc", return_center=False, check_fit=False, cleaning="v21", conv=True, ): """ Finds galactic object by employing the analysis proposed in Vitral 2021. Parameters ---------- ra : array_like Gaia designation: ra. dec : array_like Gaia designation: dec. pmra : array_like Gaia designation: pmra. pmdec : array_like Gaia designation: pmdec. epmra : array_like Gaia designation: pmra_error. epmdec : array_like Gaia designation: pmdec_error. corrpm : array_like Gaia designation: pmra_pmdec_corr. chi2 : array_like Gaia designation: astrometric_chi2_al. nu : array_like Gaia designation: astrometric_n_good_obs_al. g_mag : array_like Gaia designation: phot_g_mean_mag. br_mag : array_like Gaia designation: bp_rp. br_excess : array_like Gaia designation: phot_bp_rp_excess_factor. sd_model : string, optional Surface density model to be considered. Available options are: - 'sersic' - 'kazantzidis' - 'plummer' - 'test', for testing which among Plummer and Sersic should be used, based on AICc. The default is 'plummer'. min_method : string, optional Minimization method to be used by the pm maximum likelihood fit. The default is 'dif'. prob_method : string, optional Method of probability cut chosen. Available options are: - complete: Considers proper motions and projected radii. - pm: Considers only proper motions. - position: Considers only projected radii. The default is 'complete'. prob_limit : float, optional Probability threshold (between 0 and 1) considered to cut the data. The default is 0.9. use_hrd : boolean, optional True if the user wants to select stars inside a n-sigma contour region inside the color-magnitude diagram. The default is True. nsig : float, optional Number of sigma up to when consider stars inside the KDE color-magnitude diagram. The default is 3.3. bw_hrd : float or string, optional Bandwidth method of KDE's scipy method. The default is None. r_max : float, optional Maximum projected radius up to where consider the data. The default is None err_lim : float, optional Error threshold for proper motions. The default is None. err_handle : string, optional Way in which the error limit is handled: - 'relative', if the err_lim argument is the number of times the initial guess on the galactic object proper motion dispersion. - 'absolute', if the err_lim argument is the absolute error limit in mas/yr. The default is 'relative'. object_type : string, optional Galactic object type, to be considered when defining the default value of err_lim: - 'gc', for globular cluster. - 'dsph', for dwarf spheroidal galaxy. The default is 'gc'. return_center : boolean, optional True if the user wants an estimate of the center as output. The default is False. check_fit : boolean, optional True is the user wants to plot validity checks throughout the fitting procedure. The default is False. cleaning : string, optional Cleaning procedure to be adopted: - 'v21', to use the same cleaning from Vitral 2021 - 'vm21', to use the same cleaning from Vitral & Mamon 21 The default is 'v21'. conv : boolean, optional True, if the user wants to convolve the galactic object PDF with Gaussian errors. The defualt is True. Raises ------ ValueError Surface density model is not one of the following: - 'sersic' - 'kazantzidis' - 'plummer' - 'test' Probability method is not one of the following: - 'complete' - 'pm' - 'position' Probability threshold is not inbetween 0 and 1. nsig is not positive. err_handle is not one of the following: - 'relative' - 'absolute' object_type is not one of the following: - 'gc' - 'dsph' cleaning is not one of the following: - 'v21' - 'vm21' Returns ------- idx_final : array_like Array containing the indexes of the stars in original file who have passed the probability cuts. results_sd : array_like Results of the surface density fit. var_sd : array_like Uncertainties of the surface density fit results_pm : array_like Results of the proper motion fit. var_pm : array_like Uncertainties of the proper motion fit. """ if sd_model not in ["sersic", "plummer", "kazantzidis", "test"]: raise ValueError("Does not recognize surface density model.") if prob_method not in ["complete", "pm", "position"]: raise ValueError("Does not recognize probability model.") if prob_limit > 1 or prob_limit < 0: raise ValueError("Probability threshold must be inbetween 0 and 1.") if nsig <= 0: raise ValueError("nsig must be positive.") if err_handle not in ["relative", "absolute"]: raise ValueError("Does not recognize method to handle errors.") if object_type not in ["gc", "dsph"]: raise ValueError("Does not recognize object type.") if cleaning not in ["v21", "vm21"]: raise ValueError("Does not recognize cleaning procedure.") c, unc = position.find_center(ra, dec) ri = angle.sky_distance_deg(ra, dec, c[0], c[1]) if r_max is None: ri_sd = ri else: ri_sd = ri[np.where(ri <= r_max)] if sd_model == "sersic": results_sd, var_sd = position.maximum_likelihood(x=np.asarray([ri_sd]), model="sersic") r_cut = 10 * 10**results_sd[1] nsys = len(ri_sd) / (1 + 10**results_sd[2]) nilop = len(ri_sd) - nsys sd = (position.sd_sersic(results_sd[0], sorted(ri_sd) / 10**results_sd[1]) * nsys / (np.pi * (10**results_sd[1])**2)) prob_sd = position.prob(ri, results_sd, model="sersic") elif sd_model == "plummer": results_sd, var_sd = position.maximum_likelihood(x=np.asarray([ri_sd]), model="plummer") r_cut = 10 * 10**results_sd[0] nsys = len(ri_sd) / (1 + 10**results_sd[1]) nilop = len(ri_sd) - nsys sd = (position.sd_plummer(sorted(ri_sd) / 10**results_sd[0]) * nsys / (np.pi * (10**results_sd[0])**2)) prob_sd = position.prob(ri, results_sd, model="plummer") elif sd_model == "kazantzidis": results_sd, var_sd = position.maximum_likelihood(x=np.asarray([ri_sd]), model="kazantzidis") r_cut = 10 * 10**results_sd[0] nsys = len(ri_sd) / (1 + 10**results_sd[1]) nilop = len(ri_sd) - nsys sd = (position.sd_kazantzidis(sorted(ri_sd) / 10**results_sd[0]) * nsys / (np.pi * (10**results_sd[0])**2)) prob_sd = position.prob(ri, results_sd, model="kazantzidis") elif sd_model == "test": results_sd_s, var_sd_s = position.maximum_likelihood(x=np.asarray( [ri_sd]), model="sersic") results_sd_p, var_sd_p = position.maximum_likelihood(x=np.asarray( [ri_sd]), model="plummer") results_sd_k, var_sd_k = position.maximum_likelihood( x=np.asarray([ri_sd]), model="kazantzidis") aicc_s = get_aicc(position.likelihood_sersic(results_sd_s, ri_sd), 3, len(ri_sd)) aicc_p = get_aicc(position.likelihood_plummer(results_sd_p, ri_sd), 2, len(ri_sd)) aicc_k = get_aicc(position.likelihood_kazantzidis(results_sd_k, ri_sd), 2, len(ri_sd)) delta_aicc = aicc_p - aicc_s if delta_aicc < 2: delta_aicc = aicc_p - aicc_k if delta_aicc < 2: sd_model = "plummer" results_sd, var_sd = results_sd_p, var_sd_p r_cut = 10 * 10**results_sd[0] nsys = len(ri_sd) / (1 + 10**results_sd[1]) nilop = len(ri_sd) - nsys sd = (position.sd_plummer(sorted(ri_sd) / 10**results_sd[0]) * nsys / (np.pi * (10**results_sd[0])**2)) prob_sd = position.prob(ri, results_sd, model="plummer") else: sd_model = "kazantzidis" results_sd, var_sd = results_sd_k, var_sd_k r_cut = 10 * 10**results_sd[0] nsys = len(ri_sd) / (1 + 10**results_sd[1]) nilop = len(ri_sd) - nsys sd = (position.sd_kazantzidis( sorted(ri_sd) / 10**results_sd[0]) * nsys / (np.pi * (10**results_sd[0])**2)) prob_sd = position.prob(ri, results_sd, model="kazantzidis") else: delta_aicc = aicc_k - aicc_s if delta_aicc < 2: sd_model = "kazantzidis" results_sd, var_sd = results_sd_k, var_sd_k r_cut = 10 * 10**results_sd[0] nsys = len(ri_sd) / (1 + 10**results_sd[1]) nilop = len(ri_sd) - nsys sd = (position.sd_kazantzidis( sorted(ri_sd) / 10**results_sd[0]) * nsys / (np.pi * (10**results_sd[0])**2)) prob_sd = position.prob(ri, results_sd, model="kazantzidis") else: sd_model = "sersic" results_sd, var_sd = results_sd_s, var_sd_s r_cut = 10 * 10**results_sd[1] nsys = len(ri_sd) / (1 + 10**results_sd[2]) nilop = len(ri_sd) - nsys sd = (position.sd_sersic(results_sd[0], sorted(ri_sd) / 10**results_sd[1]) * nsys / (np.pi * (10**results_sd[1])**2)) prob_sd = position.prob(ri, results_sd, model="sersic") if check_fit is True: surf_dens = position.surface_density(x=ri_sd) fs = nilop / (np.pi * np.amax(ri_sd)**2) plt.title(sd_model) plt.bar( surf_dens[0], surf_dens[1], surf_dens[3], ec="k", color=(102 / 255, 178 / 255, 1), alpha=0.3, ) plt.xscale("log") plt.yscale("log") plt.ylabel("Surface density") plt.xlabel("Projected radii") plt.errorbar( surf_dens[0], surf_dens[1], color="black", yerr=surf_dens[2], capsize=3, ls="none", barsabove=True, zorder=11, ) plt.plot(sorted(ri_sd), sd + fs, lw=3, color="red") plt.show() if r_max is None: r_max = r_cut idx = clean_gaia( pmra, pmdec, epmra, epmdec, corrpm, chi2, nu, g_mag, br_mag, br_excess, ra, dec, c=c, r_cut=r_max, err_lim=err_lim, err_handle=err_handle, object_type=object_type, cleaning=cleaning, ) prob_sd = prob_sd[idx] pmra = pmra[idx] pmdec = pmdec[idx] epmra = epmra[idx] epmdec = epmdec[idx] corrpm = corrpm[idx] results_pm, var_pm = pm.maximum_likelihood(pmra, pmdec, eX=epmra, eY=epmdec, eXY=corrpm, min_method=min_method, conv=conv) prob_pm = pm.prob(pmra, pmdec, epmra, epmdec, corrpm, results_pm, conv=conv) if check_fit is True: ellipse_rot = angle.get_ellipse(results_pm[5], results_pm[6], results_pm[7], 200) circle = angle.get_ellipse(results_pm[2], results_pm[2], 0, 200) ranges = [ [pm.quantile(pmra, 0.025)[0], pm.quantile(pmra, 0.975)[0]], [pm.quantile(pmdec, 0.025)[0], pm.quantile(pmdec, 0.975)[0]], ] plt.xlabel("pmra") plt.ylabel("pmdec") plt.xlim(ranges[0][0], ranges[0][1]) plt.ylim(ranges[1][0], ranges[1][1]) plt.hist2d( pmra, pmdec, range=ranges, bins=(position.good_bin(pmra), position.good_bin(pmdec)), cmap="hot", norm=mc.LogNorm(), ) plt.plot( results_pm[3] + ellipse_rot[0, :], results_pm[4] + ellipse_rot[1, :], "green", lw=3, ) plt.plot(results_pm[0] + circle[0, :], results_pm[1] + circle[1, :], "blue", lw=3) plt.show() x, y = angle.rotate_axis(pmra, pmdec, results_pm[7], results_pm[3], results_pm[4]) mu_x, mu_y = angle.rotate_axis(results_pm[0], results_pm[1], results_pm[7], results_pm[3], results_pm[4]) ranges = [ [pm.quantile(x, 0.025)[0], pm.quantile(x, 0.975)[0]], [pm.quantile(y, 0.025)[0], pm.quantile(y, 0.975)[0]], ] Ux = np.linspace(ranges[0][0], ranges[0][1], 200) Uy = np.linspace(ranges[1][0], ranges[1][1], 200) projx = pm.proj_global_pdf(Ux, mu_x, results_pm[2], results_pm[5], results_pm[8], results_pm[9]) projy = pm.proj_global_pdf(Uy, mu_y, results_pm[2], results_pm[6], results_pm[8], results_pm[9]) plt.title("PMs projected on semi-major axis") plt.hist( x, 100, range=ranges[0], alpha=1, density=True, histtype="stepfilled", color=(102 / 255, 178 / 255, 1), ) plt.plot(Ux, projx, color="darkgreen", lw=3) plt.xlim(ranges[0][0], ranges[0][1]) plt.show() plt.title("PMs projected on semi-minor axis") plt.hist( y, 100, range=ranges[1], alpha=1, density=True, histtype="stepfilled", color=(102 / 255, 178 / 255, 1), ) plt.plot(Uy, projy, color="darkgreen", lw=3) plt.xlim(ranges[1][0], ranges[1][1]) plt.show() if prob_method == "complete": probs = prob_pm * prob_sd elif prob_method == "pm": probs = prob_pm elif prob_method == "position": probs = prob_sd idx_p = np.where(probs > prob_limit) if use_hrd is True: probs = probs[idx_p] fg_mag = g_mag[idx][idx_p] fbr_mag = br_mag[idx][idx_p] if bw_hrd is None: bw_hrd = hrd.bw_silver(fbr_mag, fg_mag) * 0.5 z = hrd.kde(fbr_mag, fg_mag, ww=probs, bw=bw_hrd) max_z = np.amax(z) levels = max_z * np.exp(-0.5 * np.array([nsig])**2) qx_i, qx_f = np.nanmin(fbr_mag), np.nanmax(fbr_mag) qy_i, qy_f = np.nanmin(fg_mag), np.nanmax(fg_mag) ranges = [[qx_i, qx_f], [qy_i, qy_f]] if check_fit is True: fig, ax = plt.subplots(figsize=(7, 6)) plt.title(r"Isochrone", fontsize=16) plt.xlabel(r"color", fontsize=15) plt.ylabel(r"main magnitude", fontsize=15) cax = ax.imshow( z, origin="lower", aspect="auto", extent=[ ranges[0][0], ranges[0][1], ranges[1][0], ranges[1][1] ], cmap="hot_r", norm=mc.LogNorm(vmin=0.001), ) cbar = fig.colorbar(cax) cbar.ax.set_title("KDE's PDF", fontsize=15) contours = plt.contour( z, origin="lower", levels=levels, extent=[ ranges[0][0], ranges[0][1], ranges[1][0], ranges[1][1] ], ) plt.gca().invert_yaxis() plt.show() else: contours = plt.contour( z, origin="lower", levels=levels, extent=[ ranges[0][0], ranges[0][1], ranges[1][0], ranges[1][1] ], ) plt.clf() idx_final = hrd.inside_contour(br_mag, g_mag, contours, idx[idx_p]) else: idx_final = idx_p if return_center is False: return idx_final, results_sd, var_sd, results_pm, var_pm else: return idx_final, results_sd, var_sd, results_pm, var_pm, c
def hist2D(x1t, x2t): counts, xbins, ybinx, image = plt.hist2d(x1t, x2t, bins=15) plt.axes().set_aspect('equal', 'datalim') plt.show()
def test_pcolorimage_extent(): im = plt.hist2d([1, 2, 3], [3, 5, 6], bins=[[0, 3, 7], [1, 2, 3]])[-1] assert im.get_extent() == (0, 7, 1, 3)
plt.xlabel(r'$log_{10}\, E/GeV$') plt.ylabel(r'$\cos{\theta}$') plt.title("Effective Area Distribution (in " + r'$m^2$' + ') Original') plt.colorbar(plotOutputs[3]) plt.figure() plotOutputs = plt.hist2d(datal0["logEnergy"], datal0["cosZenith"], norm = matplotlib.colors.LogNorm(), weights = weightsl0["2Variable"], bins = [binsE, binsZenith]) plt.xlabel(r'$log_{10}\, E/GeV$') plt.ylabel(r'$\cos{\theta}$') plt.title("Effective Area Distribution (in " + r'$m^2$' + ') l0') plt.colorbar(plotOutputs[3]) ''' plt.figure() plotOutputs = plt.hist2d(datal1["logEnergy"], datal1["cosZenith"], norm=matplotlib.colors.LogNorm(), weights=weightsl1["2Variable"], bins=[binsE, binsZenith]) plt.xlabel(r'$log_{10}\, E/GeV$') plt.ylabel(r'$\cos{\theta}$') plt.title("Effective Area Distribution (in " + r'$m^2$' + ') 5 Line Geometry') plt.colorbar(plotOutputs[3]) ''' plt.figure() plotOutputs = plt.hist2d(datal2["logEnergy"], datal2["cosZenith"], norm = matplotlib.colors.LogNorm(), weights = weightsl2["2Variable"], bins = [binsE, binsZenith]) plt.xlabel(r'$log_{10}\, E/GeV$') plt.ylabel(r'$\cos{\theta}$') plt.title("Effective Area Distribution (in " + r'$m^2$' + ') l2') plt.colorbar(plotOutputs[3]) ''' plt.figure()
def plot_disp(data, true_hadroness=False): """Plot the performance of reconstructed position Parameters: ----------- data: pandas DataFrame true_hadroness: boolean True: True gammas and proton events are plotted (they are separated using true hadroness). False: Gammas and protons are separated using reconstructed hadroness (hadro_rec) """ hadro = "reco_type" if true_hadroness: hadro = "mc_type" gammas = data[data[hadro] == 0] plt.subplot(221) reco_disp_norm = np.sqrt(gammas['reco_disp_dx']**2 + gammas['reco_disp_dy']**2) disp_res = ((gammas['disp_norm'] - reco_disp_norm) / gammas['disp_norm']) section = disp_res[abs(disp_res) < 0.5] mu, sigma = norm.fit(section) print("mu = {}\n sigma = {}".format(mu, sigma)) n, bins, patches = plt.hist( disp_res, bins=100, density=1, alpha=0.75, range=[-2, 1.5], ) y = norm.pdf(bins, mu, sigma) plt.plot(bins, y, 'r--', linewidth=2) plt.xlabel('$\\frac{disp\_norm_{gammas}-disp_{rec}}{disp\_norm_{gammas}}$', fontsize=15) plt.figtext(0.15, 0.7, 'Mean: ' + str(round(mu, 4)), fontsize=12) plt.figtext(0.15, 0.65, 'Std: ' + str(round(sigma, 4)), fontsize=12) plt.subplot(222) hD = plt.hist2d( gammas['disp_norm'], reco_disp_norm, bins=100, range=([0, 1.1], [0, 1.1]), ) plt.colorbar(hD[3]) plt.xlabel('$disp\_norm_{gammas}$', fontsize=15) plt.ylabel('$disp\_norm_{rec}$', fontsize=15) plt.plot(gammas['disp_norm'], gammas['disp_norm'], "-", color='red') plt.subplot(223) theta2 = (gammas['src_x'] - gammas['reco_src_x'])**2 + (gammas['src_y'] - gammas['src_y'])**2 plt.hist(theta2, bins=100, range=[0, 0.1], histtype=u'step') plt.xlabel(r'$\theta^{2}(º)$', fontsize=15) plt.ylabel(r'# of events', fontsize=15)
# -*- coding: utf-8 -*- """ Created on Wed Feb 14 15:21:55 2018 @author: cbjorkma """ #TIDVG5 distribution import os import numpy as np import matplotlib.pyplot as plt path = '//rpclustersrv1/cbjorkma/Dump studies' os.chdir(path) file1 = 'BEAM-72b-Xa.dat' file2 = 'BEAM-72b-Y.dat' f1 = np.loadtxt(file1) #f2 = np.loadtxt(file2) fig = plt.figure() plt.hist2d(f1[0:, 0], f1[0:, 1]) plt.show()
tica_data_all = np.load('../../ticas_n_8.npy') traj_log_fn = '../../trajectories.log' with open(traj_log_fn, 'r') as logfn: trajlog = logfn.readlines() for p_id in range(6383, 6391): msm_fn = 'MSMs-%d-macro40/MSMs-%d-macro40.pkl' % (p_id, p_id) assignment_fn = 'Assignments-%d.fixed.Map40.npy' % p_id subset_index = [i for i, j in enumerate(trajlog) if str(p_id) in j] tica_data_subset = tica_data_all[subset_index] tica_data = np.concatenate(tica_data_subset) plt.figure() plt.hist2d(tica_data[:, 0], tica_data[:, 1], bins=500, norm=LogNorm()) plt.xlabel("First tIC") plt.ylabel("Second tIC") plt.title("tICA Heatmap(log color scale) p%d" % p_id) #plt.savefig("tica_all.eps") #plt.show() msm = joblib.load(msm_fn) a = np.load(assignment_fn) selected_pairs_by_state = msm.draw_samples(a, 20) color = iter(cm.rainbow(np.linspace(0, 1, 40))) for state_id in range(selected_pairs_by_state.shape[0]): c = next(color) for i, (traj_id,
def HoughTransform_phi(self, numpoints, binx, biny, plotName=""): ht_phi = [] ht_rho = [] for i in range(0, len(self._Xp)): phis, rhos = self.rho_phi(self._Rsquared[i], self._Xp[i], self._Yp[i], numpoints) ht_phi.extend(phis) ht_rho.extend(rhos) # myrange=[[0, 2*np.pi], [min(ht_rho), 0.0008]] myrange = [[0, 2 * np.pi], [min(ht_rho), max(ht_rho)]] print("*** myrange: ", myrange) H, xedges, yedges = np.histogram2d(ht_phi, ht_rho, bins=(binx, biny), range=myrange) print("xedge: ", xedges.shape) print("yedge: ", yedges.shape) print("min rho: ", min(ht_rho)) print("max rho: ", max(ht_rho)) bx = (myrange[0][1] - myrange[0][0]) / binx by = (myrange[1][1] - myrange[1][0]) / biny trackpos, labels = self.cluster_test(H) unique_labels = set(labels) max_x, max_y = self.getCoords(trackpos, xedges, yedges, bx, by) fig_HT_phi = plt.figure() h = plt.hist2d(ht_phi, ht_rho, bins=(binx, biny), cmap=plt.cm.jet, range=myrange) plt.scatter(max_x, max_y, marker="o", alpha=0.5, facecolors='black', edgecolors='black', label="# hits > 112") plt.scatter(max_x, max_y, marker='x', c=[ matplotlib.cm.nipy_spectral( (float(i) + 0.5) / len(unique_labels)) for i in labels ], label="Cluster") plt.legend() # for i in range(0, len(labels)): # print("label: ", labels[i], "color: ", (float(labels[i])+1)/len(unique_labels), ", x: ", max_x[i], ", y: ", max_y[i]) plt.colorbar(h[3]) plt.xlabel(r'$\phi$ [rad]', fontsize=FONTSIZE) plt.ylabel(r'$\rho$ [1/mm]', fontsize=FONTSIZE) plt.tight_layout() if plotName: plt.savefig(plotName) else: plt.show() return H, xedges, yedges
# left plot: scatterplot of discrete data with jitter and transparency plt.subplot(1, 2, 1) sb.regplot(data=df, x='disc_var1', y='disc_var2', fit_reg=False, x_jitter=0.2, y_jitter=0.2, scatter_kws={'alpha': 1 / 3}) # right plot: heat map with bin edges between values plt.subplot(1, 2, 2) bins_x = np.arange(0.5, 10.5 + 1, 1) bins_y = np.arange(-0.5, 10.5 + 1, 1) plt.hist2d(data=df, x='disc_var1', y='disc_var2', bins=[bins_x, bins_y]) plt.colorbar() bins_x = np.arange(0.5, 10.5 + 1, 1) bins_y = np.arange(-0.5, 10.5 + 1, 1) plt.hist2d(data=df, x='disc_var1', y='disc_var2', bins=[bins_x, bins_y], cmap='viridis_r', cmin=0.5) plt.colorbar() # hist2d returns a number of different variables, including an array of counts bins_x = np.arange(0.5, 10.5 + 1, 1) bins_y = np.arange(-0.5, 10.5 + 1, 1)
16.2, 39.4, 30. , 18. , 17.5, 28.8, 22. , 34.2, 30.5, 16. , 38. , 41.5, 27.9, 22. , 29.8, 17.7, 15. , 14. , 15.5, 17.5, 12. , 29. , 15.5, 35.7, 26. , 30. , 33.8, 18. , 13. , 20. , 32.4, 16. , 27.5, 23. , 14. , 17. , 16. , 23. , 24. , 27. , 15. , 27. , 28. , 14. , 33.5, 39. , 24. , 26.5, 19.4, 15. , 25.5, 14. , 27.4, 13. , 19. , 17. , 28. , 22. , 30. , 18. , 14. , 22. , 23.8, 24. , 26. , 26. , 30. , 29. , 14. , 25.4, 19. , 12. , 20. , 27. , 22.3, 10. , 19.2, 26. , 16. , 37.3, 26. , 20.2, 13. , 21. , 25. , 20.5, 37.7, 36. , 20. , 37. , 18. , 27. , 29.5, 17.5, 25.1]) VISUALIZING BIVARIATE DISTRIBUTIONS # Generate a 2-D histogram with region covered using range(a set of 2 tuples) plt.hist2d(hp,mpg, bins=(20,20), range=((60,235),(8,48))) # Add a color bar to the histogram plt.colorbar() # Add labels, title, and display the plot plt.xlabel('Horse power [hp]') plt.ylabel('Miles per gallon [mpg]') plt.title('hist2d() plot') plt.show() # Generate a 2d histogram with hexagonal bins and region covered using extent(a set of 1 tuple) plt.hexbin(hp, mpg, gridsize=(15,12), extent=((40,235,8,48)))
def plot_fn(self): plot_path = os.path.join(self.log_path, "samples/") if not os.path.exists(plot_path): os.mkdir(plot_path) print('Sampling...') if self.args.dataset == 'face_einstein': bounds = [[0, 1], [0, 1]] else: bounds = [[-4, 4], [-4, 4]] # Plot samples with torch.no_grad(): samples = self.model.sample(self.args.num_samples) samples = samples.cpu().numpy() plt.figure(figsize=(self.args.pixels / self.args.dpi, self.args.pixels / self.args.dpi), dpi=self.args.dpi) plt.hist2d(samples[..., 0], samples[..., 1], bins=256, range=bounds) plt.xlim(bounds[0]) plt.ylim(bounds[1]) plt.axis('off') plt.savefig(os.path.join(plot_path, 'flow_samples.png'), bbox_inches='tight', pad_inches=0) # Plot density xv, yv = torch.meshgrid([ torch.linspace(bounds[0][0], bounds[0][1], self.args.grid_size), torch.linspace(bounds[1][0], bounds[1][1], self.args.grid_size) ]) x = torch.cat([xv.reshape(-1, 1), yv.reshape(-1, 1)], dim=-1).to(self.args.device) with torch.no_grad(): logprobs = self.model.log_prob(x) plt.figure(figsize=(self.args.pixels / self.args.dpi, self.args.pixels / self.args.dpi), dpi=self.args.dpi) plt.pcolormesh(xv, yv, logprobs.exp().reshape(xv.shape)) plt.xlim(bounds[0]) plt.ylim(bounds[1]) plt.axis('off') print('Range:', logprobs.exp().min().item(), logprobs.exp().max().item()) print('Limits:', 0.0, self.args.clim) plt.clim(0, self.args.clim) plt.savefig(os.path.join(plot_path, 'flow_density.png'), bbox_inches='tight', pad_inches=0) # plot true data test_data = self.eval_loader.dataset.data.numpy() if self.args.dataset == 'face_einstein': bounds = [[0, 1], [0, 1]] else: bounds = [[-4, 4], [-4, 4]] plt.figure(figsize=(self.args.pixels / self.args.dpi, self.args.pixels / self.args.dpi), dpi=self.args.dpi) plt.hist2d(test_data[..., 0], test_data[..., 1], bins=256, range=bounds) plt.xlim(bounds[0]) plt.ylim(bounds[1]) plt.axis('off') plt.savefig(os.path.join(plot_path, f'{self.args.dataset}.png'), bbox_inches='tight', pad_inches=0)
# Convert chirp mass and mass ratio to mass1 and mass2 m1 = conversions.mass1_from_mchirp_q(mc_samples['mc'], q_samples['q']) m2 = conversions.mass2_from_mchirp_q(mc_samples['mc'], q_samples['q']) # Check the 1D marginalization of mchirp and q is consistent with the # expected analytical formula n_bins = 200 xq = np.linspace(minq, maxq, 100) yq = ((1 + xq) / (xq**3))**(2 / 5) xmc = np.linspace(minmc, maxmc, 100) ymc = xmc plt.figure(figsize=(10, 10)) # Plot histograms of samples in subplots plt.subplot(221) plt.hist2d(mc_samples['mc'], q_samples['q'], bins=n_bins, cmap='Blues') plt.xlabel('chirp mass') plt.ylabel('mass ratio') plt.colorbar(fraction=.05, pad=0.05, label='number of samples') plt.subplot(222) plt.hist2d(m1, m2, bins=n_bins, cmap='Blues') plt.xlabel('mass1') plt.ylabel('mass2') plt.colorbar(fraction=.05, pad=0.05, label='number of samples') plt.subplot(223) plt.hist(mc_samples['mc'], density=True, bins=100, label='samples') plt.plot(xmc, ymc * mc_distribution.norm, label='$P(M_c)\propto M_c$') plt.xlabel('chirp mass') plt.ylabel('PDF')
def make_density_plots(path, zmin, zmax, ngc_data_ra=[], ngc_data_dec=[], sgc_data_ra=[], sgc_data_dec=[], ngc_rand_ra=[], sgc_rand_ra=[], params={}): # Import python modules import time import numpy as np import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages from matplotlib import rc # Start timing start_time = time.time() # Set up rc parameters rc(params) # Open PDF to store plots pp = PdfPages(path) # Plot NGC data plt.figure() plt.title(r'NGC Data Galaxy Density, z$\,\in {({:0.3f}, {:0.3f})}$'.format( zmin, zmax)) plt.xlabel(r'$\alpha\,\cos(\delta)$ [Degrees]') plt.ylabel(r'$\delta$ [Degrees]') plt.hist2d(ngc_data_ra * np.cos(np.radians(ngc_data_dec)), ngc_data_dec, bins=200) plt.colorbar() pp.savefig() # Plot SGC data plt.figure() plt.title(r'SGC Data Galaxy Density, z$\,\in {({:0.3f}, {:0.3f})}$'.format( zmin, zmax)) plt.xlabel(r'$\alpha\,\cos(\delta)$ [Degrees]') plt.ylabel(r'$\delta$ [Degrees]') plt.hist2d(sgc_data_ra * np.cos(np.radians(sgc_data_dec)), sgc_data_dec, bins=200) plt.colorbar() pp.savefig() # Plot NGC randoms plt.figure() plt.title( r'NGC Random Galaxy Density, z$\,\in {({:0.3f}, {:0.3f})}$'.format( zmin, zmax)) plt.xlabel(r'$\alpha\,\cos(\delta)$ [Degrees]') plt.ylabel(r'$\delta$ [Degrees]') plt.hist2d(ngc_rand_ra * np.cos(np.radians(ngc_rand_dec)), ngc_rand_dec, bins=200) plt.colorbar() pp.savefig() # Plot SGC randoms plt.figure() plt.title( r'SGC Randoms Galaxy Density, z$\,\in {({:0.3f}, {:0.3f})}$'.format( zmin, zmax)) plt.xlabel(r'$\alpha\,\cos(\delta)$ [Degrees]') plt.ylabel(r'$\delta$ [Degrees]') plt.hist2d(sgc_rand_ra * np.cos(np.radians(sgc_rand_dec)), sgc_rand_dec, bins=200) plt.colorbar() pp.savefig() # Close the PDF pp.close() # Save the end time end_time = time.time() # Return elapsed time return (end_time - start_time)
def showAndSaveHistogram_2Dim(samples, num_bins, expID, path, D, proposal_dist, sigma, n, burn_in, keep_every, num_samples_fnl, err_fnl): # get figure and axes in 3D: fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # set min and max for figure: x1_min = -20 x1_max = 15 x2_min = -15 x2_max = 20 # create a surface_plot of our target mulitvariate Guassian Mixture distribution: x1_surface = np.arange(x1_min, x1_max+1, .25) x2_surface = np.arange(x2_min, x2_max+1, .25) X1_surface, X2_surface = np.meshgrid(x1_surface, x2_surface) Z_surface = np.empty_like(X1_surface) for i in xrange (0, len(X1_surface)): for j in xrange(0, len(X1_surface)): Z_surface[i,j] = p_nDims([X1_surface[i,j], X2_surface[i,j]]) ax.plot_surface(X1_surface, X2_surface, Z_surface, rstride=5, cstride=5, alpha=0.4) # set labels, etc. for 3D surface plot: axis_label_fontdict = {'fontsize':25, 'fontweight':'bold', 'color':'r'} ax.set_xlabel('X1', fontdict=axis_label_fontdict) ax.set_xlim(-20, 15) ax.set_ylabel('X2', fontdict=axis_label_fontdict) ax.set_ylim(-15, 20) ax.set_zlabel('Z', fontdict=axis_label_fontdict) x1 = samples[0,:] x2 = samples[1,:] rng = [[x1_min,x1_max],[x2_min,x2_max]] # REFERENCE NOTE: the second half of this function for plottingin 3D was adapted from an online source: # http://matplotlib.org/examples/mplot3d/hist3d_demo.html hist, x1edges, x2edges = np.histogram2d(x1, x2, bins=50, range=rng, normed=True) elements = (len(x1edges) - 1) * (len(x2edges) - 1) x1pos, x2pos = np.meshgrid(x1edges[:-1], x2edges[:-1]) x1pos = x1pos.flatten() x2pos = x2pos.flatten() zpos = np.zeros(elements) dx1 = 0.5 * np.ones_like(zpos) dx2 = dx1.copy() dz = hist.flatten() # REFERENCE NOTE: this technique for setting the colormap was adapted from matplotlib's online demo repository: # http://matplotlib.org/examples/... offset = dz + np.abs(dz.min()) fracs = offset.astype(float)/offset.max() norm = clrs.Normalize(fracs.min(), fracs.max()) my_colors = cm.coolwarm(norm(fracs)) # plot the histogram2d data as a bar3d on the same axes: ax.bar3d(x1pos, x2pos, zpos, dx1, dx2, dz, color=my_colors, zsort='average', alpha=.4) title = "2D HISTOGRAM exp" + str(expID) +" "\ + str(D) + "D" +" "\ + "Q=" + str(proposal_dist) +" "\ + "\nsigma=" + str(sigma[0,:]) + str(sigma[1,:]) fig.suptitle(title, fontsize='20') txt = "exp" + str(expID) +" "\ + str(D) + "D\n"\ + "Q=" + str(proposal_dist) +" "\ + "sigma=" + str(sigma[0,:]) + str(sigma[1,:]) +"\n"\ + "n=" + str(n) +" "\ + "burn_in=" + str(burn_in) +"\n"\ + "keep_every=" + str(keep_every) +"\n"\ + "num_samples_fnl=" + str(num_samples_fnl) +"\n"\ + "err_fnl=" + str(round(err_fnl, 8)) plt.figtext(0, 0, txt, style='italic', bbox={'facecolor':'blue', 'alpha':0.1, 'pad':10}, figure=fig) os.chdir(path) save_name = "3DHIST_exp" + str(expID) +"_"\ + str(D) +"D_"\ + str(proposal_dist) +"_"\ + "n" + str(n) +"_"\ + "burn" + str(burn_in) +"_"\ + str(keep_every) +"th_"\ + "err" + str(round(err_fnl, 5)) +"." plt.savefig(save_name) # display the histogram2d by itself in a new figure: fig1 = plt.figure() ax1 = fig1.add_subplot() count, xedges, yedges, image = plt.hist2d(x1, x2, num_bins, normed=True) pylab.colorbar() fig1.suptitle(title, fontsize='20') plt.figtext(0, 0, txt, style='italic', bbox={'facecolor':'blue', 'alpha':0.1, 'pad':10}, figure=fig1) save_name2 = "2DHIST_exp" + str(expID) +"_"\ + str(D) +"D_"\ + str(proposal_dist) +"_"\ + "n" + str(n) +"_"\ + "burn" + str(burn_in) +"_"\ + str(keep_every) +"th_"\ + "err" + str(round(err_fnl, 5)) +"." plt.savefig(save_name2) plt.show() plt.close()
count += 1 if task == 'reacher': action[0], action[3] = 0, 0 _, end_position, observation = goal_babbling.perform_action( action) # Perform the action and get the observation if len(history) >= max_history_len: del history[0] history.append((action, end_position)) # Store the actions and end positions in buffer end_pos.append(end_position) goal_positions.append(goal) final_pos = np.asarray(end_pos) final_goals = np.asarray(goal_positions) np.savez(file_path + '/{}'.format(seed) + '/numpy_files/pos-action-noise-{}-retries-{}-eps-{}.npz'.format(action_noise, num_retries, epsilon), position=final_pos, goals=final_goals) plt.hist2d(final_pos[:, 0], final_pos[:, 1], bins=100) # plt.xlim(-0.1436, 0.22358) # plt.ylim(0.016000, 0.25002) plt.title(label='Action Noise - {} | Num retries - {} | Epsilon : {}'.format(action_noise, num_retries, epsilon)) plt.savefig(file_path + '/{}'.format(seed) + '/plots/hm-noise-{}-retries-{}-eps-{}.png'.format(action_noise, num_retries, epsilon))
#weights = pypmc.sampler.importance_sampling.combine_weights([samples[:] for samples in sampler.samples], # [weights[:][:,0] for weights in sampler.weights], # [mix] )[:][:,0] #samples = sampler.samples[:] integral_estimator = weights.sum() / len(weights) integral_uncertainty_estimator = np.sqrt( (weights**2).sum() / len(weights) - integral_estimator**2) / np.sqrt(len(weights) - 1) print('estimated integral =', integral_estimator, '+-', integral_uncertainty_estimator) print('estimated ln of integral =', log(integral_estimator)) print('effective sample size', pypmc.tools.convergence.ess(weights)) plt.figure() plt.hist2d(samples[:, 0], samples[:, 1], weights=weights, bins=100, cmap='gray_r') pypmc.tools.plot_mixture(sampler.proposal, visualize_weights=True, cmap='jet') plt.colorbar() plt.clim(0.0, 1.0) plt.xlim(0, 1) plt.ylim(0, 1) plt.title('ln Z = %f' % log(integral_estimator)) plt.savefig('funnel_integrate_pmc_%d_%d.pdf' % (ndim, difficulty), bbox_inches='tight') plt.close()
def get2DimHist(samples, num_bins): x1 = samples[0,:] x2 = samples[1,:] count, x1edges, x2edges, image = plt.hist2d(x1, x2, num_bins, normed=True) plt.close() return count, x1edges, x2edges