def plot_tica_and_clusters(component_j, transformed_data, clusterer, lag_time, component_i, label = "dot", active_cluster_ids = [], intermediate_cluster_ids = [], inactive_cluster_ids = [], tica_dir = ""): trajs = np.concatenate(transformed_data) plt.hexbin(trajs[:,component_i], trajs[:,component_j], bins='log', mincnt=1) plt.xlabel("tIC %d" %(component_i + 1)) plt.ylabel('tIC %d' %(component_j+1)) centers = clusterer.cluster_centers_ indices = [j for j in range(0,len(active_cluster_ids),1)] for i in [active_cluster_ids[j] for j in indices]: center = centers[i,:] if label == "dot": plt.scatter([center[component_i]],[center[component_j]], marker='v', c='k', s=10) else: plt.annotate('%d' %i, xy=(center[component_i],center[component_j]), xytext=(center[component_i], center[component_j]),size=6) indices = [j for j in range(0,len(intermediate_cluster_ids),5)] for i in [intermediate_cluster_ids[j] for j in indices]: center = centers[i,:] if label == "dot": plt.scatter([center[component_i]],[center[component_j]], marker='8', c='m', s=10) else: plt.annotate('%d' %i, xy=(center[component_i],center[component_j]), xytext=(center[component_i], center[component_j]),size=6) indices = [j for j in range(0,len(inactive_cluster_ids),5)] for i in [inactive_cluster_ids[j] for j in indices]: center = centers[i,:] if label == "dot": plt.scatter([center[component_i]],[center[component_j]], marker='s', c='w', s=10) else: plt.annotate('%d' %i, xy=(center[component_i],center[component_j]), xytext=(center[component_i], center[component_j]),size=6) pp = PdfPages("%s/c%d_c%d_clusters%d.pdf" %(tica_dir, component_i, component_j, np.shape(centers)[0])) pp.savefig() pp.close() plt.clf()
def make_figure(standard_id, scores, collection, args): print_fig_info() sns.set_style('white') fig_file = os.path.join(args.output_dir, '{0}_{1}_{2}.pdf'.format(args.db, collection, standard_id)) x = list(scores['germ_divergence'].values) y = list(scores['identity'].values) xy_vals = zip(x, y) trunc_xy_vals = [v for v in xy_vals if v[0] <= args.x_max and v[1] >= args.y_min] x = [v[0] for v in trunc_xy_vals] y = [v[1] for v in trunc_xy_vals] # To make sure the gridsize is correct (since it's based on the actual values) # I need to add a single value near the max and min of each axis. # They're added just outside the visible plot, so there's no effect on the plot. x.extend([args.x_min - 1, args.x_max + 1]) y.extend([args.y_min - 1, args.y_max + 1]) # plot params cmap = color.get_cmap(args.colormap) plt.subplots_adjust(hspace=0.95) plt.subplot(111) plt.hexbin(x, y, bins='log', cmap=cmap, mincnt=3, gridsize=set_gridsize(args)) plt.title(standard_id, fontsize=18) # set and label axes plt.axis([args.x_min, args.x_max, args.y_min, args.y_max]) plt.xlabel('Germline divergence') plt.ylabel('{0} identity'.format(standard_id)) # make and label the colorbar cb = plt.colorbar() cb.set_label('Sequence count (log10)', labelpad=10) # save figure and close plt.savefig(fig_file) plt.close()
def plot_results(talkers,classifiers,savefig=False): plt.subplot(111) x,y,s = [],[],[] count_talkers = Counter(talkers) count_classifiers = Counter(classifiers) for k in count_talkers: if count_classifiers.has_key(k): x.append(count_talkers[k]) y.append(count_classifiers[k]) xmin = np.min(x) xmax = np.max(x) ymin = np.min(y) ymax = np.max(y) plt.hexbin(x,y,gridsize=(20,20),bins='log',cmap=plt.cm.viridis) plt.axis([xmin,xmax,ymin,ymax]) cb = plt.colorbar() cb.set_label(r'$\log_{10} N$') plt.xlabel(r'$N_{talk comments}$',fontsize=25) plt.ylabel(r'$N_{classifications}$',fontsize=25) plt.tight_layout() if savefig: plt.savefig('talk_contributions_{0}.png'.format(project_name)) else: plt.show() return None
def plot3D(): np.random.seed(0) n = 100000 x = np.random.standard_normal(n) y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n) xmin = x.min() xmax = x.max() ymin = y.min() ymax = y.max() plt.figure('bb') plt.subplots_adjust(hspace=0.5) plt.subplot(121) plt.hexbin(x, y, cmap=plt.cm.YlOrRd_r) plt.axis([xmin, xmax, ymin, ymax]) plt.title("Hexagon binning") cb = plt.colorbar() cb.set_label('counts') plt.subplot(122) plt.hexbin(x, y, bins='log', cmap=plt.cm.YlOrRd_r) plt.axis([xmin, xmax, ymin, ymax]) plt.title("With a log color scale") cb = plt.colorbar() cb.set_label('log10(N)')
def createHeatMapForSkims(self): plt.subplots_adjust(hspace=0.1) subPlotNum = 120 for timeInt in self.timeIntervalList: subPlotNum += 1 plt.subplot(subPlotNum) t_old = self.load_file(self.locOld + 'skim%d.dat'%timeInt) t_new = self.load_file(self.locNew + 'skim%d.dat'%timeInt) t_old_min = t_old[:,-1].min() t_old_max = t_old[:,-1].max() t_old_max = 150 t_new_min = t_new[:,-1].min() t_new_max = t_new[:,-1].max() t_new_max = 100 print 'old', t_old[:5,-1] print 'new', t_new[:5,-1] plt.hexbin(t_old[:,-1],t_new[:,-1], cmap=cm.jet, bins=100) plt.axis([t_old_min, t_old_max, t_new_min, t_new_max]) plt.title("Skims Old(X) Vs New(Y) - %d" %timeInt) cb = plt.colorbar() cb.set_label('OD Count') plt.show()
def plot_pnas_vs_tics(pnas_dir, tic_dir, pnas_names, directory, scale = 7.14, refcoords_file = None): pnas = np.concatenate(load_file(pnas_dir)) pnas[:,0] *= scale print(np.shape(pnas)) print(len(pnas_names)) if("ktICA" in tic_dir): tics = load_dataset(tic_dir) else: tics = verboseload(tic_dir) print(np.shape(tics)) tics = np.concatenate(tics) print(np.shape(tics)) if len(pnas_names) != np.shape(pnas)[1]: print("Invalid pnas names") return for i in range(0,np.shape(pnas)[1]): for j in range(0,np.shape(tics)[1]): tic = tics[:,j] pnas_coord = pnas[:,i] plt.hexbin(tic, pnas_coord, bins = 'log', mincnt=1) coord_name = pnas_names[i] tic_name = "tIC.%d" %(j+1) plt.xlabel(tic_name) plt.ylabel(coord_name) pp = PdfPages("%s/%s_%s_hexbin.pdf" %(directory, tic_name, coord_name)) pp.savefig() pp.close() plt.clf() return
def main(): fnm = 'prob3.data' data = md.read_data(fnm) D1 = data[0:8,].T D2 = data[8:,].T u1 = np.matrix((np.mean(D1[0,:]), np.mean(D1[1,:]))).T u2 = np.matrix((np.mean(D2[0,:]), np.mean(D2[1,:]))).T sigma1 = np.asmatrix(np.cov(D1, bias=1)) sigma2 = np.asmatrix(np.cov(D1, bias=1)) g1 = discrim_func(u1, sigma1) g2 = discrim_func(u2, sigma2) steps = 100 x = np.linspace(-2,2,steps) y = np.linspace(-6,6,steps) X,Y = np.meshgrid(x,y) z = [g1(X[r,c], Y[r,c]) - g2(X[r,c], Y[r,c]) for r in range(0,steps) for c in range(0,steps)] Z = np.array(z) px = X.ravel() py = Y.ravel() pz = Z.ravel() gridsize = 50 plot = plt.subplot(111) plt.hexbin(px,py,C=pz, gridsize=gridsize, cmap=cm.jet, bins=None) cb = plt.colorbar() cb.set_label('g1 minus g2') return plot
def _hex_bin(root, heights, weights, cmap=matplotlib.cm.Blues): pyplot.hexbin(heights, weights, cmap=cmap) _05_myplot._save(root=root, xlabel='Height (cm)', ylabel='Weight (kg)', axis=[140, 210, 20, 200], legend=False)
def draw2(): import numpy as np import matplotlib.pyplot as plt np.random.seed(0) n = 100000 x = np.random.standard_normal(n) y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n) xmin = x.min() xmax = x.max() ymin = y.min() ymax = y.max() plt.subplots_adjust(hspace=0.5) plt.subplot(121) plt.hexbin(x, y, cmap=plt.cm.YlOrRd_r) plt.axis([xmin, xmax, ymin, ymax]) plt.title("Hexagon binning") cb = plt.colorbar() cb.set_label('counts') plt.subplot(122) plt.hexbin(x, y, bins='log', cmap=plt.cm.YlOrRd_r) plt.axis([xmin, xmax, ymin, ymax]) plt.title("With a log color scale") cb = plt.colorbar() cb.set_label('log10(N)') plt.show()
def make_figure(standard_id, scores, collection): print_fig_info() fig_file = os.path.join(args.output, '{0}_{1}_{2}.pdf'.format(args.db, collection, standard_id)) x = [100.0 - s[0] for s in scores] y = [s[1] for s in scores] xmin = min(x) xmax = max(x) ymin = min(y) # ymax = max(y) # plot params plt.subplots_adjust(hspace=0.95) plt.subplot(111) plt.hexbin(x, y, bins='log', cmap=mpl.cm.jet, mincnt=2, gridsize=100) plt.title(standard_id, fontsize=18) # set and label axes plt.axis([xmin-2, xmax+2, ymin-2, 102]) # plt.gca().invert_xaxis() plt.xlabel('Germline divergence') plt.ylabel('{0} identity'.format(standard_id)) # make and label the colorbar cb = plt.colorbar() cb.set_label('Sequence count (log10)', labelpad=10) # save figure and close plt.savefig(fig_file) plt.close() print_done()
def rama_plot(selection='all', from_state=1, to_state=1, step=1, scatter=True): """ Makes a scatter plot with the phi and psi angle pairs """ first, last = pose_from_pdb(selection) if first or last: bonds = get_glyco_bonds(first, last) con_matrix = writer(bonds) phi = [] psi = [] for state in range(from_state, to_state+1, step): for element in con_matrix: phi.append(get_phi(selection, element, state)) psi.append(get_psi(selection, element, state)) if scatter: plt.scatter(phi, psi) else: gridsize=100 #gridsize = int(2*len(phi)**(1/3)) #if gridsize < 36: # gridsize = 36 plt.hexbin(phi, psi, gridsize=gridsize, cmap=plt.cm.summer, mincnt=1) plt.xlabel('$\phi$', fontsize=16) plt.ylabel('$\psi$', fontsize=16, rotation=0) plt.xlim(-180, 180) plt.ylim(-180, 180) plt.show()
def chunks(): kincr = [] pincr = [] for doc in collection.find(parms): ktr = trajectory(doc['_id'],False,False,'karma') tr = trajectory(doc['_id'],False,False) for i in range(1,len(tr)): kincr.append(ktr[i] - ktr[i-1]) pincr.append(tr[i] - tr[i-1]) #log scaler nkincr = [] for k in kincr: if k < 0: nkincr.append(-log(-k+1)) elif k == 0: nkincr.append(0) elif k > 0 : nkincr.append(log(k+1)) npincr = [] for p in pincr: if p < 0: npincr.append(-log(-p+1)) elif p == 0: npincr.append(0) elif p > 0 : npincr.append(log(p+1)) plt.hexbin(npincr,nkincr,bins='log',mincnt=1) plt.colorbar() plt.title('Trajectory movement') plt.xlabel('log scaled change of position') plt.ylabel('log scaled change of karma') plt.show()
def main(): print "Loading file ..." with open("inverse_kinematics.txt", "rb") as handle: iv = pickle.loads(handle.read()) print "Done." coord_x = [] coord_y = [] for coord in iv.keys(): for _ in range(len(iv[coord])): coord_x.append(coord[0]) coord_y.append(coord[1]) x = np.array(coord_x) y = np.array(coord_y) xmin = x.min() xmax = x.max() ymin = y.min() ymax = y.max() gridsize = 60 plt.hexbin(x, y, gridsize=gridsize, cmap=plt.cm.jet, bins=None) plt.axis([xmin, xmax, ymin, ymax]) plt.subplot(111) cb = plt.colorbar() plt.show()
def plot_column_pair(i, num_columns, save_dir, titles, data, gmm_means, refcoords): for j in range(i+1, num_columns): plt.hexbin(data[:,i], data[:,j], bins = 'log', mincnt=1) print(gmm_means) print(gmm_means[i]) print(gmm_means[j]) for mean in gmm_means[i]: plt.axvline(x=mean,color='k',ls='dashed') for mean in gmm_means[j]: plt.axhline(y=mean,color='k',ls='dashed') if refcoords is not None: plt.scatter([refcoords[0,i]], [refcoords[0,j]], marker = 's', c='w',s=15) plt.scatter([refcoords[1,i]], [refcoords[1,j]], marker = 'v', c='k',s=15) if titles is not None: pp = PdfPages("%s/%s_%s.pdf" %(save_dir, titles[i], titles[j])) plt.xlabel(titles[i]) plt.ylabel(titles[j]) pp.savefig() pp.close() plt.clf() else: pp = PdfPages("%s/tIC.%d_tIC.%d.pdf" %(save_dir, i+1, j+1)) plt.xlabel("tIC.%d" %(i+1)) plt.ylabel("tIC.%d" %(j+1)) pp.savefig() pp.close() plt.clf()
def plot_tica(transformed_data_dir, lag_time): transformed_data = verboseload(transformed_data_dir) trajs = np.concatenate(transformed_data) plt.hexbin(trajs[:,0], trajs[:,1], bins='log', mincnt=1) pp = PdfPages("/scratch/users/enf/b2ar_analysis/tica_phi_psi_chi2_t%d.pdf" %lag_time) pp.savefig() pp.close()
def psych_chart(T, W=None, RH=None, heatplot=False, lims = (0, 90, 0, 0.02), **kwargs): if (W==None) & (RH==None): return None # TODO exception? elif W==None: W = humidity_ratio(RH, T) fig = plt.figure() if heatplot: plt.hexbin(T, W, extent=lims, **kwargs) else: plt.scatter(T, W, **kwargs) ts = np.linspace(lims[0],lims[1],21) if heatplot: linecolor = 'w' else: linecolor = 'k' for rh in np.linspace(0.1,1,10): plt.plot(ts, humidity_ratio(rh, ts), linecolor) plt.xlabel('Temperature [degF]') plt.ylabel('Humidity Ratio') plt.ylim(lims[2], lims[3]) plt.xlim(lims[0], lims[1]) # RH labels on right hand side ax1 = plt.axes() ax2 = ax1.twinx() ax2.get_yaxis().set_major_locator(ticker.FixedLocator(humidity_ratio(np.linspace(0,1,11), lims[1])/(lims[3]-lims[2]))) ax2.get_yaxis().set_major_formatter(ticker.FixedFormatter(np.linspace(0,100,11))) plt.ylabel('Relative Humidity [%]') return ax1
def my_plot_histogram_hexbin(x, y, bins='log', gridsize=None, ttitle="", txlabel='x', tylabel='y', ymin=None, file_name=None): """Plot a 2d-histogram using hexbins """ fig = plt.figure(figsize = (9,8)) ax = fig.add_subplot(111) xdata = x.ravel() ydata = y.ravel() if gridsize is not None: plt.hexbin(xdata,ydata,cmap=plt.cm.gray_r,bins='log',gridsize=gridsize) else: plt.hexbin(xdata,ydata,cmap=plt.cm.gray_r,bins='log') plt.colorbar() plt.title(ttitle, color='k') if ymin is not None: ax.set_ylim(ymin,np.max(ydata)) plt.xlabel(txlabel) plt.ylabel(tylabel) if file_name is not None: plt.savefig(file_name) plt.show() return 1
def SummarizeWeight(rows, input_limit=None): years = [1981, 1982, 1985, 1986, 1988, 1989, 1990, 1992, 1993, 1994, 1996, 1998, 2000, 2002, 2004, 2006, 2008] all_diffs = [] for i, row in enumerate(rows): if i == input_limit: break id, race, sex = row[:3] weights = row[3:] print id diffs = Differences(years, weights, jitter=3) all_diffs.extend(diffs) weights, changes = zip(*all_diffs) print 'Mean weight', thinkstats.Mean(weights) print 'Mean change', thinkstats.Mean(changes) print numpy.corrcoef(weights, changes) pyplot.hexbin(weights, changes, cmap=matplotlib.cm.gray_r) myplot.Save('nlsy_scatter', title = 'Weight change vs. weight', xlabel = 'Current weight (pounds)', ylabel = 'Weight change (pounds)', axis = [70, 270, -25, 25], legend=False, show=True, )
def plot(x, y, c, filename): xmin=-180 xmax = 180 ymin = -300 ymax = 300 val = np.clip(c, -0.005, 1.05) #print val fig = plt.figure(1, (12,6)) # plt.subplots_adjust(hspace=0.5) plt.subplot(111, axisbg='black') plt.hexbin(x, y, val, cmap=plt.cm.gist_heat_r) # plt.scatter(x, y, cmap=plt.cm.YlOrRd_r) plt.axis([xmax, xmin, ymin, ymax]) plt.title("Longitude-Velocity") plt.xlabel('Galactic longitude (l)') plt.ylabel('LSR Velocity (km/s)') cb = plt.colorbar() cb.set_label(r'$e^{(-\tau)}$') #plt.subplot(122) #plt.hexbin(x, y, c, bins='log', cmap=plt.cm.YlOrRd_r) #plt.axis([xmin, xmax, ymin, ymax]) #plt.title("With a log color scale") #cb = plt.colorbar() #cb.set_label('log10(N)') plt.savefig(filename) # plt.show() return
def cnt_map(region, table = 'sample', draw = True): """Draw a region map of tweets""" twt_lst = dataset.loadrows(GEOTWEET, ('lat', 'lng'), ('MBRContains({0}, geo)'.format(dataset.geo_rect(*region)),), table) lat = list(); lng = list(); for twt in twt_lst: lat.append(twt['lat']) lng.append(twt['lng']) if draw: x = np.array(lng) y = np.array(lat) xmin = x.min() xmax = x.max() ymin = y.min() ymax = y.max() plt.hexbin(x,y, gridsize=200, cmap=cm.jet) plt.axis([xmin, xmax, ymin, ymax]) plt.title("Hexagon binning") cb = plt.colorbar() cb.set_label('counts') plt.show() return lat, lng
def hexbin_scatter(datasets, d_cmap, with_points=False): # Check we have only one dataset assert len(datasets.keys()) == 1 dataset_name = list(datasets.keys())[0] gaze_by_point = datasets[dataset_name] # Initialize hexbin parameters gridsize = None num_hexagones = 30 # HexBin scatter for idx, pt in enumerate(gaze_by_point.keys()): measures = np.array xmin, xmax = measures[:, 0].min(), measures[:, 0].max() ymin, ymax = measures[:, 1].min(), measures[:, 1].max() # gridsize specifiew how many hexagons will be plotted # logic below makes sure that for each pt, we have approximately similar size hexagons if gridsize is None: grid_width = (xmax - xmin) / num_hexagones x_grid_size = num_hexagones y_grid_size = int((ymax - ymin) / grid_width) gridsize = (x_grid_size, y_grid_size) else: gridsize = int((xmax - xmin) / grid_width), int((ymax - ymin) / grid_width) # mincnt makes sure that bins without data are plotted in white plt.hexbin(measures[:, 0], measures[:, 1], gridsize=gridsize, cmap=d_cmap[pt], linewidths=0.1, mincnt=1, norm=LogNorm())
def MakeFigures(pool, firsts, others): """Creates several figures for the book.""" # plot CDFs of birth ages for first babies and others line_options = [ dict(linewidth=0.5), dict(linewidth=0.5) ] myplot.Cdfs([firsts.age_cdf, others.age_cdf], root='nsfg_age_cdf', line_options=line_options, title="Mother's age CDF", xlabel='age (years)', ylabel='probability') # make a scatterplot of ages and weights ages, weights = GetAgeWeight(pool) pyplot.clf() #pyplot.scatter(ages, weights, alpha=0.2) pyplot.hexbin(ages, weights, cmap=matplotlib.cm.gray_r) myplot.Save(root='age_scatter', xlabel='Age (years)', ylabel='Birth weight (oz)', legend=False)
def execute(model, data, savepath, lwr_data, *args, **kwargs): parameters = {'alpha' : np.logspace(-6,0,20), 'gamma': np.logspace(-1.5,1.5,20)} grid_scores = [] for a in parameters['alpha']: for y in parameters['gamma']: model = KernelRidge(alpha= a, gamma= y, kernel='rbf') rms = leaveout_cv(model, data, num_runs = 200) #rms = kfold_cv(model, data, num_folds = 5, num_runs = 50) #rms = alloy_cv(model, data) #rms = atr2_extrap(model, data) #rms = lwr_extrap(model, data, lwr_data) grid_scores.append((a, y, rms)) grid_scores = np.asarray(grid_scores) with open(savepath.replace(".png","").format("grid_scores.csv"),'w') as f: writer = csv.writer(f, lineterminator = '\n') x = ["alpha", "gamma", "rms"] writer.writerow(x) for i in grid_scores: writer.writerow(i) #Heatmap of RMS scores vs the alpha and gamma plt.figure(1) plt.hexbin(np.log10(grid_scores[:,0]), np.log10(grid_scores[:,1]), C = grid_scores[:,2], gridsize=15, cmap=cm.plasma, bins=None, vmax = 60) plt.xlabel('log alpha') plt.ylabel('log gamma') cb = plt.colorbar() cb.set_label('rms') plt.savefig(savepath.format("alphagammahex"), dpi=200, bbox_inches='tight') plt.close()
def bivar_hexbin_latlon(lat, lon): fig, ax = plt.subplots(figsize=(13, 7)) # set major ticks every 20 x_major_ticks = np.arange(0, 361, 20) ax.set_xticks(x_major_ticks) y_major_ticks = np.arange(-90, 91, 10) ax.set_yticks(y_major_ticks) ax.set_xlim([0, 360]) ax.set_ylim([-90, 90]) plt.hexbin(lon, lat, gridsize=140, cmap=mpl.cm.plasma, bins=None) cb = plt.colorbar() cb.set_label('Frequency') ax.set_title("Crater Distribution on Mars Surface (System: Planetocentric)",fontsize=14) ax.set_xlabel("LONGITUDE East (degrees)",fontsize=12) ax.set_ylabel("LATITUDE (degrees)",fontsize=12) # show plot figure #plt.show() # save plot figure filename = 'graph_6.png' plt.savefig(filename, dpi = 150) plt.close()
def HexBin(self, root, heights, weights, cmap=pyplot.cm.Blues): pyplot.hexbin(heights, weights, cmap=cmap) myplot.Save(root=root, xlabel='Height (cm)', ylabel='Weight (kg)', axis=[140, 210, 20, 200], legend=False)
def plot_heatmap(self, metric, gridsize=None): dm = self.distance_matrices[metric] length = len(dm) (X, Y) = np.meshgrid(np.arange(length), np.arange(length)) x = X.ravel() y = Y.ravel() z = dm.ravel() if not gridsize: gridsize = length / 2 fig = plt.figure(figsize=(11.7, 8.3)) plt.subplot(111) plt.hexbin( x, y, C=z, gridsize=gridsize, cmap=CM.jet, bins=None, ) plt.axis([x.min(), x.max(), y.min(), y.max()]) cb = plt.colorbar() cb.set_label('Distance') return fig
def main(): trajs = load_trajs() pca = fit_pca(trajs) cmap2 = matplotlib.cm.hot_r #cmap1 = brewer2mpl.get_map('OrRd', 'Sequential', 9).mpl_colormap for i, mfn in enumerate(model_fns): msm = MarkovStateModel.load(mfn) try: X = np.vstack([trajs[str(t)] for t in msm.traj_filenames]) except KeyError as e: logging.exception('Not found? round %d', i) continue X_reduced = pca.transform(X) pp.hexbin(X_reduced[:,0], X_reduced[:,1], cmap=cmap2, bins='log', vmin=0, vmax=VMAX) pp.xlabel('PC 1') pp.ylabel('PC 2') pp.title('Round %d PCA: %d frames' % (i, X_reduced.shape[0])) pp.xlim(*XLIM) pp.ylim(*YLIM) cb = pp.colorbar() cb.set_label('log10(N)') fn = 'plot_%04d.png' % i print fn pp.savefig(fn) pp.clf() subprocess.check_output('echo y | avconv -i plot_%04d.png -c:v libx264 -preset slow -crf 1 output.avi', shell=True)
def _scatter(Ts, xi, xj, yi, yj): pp.grid(False) pp.hexbin(Ts[:, xi, xj], Ts[:, yi, yj], cmap='hot_r', vmin=0, vmax=100) pp.xlabel('T_{%d,%d}' % (xi+1, xj+1)) pp.ylabel('T_{%d,%d}' % (yi+1, yj+1)) pp.plot([0,1], [1,0], c='k') pp.ylim(0, 1) pp.xlim(0, 1)
def HexBin(xs, ys, **options): """Makes a scatter plot. xs: x values ys: y values options: options passed to pyplot.scatter """ options = _Underride(options, cmap=matplotlib.cm.Blues) pyplot.hexbin(xs, ys, **options)
def phase_map(na,nb,speciesa,speciesb,ciclosinic,ciclosfin): auxa=[] auxb=[] for k in range (ciclosinic,ciclosfin): auxa.append(na[k][speciesa]) auxb.append(nb[k][speciesb]) plt.hexbin(auxa,auxb,bins='log',cmap=cm.YlOrRd) plt.show() return
def orientation_density(): thetas = [] # List to collect data for later histogramming phis = [] thetasOh = [] phisOh = [] for frame in cnsn[:, :]: for cn in frame: theta, phi = spherical_coordinates( numpy.array(cn)) # Values used for ORIENTATION thetas.append(theta) #append this data point to lists phis.append(phi) # Now apply symmetry operations for looking at absolute orientation... # Exploits full Oh symmetry with two lines - see workings in Jarv's notebooks # (rear page of Orange book, 16-4-14) cn = abs(cn) cn = sorted( abs(cn), reverse=True ) #Exploit Oh symmetry - see workings in Jarv's notebooks thetaOh, phiOh = spherical_coordinates(numpy.array(cn)) thetasOh.append(thetaOh) phisOh.append(phiOh) # 2D density plot of the theta/phi information fig = plt.figure() ax = fig.add_subplot(111) # Use Cubehelix colourmap, as it is wicked. # Read why: http://www.ifweassume.com/2013/05/cubehelix-or-how-i-learned-to-love.html plt.hexbin(phis, thetas, gridsize=36, marginals=False, cmap=plt.cm.cubehelix_r) #PuRd) #cmap=plt.cm.jet) plt.colorbar() pi = numpy.pi # Full spherical coordinate axes plt.xticks([-pi, -pi / 2, 0, pi / 2, pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'], fontsize=14) plt.yticks([-pi / 2, 0, pi / 2], [r'$-\pi/2$', r'$0$', r'$\pi/2$'], fontsize=14) if (DisplayFigures): plt.show() fig.savefig("%s-orientation_density_nosymm.png" % fileprefix, bbox_inches='tight', pad_inches=0, dpi=200) fig.savefig("%s-orientation_density_nosymm.pdf" % fileprefix, bbox_inches='tight', pad_inches=0) fig.savefig("%s-orientation_density_nosymm.eps" % fileprefix, bbox_inches='tight', pad_inches=0.2) # 2D density plot of the theta/phi information - MOLLWEIDE projection fig = plt.figure() ax = fig.add_subplot(111, projection='mollweide') plt.hexbin(phis, thetas, gridsize=36, marginals=False, cmap=plt.cm.cubehelix_r) #PuRd) #cmap=plt.cm.jet) plt.colorbar() pi = numpy.pi # Full spherical coordinate axes # plt.xticks( [-pi,-pi/2,0,pi/2,pi], # [r'$-\pi$',r'$-\pi/2$',r'$0$',r'$\pi/2$',r'$\pi$'], # fontsize=14) # plt.yticks( [0,pi/2,pi], # [r'$0$',r'$\pi/2$',r'$\pi$'], # fontsize=14) plt.xticks([], []) if (DisplayFigures): plt.show() fig.savefig("%s-orientation_density_nosymm_mollweide.png" % fileprefix, bbox_inches='tight', pad_inches=0, dpi=200) fig.savefig("%s-orientation_density_nosymm_mollweide.pdf" % fileprefix, bbox_inches='tight', pad_inches=0) fig.savefig("%s-orientation_density_nosymm_mollweide.eps" % fileprefix, bbox_inches='tight', pad_inches=0.2) fig = plt.figure() ax = fig.add_subplot(111) plt.hexbin(phisOh, thetasOh, gridsize=36, marginals=False, cmap=plt.cm.cubehelix_r) #PuRd) #cmap=plt.cm.jet) plt.colorbar() pi = numpy.pi # Full symm axes plt.xticks([0.01, pi / 4], [r'$0$', r'$\pi/4$'], fontsize=14) # Now _THAT_ is a magic number. # (OK, actually it's a Trig identity, just a bit of a complex one) plt.yticks([-0.6154797, -0.01], [r'$-0.62$', r'$0$'], fontsize=14) if (DisplayFigures): plt.show() fig.savefig("%s-orientation_density_Oh_symm.png" % fileprefix, bbox_inches='tight', pad_inches=0, dpi=200) fig.savefig("%s-orientation_density_Oh_symm.pdf" % fileprefix, bbox_inches='tight', pad_inches=0) fig.savefig("%s-orientation_density_Oh_symm.eps" % fileprefix, bbox_inches='tight', pad_inches=0.2)
def plot_projections( projections, filename=None, colours=None, marker_size=3, font_size=6, gridsize=None, label_indices=False, epochs=None, colour_map=None, ): projections_all = projections # http://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear matplotlib.use("Agg") # use a non-interactive backend from matplotlib import pylab, pyplot if epochs is not None and colour_map is not None: epochs = flex.double(epochs) epochs -= flex.min(epochs) epochs /= flex.max(epochs) cmap = matplotlib.cm.get_cmap(colour_map) colours = [cmap(e) for e in epochs] elif colours is None or len(colours) == 0: colours = ["b"] * len(projections_all) elif len(colours) < len(projections_all): colours = colours * len(projections_all) fig = pyplot.figure() pyplot.scatter([0], [0], marker="+", c="0.75", s=100) cir = pylab.Circle((0, 0), radius=1.0, fill=False, color="0.75") pylab.gca().add_patch(cir) if gridsize is not None: x = flex.double() y = flex.double() for i, projections in enumerate(projections_all): x_, y_ = projections.parts() x.extend(x_) y.extend(y_) hb = pyplot.hexbin(x, y, gridsize=gridsize, linewidths=0.2) pyplot.colorbar(hb) else: for i, projections in enumerate(projections_all): x, y = projections.parts() pyplot.scatter( x.as_numpy_array(), y.as_numpy_array(), c=colours[i], s=marker_size, edgecolors="none", ) if label_indices: for j, (hkl, proj) in enumerate(zip(label_indices, projections)): # hack to not write two labels on top of each other p1, p2 = (projections - proj).parts() if (flex.sqrt(flex.pow2(p1) + flex.pow2(p2)) < 1e-3).iselection()[0] != j: continue pyplot.text(proj[0], proj[1], str(hkl), fontsize=font_size) fig.axes[0].set_aspect("equal") pyplot.xlim(-1.1, 1.1) pyplot.ylim(-1.1, 1.1) if filename is not None: pyplot.savefig(filename, dpi=300)
def skill_vs_weight_density_plot(): fout = '/Users/u0116961/Documents/work/LDAS/2020-03_scaling/plots/skillgain_vs_pert_density.png' io = LDAS_io() xlim = [0, 0.5] ylim = [-0.6, 0.6] fname = '/Users/u0116961/Documents/work/MadKF/CLSM/SMOS40/iter_612/result_files/mse.csv' mse = pd.read_csv(fname, index_col=0) mse.index = colrow2easegpi(io.grid.tilecoord.i_indg, io.grid.tilecoord.j_indg, glob=True) mse.loc[:, f'rel_mse_spc_avg'] = 0 for spc in np.arange(1, 5): mse.loc[:, f'rel_mse_spc{spc}'] = mse['mse_fcst_spc%i' % spc] / mse['mse_obs_spc%i' % spc] mse.loc[:, f'rel_mse_spc_avg'] += mse.loc[:, f'rel_mse_spc{spc}'] mse.loc[:, f'rel_mse_spc_avg'] /= 4 res = pd.read_csv( '/Users/u0116961/Documents/work/LDAS/2020-03_scaling/validation/ascat_eval_old.csv', index_col=0) exps = ['SMOSSMAP_short', 'MadKF_SMOS40'] modes = ['absolute', 'longterm', 'shortterm'] cols = [] for m in modes: for e in exps: col = f'ana_r_{e}_{m}' ref_col = f'ana_r_open_loop_{m}' res[f'{e} - {m}'] = (res[col] - res[ref_col]) / (1 - res[ref_col]) cols += [ f'{e} - {m}', ] # res = res[[x for x in res.columns if 'diff_' in x]] res['P/R'] = mse[f'rel_mse_spc_avg'] res = res[(res['P/R'] >= xlim[0]) & (res['P/R'] <= xlim[1])] sns.set_context('notebook', font_scale=0.8) cmap = sns.cubehelix_palette(as_cmap=True, dark=0, light=1, reverse=False) f = plt.figure(figsize=(12, 8)) for i, m in enumerate(modes): for j, e in enumerate(exps): ax = plt.subplot(len(modes), len(exps), i * len(exps) + j + 1) tmp_df = res[['P/R', f'{e} - {m}']].dropna() med = res[f"{e} - {m}"].median() ax.text(0.35, -0.5, f'Median = {med:.3f}', fontsize=10) plt.hexbin(tmp_df['P/R'], tmp_df[f'{e} - {m}'], bins='log', gridsize=50, cmap=cmap) plt.axhline(0, color='black', linestyle='--', linewidth=1.5) plt.axvline(1, color='black', linestyle='--', linewidth=1.5) plt.xlim(xlim) plt.ylim(ylim) plt.title(f'{e} - {m}') if i == 2: plt.xlabel('P / R') if (i == 1) & (j == 0): plt.ylabel('Skill gain w.r.t. open-loop') f.subplots_adjust(hspace=0.35) f.savefig(fout, dpi=300, bbox_inches='tight') plt.close()
import matplotlib.pyplot as plt import pandas as pd import numpy as np df = pd.read_csv("auto-mpg.csv", header=0) # print(df.head()) hp = df.hp.values mpg = df.mpg.values plt.hexbin(hp, mpg, gridsize=(15, 12), extent=(np.min(hp), np.max(hp), np.min(mpg), np.max(mpg))) plt.colorbar() plt.xlabel("Horse Power (HP)") plt.ylabel("Miles per Gallon (mpg)") plt.title("hex bin plot") plt.show()
#!/usr/bin/env python import pandas as pd from matplotlib import pyplot as plt from scipy import stats import numpy as np # read mean RPKM values into a dataframe reviews = pd.read_csv('1_hexbin.txt', sep='\t') # Set figure size plt.figure(figsize=(8, 6)) plt.hexbin( x=reviews.total, y=reviews.su, gridsize= 60, # this parameter sets the number of hexagons (higher integeter means more hexagons across each axis) xscale='log', yscale='log', cmap=plt.cm.BuPu) # Log scaled hexbins don't set the axis limits correctly for some reason # auto scaled plt.xlim(reviews.total.min(), reviews.total.max()) plt.ylim(reviews.su.min(), reviews.su.max()) # custom scaled e.g xlim(left,right) #plt.xlim(0.001, 1000) #plt.ylim(0.001, 1000) plt.xlabel('Total RNA')
import pandas as pd import numpy as np from sklearn.tree import DecisionTreeClassifier from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelEncoder import matplotlib.pyplot as plt # Read in & munge wine information dataset. US wines only wine_df=pd.read_csv('winemag-data_first150k.csv') wine_df=wine_df.loc[wine_df.country=='US',['points','price','region_1', \ 'variety','winery']] wine_df=wine_df.dropna(axis=0,how='any') # Hexbin of points on price plt.figure(figsize=(7,4)) plt.hexbin(wine_df.price,wine_df.points,cmap='bone_r',gridsize=35, extent=(0,100,73,115)) plt.xlim(0,100) plt.ylim(80,98) plt.title('Point Score by Wine Price',size=14) plt.ylabel('Points') plt.xlabel('Wine Price') plt.show() # Violin plot of points on region - use regions with 50 or more wines only vio_dfm=wine_df.loc[:,['region_1','points']].groupby('region_1').median() vio_dfm.reset_index(inplace=True) vio_df=wine_df.loc[:,['region_1','points']].groupby('region_1').count() vio_df.reset_index(inplace=True) vio_df=vio_df.merge(vio_dfm,'inner',left_on='region_1',right_on='region_1') vio_df=vio_df.sort_values('points_y',ascending=False) vio_df=vio_df.loc[vio_df.points_x>=50,:]
color='r') plt.savefig("fig-hist2.png") plt.show() #### 2D histogram binnings ########## mean = [0, 0] cov = [[1, 1], [1, 2]] x, y = np.random.multivariate_normal(mean, cov, 1000000).T plt.hist2d(x, y, bins=30, cmap='Blues') cb = plt.colorbar() cb.set_label('counts in bin') plt.savefig("fig-2dhist1.png") plt.show() # Not sure what this is doing. plt.hexbin(x, y, gridsize=30, cmap="Reds") cb = plt.colorbar(label='count in bin') plt.savefig("fig-2dhist.png") plt.show() ####### sns histogram grid ########## import seaborn as sns tips = sns.load_dataset("tips") tips.head() tips['tip_pct'] = 100 * tips['tip'] / tips['total_bill'] grid = sns.FacetGrid(tips, row='sex', col='time', margin_titles=True) grid.map(plt.hist, 'tip_pct', bins=np.linspace(0, 40, 15)) plt.suptitle("Figure histogram-grid tips")
v_wind, wind_theta, wind_phi, \ Altitude) step = len(show_x) - 1 distance = show_x[step] array_set_C_D.append(set_C_D) array_set_C_L.append(set_C_L) array_distance.append(distance) plt.figure(3) ax = fig.add_subplot(111) plt.xlabel(r'$C_D$', fontweight='bold', fontsize=25) plt.ylabel(r'$C_L$', fontweight='bold', fontsize=25) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.hexbin(array_set_C_D, array_set_C_L, C=array_distance, gridsize=set_grid_sections, cmap=cm.jet, bins=None) plt.axis([ min(array_set_C_D), max(array_set_C_D), min(array_set_C_L), max(array_set_C_L) ]) #cb = plt.colorbar(image,spacing='uniform',extend='max') cb = plt.colorbar() cb.set_label('Flight distance (m)', fontsize=25) cb.ax.tick_params(labelsize=15) plt.savefig('Case7-Fig3.eps', format='eps', dpi=1000, bbox_inches='tight')
def plot_far_field(db_name): #db_name = "test_simulate" #db_name = "DE_xpol_ff" #path = "../db" path = "../db/initial_results" print("{}/{}.json".format(path,db_name)) #path = "../db/initial_results/{}.json".format(db_name) lambda_wl = [] for n in range(len(freqs)): lambda_wl.append(round(1000/freqs[n],1)) colors = ['darkred','red', 'darkorange','limegreen','aquamarine','teal','navy', 'blue'] db = read("{}/{}.json".format(path,db_name)) pyr = 1 ff_at_angle = db[pyr]["result"]["ff_at_angle"] far_field, npts, nfreq, ff_angle, ph, fcen = extract_data_from_db(db_name,path, pyr) ff_values = return_field_values_from_ff(far_field) ff_pos = return_position_values_from_ff(far_field) Pr_array = calculate_poynting_values(ff_values) radius = 2*math.pow(ph,2)*fcen*2*10 x,y,z = unpack_3_list(ff_pos) for k in range(nfreq): freq = k ff_at_angle_freq = ff_at_angle[freq] Pr_array_freq = get_poynting_per_frequency(Pr_array, freq) flux_per_freq = get_flux(Pr_array_freq,math.pi/ff_angle,npts,radius) theta_pts = int(math.sqrt((npts-1)/2)) theta = math.pi / ff_angle theta_angles = np.linspace(0+theta/theta_pts,theta,theta_pts) cum_flux_per_angle = get_cum_flux_per_angle(Pr_array_freq, 3, npts, radius) cum_flux_per_angle_norm = [100*i / ff_at_angle_freq for i in cum_flux_per_angle] theta_angles_reversed = [i * -1 for i in theta_angles] flux_conc = cum_flux_per_angle + cum_flux_per_angle[::-1] theta_angles_conc = list(theta_angles)[::-1] + list(theta_angles_reversed) theta_angles_degrees = [i * 180/math.pi for i in theta_angles] plt.plot(theta_angles_degrees,cum_flux_per_angle_norm, label=str(lambda_wl[k])+"nm",color=colors[k]) plt.legend(loc='best') plt.title('Share of far field flux within angle') plt.ylabel('(%) of far-field within angle') plt.xlabel('degrees') plt.savefig('angles.png') plt.grid(visible=True) plt.clf() norm_val = max(Pr_array_freq) Pr_array_freq_normed = [i / norm_val for i in Pr_array_freq] elements = int(1*npts/nfreq) theta_val = [] phi_val = [] for n in range(len(x)): theta_val.append(math.acos(z[n]/radius)) if x[n] > 0: phi_val.append(y[n]/x[n]) elif x[n] < 0 and y[n] >= 0: phi_val.append(y[n]/x[n]+math.pi) elif x[n] < 0 and y[n] < 0: phi_val.append(y[n]/x[n] - math.pi) elif x[n] == 0 and y[n] > 0: phi_val.append(math.pi/2) elif x[n] == 0 and y[n] < 0: phi_val.append(-math.pi/2) else: phi_val.append(0) X,Y = np.meshgrid(theta_val,phi_val) Pr_mesh = np.meshgrid(Pr_array_freq_normed,Pr_array_freq_normed) #plt.Circle((0,0),15, fill=False) #plt.plot() plt.hexbin(x,y,Pr_array_freq_normed) plt.title('Sampled flux density') plt.xlabel('degrees') plt.ylabel('degrees') plt.colorbar(plt.hexbin(x,y,Pr_array_freq_normed)) plt.savefig('flux2.png')
def get_landscape(combo): envDiff, enterD, exitD = combo n = 1 * 10**3 # size of sample to be draw from a beta distribution Alpha, Beta = 10, 3 Nx = np.random.beta(Alpha, Beta, n).tolist() Nx = filter(lambda a: a >= 0.5, Nx) Sx = np.random.beta(Beta, Alpha, n).tolist() Sx = filter(lambda a: a <= 0.5, Sx) Ny = np.random.uniform(0, 1, len(Nx)).tolist() Sy = np.random.uniform(0, 1, len(Sx)).tolist() if envDiff == 'differ': imageN = plt.hexbin(Nx, Ny, mincnt=0, gridsize=20, cmap=plt.cm.YlOrBr, alpha=0.6) imageS = plt.hexbin(Sx, Sy, mincnt=0, gridsize=20, cmap=plt.cm.YlGn, alpha=0.6) elif envDiff == 'same': imageN = plt.hexbin(Nx, Ny, mincnt=0, gridsize=20, cmap=plt.cm.YlGn, alpha=0.6) imageS = plt.hexbin(Sx, Sy, mincnt=0, gridsize=20, cmap=plt.cm.YlGn, alpha=0.6) Ncounts = imageN.get_array() Nverts = imageN.get_offsets() Nverts = sample(Nverts, 10) Nverts = np.array(Nverts) Scounts = imageS.get_array() Sverts = imageS.get_offsets() Sverts = sample(Sverts, 10) Sverts = np.array(Sverts) NRow1Xs = [0.15] * 10 NRow2Xs = [0.25] * 10 NRowYs = [0.26, 0.32, 0.38, 0.44, 0.50, 0.56, 0.62, 0.68, 0.74, 0.80] SRow1Xs = [0.75] * 10 SRow2Xs = [0.85] * 10 SRowYs = [0.26, 0.32, 0.38, 0.44, 0.50, 0.56, 0.62, 0.68, 0.74, 0.80] return [[NRowYs, NRow1Xs, NRow2Xs, SRowYs, SRow1Xs], [SRow2Xs, Ncounts, Nverts, Scounts, Sverts]]
'figure.figsize': [figsize_x, figsize_y]} plt.rcParams.update(params) xmin = x.min() xmax = x.max() ymin = y.min() ymax = y.max() fig = plt.figure(figsize=(figsize_x, figsize_y)) fig.canvas.set_window_title(filename) #plt.subplots_adjust(hspace=0.5) plt.subplots_adjust(bottom=0.15, right=1.0, top=0.9) #plt.subplot(111) ax = fig.add_subplot(111, aspect='equal') plt.hexbin(x, y, gridsize=50, cmap=cm.jet, bins='log', C=w2) plt.grid(True) plt.xlim([-0.03, 0.03]) plt.ylim([-0.03, 0.03]) ax.set_xticks([-0.03, -0.015, 0.0, 0.015, 0.03]) ax.set_yticks([-0.03, -0.015, 0.0, 0.015, 0.03]) plt.xlabel(r'$\mathrm{Re}[M_p]$ (arb.)') plt.ylabel(r'$\mathrm{Im}[M_p]$ (arb.)') #plt.ylim([0.8, 4.2]) # #plt.axis([xmin, xmax, ymin, ymax]) # plt.subplot(212) # plt.hexbin(x, y, gridsize=50, cmap=cm.jet, bins='log') # #plt.axis([xmin, xmax, ymin, ymax]) # #plt.title("Hexagon binning with log color scale") # #cb = plt.colorbar()
from keras.models import load_model import pickle as pkl encoder = load_model('encoder.h5') x_load = open("test_x.pickle", "rb") x_test = pkl.load(x_load) y_load = open("test_y.pickle", "rb") y_test = pkl.load(y_load) x_test_encoded = encoder.predict(x_test, batch_size=100) # display a 2D plot of the policies in the latent space plt.figure(figsize=(6, 6)) plt.hexbin(x_test_encoded[:, 0], x_test_encoded[:, 1], C=y_test, cmap='rainbow', linewidths=0.2, gridsize=200) plt.colorbar() plt.ylim((-3, 3)) plt.xlim((-3, 3)) # plt.savefig('Failed_overlay.png', dpi=300) # plt.figure(figsize=(6, 6)) # plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test) # plt.colorbar() # plt. ylim((-3, 3)) # plt. xlim((-3, 3)) plt.show()
## some basic analysis nhist = 31; dist = w.calculateDistances(n = nhist); rots = w.calculateRotations(n = nhist); plt.figure(10); plt.clf(); for dd in range(10): plt.subplot(2,5,dd+1); #plt.plot(np.log(dd[1:,k]), ac[:,k] / ((k+1) * np.pi),'.'); d = 3 * dd; #plt.hexbin(np.log(dd[1:,k]), ac[:,k] / ((k+1) * np.pi)); #plt.hexbin(dist[1:,d], rots[:,d] / ((d + 1) * np.pi), bins = 'log'); #plt.hexbin(np.log(dist[1:,d]), rots[:,d] / ((d + 1) * np.pi), bins = 'log'); plt.hexbin(np.log(dist[1:,d]), rots[:,d] / ((d + 1) * np.pi)); #plt.xlabel('distance'); #plt.ylabel('rotation'); ## generat feature data from scipy.cluster.vq import whiten, kmeans, kmeans2 from scipy.cluster.hierarchy import linkage, dendrogram; feat = w.calculateFeatures(n = 30, features = ['rotations']); #feat = w.calculateFeatures(n = 20, features = ['rotations', 'distances', 'lengths']); #feat = w.calculateFeatures(n = 20, features = ['rotations']); #feat = feat[:,-5:]; feat.shape
''' import matplotlib.pyplot as plt import pandas as pd df = pd.read_csv('../datasets/auto-mpg.csv', usecols=['hp', 'mpg']) hp = df.hp.values mpg = df.mpg.values ''' INSTRUCTIONS * Generate a two-dimensional histogram with plt.hexbin() to view the joint variation of the mpg and hp vectors. * Put hp along the horizontal axis and mpg along the vertical axis. * Specify a hexagonal tesselation with 15 hexagons across the x-direction and 12 hexagons across the y-direction using gridsize. * Specify the rectangular region covered with the optional extent argument: use hp from 40 to 235 and mpg from 8 to 48. * Add a color bar to the histogram. ''' # Generate a 2d histogram with hexagonal bins plt.hexbin(hp, mpg, gridsize=(15, 12), extent=(40, 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('hexbin() plot') plt.show()
def plot_hex_and_violin(abscissa, ordinate, bin_edges, extent=None, xlabel="", ylabel="", zlabel="", do_hex=True, do_violin=True, cm=plt.cm.inferno, axis=None, v_padding=.015, **kwargs): """ takes two arrays of coordinates and creates a 2D hexbin plot and a violin plot (or just one of them) Parameters ---------- abscissa, ordinate : arrays the coordinates of the data to plot bin_edges : array bin edges along the abscissa extent : 4-tuple of floats (default: None) extension of the abscissa, ordinate; given as is to plt.hexbin xlabel, ylabel : strings (defaults: "") labels for the two axes of either plot zlabel : string (default: "") label for the colorbar of the hexbin plot do_hex, do_violin : bools (defaults: True) whether or not to do the respective plots cm : colour map (default: plt.cm.inferno) colour map to be used for the hexbin plot kwargs : args dictionary more arguments to be passed to plt.hexbin """ if axis: if do_hex and do_violin: from matplotlib.axes import Axes from matplotlib.transforms import Bbox axis_bbox = axis.get_position() axis.axis("off") else: plt.sca(axis) # make a normal 2D hexplot from the given data if do_hex: # if we do both plot types, if do_violin: if axis: ax_hex_pos = axis_bbox.get_points().copy( ) # [[x0, y0], [x1, y1]] ax_hex_pos[0, 1] += np.diff(ax_hex_pos, axis=0)[0, 1] * (.5 + v_padding) ax_hex = Axes(plt.gcf(), Bbox.from_extents(ax_hex_pos)) plt.gcf().add_axes(ax_hex) plt.sca(ax_hex) ax_hex.set_xticklabels([]) else: plt.subplot(211) plt.hexbin(abscissa, ordinate, gridsize=40, extent=extent, cmap=cm, **kwargs) cb = plt.colorbar() cb.set_label(zlabel) plt.xlabel(xlabel) plt.ylabel(ylabel) if extent: plt.xlim(extent[:2]) plt.ylim(extent[2:]) # prepare and draw the data for the violin plot if do_violin: # if we do both plot types, open a subplot if do_hex: if axis: ax_vio_pos = axis_bbox.get_points().copy( ) # [[x0, y0], [x1, y1]] ax_vio_pos[1, 1] -= np.diff(ax_vio_pos, axis=0)[0, 1] * (.5 + v_padding) ax_vio = Axes(plt.gcf(), Bbox.from_extents(ax_vio_pos)) plt.gcf().add_axes(ax_vio) plt.sca(ax_vio) else: plt.subplot(212) # to plot the violins, sort the ordinate values into a dictionary # the keys are the central values of the bins given by `bin_edges` val_vs_dep = {} bin_centres = (bin_edges[1:] + bin_edges[:-1]) / 2. for dep, val in zip(abscissa, ordinate): # get the bin number this event belongs into # outliers are put into the first and last bin accordingly ibin = np.clip( np.digitize(dep, bin_edges) - 1, 0, len(bin_centres) - 1) # the central value of the bin is the key for the dictionary if bin_centres[ibin] not in val_vs_dep: val_vs_dep[bin_centres[ibin]] = [val] else: val_vs_dep[bin_centres[ibin]] += [val] keys = [k[0] for k in sorted(val_vs_dep.items())] vals = [k[1] for k in sorted(val_vs_dep.items())] # calculate the widths of the violins as 90 % of the corresponding bin width widths = [] for cen, wid in zip(bin_centres, (bin_edges[1:] - bin_edges[:-1])): if cen in keys: widths.append(wid * .9) plt.violinplot(vals, keys, points=60, widths=widths, showextrema=False, showmedians=True) plt.xlabel(xlabel) plt.ylabel(ylabel) if extent: # adding a colour bar to the hexbin plot reduces its width by 1/5 # adjusting the extent of the violin plot to sync up with the hexbin plot plt.xlim([ extent[0], (5. * extent[1] - extent[0]) / 4. if do_hex else extent[1] ]) # for good measure also sync the vertical extent plt.ylim(extent[2:]) plt.grid()
def plot_embedding_features(data, meta, umap_f, fig_dir, min_neighbor=None, min_distance=None, labels_f=None, labels_to_keep=None, geno_col="Sample", max_samp=5000): "results/{norm}/{dim}_out/{min_neighbor}_{min_distance}/embedding_{sim}.p" #for curr_param in glob.glob(f"{umap_dir}/*p"): cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True) inp = pickle.load(open(umap_f, "rb")) if not len(inp) == 2: return curr_df = subset_on_clusters(data, meta, umap_f, labels_f=labels_f, labels_to_keep=labels_to_keep) # Add the well information curr_df["Well"] = curr_df.apply( lambda x: str(x["Stimuli"]) + "_" + str(x["Sample"]), axis=1) # Run on full data hex density of timepoints run_plot_hex_tp(curr_df, col="Well", vals=None, outdir=fig_dir, show_cbar=True) for i in data.columns.values: f = plt.figure() hb = plt.hexbin(x=curr_df["embedding_1"].values, y=curr_df["embedding_2"].values, cmap=cmap, C=curr_df[i].values.astype(float), reduce_C_function=np.median) cb = f.colorbar(hb, ax=plt.gca()) #plt.cm.YlOrRd_r helper_save(os.path.join(fig_dir, i + "_umap_hex_wells"), to_svg=False) plt.close() if min_neighbor is None or min_distance is None: neigh, dist = os.path.basename(fig_dir).split("_") else: neigh = min_neighbor dist = min_distance # Create figures color_labels = curr_df["Stimuli"].unique() rgb_values = (sns.color_palette("Set2", len(color_labels))) #color_map = dict(zip(color_labels, rgb_values)) if len(curr_df[geno_col].unique()) > 6: # Too many style markers to plot print("No style marker will be used") geno_col = None if curr_df.shape[0] > max_samp: curr_df = curr_df.sample(max_samp) # Create separate panel for each well run_plot_hex(curr_df, col="Well", vals=None, norm_color=False, show_cbar=False, outdir=fig_dir) run_plot(curr_df, col="Well", vals=None, outdir=fig_dir) for i in data.columns.values: plt.figure(figsize=(15, 15)) cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True) sns.scatterplot(data=curr_df, x="embedding_1", y="embedding_2", hue=i, palette=cmap) #, style=geno_col)#, #size=0.5) # curr_df.plot.scatter("embedding_1","embedding_2", hue=curr_df['Stimuli'])#.map(color_map),s=0.5) plt.legend(loc='upper right') plt.savefig(os.path.join(fig_dir, f"{i.replace(' ','')}.png")) plt.savefig(os.path.join(fig_dir, f"{i.replace(' ','')}.pdf")) plt.close() pallete = sns.color_palette("bright", len(color_labels)) plt.figure(figsize=(15, 15)) sns.scatterplot(data=curr_df, x="embedding_1", y="embedding_2", palette=pallete, hue='Stimuli', style=geno_col) #, size=0.5) plt.legend(loc='upper right') title = f"Neighbors={neigh}\nMinimum Distance={dist}\nN Samples = {len(curr_df)}" plt.title(title) plt.savefig(os.path.join(fig_dir, f"umap_stimuli.png")) plt.savefig(os.path.join(fig_dir, f"umap_stimuli.pdf")) plt.close() plt.figure(figsize=(15, 15)) sns.scatterplot(data=curr_df, x="embedding_1", y="embedding_2", hue='Timepoint', style=geno_col, size=0.5) plt.legend(loc='upper right') title = f"Neighbors={neigh}\nMinimum Distance={dist}\nN Samples = {len(curr_df)}" plt.title(title) plt.savefig(os.path.join(fig_dir, f"umap_stimuli_tp.png")) plt.savefig(os.path.join(fig_dir, f"umap_stimuli_tp.pdf")) plt.close() return
def plot_conditional(self, dim1, dim2, grid_points = 40, **kwargs): values = self.analyser.get_equal_weighted_posterior() return plt.hexbin(x=values[:,dim1], y=values[:,dim2], gridsize=grid_points, reduce_C_function=numpy.nansum, **kwargs)
X, Y = np.meshgrid(u, v) Z = np.sin(3 * np.sqrt(X**2 + Y**2)) plt.set_cmap('gray') ## not 'grey'! plt.pcolor(Z) plt.set_cmap('viridis') plt.contour(X, Y, Z) #%% # 2d histograms plt.hist2d('total_bill', 'tip', data=tips, bins=(20, 10)) plt.colorbar() plt.hexbin('total_bill', 'tip', data=tips, gridsize=(20, 10)) plt.colorbar() #%% # covariance / correlation w/ heatmap covIris = iris.cov() print(covIris) plt.figure(figsize=(12, 8)) sns.heatmap(covIris) covTips = tips.cov() print(covTips) plt.figure(figsize=(12, 8))
# To fix this, we can cut off all the results with cost > 0.65, which creates this much more interpretable result: # In[ ]: z2 = np.minimum(z, 0.65) # In[ ]: import matplotlib.pyplot as plt get_ipython().magic(u'matplotlib inline') # In[ ]: hb = plt.hexbin(x, y, C=z2, gridsize=30, cmap='jet') plt.colorbar(hb) plt.xlabel("Collaborative") plt.ylabel("Locale") plt.show() # The optimum seems to be around (1.3, 1.8). # In[ ]: list(product(locale, collaborative))[1995] # Next, we can try to find the best argument for the similarity # recommender when fixing the other weights.
# In[6]: # Function for computing the absolute magnitude def abs_mag(parallax,apparent_mag): return apparent_mag+5*np.log10(np.abs(parallax))-10 # 3. Plot the scatter plot for Gaia Abs Mag vs B_V # In[8]: # Compute the absolute magnitude for Gaia abs_mag_gaia = abs_mag(gaiaplx,ap_gmag) # Plot the plt.hexbin(hip_bv, abs_mag_gaia, gridsize = 300,cmap=cm.jet, bins='log') #plt.scatter(hip_bv,abs_mag_gaia, c=heatmap , s=10, cmap=cm.hot , lw=0) plt.gca().invert_yaxis() plt.colorbar() plt.show() # 4. Plot the scatter plot for Hipparcos Abs Mag vs B_V # In[9]: # Compute the absolute magnitude for Hipparcos abs_mag_hip = abs_mag(hipplx,ap_gmag) # Plot the plt.hexbin(hip_bv, abs_mag_hip, gridsize = 300,cmap=cm.jet, bins='log')
x = y = NP.linspace(-5, 5, 100) X, Y = NP.meshgrid(x, y) Z1 = ML.bivariate_normal(X, Y, 2, 2, 0, 0) Z2 = ML.bivariate_normal(X, Y, 4, 1, 1, 1) ZD = Z2 - Z1 x = X.ravel() y = Y.ravel() z = ZD.ravel() gridsize=30 PLT.subplot(111) # if 'bins=None', then color of each hexagon corresponds directly to its count # 'C' is optional--it maps values to x-y coordinates; if 'C' is None (default) then # the result is a pure 2D histogram PLT.hexbin(x, y, C=z, gridsize=gridsize, cmap=CM.jet, bins=None) PLT.axis([x.min(), x.max(), y.min(), y.max()]) cb = PLT.colorbar() cb.set_label('mean value') PLT.show() # In[28]: import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import matplotlib.pyplot as plt H = np.ones((4, 4)).cumsum().reshape(4, 4) print H[::-1] # This shows the bin content in the order as plotted
def make2D_KDE(X, n_samp = 1e5, bandwidth = None, n_folds = 3, bw_train_size = 1000, bw_range_size = 20, doplot = True): """ Make a 2D Kernel Density Estimation and draw a n_samp number of samples from it best bandwidth obtained from previous runs bandwidth = 0.0546938775510204 bandwidth = 0.05894736842105264 Input: X (2D numpy array): the training data, consisting of the Y - J and J - H colours.\n n_samp (int): the number of samples to draw from the KDE. Default = 100000.\n bandwidth (float): the bandwidth to use for the KDE from which the samples will be drawn. Set to None to let the script find the best bandwidth. Default = None.\n n_folds (int): the number of folds to use when determining the bandwidth.\n bw_train_size (int): size of the training set that will be used to determine the best bandwidth. Default = 1000.\n bw_range_size (int); the amount of bandwidths to try out in the interval 0.04 to 0.1. Default = 20.\n doplot (boolean): whether to make a hex-bin plot of the drawn samples or not. Default = True. Output: samples (2D numpy array): the samples drawn from the KDE. """ import matplotlib.pyplot as plt import seaborn as sns from sklearn.neighbors import KernelDensity from sklearn.model_selection import KFold from matplotlib import rcParams rcParams['font.family'] = 'Latin Modern Roman' from matplotlib.colors import LogNorm #shuffle the data np.random.shuffle(X) #determine the best bandwidth if it is not provided if bandwidth == None: #first we find the optimum bandwidth kf = KFold(n_splits = n_folds) #range of bandwidths to try bwrange = np.linspace(0.02, 0.08, bw_range_size) #the array which will store the likelyhood likelyhood = np.zeros(len(bwrange)) print('Finding the best bandwidth...') for bw, i in zip(bwrange, np.arange(len(bwrange))): print('Iteration {0}, bandwidth {1}'.format(i, bw)) lh = [] #split the data into a train and test set using only the first 1000 samples for train_i, test_i in kf.split(X[:,:bw_train_size]): Xtrain, Xtest = X[train_i], X[test_i] kde = KernelDensity(bandwidth = bw, kernel = 'gaussian').fit(Xtrain) lhscore = kde.score(Xtest) lh = np.append(lh, lhscore) print('Bandwidth: {0}, score: {1}'.format(bw, lhscore)) likelyhood[i] = np.mean(lh) plt.plot(bwrange, likelyhood) plt.xlabel('Bandwidth') plt.ylabel('Likelyhood') plt.title('KDE likelyhood for different bandwidths') plt.savefig('2D_KDE_likelyhood_run4.png', dpi = 300) plt.close() #find the bandwidth which gave the highest likelyhood bandwidth = bwrange[np.argmax(likelyhood)] print('Best bandwidth: {0}'.format(bandwidth)) kde = KernelDensity(bandwidth = bandwidth, kernel = 'gaussian').fit(X) #pull samples from the kde samples = kde.sample(int(n_samp)) #plot the samples in a hexbin plot if doplot: plt.hexbin(samples[:, 0], samples[:, 1], bins = 'log', cmap = 'Reds') plt.colorbar(label = 'Density of samples [logarithmic]') plt.xlabel('Y - J') plt.ylabel('J - H') plt.title('Distribution of samples in (Y-J, J-H) colour space') plt.savefig('Samples_distribution_hex.pdf', dpi = 300) plt.show() return samples
######################################## plt.figure(200) plt.subplot(131) alpha = np.linspace(0, 2*np.pi, num=1000) R1 = 0.9 b_x = R1*np.cos(alpha) b_y = R1*np.sin(alpha) plt.plot(b_x, b_y, 'r') R2 = 1.1 b_x = R2*np.cos(alpha) b_y = R2*np.sin(alpha) plt.plot(b_x, b_y, 'r') # plt.hexbin(xx_be, yy_be, bins='log', cmap=plt.cm.Reds, alpha = 0.3) #plt.plot(xx_be, yy_be, '.b') plt.axis([-1.2, 1.2, -1.2, 1.2]) plt.axis('equal') # plt.subplot(132) R1 = 0.9 b_x = R1*np.cos(alpha) b_y = R1*np.sin(alpha) plt.plot(b_x, b_y, 'r') R2 = 1.1 b_x = R2*np.cos(alpha) b_y = R2*np.sin(alpha) plt.plot(b_x, b_y, 'r') # plt.hexbin(xx_du, yy_du, bins='log', cmap=plt.cm.Reds, alpha = 0.3)
print(GlobClust_Log_RA[i]) print(GlobClust_Log_DEC[i]) globquery = """select ra, dec, l, b, pmra, pmdec, phot_g_mean_mag, phot_rp_mean_mag, phot_bp_mean_mag from gaiadr2.gaia_source where abs(ra-%f)<%f and abs(dec-(%f))<%f""" % ( coord_ra_dec.ra.deg[i], GlobClust_Log_r_t[i] / 60 * 4, coord_ra_dec.dec.deg[i], GlobClust_Log_r_t[i] / 60 * 4) out = query.query(globquery, local=True, timeit=True) df_t = pd.DataFrame(np.array(out)) df_t.to_csv('out_' + GlobClust_Log_ID[i] + '_full_4-rt_ra_dec.csv', index=True, header=True) fig = plt.figure(figsize=(16, 12)) plt.hexbin(x=out['ra'], y=out['dec'], cmap='plasma', bins='log') plt.xlabel('ra (deg)', fontsize=15) plt.ylabel('dec (deg)', fontsize=15) plt.title('Window (4*r_t):' + 'ra (deg) vs dec (deg) of ' + GlobClust_Log_ID[i], fontsize=15) cb = plt.colorbar() cb.ax.tick_params(labelsize=15) cb.set_label(r'$\mathrm{pmra}$', fontsize=15) plt.gca().tick_params(labelsize=15) plt.show() fig.savefig('GC_' + GlobClust_Log_ID[i] + '_full_pmra_4-rt_ra_dec.pdf') fig = plt.figure(figsize=(16, 12)) plt.scatter(x=out['ra'], y=out['dec'], c=out['pmdec'], cmap='inferno') plt.xlabel('ra (deg)', fontsize=15)
def hist(lonp, latp, fname, tind='final', which='contour', vmax=None, fig=None, ax=None, bins=(40, 40), N=10, grid=None, xlims=None, ylims=None, C=None, Title=None, weights=None, Label='Final drifter location (%)', isll=True, binscale=None): """ Plot histogram of given track data at time index tind. Args: lonp,latp: Drifter track positions in lon/lat [time x ndrifters] fname: Plot name to save tind (Optional): Default is 'final', in which case the final position of each drifter in the array is found and plotted. Alternatively, a time index can be input and drifters at that time will be plotted. Note that once drifters hit the outer numerical boundary, they are nan'ed out so this may miss some drifters. which (Optional[str]): 'contour', 'pcolor', 'hexbin', 'hist2d' for type of plot used. Default 'hexbin'. bins (Optional): Number of bins used in histogram. Default (15,25). N (Optional[int]): Number of contours to make. Default 10. grid (Optional): grid as read in by inout.readgrid() xlims (Optional): value limits on the x axis ylims (Optional): value limits on the y axis isll: Default True. Inputs are in lon/lat. If False, assume they are in projected coords. Note: Currently assuming we are plotting the final location of each drifter regardless of tind. """ if grid is None: loc = 'http://barataria.tamu.edu:8080/thredds/dodsC/NcML/txla_nesting6.nc' grid = inout.readgrid(loc) if isll: # if inputs are in lon/lat, change to projected x/y # Change positions from lon/lat to x/y xp, yp = grid.proj(lonp, latp) # Need to retain nan's since basemap changes them to values ind = np.isnan(lonp) xp[ind] = np.nan yp[ind] = np.nan else: xp = lonp yp = latp if fig is None: fig = plt.figure(figsize=(11, 10)) else: fig = fig background(grid) # Plot coastline and such if tind == 'final': # Find final positions of drifters xpc, ypc = tools.find_final(xp, yp) elif isinstance(tind, int): xpc = xp[:, tind] ypc = yp[:, tind] else: # just plot what is input if some other string xpc = xp.flatten() ypc = yp.flatten() if which == 'contour': # Info for 2d histogram H, xedges, yedges = np.histogram2d(xpc, ypc, range=[[grid.x_rho.min(), grid.x_rho.max()], [grid.y_rho.min(), grid.y_rho.max()]], bins=bins) # Contour Plot XE, YE = np.meshgrid(op.resize(xedges, 0), op.resize(yedges, 0)) d = (H/H.sum())*100 # # from http://matplotlib.1069221.n5.nabble.com/question-about-contours-and-clim-td21111.html # locator = ticker.MaxNLocator(50) # if you want no more than 10 contours # locator.create_dummy_axis() # locator.set_bounds(0,1)#d.min(),d.max()) # levs = locator() con = fig.contourf(XE, YE, d.T, N) # ,levels=levs)#(0,15,30,45,60,75,90,105,120)) con.set_cmap('YlOrRd') if Title is not None: plt.set_title(Title) # Horizontal colorbar below plot cax = fig.add_axes([0.3725, 0.25, 0.48, 0.02]) # colorbar axes cb = fig.colorbar(con, cax=cax, orientation='horizontal') cb.set_label('Final drifter location (percent)') # Save figure into a local directory called figures. Make directory # if it doesn't exist. if not os.path.exists('figures'): os.makedirs('figures') fig.savefig('figures/' + fname + 'histcon.png', bbox_inches='tight') elif which == 'pcolor': # Info for 2d histogram H, xedges, yedges = np.histogram2d(xpc, ypc, range=[[grid.x_rho.min(), grid.x_rho.max()], [grid.y_rho.min(), grid.y_rho.max()]], bins=bins, weights=weights) # Pcolor plot # C is the z value plotted, and is normalized by the total number of # drifters if C is None: C = (H.T/H.sum())*100 else: # or, provide some other weighting C = (H.T/C)*100 p = plt.pcolor(xedges, yedges, C, cmap='YlOrRd') if Title is not None: plt.set_title(Title) # Set x and y limits if xlims is not None: plt.xlim(xlims) if ylims is not None: plt.ylim(ylims) # Horizontal colorbar below plot cax = fig.add_axes([0.3775, 0.25, 0.48, 0.02]) # colorbar axes cb = fig.colorbar(p, cax=cax, orientation='horizontal') cb.set_label('Final drifter location (percent)') # Save figure into a local directory called figures. Make directory # if it doesn't exist. if not os.path.exists('figures'): os.makedirs('figures') fig.savefig('figures/' + fname + 'histpcolor.png', bbox_inches='tight') # savefig('figures/' + fname + 'histpcolor.pdf',bbox_inches='tight') elif which == 'hexbin': if ax is None: ax = plt.gca() else: ax = ax if C is None: # C with the reduce_C_function as sum is what makes it a percent C = np.ones(len(xpc))*(1./len(xpc))*100 else: C = C*np.ones(len(xpc))*100 hb = plt.hexbin(xpc, ypc, C=C, cmap='YlOrRd', gridsize=bins[0], extent=(grid.x_psi.min(), grid.x_psi.max(), grid.y_psi.min(), grid.y_psi.max()), reduce_C_function=sum, vmax=vmax, axes=ax, bins=binscale) # Set x and y limits if xlims is not None: plt.xlim(xlims) if ylims is not None: plt.ylim(ylims) if Title is not None: ax.set_title(Title) # Want colorbar at the given location relative to axis so this works # regardless of # of subplots, so convert from axis to figure # coordinates. To do this, first convert from axis to display coords # transformations: # http://matplotlib.org/users/transforms_tutorial.html # axis: [x_left, y_bottom, width, height] ax_coords = [0.35, 0.25, 0.6, 0.02] # display: [x_left,y_bottom,x_right,y_top] disp_coords = ax.transAxes.transform([(ax_coords[0], ax_coords[1]), (ax_coords[0]+ax_coords[2], ax_coords[1]+ax_coords[3])]) # inverter object to go from display coords to figure coords inv = fig.transFigure.inverted() # figure: [x_left,y_bottom,x_right,y_top] fig_coords = inv.transform(disp_coords) # actual desired figure coords. figure: # [x_left, y_bottom, width, height] fig_coords = [fig_coords[0, 0], fig_coords[0, 1], fig_coords[1, 0] - fig_coords[0, 0], fig_coords[1, 1] - fig_coords[0, 1]] # Inlaid colorbar cax = fig.add_axes(fig_coords) # # Horizontal colorbar below plot # cax = fig.add_axes([0.3775, 0.25, 0.48, 0.02]) # colorbar axes cb = fig.colorbar(hb, cax=cax, orientation='horizontal') cb.set_label(Label) # Save figure into a local directory called figures. Make directory # if it doesn't exist. if not os.path.exists('figures'): os.makedirs('figures') fig.savefig('figures/' + fname + 'histhexbin.png', bbox_inches='tight') # savefig('figures/' + fname + 'histhexbin.pdf',bbox_inches='tight') elif which == 'hist2d': plt.hist2d(xpc, ypc, bins=40, range=[[grid.x_rho.min(), grid.x_rho.max()], [grid.y_rho.min(), grid.y_rho.max()]], normed=True) plt.set_cmap('YlOrRd') # Set x and y limits if xlims is not None: xlim(xlims) if ylims is not None: ylim(ylims) # Horizontal colorbar below plot cax = fig.add_axes([0.3775, 0.25, 0.48, 0.02]) # colorbar axes cb = fig.colorbar(cax=cax, orientation='horizontal') cb.set_label('Final drifter location (percent)') # Save figure into a local directory called figures. Make directory # if it doesn't exist. if not os.path.exists('figures'): os.makedirs('figures') fig.savefig('figures/' + fname + 'hist2d.png', bbox_inches='tight')
D, _ = datasets.make_classification( n_samples=200, n_features=2, n_informative=2, n_redundant=0, n_classes=3, n_clusters_per_class=1, class_sep=2 ) x, y = D[:,0], D[:,1] plt.scatter(x, y) plt.show() # Display the histograms to get some ideas about the density of the given data nbins=50 plt.hexbin(x, y, gridsize=nbins) plt.show() plt.hist2d(x, y, bins=nbins) plt.show() # 1.a Perform density estimation using Kernel Density Estimator (KDE) with Gaussian kernel # Create a KED model with scipy """Note: Bandwidth selection strongly influences the estimate obtained from the KDE. scipy includes automatic bandwidth determination. The estimation works best for a unimodal distribution; bimodal or multi-modal distributions tend to be oversmoothed. For the details of bandwidth selection in scipy, we refer to: https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.stats.gaussian_kde.html
plt.plot(x_line3,y_line1,lw=4.5,color='white') plt.plot(x_line4,y_line2,lw=4.5,color='white') plt.plot(x_line1,y_line3,lw=4.5,color='white') plt.plot(x_line2,y_line4,lw=4.5,color='white') plt.plot(x_line3,y_line1,lw=1.5,color='black') plt.plot(x_line4,y_line2,lw=1.5,color='black') plt.plot(x_line1,y_line3,lw=1.5,color='black') plt.plot(x_line2,y_line4,lw=1.5,color='black') cmap = 'coolwarm_r' #plt.scatter(x_dec_eligible,y_dec_eligible,s=0.05,label="color-selected") #plt.hexbin(x_dec_eligible, y_dec_eligible, C=np.abs(sub_SFR_eligible/np.mean(sub_SFR_eligible)), gridsize=50, bins='log', cmap='Greys') if mode == "SFR": #plt.hexbin(x_dec_eligible, y_dec_eligible, C=(sub_SFR_eligible/np.mean(sub_SFR_eligible)), gridsize=50, bins='log', cmap='Greys')#'inferno')#'Greys') plt.hexbin(x_dec_eligible, y_dec_eligible, C=(sub_SFR_eligible), gridsize=50, bins='log', cmap=cmap) cb = plt.colorbar() cb.set_label(r"${\rm SFR} \ [M_{\odot}{\rm yr}^{-1}]$") elif mode == "sSFR": #og plt.hexbin(x_dec_eligible, y_dec_eligible, C=(sub_sSFR_eligible/np.mean(sub_sSFR_eligible)), gridsize=50, bins='log', vmin = 0., vmax = 0.8, cmap=cmap) # TESTING #plt.hexbin(x_dec_eligible, y_dec_eligible, C=(sub_sSFR_eligible), gridsize=50, bins='log', cmap=cmap) cb = plt.colorbar() oldlabels = cb.ax.get_yticklabels() print("mean sSFR = ",np.mean(np.log10(sub_sSFR_eligible[sub_sSFR_eligible > 0.]))) print("mean sSFR = ",np.mean((sub_sSFR_eligible))) y_new = np.linspace(0.,5.e-10,11)
y = t['catsh_SubhaloSFR'][:] y[y < 1e-13] = 1e-3 x = t['scalar_star_mass'][:] color = t['scalar_star_v_sigma'][:] index = np.argwhere(x > 10e10) q = np.array([]) w = np.array([]) for i in range(len(index)): q = np.append(q, x[index[i]]) w = np.append(w, y[index[i]]) plt.figure(figsize=(10, 10)) plt.hexbin(q, w, C=color, xscale='log', yscale='log', gridsize=50, cmap='rainbow') #sns.kdeplot(q, w, color='black', n_levels=5) plt.axis([10e10, 10e12, 1e-3, 100]) plt.colorbar() plt.xlabel("log M★[M☉]", fontsize=16) plt.ylabel("log SFR[M☉ yr-1]", fontsize=16) plt.xticks(fontsize=16) plt.yticks(fontsize=16) plt.title('Star formation rate versus M* color coded by v/sigma', fontsize=16) plt.show()
xpts_sections, ypts_sections = mapProj(pd_data_leads_all.lons.values, pd_data_leads_all.lats.values) if (hem == 'nh'): fig, axs = plt.subplots(nrows=1, ncols=4, figsize=(11, 5)) plt.subplots_adjust(bottom=0.01, left=0.02, top=0.95, right=0.98) for i in range(len(data)): ax = axs.flatten()[i] plt.sca(ax) im1 = plt.hexbin(xpts_sections, ypts_sections, C=data[i], vmin=minval[i], vmax=maxval[i], gridsize=2000, cmap=cmaps[i], zorder=2, rasterized=True) mapProj.drawcoastlines(linewidth=0.25, zorder=5) mapProj.drawparallels(np.arange(90, -90, -10), linewidth=0.25, zorder=10) mapProj.drawmeridians(np.arange(-180., 180., 30.), latmax=80, linewidth=0.25, zorder=10) mapProj.fillcontinents(color='0.95', lake_color='0.9', zorder=3)
plt.show() # Plot stddev by intensity hexbin_grid = int(1.0 / np.sqrt(2) * nbins) lsp_list = -2.5 * np.log10(np.array(intensity_list)) fwhm_list = np.array(fwhm_list) # Compute plot limits x_min = np.percentile(lsp_list[~np.isnan(lsp_list)], 0.5) x_max = np.percentile(lsp_list[~np.isnan(lsp_list)], 99.5) y_min = np.percentile(fwhm_list[~np.isnan(fwhm_list)], 0.5) y_max = np.percentile(fwhm_list[~np.isnan(fwhm_list)], 99.5) plt.hexbin(lsp_list, fwhm_list, gridsize=(hexbin_grid, hexbin_grid), extent=(x_min, x_max, \ y_min, y_max)) plt.xlabel('Uncalibrated magnitude') plt.ylabel('PSF FWHM') plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.gca().invert_xaxis() plt.savefig(os.path.join(ff_dir, 'PSF_FWHM_vs_mag.png'), dpi=300) plt.show() # Plot stddev by X and Y fig, (ax1, ax2) = plt.subplots(nrows=2)