def boxplot(data, segments, outdir, filename_base, show_minmax=False, **kwargs): fig = plt.figure(figsize=(2.5, 3)) fig.patch.set_facecolor('white') celldata = [] minv = [] maxv = [] for segment in segments: celldata.append(data[segment]) if len(data[segment]) > 0: minv.append(min(data[segment])) maxv.append(max(data[segment])) plt.boxplot(celldata, positions=range(len(segments)), **kwargs) if show_minmax and len(minv)>0 and len(maxv)>0: for idx in range(len(minv)): w = 0.1 plt.plot([idx-w, idx+w], [minv[idx]]*2, 'k-') plt.plot([idx-w, idx+w], [maxv[idx]]*2, 'k-') plt.margins(0.1, 0.1) ax = plt.gca() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_major_formatter(ticker.FuncFormatter(simplified_SI_format)) plt.tick_params(axis='y', which='major', labelsize='x-small') plt.tick_params(axis='y', which='minor', labelsize='xx-small') plt.savefig("%s/%s.pdf" % (outdir, filename_base), bbox_inches='tight') plt.savefig("%s/%s.png" % (outdir, filename_base), bbox_inches='tight') # free memory plt.close() # closes current figure gc.collect()
def plot_image(data): plt.imshow(data, interpolation='nearest', cmap=plt.gray()) plt.axis('off') plt.margins(0, 0, tight=True) plt.show() filename = "images/" + DATA_DIR.split("-")[-1] matplotlib.image.imsave(filename, data)
def exercise_2b(): X, y = make_blobs(n_samples=1000,centers=50, n_features=2, random_state=0) kf = ShuffleSplit(100, train_size= 0.9, test_size=0.1, random_state=0) # kf = KFold(1000, n_folds=10, shuffle=False, random_state=None) accuracy_lst = np.zeros([49, 2], dtype=float) accuracy_current = np.zeros(10, dtype=float) for k in range(1,50): iterator = 0 for train_index, test_index in kf: X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] clf = KNeighborsClassifier(n_neighbors=k) clf.fit(X_train, y_train) accuracy_current[iterator] = (1. - clf.score(X_test,y_test)) iterator+=1 print mean_squared_error(y_test, clf.predict(X_test)) accuracy_lst[k-1, 0] = accuracy_current.mean() accuracy_lst[k-1, 1] = accuracy_current.var()#*2 #confidence interval 95% x = np.arange(1,50, dtype=int) plt.style.use('ggplot') plt.plot(x, accuracy_lst[:, 1], '#009999', marker='o') # plt.errorbar(x, accuracy_lst[:, 0], accuracy_lst[:, 1], linestyle='None', marker='^') plt.xticks(x, x) plt.margins(0.02) plt.xlabel('K') plt.ylabel('Variance') plt.show()
def plot_teh(damage,title,subplot): plt.subplot(subplot) h5 = read_csv(5,damage) h4 = read_csv(4,damage) h3 = read_csv(3,damage) h2 = read_csv(2,damage) h1 = read_csv(1,damage) xlist = [z for z in range(len(h5))] ph5 = plt.plot(xlist,h5,'-o',label='h5',marker = 'v',markersize=8,color='blue') ph4 = plt.plot(xlist,h4,'-o',label='h4',marker = 'o',markersize=8,color='green') ph3 = plt.plot(xlist,h3,'-o',label='h3',marker = 'D',markersize=8,color='orange') ph2 = plt.plot(xlist,h2,'-o',label='h2',marker = '*',markersize=8,color='red') ph1 = plt.plot(xlist,h1,'-o',label='h1',marker = 's',markersize=8,color='black') #plt.legend(loc='upper left', handlelength=5, borderpad=1.2, labelspacing=1.2) #plt.legend() plt.legend(loc='upper right', fontsize = 'xx-large') plt.tick_params(axis='both',which='major', labelsize='large') plt.title(title,size = 'xx-large',weight='bold') plt.xlabel('Rank',size='xx-large',weight='bold') plt.ylabel('Transfer Entropy',size='xx-large',weight='bold') sns.axes_style("darkgrid", {"axes.facecolor": ".9"}) plt.ylim(ymin=0.0,ymax = 1.0) plt.xlim(xmin=0.0,xmax = len(h5)) plt.margins(0.2) plt.tight_layout(pad=2.5) return
def gen_graph(kind, xvals, yvals, xlabel, ylabel): if len(xvals) > 10: xvals = xvals[:10] if len(yvals) > 10: yvals = yvals[:10] fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ind = np.arange(len(yvals)) if kind == 'line': ax.plot(ind, yvals[::-1]) elif kind == 'bar': ax.bar(ind, yvals[::-1]) plt.xticks(ind + .3 / 2, xvals[::-1]) plt.xlabel(xlabel, labelpad=20) plt.ylabel(ylabel) plt.margins(xmargin=0.05) plt.xticks(range(0, len(xvals))) plt.subplots_adjust(bottom=0.3, right=0.9) ax.set_xticklabels(xvals[::-1], rotation=45) io = StringIO() fig.savefig(io, format='png') graph = io.getvalue().encode('base64') return graph
def exercise_1(): X, y = make_blobs(n_samples=1000,centers=50, n_features=2, random_state=0) n_samples = len(X) kf = cross_validation.KFold(n_samples, n_folds=10, shuffle=False, random_state=None) # kf = cross_validation.ShuffleSplit(1000,n_iter=25, test_size=0.1, train_size=0.9, random_state=None) error_total = np.zeros([49, 1], dtype=float) for k in range(1,50): error = [] clf = KNeighborsClassifier(n_neighbors=k) for train_index, test_index in kf: X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] clf.fit(X_train, y_train) error.append( zero_one_loss(y_test, clf.predict(X_test)) ) # error.append(clf.predict(X_test)) # error.append( 1. - clf.score(X_test, y_test) ) #, accuracy_score(y_test, clf.predict(X_test)) # error.append(mean_squared_error(y_test, clf.predict(X_test))) # error.append() # print error error_total[k-1, 0] = np.array(error).mean() # print error_total x = np.arange(1,50, dtype=int) plt.style.use('ggplot') plt.plot(x, error_total[:, 0], '#009999', marker='o') # plt.errorbar(x, accuracy_lst[:, 0], accuracy_lst[:, 1], linestyle='None', marker='^') plt.xticks(x, x) plt.margins(0.02) plt.xlabel('K values') plt.ylabel('Missclasification Error') plt.show()
def exercise_2a(): X, y = make_blobs(n_samples=1000,centers=50, n_features=2, random_state=0) # plt.scatter(X[:, 0], X[:, 1], marker='o', c=y) # plt.show() kf = KFold(1000, n_folds=10, shuffle=False, random_state=None) accuracy_lst = np.zeros([49, 2], dtype=float) accuracy_current = np.zeros(10, dtype=float) for k in range(1,50): iterator = 0 clf = KNeighborsClassifier(n_neighbors=k) for train_index, test_index in kf: X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] clf.fit(X_train, y_train) accuracy_current[iterator] = (1. - clf.score(X_test,y_test)) iterator+=1 accuracy_lst[k-1, 0] = accuracy_current.mean() # accuracy_lst[k-1, 1] = accuracy_current.std() #confidence interval 95% x = np.arange(1,50, dtype=int) plt.style.use('ggplot') plt.plot(x, accuracy_lst[:, 0], '#009999', marker='o') # plt.errorbar(x, accuracy_lst[:, 0], accuracy_lst[:, 1], linestyle='None', marker='^') plt.xticks(x, x) plt.margins(0.02) plt.xlabel('K values') plt.ylabel('Missclasification Error') plt.show()
def plot_TE_scale(dictO, result_file_name, viz_file_name): dictA = copy.deepcopy(dictO) #print "DDDDDDD" xlist = [x for x in range(len(dictA.keys()))] #list_node_names = list(dicA.keys()) dictA_values = list(dictA.values()) sorted_dicA_values = sorted(dictA_values) sorted_dicA_values.reverse() ylist = sorted_dicA_values axis_labels = [] for u in ylist: for i, j in dictA.iteritems(): if j == u: axis_labels.append(i) del dictA[i] break result_file = open(result_file_name, 'w') for i in range(len(xlist)): result_file.write('%s\t%f\n'%(axis_labels[i], ylist[i])) plt.figure(figsize=(12,8)) plt.plot(xlist, ylist, '-o') #plt.xticks(xlist, axis_labels, rotation='vertical') plt.grid() plt.margins(0.2) # Tweak spacing to prevent clipping of tick-labels plt.subplots_adjust(bottom=0.25) plt.savefig(viz_file_name)
def make_plot(counts): """ Plot the counts for the positive and negative words for each timestep. Use plt.show() so that the plot will popup. """ positives=[] negatives=[] for i in counts: for j in i: if j[0]=="positive": positives.append(j[1]) else: negatives.append(j[1]) x_ticks=range(0,12) lineP=plt.plot(positives) plt.setp(lineP, color='b', marker='o', label="positive") lineN=plt.plot(negatives) plt.setp(lineN, color='g', marker='o', label="negative") plt.margins(0.1) plt.ylabel("Word count") plt.xlabel("Time step") plt.xticks(x_ticks) plt.legend(loc=0) plt.show()
def plot_from_avgs(avgs_for_experiments): # print "avgs:", avgs_for_experiments plt.rcParams.update({'font.size': 25}) plt.ylabel('Average goodness of fit') # plt.xlabel('DG-weighting') plt.title('Average goodness of fit by set size and consolidation scheme') x = [2, 3, 4, 5] p2 = plt.plot(x, avgs_for_experiments[0], marker='o', linestyle='--', linewidth=3.0) p6 = plt.plot(x, avgs_for_experiments[4], marker='o', linestyle='--', linewidth=3.0) p3 = plt.plot(x, avgs_for_experiments[1], marker='o', linestyle='--', linewidth=3.0) p7 = plt.plot(x, avgs_for_experiments[5], marker='o', linestyle='--', linewidth=3.0) p4 = plt.plot(x, avgs_for_experiments[2], marker='o', linestyle='--', linewidth=3.0) p9 = plt.plot(x, avgs_for_experiments[6], marker='o', linestyle='--', linewidth=3.0) p5 = plt.plot(x, avgs_for_experiments[3], marker='o', linestyle='--', linewidth=3.0) p10 = plt.plot(x, avgs_for_experiments[7], marker='o', linestyle='--', linewidth=3.0) p8 = plt.plot(x, BaselineAvgs.avgs, marker='^', linestyle='-', color='black', linewidth=3.0) plt.xticks(range(2,6), ['2x5', '3x5', '4x5', '5x5']) plt.xlabel('Set size') plt.legend((p2[0], p6[0], p3[0], p7[0], p4[0], p9[0], p5[0], p10[0], p8[0]), ('(15) Sync 15 iters', '(200) Sync 15 iters', '(15) Sync 50 iters', '(200) Sync 50 iters', '(15) Async 15 iters, tm=0', '(200) Async 15 iters, tm=0', '(15) Async 15 iters, tm=1', '(200) Async 15 iters, tm=1', '(15) Baseline averages'), bbox_to_anchor=(1, 1.0), ncol=1, fancybox=True, shadow=True) plt.margins(0.14) plt.grid(True) plt.show()
def calculate_write_data_rate(loop_time): f = open('create_rand_data.txt', 'a+'); block_size_list = []; i = 128; while i < (3*1024*1024): block_size_list.append(i); i = i*2; fastest_block_index_count = len(block_size_list)*[0]; for j in range(loop_time): #block_size_list = [100, 1000, 4000, 10000, 20000, 40000, 60000, 80000, 100000, 150000, 200000, 500000, 1000000, 2000000, 3000000] data_rates = [] max_rate = 0; max_block_index = 0; file_size = 30*1024*1024; #30mb for i in range(len(block_size_list)): fname = "test" + str(i+1) + ".txt"; out = subprocess.check_output(["./create_random_file", fname, str(file_size), str(block_size_list[i])]); data = out.split(":"); time_used = int(data[1].strip()[:-1]); data_rate = (file_size / time_used); data_rates.append(data_rate); if data_rate > max_rate: max_rate = data_rate; max_block_index = i; #print('writing' + str(i+1)); f.write('Block size:\n'); f.write(str(block_size_list)+'\n'); f.write('Data rate:\n'); f.write(str(data_rates)+'\n'); max_block_size = block_size_list[max_block_index]; single_info = "max rate: "+str(max_rate)+", "+"optimal_block_size: "+str(max_block_size)+"\n\n"; f.write(single_info); plt.figure(j+1); plt.plot(block_size_list, data_rates, linestyle='-', marker='o', color='b',linewidth=2.0); plt.ylabel("data rate (b/ms)"); plt.xlabel("block size (b)"); label_text = "max:" + str(max_rate) + " " + "block:" + str(max_block_size); plt.annotate(label_text, xy=(max_block_size, max_rate), xytext=(max_block_size+20000, max_rate+20000), arrowprops=dict(facecolor='black', shrink=0.05)); plt.ylim(0,200000); plt.xscale('log'); plt.xticks(block_size_list); plt.axes().get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()); plt.axes().tick_params(axis='x', labelsize=13); plt.xticks(rotation=30); plt.margins(x=0); save_fig_name = 'create_plot_' + str(j+1) + '.png'; plt.savefig(save_fig_name, bbox_inches="tight"); print(str(j+1)+"th iteration ends"); fastest_block_index_count[max_block_index] += 1; max_count_index = 0; optimal_index = 0; for x in range(len(block_size_list)): if fastest_block_index_count[x] > max_count_index: max_count_index = fastest_block_index_count[x]; optimal_index = x; f.write('OPTIMAL BLOCK SIZE: ' + str(block_size_list[optimal_index])); f.close();
def plot_perfect_recall_rates_for_turnover_rates(parsed_data, log_filename): set_size_buckets = Parser.get_dictionary_list_of_convergence_and_perfect_recall_for_turnover_rates( Parser.get_data_with_turnover_rates(parsed_data, log_filename)) # x, y_iters, std_iters, y_ratios, std_ratios x = [x * 0.02 for x in range(30)] results_2 = Parser.get_avg_perfect_recall_for_x_and_set_size(2, set_size_buckets, x) results_3 = Parser.get_avg_perfect_recall_for_x_and_set_size(3, set_size_buckets, x) results_4 = Parser.get_avg_perfect_recall_for_x_and_set_size(4, set_size_buckets, x) results_5 = Parser.get_avg_perfect_recall_for_x_and_set_size(5, set_size_buckets, x) plt.rcParams.update({'font.size': 25}) plt.ylabel('Perfect recall rate') plt.xlabel('Turnover rate') plt.title('Average perfect recall rate by turnover rate') # p2 = plt.errorbar(results_2[0], results_2[1], results_2[2]) # p3 = plt.errorbar(results_3[0], results_3[1], results_3[2]) # p4 = plt.errorbar(results_4[0], results_4[1], results_4[2]) # p5 = plt.errorbar(results_5[0], results_5[1], results_5[2]) p2 = plt.plot(results_2[0], results_2[1], linewidth=3.0) p3 = plt.plot(results_3[0], results_3[1], linewidth=3.0) p4 = plt.plot(results_4[0], results_4[1], linewidth=3.0) p5 = plt.plot(results_5[0], results_5[1], linewidth=3.0) plt.legend((p2[0], p3[0], p4[0], p5[0]), ('2x5', '3x5', '4x5', '5x5'), bbox_to_anchor=(1, 1.0155), ncol=4, fancybox=True, shadow=True) plt.xticks([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.58]) plt.margins(0.09) plt.grid(True) plt.show()
def plot_perfect_recall_rates_for_dg_weightings_no_err_bars(parsed_data, additional_plot_title): set_size_buckets = Parser.get_dictionary_list_of_convergence_and_perfect_recall_for_dg_weightings(parsed_data) # x, y_iters, std_iters, y_ratios, std_ratios x = range(30) results_2 = Parser.get_avg_convergence_for_x_and_set_size(2, set_size_buckets, x) results_3 = Parser.get_avg_convergence_for_x_and_set_size(3, set_size_buckets, x) results_4 = Parser.get_avg_convergence_for_x_and_set_size(4, set_size_buckets, x) results_5 = Parser.get_avg_convergence_for_x_and_set_size(5, set_size_buckets, x) plt.rcParams.update({'font.size': 25}) plt.ylabel('Convergence ratio') plt.xlabel('Turnover rate') plt.title('Average convergence rate by DG-weighting, ' + additional_plot_title) p2 = plt.plot(results_2[0], results_2[3]) p3 = plt.plot(results_3[0], results_3[3]) p4 = plt.plot(results_4[0], results_4[3]) p5 = plt.plot(results_5[0], results_5[3]) plt.legend((p2[0], p3[0], p4[0], p5[0]), ('2x5', '3x5', '4x5', '5x5')) # bbox_to_anchor=(1, 0.9), ncol=1, fancybox=True, shadow=True) plt.grid(True) plt.margins(0.01) plt.yticks(np.arange(0, 1.1, .1)) plt.show()
def make_return_dist_fig(sim_lookup, predictions, pick_K=100, n_bins=200, n_boots=5000): sim_net = sim_lookup['net_ret'].values sim_weights = sim_lookup['weights'].values bin_locs = np.linspace(0, 100, n_bins)[::-1] bins = np.percentile(sim_lookup['pred'].values, bin_locs) sim_samps_per_bin = len(sim_lookup)/float(n_bins) pred_bins = np.digitize(predictions['returns'] / 100., bins) #find bins of first max_K points in prediction sim_returns = np.zeros(n_boots) boot_samps = sim_samps_per_bin*pred_bins[:pick_K] + np.random.randint(0, sim_samps_per_bin, size=(n_boots, pick_K)) boot_samps = boot_samps.astype(int) sim_returns = np.sum(sim_net[boot_samps], axis=1) / np.sum(sim_weights[boot_samps], axis=1) sim_returns = LCM.annualize_returns(sim_returns) fig,ax=plt.subplots(figsize=(5.0,4.0)) sns.distplot(sim_returns,bins=100, hist=False, rug=False, ax=ax, kde_kws={'color':'k','lw':3}) plt.xlabel('Annual returns (%)',fontsize=14) plt.ylabel('Probability',fontsize=14) plt.title('Estimated portfolio returns', fontsize=18) plt.tick_params(axis='both', which='major', labelsize=10) plt.margins(.01, .01) plt.tight_layout() return fig
def on_epoch_end(self, callback_data, model, epoch): # convert to numpy arrays data_batch = model.data_batch.get() noise_batch = model.noise_batch.get() # value transform data_batch = self._value_transform(data_batch) noise_batch = self._value_transform(noise_batch) # shape transform data_canvas = self._shape_transform(data_batch) noise_canvas = self._shape_transform(noise_batch) # plotting options im_args = dict(interpolation="nearest", vmin=0., vmax=1.) if self.nchan == 1: im_args['cmap'] = plt.get_cmap("gray") fname = self.filename+'_data_'+'{:03d}'.format(epoch)+'.png' Image.fromarray(np.uint8(data_canvas*255)).convert('RGB').save(fname) fname = self.filename+'_noise_'+'{:03d}'.format(epoch)+'.png' Image.fromarray(np.uint8(noise_canvas*255)).convert('RGB').save(fname) # plot logged WGAN costs if logged if model.cost.costfunc.func == 'wasserstein': giter = callback_data['gan/gen_iter'][:] nonzeros = np.where(giter) giter = giter[nonzeros] cost_dis = callback_data['gan/cost_dis'][:][nonzeros] w_dist = medfilt(np.array(-cost_dis, dtype='float64'), kernel_size=101) plt.figure(figsize=(400/self.dpi, 300/self.dpi), dpi=self.dpi) plt.plot(giter, -cost_dis, 'k-', lw=0.25) plt.plot(giter, w_dist, 'r-', lw=2.) plt.title(self.filename, fontsize=self.font_size) plt.xlabel("Generator Iterations", fontsize=self.font_size) plt.ylabel("Wasserstein estimate", fontsize=self.font_size) plt.margins(0, 0, tight=True) plt.savefig(self.filename+'_training.png', bbox_inches='tight') plt.close()
def plot_hist(roi_betas, stat, score, component = 1, title="GLM"): labels, names = read_roiLabel(os.path.join('lpba40.label.xml')) names.append('rest_brain') roires = pd.DataFrame(zip(roi_betas.tolist(), names), columns=["beta", "roi"]) roires.sort_values('beta', inplace=True, ascending=False) roi_beta = roires['beta'].tolist() labels = roires['roi'].tolist() roires.to_csv(os.path.join(score + "_" + stat + '_roi-info.csv'), index = None) # select data to plot figure y = roi_beta x = np.arange(len(y)) # plot figure fig = plt.figure() ax = fig.add_subplot(111) width = 0.35 ## the bars rects = ax.bar(x, y, width, color='blue') # axes and labels plt.xticks(x, labels, rotation='vertical') ax.set_xlim(-width,len(x)+width) ax.set_ylim(np.min(roi_betas),np.max(roi_betas)) plt.margins(1) plt.subplots_adjust(bottom=0.35) plt.ylabel(score + "_" + stat) plt.savefig(score + "_" + stat + "_"+ title +".png") plt.close() return roi_beta, x
def main(): options = _parse_args() n_g = 3 n_h = 1 b_3 = 11 - 4/3 * n_g b_2 = 22/3 - 4/3 * n_g - 1/6 * n_h b_1 = - 4/3 * n_g - 1/10 * n_h a_inv_1 = 58.98 a_inv_2 = 29.60 a_inv_3 = 8.47 q = np.logspace(4, 20, 10) m_z = 91 a_inv_1_q = a_inv_1 + b_1 / (2 * np.pi) * np.log(q / m_z) a_inv_2_q = a_inv_2 + b_2 / (2 * np.pi) * np.log(q / m_z) a_inv_3_q = a_inv_3 + b_3 / (2 * np.pi) * np.log(q / m_z) pl.plot(q, a_inv_1_q, label='U(1)') pl.plot(q, a_inv_2_q, label='SU(2)') pl.plot(q, a_inv_3_q, label='SU(3)') pl.xscale('log') pl.margins(0.05) pl.savefig('running.pdf') np.savetxt('running-1.txt', np.column_stack([q, a_inv_1_q])) np.savetxt('running-2.txt', np.column_stack([q, a_inv_2_q])) np.savetxt('running-3.txt', np.column_stack([q, a_inv_3_q]))
def plot_teh(damage): h5 = read_csv(5,damage) h4 = read_csv(4,damage) h3 = read_csv(3,damage) h2 = read_csv(2,damage) h1 = read_csv(1,damage) xlist = [z for z in range(len(h5))] plt.figure(figsize=(15,15)) ph5 = plt.plot(xlist,h5,'-o',label='h5') ph4 = plt.plot(xlist,h4,'-o',label='h4') ph3 = plt.plot(xlist,h3,'-o',label='h3') ph2 = plt.plot(xlist,h2,'-o',label='h2') ph1 = plt.plot(xlist,h1,'-o',label='h1') #plt.xticks(xlist,x,rotation='vertical') plt.legend(bbox_to_anchor=(1, 1), loc=2, borderaxespad=0.) plt.legend() d_title = 'TE for %s_Normal with varying history length and max step size 20 with each individually scaled.'%(damage) plt.title(d_title) plt.xlabel('Edge') plt.ylabel('Transfer Entropy') plt.grid() plt.ylim(ymin=0.0) plt.xlim(xmin=0.0,xmax = len(h5)) plt.margins(0.2) #plt.subplots_adjust(bottom=0.25) plt.tight_layout(pad=2.5) plt.savefig("te-step20-hd-%s-normal_all_scaled.pdf"%(damage)) plt.show() return
def feature_importance(self, outfile="importance.png", features_list=[]): """Show each feature importance""" if (self.method in ['rf', 'etc']): importances = self.clf.feature_importances_ if len(features_list) > 0 and len(features_list) != len(importances): raise ValueError("Number of features does not fit!") indices = np.argsort(importances)[::-1] n_feats = len(features_list) np.savetxt(outfile + ".txt", np.array([tree.feature_importances_ for tree in self.clf.estimators_]), delimiter=',', fmt='%1.3e') std = np.std( [tree.feature_importances_ for tree in self.clf.estimators_], axis=0) plt.figure() plt.title("Feature importances") plt.bar(range(n_feats), importances[ indices], width=0.5, color="b", yerr=std[indices], align="center") if len(features_list) > 0: features_list = np.asarray(features_list)[indices] plt.xticks(range(n_feats), features_list, rotation='vertical') plt.xlim([-1, n_feats]) plt.margins(0.2) plt.subplots_adjust(bottom=0.15) plt.savefig(outfile, bbox_inches='tight') else: raise NotImplementedError( "Not supported for classifier other than Ensembl Tree")
def set_plt_format(): # sns.set() # seaborn coloring sns.set(font='Segoe UI', rc={'figure.figsize':(11.7,6.27), 'font.size':11 \ ,'axes.titlesize':18,'axes.labelsize':13 \ ,'axes.titlepad': 20,'axes.labelpad': 10 }) plt.margins(0.02) plt.subplots_adjust(bottom=0.1)
def plot_timeseries(t, y, ymin, ymax, args): yD = y.data.flatten() fig, ax = plt.subplots() plt.grid() plt.margins(y=.1, x=.1) plt.plot(t, yD, c='b', marker='.', lw = .75) # Format date axis df = mdates.DateFormatter('%Y-%m-%d') ax.xaxis.set_major_formatter(df) fig.autofmt_xdate() # Format y-axis to disable offset y_formatter = ticker.ScalarFormatter(useOffset=False) ax.yaxis.set_major_formatter(y_formatter) # Labels ax.set_ylabel(args[1] + " ("+ y.units + ")", fontsize=10) ax.set_title(args[0] + " " + str(args[3])[0:19] + " to " + str(args[4])[0:19], fontsize=10) ax.legend(["Max: %f" % ymax + "\nMin: %f" % ymin], loc='best', fontsize=8) filename = args[0] + "_" + args[1] + "_" + str(args[3])[0:10] + "_to_" + str(args[4])[0:10] save_file = os.path.join(args[2], filename) # create save file name plt.savefig(str(save_file),dpi=150) # save figure plt.close()
def draw_domain(mesh, name, dpi, color): c = mesh.nodes_coord plt.figure(name, dpi=dpi, frameon=False) X, Y = c[:, 0], c[:, 1] G = nx.Graph() label = [] for i in range(len(X)): label.append(i) G.add_node(i, posxy=(X[i], Y[i])) for plTag, lineTag in mesh.physicalLine.items(): lineNodes = mesh.line[lineTag] G.add_edge(lineNodes[0], lineNodes[1]) positions = nx.get_node_attributes(G, 'posxy') nx.draw_networkx_edges(G, positions, edge_color=color, font_size=0, width=1, origin='lower') plt.xlabel(r'$x$', fontsize=14) plt.ylabel(r'$y$', fontsize=14) plt.axes().set_aspect('equal') plt.axes().autoscale_view(True, True, True) plt.margins(y=0.1, x=0.1, tight=False) # limits=plt.axis('off') plt.draw()
def draw_label(label, img, label_names, colormap=None): plt.subplots_adjust(left=0, right=1, top=1, bottom=0, wspace=0, hspace=0) plt.margins(0, 0) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) if colormap is None: colormap = label_colormap(len(label_names)) label_viz = label2rgb(label, img, n_labels=len(label_names)) plt.imshow(label_viz) plt.axis('off') plt_handlers = [] plt_titles = [] for label_value, label_name in enumerate(label_names): fc = colormap[label_value] p = plt.Rectangle((0, 0), 1, 1, fc=fc) plt_handlers.append(p) plt_titles.append(label_name) plt.legend(plt_handlers, plt_titles, loc='lower right', framealpha=.5) f = io.BytesIO() plt.savefig(f, bbox_inches='tight', pad_inches=0) plt.cla() plt.close() out_size = (img.shape[1], img.shape[0]) out = PIL.Image.open(f).resize(out_size, PIL.Image.BILINEAR).convert('RGB') out = np.asarray(out) return out
def graph_TwoLines(aValues, bValues, l, labels, molecule, worksheet, augmented, yLabel, graphFolder, aLabel, bLabel): #graph_HFandCCSDT '''graphs CCSDT and HF on same axis''' if worksheet==worksheetCharged: chargeFolder=chargedFolder if worksheet==worksheetNeutral: chargeFolder=neutralFolder if augmented==True: augmentedFolder=augFolder if augmented==False: augmentedFolder=ccFolder plt.plot(l, aValues, color=primaryColor, lw=2, ls='-', marker='s', label=molecule + aLabel) plt.plot(l, bValues, color=secondaryColor, lw=2, ls='-', marker='o', label=molecule + bLabel) plt.xticks(l, labels, rotation = '30', ha='right') plt.margins(0.015, 0.05) plt.subplots_adjust(bottom=0.2, top=0.85) plt.ylabel(yLabel) plt.legend(loc='upper center', bbox_to_anchor=(.5, 1.2), numpoints = 1, shadow=True, ncol=2) plt.grid(True) if not os.path.exists(path + '/ALL GRAPHS' + graphFolder +chargeFolder+augmentedFolder): os.makedirs(path + '/ALL GRAPHS' + graphFolder +chargeFolder+augmentedFolder) plt.savefig(path + '/ALL GRAPHS' + graphFolder +chargeFolder+augmentedFolder+ molecule + '.eps') #plt.show() plt.close()
def generate(self, title=None): max_duration = 0 for machine_nr in self.__tasks: machine = self.__tasks[machine_nr] for start_time in machine: job_nr = machine[start_time]['job_nr'] duration = machine[start_time]['duration'] color = self.__colors[job_nr % len(self.__colors)] plt.hlines(machine_nr, start_time, start_time + duration, colors=color, lw=50) plt.text(start_time + 0.1, machine_nr + 0.1, str(job_nr), bbox=dict(facecolor='white', alpha=1.0)) #fontdict=dict(color='white')) if duration + start_time > max_duration: max_duration = duration + start_time plt.margins(1) if self.__n_machines is 0: plt.axis([0, max_duration, 0.8, len(self.__tasks)]) else: plt.axis([0, max_duration, -0.8, self.__n_machines]) plt.xticks(range(0, max_duration, 1)) if title: plt.title(title) plt.xlabel("Time") plt.ylabel("Machines") if self.__n_machines is 0: plt.yticks(range(0, len(self.__tasks), 1)) else: plt.yticks(range(0, self.__n_machines, 1)) self.__fig.savefig(self.__out_dir + sep + title + '.png')
def stepplot(x, y, labels, plot_titles): """Generates Correlation Graph. With the x,y coordinates, labels, and plot titles established, the step-plots can be generated. Output is PDF file format""" plt.figure() #makes new image for each plot #plot x & y stemplot. format plot points plt.stem(x, y, linefmt='k--', markerfmt='ro', basefmt='k-') #set x-axis labels and set them vertical. size 10 font. plt.xticks(x, labels, rotation='vertical', fontsize = 10) #set titles for graph and axes plt.title(plot_titles[0]) plt.xlabel("Biomarkers") plt.ylabel("Correlation Values") # slightly move axis away from plot. prevents clipping the labels plt.margins(0.2) # Tweak spacing to prevent clipping of tick-labels plt.subplots_adjust(bottom=0.15) plt.tight_layout() #prevents labels from being clipped with PdfPages(plot_titles[0]+'.pdf') as pdf: #creates new file for each figure pdf.savefig()
def graph_OneLine(values, l, labels, molecule, worksheet, augmented, yLabel, graphFolder): #graph_HF_CORR #line graphs HF values and corr values #s all graphs with just one line if worksheet==worksheetCharged: chargeFolder=chargedFolder if worksheet==worksheetNeutral: chargeFolder=neutralFolder if augmented==True: augmentedFolder=augFolder if augmented==False: augmentedFolder=ccFolder plt.plot(l, values, color=primaryColor, lw=2, ls='-', marker='s', label=molecule) plt.xticks(l, labels, rotation = '30', ha='right') plt.margins(0.09, 0.09) #y_formatter = plt.ticker.ScalarFormatter(useOffset=False) #ax.yaxis.set_major_formatter(y_formatter) plt.subplots_adjust(bottom=0.2, top=0.85) plt.ylabel(yLabel) plt.legend(loc='upper center', bbox_to_anchor=(.5, 1.2), numpoints = 1, shadow=True, ncol=3) plt.grid(True) if not os.path.exists(path + '/ALL GRAPHS' + graphFolder +chargeFolder+augmentedFolder): os.makedirs(path + '/ALL GRAPHS' + graphFolder +chargeFolder+augmentedFolder) plt.savefig(path + '/ALL GRAPHS' + graphFolder +chargeFolder+augmentedFolder+ molecule + '.eps') plt.close()
def plot_faces(filename, args): dset = gusto_dataset.GustoDataset(filename) segments = dset.get_face_segments() for seg in segments: plt.plot(seg[:,0], seg[:,2], '-o', c='k') plt.axis('equal') plt.margins(0.1)
def _make_plot(cls, title, depths, names, filename): """ Create a PNG plot of the graph data Args: title: title of the plot depths: Values to be graphed names: Names of each of the bins being graphed filename: Full path of the file for the resulting PNG Returns: None Raises: None """ plt.figure() plt.title(title) plt.xlabel('Graph Depth') plt.ylabel('Count') plt.margins(0.01) plt.subplots_adjust(bottom=0.15) plt.hist(depths, histtype='bar', label=names, bins=10) plt.rc('legend', **{'fontsize': 8}) plt.legend(shadow=True, fancybox=True) plt.savefig(filename, format='png')
def main(): # Write a part to put image directories into "groups" source_dirs = [ '/home/sbraden/400mpp_15x15_clm_wac/mare/', '/home/sbraden/400mpp_15x15_clm_wac/pyro/', '/home/sbraden/400mpp_15x15_clm_wac/imps/', '/home/sbraden/400mpp_15x15_clm_wac/mare_immature/' ] for directory in source_dirs: print directory groupname = os.path.split(os.path.dirname(directory))[1] print groupname # read in LROC WAC images wac_img_list = iglob(directory+'*_wac.cub') # read in Clementine images clm_img_list = iglob(directory+'*_clm.cub') make_cloud_plot(wac_img_list, colorloop.next(), groupname) fontP = FontProperties() fontP.set_size('small') #plt.legend(loc='upper left', fancybox=True, prop=fontP, scatterpoints=1) #plt.axis([0.70, 0.86, 0.90, 1.15],fontsize=14) plt.axis([0.60, 0.90, 0.90, 1.20],fontsize=14) plt.axes().set_aspect('equal') # THIS next line does not get called: plt.margins(0.20) # 4% add "padding" to the data limits before they're autoscaled plt.xlabel('WAC 320/415 nm', fontsize=14) plt.ylabel('CLM 950/750 nm', fontsize=14) plt.savefig('lunar_roi_cloud_plot.png', dpi=300) plt.close()
time=np.arange(start=0, stop=tao[0], step=1) t_result=np.append(time,t) time_zero_deflection=np.zeros(t[0]-time[0]) #print(time_zero_deflection) #Deflection plot calculation #length required by user length_input=5 #in m for i in range(len(point_along_beam)): if (length_input*10**3)==point_along_beam[i]: deflection_plot_array=deflection_array[i] deflection_plot_array=np.append(time_zero_deflection,deflection_plot_array) break #print(deflection_plot_array) py.plot(t_result[0:len(t_result)-1] , deflection_plot_array[0:len(t_result)-1],'r') py.xticks([0 ,100 ,200 ,300 ,400, 500, 600, 700, 800, 900, 1000]) py.xlabel("Number of Days") py.ylabel("Deflection (mm)") py.margins(x=0) py.show() #Code to display deflection versus time - this is for t(0) #py.plot(point_along_beam, second_integration[0],'or') #py.show() #Code to display Curvature versus time - this is for t(0) #py.plot(point_along_beam, time_points_curve[0],'or') #py.show()
# encoding=utf8 import matplotlib.pyplot as plt from pylab import * #支持中文 mpl.rcParams['font.sans-serif'] = ['SimHei'] names = [ '0.5', '0.6', '0.7', '0.8', '0.9', '1.0', '1.1', '1.2', '1.3', '1.4', '1.5', '1.6', '1.7', '1.8', '1.9', '2.0' ] x = range(len(names)) y = [ 21.83468967, 22.6782449, 22.93430108, 21.25625279, 20.44286396, 20.91914576, 21.23495125, 21.59001477, 22.36346805, 23.08897248, 23.75214622, 24.93747974, 25.21955438, 26.10012006, 26.54489741, 27.13799267 ] y1 = [ 329.4249826, 252.4586697, 140.2121354, 92.71050662, 72.19954984, 59.75124684, 53.56468332, 49.07447623, 44.70338388, 43.59911693, 43.42282239, 40.73737397, 38.33333555, 37.6000134, 37.36318906, 37.06890861 ] #plt.plot(x, y, 'ro-') #plt.plot(x, y1, 'bo-') plt.xlim(-1, 3) # 限定横轴的范围 plt.ylim(0, 350) # 限定纵轴的范围 plt.plot(x, y, marker='o', color='r', mec='r', mfc='w', label=u'无OTN交换的传统场景') plt.plot(x, y1, marker='^', ms=5, label=u'OTN交换场景') plt.legend() # 让图例生效 plt.xticks(x, names, rotation=45) plt.margins(0) plt.subplots_adjust(bottom=0.15)
def predict_3D(input, regressor, device, silhouettes_from='densepose', proxy_rep_input_wh=512, save_proxy_vis=True, render_vis=True): # Set-up proxy representation predictors. joints2D_predictor, silhouette_predictor = setup_detectron2_predictors( silhouettes_from=silhouettes_from) # Set-up SMPL model. smpl = SMPL(config.SMPL_MODEL_DIR, batch_size=1).to(device) if render_vis: # Set-up renderer for visualisation. wp_renderer = Renderer(resolution=(proxy_rep_input_wh, proxy_rep_input_wh)) if os.path.isdir(input): image_fnames = [ f for f in sorted(os.listdir(input)) if f.endswith('.png') or f.endswith('.jpg') ] for fname in image_fnames: print("Predicting on:", fname) image = cv2.imread(os.path.join(input, fname)) # Pre-process for 2D detectors image = pad_to_square(image) image = cv2.resize(image, (proxy_rep_input_wh, proxy_rep_input_wh), interpolation=cv2.INTER_LINEAR) # Predict 2D joints2D, joints2D_vis = predict_joints2D(image, joints2D_predictor) if silhouettes_from == 'pointrend': silhouette, silhouette_vis = predict_silhouette_pointrend( image, silhouette_predictor) elif silhouettes_from == 'densepose': silhouette, silhouette_vis = predict_densepose( image, silhouette_predictor) silhouette = convert_multiclass_to_binary_labels(silhouette) # Create proxy representation proxy_rep = create_proxy_representation( silhouette, joints2D, in_wh=proxy_rep_input_wh, out_wh=config.REGRESSOR_IMG_WH) proxy_rep = proxy_rep[None, :, :, :] # add batch dimension proxy_rep = torch.from_numpy(proxy_rep).float().to(device) # Predict 3D regressor.eval() with torch.no_grad(): pred_cam_wp, pred_pose, pred_shape = regressor(proxy_rep) # Convert pred pose to rotation matrices if pred_pose.shape[-1] == 24 * 3: pred_pose_rotmats = batch_rodrigues( pred_pose.contiguous().view(-1, 3)) pred_pose_rotmats = pred_pose_rotmats.view(-1, 24, 3, 3) elif pred_pose.shape[-1] == 24 * 6: pred_pose_rotmats = rot6d_to_rotmat( pred_pose.contiguous()).view(-1, 24, 3, 3) pred_smpl_output = smpl( body_pose=pred_pose_rotmats[:, 1:], global_orient=pred_pose_rotmats[:, 0].unsqueeze(1), betas=pred_shape, pose2rot=False) pred_vertices = pred_smpl_output.vertices pred_vertices2d = orthographic_project_torch( pred_vertices, pred_cam_wp) pred_vertices2d = undo_keypoint_normalisation( pred_vertices2d, proxy_rep_input_wh) pred_reposed_smpl_output = smpl(betas=pred_shape) pred_reposed_vertices = pred_reposed_smpl_output.vertices # Numpy-fying pred_vertices = pred_vertices.cpu().detach().numpy()[0] pred_vertices2d = pred_vertices2d.cpu().detach().numpy()[0] pred_reposed_vertices = pred_reposed_vertices.cpu().detach().numpy( )[0] pred_cam_wp = pred_cam_wp.cpu().detach().numpy()[0] if not os.path.isdir(os.path.join(input, 'verts_vis')): os.makedirs(os.path.join(input, 'verts_vis')) plt.figure() plt.imshow(image[:, :, ::-1]) plt.scatter(pred_vertices2d[:, 0], pred_vertices2d[:, 1], s=0.3) plt.gca().set_axis_off() plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.margins(0, 0) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.savefig(os.path.join(input, 'verts_vis', 'verts_' + fname)) if render_vis: rend_img = wp_renderer.render(verts=pred_vertices, cam=pred_cam_wp, img=image) rend_reposed_img = wp_renderer.render( verts=pred_reposed_vertices, cam=np.array([0.8, 0., -0.2]), angle=180, axis=[1, 0, 0]) if not os.path.isdir(os.path.join(input, 'rend_vis')): os.makedirs(os.path.join(input, 'rend_vis')) cv2.imwrite(os.path.join(input, 'rend_vis', 'rend_' + fname), rend_img) cv2.imwrite( os.path.join(input, 'rend_vis', 'reposed_' + fname), rend_reposed_img) if save_proxy_vis: if not os.path.isdir(os.path.join(input, 'proxy_vis')): os.makedirs(os.path.join(input, 'proxy_vis')) cv2.imwrite( os.path.join(input, 'proxy_vis', 'silhouette_' + fname), silhouette_vis) cv2.imwrite( os.path.join(input, 'proxy_vis', 'joints2D_' + fname), joints2D_vis)
_ = plt.ylabel('Price') plt.show() #Box plot _=sns.boxplot(x='RM',y='MEDV',data=df) _=plt.xlabel('RM') _=plt.ylabel('MEDV') plt.show() #ECDF x = np.sort(df['MEDV']) y = np.arange(1, len(x)+1) / len(x) _ = plt.plot(x, y, marker='.', linestyle='none') _ = plt.xlabel('The price') _ = plt.ylabel('ECDF') plt.margins(0.02) plt.show() #Regression Model x = df['RM'].values.reshape(-1,1) y = df['MEDV'].values.reshape(-1,1) from sklearn.model_selection import train_test_split x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42) from sklearn.linear_model import LinearRegression slr = LinearRegression() slr.fit(x_train, y_train) print('Slope: %.3f' % slr.coef_[0]) print('Intercept: %.3f' % slr.intercept_) def lin_regplot(X, y, model): plt.scatter(X, y, c='steelblue', edgecolor='white', s=70)
if __name__ == "__main__": import numpy as np import matplotlib.pyplot as plt from scipy import io mov_avg_filter = MovingAverageFilter(sample_size=10) num_sampels = 500 sample_ys = np.zeros((num_sampels, 1)) mov_avg_ys = np.zeros((num_sampels, 1)) sonar_alt = io.loadmat("SonarAlt.mat")["sonarAlt"][0] for i in range(num_sampels): sample = sonar_alt[i] mov_avg = mov_avg_filter.get_moving_average(sample) sample_ys[i] = sample mov_avg_ys[i] = mov_avg dt = 0.02 t = np.arange(0, num_sampels * dt, step=dt) plt.margins(0, 0) plt.plot(t, sample_ys, "r.", markersize=2, label="Measured") plt.plot(t, mov_avg_ys, "b", label="Moving Average") plt.legend() plt.savefig("fig/moving_average_filter.png", bbox_inches="tight")
grid(True, color='0.9', linestyle='-', which='both', axis='both') title('Poles and zeros') # Display zeros, poles and gain print str(len(z)) + " zeros: " + str(z) print str(len(p)) + " poles: " + str(p) print "gain: " + str(k) # Impulse response index = arange(0,20) u = 1.0*(index==0) y = lfilter(b, a, u) subplot(2, 2, 3) stem(index,y) title('Impulse response') margins(0, 0.1) grid(True, color='0.9', linestyle='-', which='both', axis='both') show() # Frequency response w, h = freqz(b, a) subplot(2, 2, 2) plot(w/pi, 20*log10(abs(h))) xscale('log') title('Frequency response') xlabel('Normalized frequency') ylabel('Amplitude [dB]') margins(0, 0.1) grid(True, color = '0.7', linestyle='-', which='major', axis='both') grid(True, color = '0.9', linestyle='-', which='minor', axis='both')
def main(): """Core logic""" arg = get_arguments() date_limits = date_check(arg) date_list, xlegend = date_range(date_limits) nran = 0 # Range number daysplt = [] # List for all day splits with their events ftmp = tempfile.NamedTemporaryFile().name # File to store Tuptime csv # Iterate over each element in (since, until) list for nran, _ in enumerate(date_list): tsince = str(int(date_list[nran][0])) # Timestamp arg tsince tuntil = str(int(date_list[nran][1])) # timestamp arg tuntil # Query tuptime for every (since, until) and save output to a file with open(ftmp, "wb", 0) as out: if arg.dbfile: # If a file is passed, avoid update it subprocess.call([ "tuptime", "-lsc", "--tsince", tsince, "--tuntil", tuntil, "-f", arg.dbfile, "-n" ], stdout=out) else: subprocess.call([ "tuptime", "-lsc", "--tsince", tsince, "--tuntil", tuntil ], stdout=out) # Get csv file daysplit_events = [] with open(ftmp) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') bad = None # Register bad shutdown for row in csv_reader: l_row = [] # List for events in csv rows # Control how was the shutdown if (row[0] == 'Shutdown') and (row[1] == 'BAD'): bad = True # Populate list with (uptime, downtime ok, downtime bad) if (row[0] == 'Uptime') or (row[0] == 'Downtime'): if row[0] == 'Uptime': # Add 0 or value to first position l_row.append(int(row[1])) else: l_row.append(0) if row[0] == 'Downtime': # Add (0, value) or (value, 0) to the end if bad is True: l_row.extend((0, int(row[1]))) else: l_row.extend((int(row[1]), 0)) bad = False # Reset false else: l_row.extend((0, 0)) # Add to events list per day daysplit_events.append(l_row) print('Got range --->\t' + str(nran) + ' with ' + str(len(daysplit_events)) + ' events') # Per day, get total value for each type of event and convert seconds to hours daysplit_events = [(sum(j) / 3600) for j in zip(*daysplit_events)] # Populate daysplt list with totals daysplt.append(daysplit_events) print('Ranges got:\t' + str(len(daysplt))) # At this poing daysplt have: # # list_with_days[ # list_with_total_time of_each_type_of_event[ # uptime, downtime_ok, downtime_bad ]] # Matplotlib requires stack with slices # # y # | uptime uptime uptime # | down_ok down_ok down_ok # | down_bad down_bad down_bad # |----------------------------------x # | day1 day2 dayN # Get each state values slice from each day days = {'uptime': [], 'down_ok': [], 'down_bad': []} for i in daysplt: if not i: i = [0, 0, 0] # Set empty days['uptime'].append(i[0]) days['down_ok'].append(i[1]) days['down_bad'].append(i[2]) ind = np.arange(len(daysplt)) # number of days on x plt.figure(figsize=(arg.width, arg.height)) # Old bar plot #width = 0.9 # column size #plt.bar(ind, days['uptime'], width, color='green', label='Uptime') #plt.bar(ind, days['down_ok'], width, color='grey', label='Downtime OK', bottom=days['uptime']) #plt.bar(ind, days['down_bad'], width, color='black', label='Downtime BAD', bottom=[i+j for i, j in zip(days['uptime'], days['down_ok'])]) plt.plot(ind, days['uptime'], linewidth=2, marker='o', color='green', label='Uptime') plt.plot(ind, days['down_ok'], linewidth=2, marker='o', color='grey', label='Down Ok') plt.plot(ind, days['down_bad'], linewidth=2, marker='o', color='black', label='Down Bad') plt.ylabel('Hours') plt.title('Hours per State by Day') plt.xticks(ind, xlegend, rotation=85, ha="center") plt.margins(y=0, x=0.01) plt.yticks(np.arange(0, 25, 2)) plt.ylim(top=26) plt.grid(color='lightgrey', linestyle='--', linewidth=0.5, axis='y') plt.grid(color='lightblue', linestyle='--', linewidth=0.5, axis='x') plt.tight_layout() cfig = plt.get_current_fig_manager() cfig.canvas.set_window_title("Tuptime") plt.legend() plt.show()
def plotmaker_seconds_pooled(gr_ctr_pooled, het_ctr_pooled, gr_ctr, het_ctr, gr_com, het_com, conc, drugname, savename): in_gr_ctr = gr_ctr.copy(deep=True) in_het_ctr = het_ctr.copy(deep=True) in_gr_com = gr_com.copy(deep=True) in_het_com = het_com.copy(deep=True) in_gr_ctr_pooled = gr_ctr_pooled.copy(deep=True) in_het_ctr_pooled = het_ctr_pooled.copy(deep=True) timepoints = np.arange(1, 61) f, ((ax1, ax2, ax3), (ax4, ax5, ax6), (ax7, ax8, ax9), (ax10, ax11, ax12), (ax13, ax14, ax15), (ax16, ax17, ax18), (ax19, ax20, ax21), (ax22, ax23, ax24)) = plt.subplots(nrows=8, ncols=3, sharey='all', figsize=(15, 30)) plt.subplots_adjust(wspace=0.1, hspace=0.2, top=0.95) plt.suptitle('Average distance moved per second at ' + conc + 'uM ' + drugname, fontsize='x-large') plt.minorticks_off() plt.margins(0.01, 0.01) ax = [ ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8, ax9, ax10, ax11, ax12, ax13, ax14, ax15, ax16, ax17, ax18, ax19, ax20, ax21, ax22, ax23, ax24 ] for i in ax: i.tick_params(top='off', right='off', which='both') i.set_xlabel('Seconds') # fills ax1: light condition pre-treatment, tp1 ax1.plot(timepoints, in_gr_ctr_pooled['L tp1 dist 0'], color='goldenrod', label='gr ctr') ax1.plot(timepoints, in_het_ctr_pooled['L tp1 dist 0'], color='grey', label='het ctr') ax1.set_ylabel('Distance (mm)') ax1.set_title('Pre-Treatment', fontsize=10) # fills ax2: light condition 1h incubation, tp1 ax2.plot(timepoints, in_gr_com['L tp1 dist 1'], color='brown', label='gr drug') ax2.plot(timepoints, in_gr_ctr['L tp1 dist 1'], color='goldenrod', label='gr ctr') ax2.plot(timepoints, in_het_com['L tp1 dist 1'], color='k', label='het drug') ax2.plot(timepoints, in_het_ctr['L tp1 dist 1'], color='grey', label='het ctr') ax2.set_title('Incubation Time 1h', fontsize=10) # fills ax3: light condition 24h incubation, tp1 ax3.plot(timepoints, in_gr_com['L tp1 dist 24'], color='brown', label='gr drug') ax3.plot(timepoints, in_gr_ctr['L tp1 dist 24'], color='goldenrod', label='gr ctr') ax3.plot(timepoints, in_het_com['L tp1 dist 24'], color='k', label='het drug') ax3.plot(timepoints, in_het_ctr['L tp1 dist 24'], color='grey', label='het ctr') ax3.set_title('Incubation Time 24h', fontsize=10) ax3.legend(loc=5, fontsize='x-small', bbox_to_anchor=(1.5, 0.5), borderaxespad=0, title=('TP1, Light Condition')) # fills ax4: Dark condition pre-treatment, tp1 ax4.plot(timepoints, in_gr_ctr_pooled['D tp1 dist 0'], color='goldenrod', label='gr ctr') ax4.plot(timepoints, in_het_ctr_pooled['D tp1 dist 0'], color='grey', label='het ctr') ax4.set_ylabel('Distance (mm)') # fills ax5: Dark condition 1h incubation, tp1 ax5.plot(timepoints, in_gr_com['D tp1 dist 1'], color='brown', label='gr drug') ax5.plot(timepoints, in_gr_ctr['D tp1 dist 1'], color='goldenrod', label='gr ctr') ax5.plot(timepoints, in_het_com['D tp1 dist 1'], color='k', label='het drug') ax5.plot(timepoints, in_het_ctr['D tp1 dist 1'], color='grey', label='het ctr') # fills ax6: Dark condition 24h incubation, tp1 ax6.plot(timepoints, in_gr_com['D tp1 dist 24'], color='brown', label='gr drug') ax6.plot(timepoints, in_gr_ctr['D tp1 dist 24'], color='goldenrod', label='gr ctr') ax6.plot(timepoints, in_het_com['D tp1 dist 24'], color='k', label='het drug') ax6.plot(timepoints, in_het_ctr['D tp1 dist 24'], color='grey', label='het ctr') ax6.legend(loc=5, fontsize='x-small', bbox_to_anchor=(1.5, 0.5), borderaxespad=0, title=('TP1, Dark Condition')) # fills ax7: light condition pre-treatment, tp2 ax7.plot(timepoints, in_gr_ctr_pooled['L tp2 dist 0'], color='goldenrod', label='gr ctr') ax7.plot(timepoints, in_het_ctr_pooled['L tp2 dist 0'], color='grey', label='het ctr') ax7.set_ylabel('Distance (mm)') # fills ax8: light condition 1h incubation, tp2 ax8.plot(timepoints, in_gr_com['L tp2 dist 1'], color='brown', label='gr drug') ax8.plot(timepoints, in_gr_ctr['L tp2 dist 1'], color='goldenrod', label='gr ctr') ax8.plot(timepoints, in_het_com['L tp2 dist 1'], color='k', label='het drug') ax8.plot(timepoints, in_het_ctr['L tp2 dist 1'], color='grey', label='het ctr') # fills ax9: light condition 24h incubation, tp2 ax9.plot(timepoints, in_gr_com['L tp2 dist 24'], color='brown', label='gr drug') ax9.plot(timepoints, in_gr_ctr['L tp2 dist 24'], color='goldenrod', label='gr ctr') ax9.plot(timepoints, in_het_com['L tp2 dist 24'], color='k', label='het drug') ax9.plot(timepoints, in_het_ctr['L tp2 dist 24'], color='grey', label='het ctr') ax9.legend(loc=5, fontsize='x-small', bbox_to_anchor=(1.5, 0.5), borderaxespad=0, title=('TP2, Light Condition')) # fills ax10: Dark condition pre-treatment, tp2 ax10.plot(timepoints, in_gr_ctr_pooled['D tp2 dist 0'], color='goldenrod', label='gr ctr') ax10.plot(timepoints, in_het_ctr_pooled['D tp2 dist 0'], color='grey', label='het ctr') ax10.set_ylabel('Distance (mm)') # fills ax11: Dark condition 1h incubation, tp2 ax11.plot(timepoints, in_gr_com['D tp2 dist 1'], color='brown', label='gr drug') ax11.plot(timepoints, in_gr_ctr['D tp2 dist 1'], color='goldenrod', label='gr ctr') ax11.plot(timepoints, in_het_com['D tp2 dist 1'], color='k', label='het drug') ax11.plot(timepoints, in_het_ctr['D tp2 dist 1'], color='grey', label='het ctr') # fills ax12: Dark condition 24h incubation, tp2 ax12.plot(timepoints, in_gr_com['D tp2 dist 24'], color='brown', label='gr drug') ax12.plot(timepoints, in_gr_ctr['D tp2 dist 24'], color='goldenrod', label='gr ctr') ax12.plot(timepoints, in_het_com['D tp2 dist 24'], color='k', label='het drug') ax12.plot(timepoints, in_het_ctr['D tp2 dist 24'], color='grey', label='het ctr') ax12.legend(loc=5, fontsize='x-small', bbox_to_anchor=(1.5, 0.5), borderaxespad=0, title=('TP2, Dark Condition')) # fills ax13: light condition pre-treatment, tp3 ax13.plot(timepoints, in_gr_ctr_pooled['L tp3 dist 0'], color='goldenrod', label='gr ctr') ax13.plot(timepoints, in_het_ctr_pooled['L tp3 dist 0'], color='grey', label='het ctr') ax13.set_ylabel('Distance (mm)') # fills ax14: light condition 1h incubation, tp3 ax14.plot(timepoints, in_gr_com['L tp3 dist 1'], color='brown', label='gr drug') ax14.plot(timepoints, in_gr_ctr['L tp3 dist 1'], color='goldenrod', label='gr ctr') ax14.plot(timepoints, in_het_com['L tp3 dist 1'], color='k', label='het drug') ax14.plot(timepoints, in_het_ctr['L tp3 dist 1'], color='grey', label='het ctr') # fills ax15: light condition 24h incubation, tp3 ax15.plot(timepoints, in_gr_com['L tp3 dist 24'], color='brown', label='gr drug') ax15.plot(timepoints, in_gr_ctr['L tp3 dist 24'], color='goldenrod', label='gr ctr') ax15.plot(timepoints, in_het_com['L tp3 dist 24'], color='k', label='het drug') ax15.plot(timepoints, in_het_ctr['L tp3 dist 24'], color='grey', label='het ctr') ax15.legend(loc=5, fontsize='x-small', bbox_to_anchor=(1.5, 0.5), borderaxespad=0, title=('TP3, Light Condition')) # fills ax16: Dark condition pre-treatment, tp3 ax16.plot(timepoints, in_gr_ctr_pooled['D tp3 dist 0'], color='goldenrod', label='gr ctr') ax16.plot(timepoints, in_het_ctr_pooled['D tp3 dist 0'], color='grey', label='het ctr') ax16.set_ylabel('Distance (mm)') # fills ax17: Dark condition 1h incubation, tp3 ax17.plot(timepoints, in_gr_com['D tp3 dist 1'], color='brown', label='gr drug') ax17.plot(timepoints, in_gr_ctr['D tp3 dist 1'], color='goldenrod', label='gr ctr') ax17.plot(timepoints, in_het_com['D tp3 dist 1'], color='k', label='het drug') ax17.plot(timepoints, in_het_ctr['D tp3 dist 1'], color='grey', label='het ctr') # fills ax18: Dark condition 24h incubation, tp3 ax18.plot(timepoints, in_gr_com['D tp3 dist 24'], color='brown', label='gr drug') ax18.plot(timepoints, in_gr_ctr['D tp3 dist 24'], color='goldenrod', label='gr ctr') ax18.plot(timepoints, in_het_com['D tp3 dist 24'], color='k', label='het drug') ax18.plot(timepoints, in_het_ctr['D tp3 dist 24'], color='grey', label='het ctr') ax18.legend(loc=5, fontsize='x-small', bbox_to_anchor=(1.5, 0.5), borderaxespad=0, title=('TP3, Dark Condition')) # fills ax19: light condition pre-treatment, tp4 ax19.plot(timepoints, in_gr_ctr_pooled['L tp4 dist 0'], color='goldenrod', label='gr ctr') ax19.plot(timepoints, in_het_ctr_pooled['L tp4 dist 0'], color='grey', label='het ctr') ax19.set_ylabel('Distance (mm)') # fills ax20: light condition 1h incubation, tp4 ax20.plot(timepoints, in_gr_com['L tp4 dist 1'], color='brown', label='gr drug') ax20.plot(timepoints, in_gr_ctr['L tp4 dist 1'], color='goldenrod', label='gr ctr') ax20.plot(timepoints, in_het_com['L tp4 dist 1'], color='k', label='het drug') ax20.plot(timepoints, in_het_ctr['L tp4 dist 1'], color='grey', label='het ctr') # fills ax21: light condition 24h incubation, tp4 ax21.plot(timepoints, in_gr_com['L tp4 dist 24'], color='brown', label='gr drug') ax21.plot(timepoints, in_gr_ctr['L tp4 dist 24'], color='goldenrod', label='gr ctr') ax21.plot(timepoints, in_het_com['L tp4 dist 24'], color='k', label='het drug') ax21.plot(timepoints, in_het_ctr['L tp4 dist 24'], color='grey', label='het ctr') ax21.legend(loc=5, fontsize='x-small', bbox_to_anchor=(1.5, 0.5), borderaxespad=0, title=('TP4, Light Condition')) # fills ax22: Dark condition pre-treatment, tp4 ax22.plot(timepoints, in_gr_ctr_pooled['D tp4 dist 0'], color='goldenrod', label='gr ctr') ax22.plot(timepoints, in_het_ctr_pooled['D tp4 dist 0'], color='grey', label='het ctr') ax22.set_ylabel('Distance (mm)') # fills ax23: Dark condition 1h incubation, tp4 ax23.plot(timepoints, in_gr_com['D tp4 dist 1'], color='brown', label='gr drug') ax23.plot(timepoints, in_gr_ctr['D tp4 dist 1'], color='goldenrod', label='gr ctr') ax23.plot(timepoints, in_het_com['D tp4 dist 1'], color='k', label='het drug') ax23.plot(timepoints, in_het_ctr['D tp4 dist 1'], color='grey', label='het ctr') # fills ax24: Dark condition 24h incubation, tp4 ax24.plot(timepoints, in_gr_com['D tp4 dist 24'], color='brown', label='gr drug') ax24.plot(timepoints, in_gr_ctr['D tp4 dist 24'], color='goldenrod', label='gr ctr') ax24.plot(timepoints, in_het_com['D tp4 dist 24'], color='k', label='het drug') ax24.plot(timepoints, in_het_ctr['D tp4 dist 24'], color='grey', label='het ctr') ax24.legend(loc=5, fontsize='x-small', bbox_to_anchor=(1.5, 0.5), borderaxespad=0, title=('TP4, Dark Condition')) # save figure f.savefig(savename, bbox_inches='tight') plt.close('all')
def plotmaker_abs_avg(gr_ctr, het_ctr, gr_com, het_com, conc, drugname, sec_analysed, savename): # make copies for safety cop1 = gr_ctr.copy(deep=True) cop2 = het_ctr.copy(deep=True) cop3 = gr_com.copy(deep=True) cop4 = het_com.copy(deep=True) # make all 0s NaN in_gr_ctr = cop1.replace(to_replace=0, value=np.nan) in_gr_com = cop3.replace(to_replace=0, value=np.nan) in_het_ctr = cop2.replace(to_replace=0, value=np.nan) in_het_com = cop4.replace(to_replace=0, value=np.nan) # x axis values timepoints = np.arange(1, 5) # makes general plot outline f, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3, sharex=True, sharey='all', figsize=(12, 6)) plt.subplots_adjust(wspace=0.2, hspace=0.2, top=0.87) plt.suptitle('Average distance moved per timepoint at ' + conc + 'uM ' + drugname, fontsize='x-large') plt.minorticks_off() plt.xticks(timepoints) plt.margins(0.1, 0.1) ax = [ax1, ax2, ax3, ax4, ax5, ax6] for i in ax: i.tick_params(top='off', right='off') # fills ax1: light condition pre-treatment ax1.errorbar(timepoints, in_gr_com['L mean 0'], yerr=in_gr_com['L sem 0'], ecolor='brown', color='brown', label='gr drug') ax1.errorbar(timepoints, in_gr_ctr['L mean 0'], yerr=in_gr_ctr['L sem 0'], color='goldenrod', ecolor='goldenrod', label='gr ctr') ax1.errorbar(timepoints, in_het_com['L mean 0'], yerr=in_het_com['L sem 0'], ecolor='k', color='k', label='het drug') ax1.errorbar(timepoints, in_het_ctr['L mean 0'], yerr=in_het_ctr['L sem 0'], color='grey', ecolor='grey', label='het ctr') ax1.set_ylabel('Light, Distance (mm)') ax1.set_title('Pre-Treatment', fontsize=10) # fills ax2: light condition 1h incubation ax2.errorbar(timepoints, in_gr_com['L mean 1'], yerr=in_gr_com['L sem 1'], ecolor='brown', color='brown', label='gr drug') ax2.errorbar(timepoints, in_gr_ctr['L mean 1'], yerr=in_gr_ctr['L sem 1'], color='goldenrod', ecolor='goldenrod', label='gr ctr') ax2.errorbar(timepoints, in_het_com['L mean 1'], yerr=in_het_com['L sem 1'], ecolor='k', color='k', label='het drug') ax2.errorbar(timepoints, in_het_ctr['L mean 1'], yerr=in_het_ctr['L sem 1'], color='grey', ecolor='grey', label='het ctr') ax2.set_title('Incubation Time 1h', fontsize=10) # fills ax3: light condition 24h incubation ax3.errorbar(timepoints, in_gr_com['L mean 24'], yerr=in_gr_com['L sem 24'], ecolor='brown', color='brown', label='gr drug') ax3.errorbar(timepoints, in_gr_ctr['L mean 24'], yerr=in_gr_ctr['L sem 24'], color='goldenrod', ecolor='goldenrod', label='gr ctr') ax3.errorbar(timepoints, in_het_com['L mean 24'], yerr=in_het_com['L sem 24'], ecolor='k', color='k', label='het drug') ax3.errorbar(timepoints, in_het_ctr['L mean 24'], yerr=in_het_ctr['L sem 24'], color='grey', ecolor='grey', label='het ctr') ax3.set_title('Incubation Time 24h', fontsize=10) ax3.legend(loc=5, fontsize='x-small', bbox_to_anchor=(1.6, 0.3), borderaxespad=0, title=(sec_analysed + ' sec analysed')) # fills ax4: dark condition pre-treatment ax4.errorbar(timepoints, in_gr_com['D mean 0'], yerr=in_gr_com['D sem 0'], ecolor='brown', color='brown', label='gr drug') ax4.errorbar(timepoints, in_gr_ctr['D mean 0'], yerr=in_gr_ctr['D sem 0'], color='goldenrod', ecolor='goldenrod', label='gr ctr') ax4.errorbar(timepoints, in_het_com['D mean 0'], yerr=in_het_com['D sem 0'], ecolor='k', color='k', label='het drug') ax4.errorbar(timepoints, in_het_ctr['D mean 0'], yerr=in_het_ctr['D sem 0'], color='grey', ecolor='grey', label='het ctr') ax4.set_xlabel('Timepoint') ax4.set_ylabel('Dark, Distance (mm)') # fills ax5: dark condition 1h incubation ax5.errorbar(timepoints, in_gr_com['D mean 1'], yerr=in_gr_com['D sem 1'], ecolor='brown', color='brown', label='gr drug') ax5.errorbar(timepoints, in_gr_ctr['D mean 1'], yerr=in_gr_ctr['D sem 1'], color='goldenrod', ecolor='goldenrod', label='gr ctr') ax5.errorbar(timepoints, in_het_com['D mean 1'], yerr=in_het_com['D sem 1'], ecolor='k', color='k', label='het drug') ax5.errorbar(timepoints, in_het_ctr['D mean 1'], yerr=in_het_ctr['D sem 1'], color='grey', ecolor='grey', label='het ctr') ax5.set_xlabel('Timepoint') # fills ax6: dark condition 24h incubation ax6.errorbar(timepoints, in_gr_com['D mean 24'], yerr=in_gr_com['D sem 24'], ecolor='brown', color='brown', label='gr drug') ax6.errorbar(timepoints, in_gr_ctr['D mean 24'], yerr=in_gr_ctr['D sem 24'], color='goldenrod', ecolor='goldenrod', label='gr ctr') ax6.errorbar(timepoints, in_het_com['D mean 24'], yerr=in_het_com['D sem 24'], ecolor='k', color='k', label='het drug') ax6.errorbar(timepoints, in_het_ctr['D mean 24'], yerr=in_het_ctr['D sem 24'], color='grey', ecolor='grey', label='het ctr') ax6.set_xlabel('Timepoint') # save figure f.savefig(savename, bbox_inches='tight') plt.close('all')
def create_run_plots(self): # Fitnesses of Population over Generations gen = self.summary_log_dict['gen'] avg_fit = self.summary_log_dict['avg'] best_fit = self.summary_log_dict['min'] print('Preparing plot 1...') fig, ax1 = plt.subplots() line1 = ax1.plot(gen, best_fit, "b-", label="Minimum (Best) Fitness") ax1.set_xlabel("Generation") ax1.set_ylabel("Fitness") line2 = ax1.plot(gen, avg_fit, "r-", label="Average Fitness") lns = line1 + line2 labs = [l.get_label() for l in lns] ax1.legend(lns, labs, loc="best") ax1.set_title('Run {} Fitnesses over Generations'.format( self.run_number)) print('Saving plot 1') fig.savefig('{}/fitness_best_avg_graph.png'.format(self.run_directory), bbox_inches='tight') # Speed Signal of Best Ind print('Preparing plot 2...') details_of_best_ind = self.detailed_log[str(self.hof[0])] self.df = pd.read_json(details_of_best_ind['dataFrame'], orient='columns') self.df = self.df.sort_index() self.df = self.df[['Actual Speed', 'Goal Speed', 'Error', 'Time']] #print(list(self.df.columns)) ax2 = self.df.plot(x='Time') #ax2.legend(['Actual Speed', 'Error', 'Goal Speed'], loc="upper right") ax2.legend(['Actual Speed', 'Goal Speed', 'Error'], bbox_to_anchor=(1.0, 0.8)) plt.ylabel('meters / second') ax2.set_title('Run {} Best Individuals Speed Signal'.format( self.run_number)) plt.text(0.5, 0.03, 'Fitness: {}'.format(self.best_fitness), horizontalalignment='center', verticalalignment='center', transform=ax2.transAxes) fig2 = ax2.get_figure() plt.margins(x=0.0, y=0.1) print('Saving plot 2') fig2.savefig('{}/best_ind_speed_plot.png'.format(self.run_directory), bbox_inches='tight') # Just best fitness over generations print('Preparing plot 3...') fig3, ax3 = plt.subplots() line1 = ax3.plot(gen, best_fit, "b-", label="Minimum (Best) Fitness") ax3.set_xlabel("Generation") ax3.set_ylabel("Fitness") lns = line1 labs = [l.get_label() for l in lns] ax3.legend(lns, labs, loc="best") ax3.set_title('Run {} Fitnesses over Generations'.format( self.run_number)) print('Saving plot 3') fig3.savefig('{}/fitness_best_graph.png'.format(self.run_directory), bbox_inches='tight')
def plt_bboxes(img, classes, scores, bboxes, label, figsize=(10, 10), linewidth=1.5, change=False): """Visualize bounding boxes. Largely inspired by SSD-MXNET! """ fig = plt.figure(figsize=figsize) plt.imshow(img) plt.axis('off') plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0) plt.margins(0, 0) height = img.shape[0] width = img.shape[1] colors = dict() show_caption = True if len(classes) > 10: show_caption = False for i in range(classes.shape[0]): cls_id = int(classes[i]) if cls_id >= 0: score = scores[i] if label[int(cls_id)] not in colors: colors[label[int(cls_id)]] = (random.random(), random.random(), random.random()) if change: bboxes[i] *= [height, width, height, width] ymin = int(bboxes[i, 0]) xmin = int(bboxes[i, 1]) ymax = int(bboxes[i, 2]) xmax = int(bboxes[i, 3]) rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, edgecolor=colors[label[int(cls_id)]], linewidth=linewidth) plt.gca().add_patch(rect) if show_caption: class_name = str(label[int(cls_id)]) plt.gca().text(xmin, ymin - 2, '{:s} | {:.3f}'.format(class_name, score), bbox=dict( facecolor=colors[label[int(cls_id)]], alpha=0.5), fontsize=12, color='white') return colors
def absolute_plotmaker(com1, com2, drugname, sec_analysed, tp_analysed, savename): # make copy for safety cp1 = com1.copy(deep=True) cp2 = com2.copy(deep=True) # concatenate all absolute dataframe copies df = pd.concat([cp1, cp2]).set_index(np.arange(1, 3)) # make all 0s NaN abs_com = df.replace(to_replace=0, value=np.nan) # x axis values timepoints = np.arange(1, 3) f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey='all', figsize=(12, 8)) plt.suptitle('Average change for ' + drugname + ' compared to DMSO', fontsize='x-large') plt.subplots_adjust(wspace=0.2, hspace=0.2, top=0.87) plt.minorticks_off() plt.xticks(timepoints, ('7', '20')) plt.margins(0.1, 0.1) ax = [ax1, ax2, ax3, ax4] for i in ax: i.tick_params(top='off', right='off') # fills in subplots for absolute ax1.plot(timepoints, abs_com['gr com L 1'], color='brown', label='gr drug') ax1.plot(timepoints, abs_com['gr ctr L 1'], color='goldenrod', label='gr ctr') ax1.plot(timepoints, abs_com['het com L 1'], color='k', label='het drug') ax1.plot(timepoints, abs_com['het ctr L 1'], color='grey', label='het ctr') ax1.set_title('Incubation Time 1h', fontsize=10) ax1.set_ylabel('Light, Distance (mm)') ax2.plot(timepoints, abs_com['gr com L 24'], color='brown', label='gr drug') ax2.plot(timepoints, abs_com['gr ctr L 24'], color='goldenrod', label='gr ctr') ax2.plot(timepoints, abs_com['het com L 24'], color='k', label='het drug') ax2.plot(timepoints, abs_com['het ctr L 24'], color='grey', label='het ctr') ax2.set_title('Incubation Time 24h', fontsize=10) ax2.legend(loc=5, fontsize='x-small', bbox_to_anchor=(1.4, 0.3), borderaxespad=0, title=(sec_analysed + ' sec, tp ' + tp_analysed)) ax3.plot(timepoints, abs_com['gr com D 1'], color='brown', label='gr drug') ax3.plot(timepoints, abs_com['gr ctr D 1'], color='goldenrod', label='gr ctr') ax3.plot(timepoints, abs_com['het com D 1'], color='k', label='het drug') ax3.plot(timepoints, abs_com['het ctr D 1'], color='grey', label='het ctr') ax3.set_xlabel('Concentration (uM)') ax3.set_ylabel('Dark, Distance (mm)') ax4.plot(timepoints, abs_com['gr com D 24'], color='brown', label='gr drug') ax4.plot(timepoints, abs_com['gr ctr D 24'], color='goldenrod', label='gr ctr') ax4.plot(timepoints, abs_com['het com D 24'], color='k', label='het drug') ax4.plot(timepoints, abs_com['het ctr D 24'], color='grey', label='het ctr') ax4.set_xlabel('Concentration (uM)') f.savefig(savename, bbox_inches='tight') plt.close('all')
def render_levels(self): ctrl_names = self.ctrl_names fig, ax = plt.subplots() ax.imshow(self.levels_image) # ax.axis["xzero"].set_axisline_style("-|>") # plt.tick_params( # axis='x', # which='both', # bottom=False, # top=False, # labelbottom=False) if ctrl_names[1] is None: plt.xlabel(ctrl_names[0]) # wut im_width = np.array( self.levels_image).shape[1] / self.cell_scores.shape[0] plt.xticks( np.arange(N_LVL_BINS) * (im_width * LVL_RENDER_INTERVAL) + im_width / 2, labels=[ int(round(self.ctrl_ranges[0][i * LVL_RENDER_INTERVAL], 0)) for i in range(N_LVL_BINS) ], ) pad_inches = 0 hspace = 0 bottom = 0 plt.tick_params(axis="y", which="both", left=False, right=False, labelleft=False) else: plt.xlabel(ctrl_names[1]) plt.ylabel(ctrl_names[0]) im_width = np.array( self.levels_image).shape[1] / self.cell_scores.shape[1] im_height = np.array( self.levels_image).shape[0] / self.cell_scores.shape[0] # FIXME: hack if len(self.ctrl_ranges[1]) < N_LVL_BINS: x_labels = [ int(round(self.ctrl_ranges[1][i * LVL_RENDER_INTERVAL], 0)) for i in range(N_LVL_BINS) ], else: ranges = np.arange( self.ctrl_ranges[1][0], self.ctrl_ranges[1][1] + 1, (self.ctrl_ranges[1][1] - self.ctrl_ranges[1][0]) / N_BINS) x_labels = [ int(round(ranges[i * LVL_RENDER_INTERVAL], 0)) for i in range(N_LVL_BINS) ] if len(self.ctrl_ranges[0]) < N_LVL_BINS: x_labels = [ int(round(self.ctrl_ranges[0][i * LVL_RENDER_INTERVAL], 0)) for i in range(N_LVL_BINS) ], else: ranges = np.arange( self.ctrl_ranges[0][0], self.ctrl_ranges[0][1] + 1, (self.ctrl_ranges[0][1] - self.ctrl_ranges[0][0]) / N_BINS) y_labels = [ int(round(ranges[i * LVL_RENDER_INTERVAL], 0)) for i in range(N_LVL_BINS) ] plt.xticks(np.arange(N_LVL_BINS) * (im_width * LVL_RENDER_INTERVAL) + im_width / 2, labels=x_labels) plt.yticks( np.arange(N_LVL_BINS) * (im_height * LVL_RENDER_INTERVAL) + im_height / 2, labels=y_labels[::-1], ) # ax.set_xticklabels([round(x, 1) for x in ctrl_ranges[0]]) # ax.set_yticklabels([round(x, 1) for x in ctrl_ranges[1][::-1]]) pad_inches = 0 hspace = 0 bottom = 0.05 # plt.gca().set_axis_off() plt.subplots_adjust(top=1, bottom=bottom, right=1, left=0, hspace=hspace, wspace=0) plt.margins(0, 0) # plt.gca().xaxis.set_major_locator(plt.NullLocator()) # plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.savefig(self.levels_im_path, bbox_inches="tight", pad_inches=pad_inches)
def plot_fig3(exp1, input1, exp2, input2, exp3, input3, exp4, input4): filename = [input1 + exp1 + '_ocean_month_1985_2013_d20_cli.cdf',input2 + exp2 + '_ocean_month_1985_2013_d20_cli.cdf',input3 + exp3 +\ '_ocean_month_1985_2013_d20_cli.cdf', input4 + exp4 + '_d20_access_om2_grid.nc'] f_index = 0 for ff in (filename): f_index = f_index + 1 print(f_index) ncfile = Dataset(ff) # read variables if f_index == 4: print('if ==4 case') d20_tmp = ncfile.variables['D20_WOA13'][:] else: print('else case') d20_tmp = ncfile.variables['D20_CLI'][:] #Reading lat and lon for each experiments---------------------------------------------------------------------- if f_index == 1: Xlon1 = ncfile.variables['XT_OCEAN301_420'][:] Ylat1 = ncfile.variables['YT_OCEAN78_197'][:] elif f_index == 2: Xlon2 = ncfile.variables['XT_OCEAN1201_1680'][:] Ylat2 = ncfile.variables['YT_OCEAN373_624'][:] elif f_index == 3: Xlon3 = ncfile.variables['XT_OCEAN3001_4201'][:] Ylat3 = ncfile.variables['YT_OCEAN931_1560'][:] elif f_index == 4: Xlon4 = ncfile.variables['GRID_X_T301_410'][:] Ylat4 = ncfile.variables['GRID_Y_T78_208'][:] ncfile.close() # printing dimension of the variable print('D20 var size =', d20_tmp.shape) # assign temp var to array and calculating mean with time (0) and removing the dimension by using squeeze----------------------------- if f_index == 1: d20_1 = np.squeeze(np.nanmean(d20_tmp, axis=0)) elif f_index == 2: d20_2 = np.squeeze(np.nanmean(d20_tmp, axis=0)) elif f_index == 3: d20_3 = np.squeeze(np.nanmean(d20_tmp, axis=0)) elif f_index == 4: d20_4 = np.squeeze(np.nanmean(d20_tmp, axis=0)) del d20_tmp #print('Sizes d20_1, d20_2, Xlon1, Ylat1 Xlon2, Ylat2 = ', d20_1.shape, d20_2.shape, Xlon1.shape, Ylat1.shape, Xlon1.shape, Ylat1.shape) # Creating mesh grid for each experiment----------------------------------------------------------------------------------------------- Y1, X1 = np.meshgrid(Ylat1, Xlon1, sparse=False, indexing='ij') print('Sizes X1, Y1 = ', X1.shape, Y1.shape) Y2, X2 = np.meshgrid(Ylat2, Xlon2, sparse=False, indexing='ij') print('Sizes X2, Y2 = ', X2.shape, Y2.shape) Y3, X3 = np.meshgrid(Ylat3, Xlon3, sparse=False, indexing='ij') print('Sizes X3, Y3 = ', X3.shape, Y3.shape) Y4, X4 = np.meshgrid(Ylat4, Xlon4, sparse=False, indexing='ij') print('Sizes X4, Y4 = ', X4.shape, Y4.shape) # plot fig = plt.figure(figsize=(12, 8)) #(16,8)) #====================================================ACCESS-OM2 ================================================================= ax = fig.add_subplot(221) ax.set_title('a) ACCESS-OM2', fontsize=18) # basemap #m = Basemap(projection='cyl',llcrnrlat=Y1.min(),urcrnrlat=Y1.max(),llcrnrlon=X1.min(),urcrnrlon=X1.max(),resolution='c',ax=ax) #x, y = m(X1,Y1) m = Basemap(projection='cyl', llcrnrlat=-30, urcrnrlat=30, llcrnrlon=30, urcrnrlon=120, resolution='c', ax=ax) x, y = m(X1, Y1) #cs = m.pcolormesh(X1,Y1,d20_1,shading='flat',cmap=plt.cm.PiYG,latlon=True,vmin=40., vmax=200.,ax=ax) cs = m.pcolormesh(X1, Y1, d20_1, shading='flat', cmap=plt.cm.rainbow, latlon=True, vmin=40., vmax=200.) # fig.colorbar(cs, extend='both') m.drawcoastlines(linewidth=0.15) m.fillcontinents(color='gray', lake_color='aqua') # draw parallels and meridians. parallels = np.arange(-30., 30, 10.) #arallels = np.arange(-30.,91,30.) # labels = [left,right,top,bottom] m.drawparallels(parallels, labels=[True, False, True, False]) meridians = np.arange(30., 120., 20.) m.drawmeridians(meridians, labels=[True, False, False, True]) #plt.xlabel('longitude') #plt.ylabel('latitude') #plt.colorbar(cs, shrink=0.7) plt.margins(tight=True) #Steps for ploting SD ridge box region----------------------------------------------------------------------------------------------------------- m.drawgreatcircle(50, -10, 75, -10, linewidth=2, color='k') m.drawgreatcircle(75, -10, 75, -5, linewidth=2, color='k') m.drawgreatcircle(75, -5, 50, -5, linewidth=2, color='k') m.drawgreatcircle(50, -5, 50, -10, linewidth=2, color='k') m.drawcoastlines() m.fillcontinents() # plt.savefig(outputdir1 + 'access-om2-1deg_jra-ryf_compar_CORE1_fig3.png') #=========================================================================== ACCESS-OM2-025======================================================= ax = fig.add_subplot(222) ax.set_title('b) ACCESS-OM-025', fontsize=18) # basemap m = Basemap(projection='cyl', llcrnrlat=-30, urcrnrlat=30, llcrnrlon=30, urcrnrlon=120, resolution='c', ax=ax) x, y = m(X2, Y2) cs = m.pcolormesh(X2, Y2, d20_2, shading='flat', cmap=plt.cm.rainbow, latlon=True, vmin=40., vmax=200.) m.drawcoastlines(linewidth=0.15) m.fillcontinents(color='gray', lake_color='aqua') # draw parallels and meridians. parallels = np.arange(-30., 30, 10.) # labels = [left,right,top,bottom] m.drawparallels(parallels, labels=[True, False, True, False]) meridians = np.arange(30., 120., 20.) m.drawmeridians(meridians, labels=[True, False, False, True]) #plt.xlabel('longitude') #plt.ylabel('latitude') #plt.colorbar(cs, shrink=0.7) #fig.colorbar(cs, extend='both') #plt.margins(tight=True) #Steps for ploting SD ridge region----------------------------------------------------------------------------------------------------------------- m.drawgreatcircle(50, -10, 75, -10, linewidth=2, color='k') m.drawgreatcircle(75, -10, 75, -5, linewidth=2, color='k') m.drawgreatcircle(75, -5, 50, -5, linewidth=2, color='k') m.drawgreatcircle(50, -5, 50, -10, linewidth=2, color='k') m.drawcoastlines() m.fillcontinents() #plt.show() #plt.savefig(outputdir1 + 'access-om2-1deg_jra-ryf_compar_CORE1_fig3.png #========================================================access-om-01===================================================================================== ax = fig.add_subplot(223) ax.set_title('c) ACCESS-OM2-01', fontsize=18) # basemap m = Basemap(projection='cyl', llcrnrlat=-30, urcrnrlat=30, llcrnrlon=30, urcrnrlon=120, resolution='c', ax=ax) x, y = m(X3, Y3) cs = m.pcolormesh(X3, Y3, d20_3, shading='flat', cmap=plt.cm.rainbow, latlon=True, vmin=40., vmax=200.) m.drawcoastlines(linewidth=0.15) m.fillcontinents(color='gray', lake_color='aqua') # draw parallels and meridians. parallels = np.arange(-30., 30, 10.) # labels = [left,right,top,bottom] m.drawparallels(parallels, labels=[True, False, True, False]) meridians = np.arange(30., 120., 20.) m.drawmeridians(meridians, labels=[True, False, False, True]) # plt.xlabel('longitude') # plt.ylabel('latitude') # fig.colorbar(cs, extend='both') #plt.colorbar(cs, shrink=0.7) plt.margins(tight=True) #Steps for ploting SD ridge region------------------------------------------------------------------------------------------------------------------- m.drawgreatcircle(50, -10, 75, -10, linewidth=2, color='k') m.drawgreatcircle(75, -10, 75, -5, linewidth=2, color='k') m.drawgreatcircle(75, -5, 50, -5, linewidth=2, color='k') m.drawgreatcircle(50, -5, 50, -10, linewidth=2, color='k') m.drawcoastlines() m.fillcontinents() #=========================================================Observed from WOA13======================================================================= ax = fig.add_subplot(224) ax.set_title('d) WOA13', fontsize=18) # basemap m = Basemap(projection='cyl', llcrnrlat=-30, urcrnrlat=30, llcrnrlon=30, urcrnrlon=120, resolution='c', ax=ax) x, y = m(X4, Y4) cs = m.pcolormesh(X4, Y4, d20_4, shading='flat', cmap=plt.cm.rainbow, latlon=True, vmin=40., vmax=200.) m.drawcoastlines(linewidth=0.15) m.fillcontinents(color='gray', lake_color='aqua') # draw parallels and meridians. parallels = np.arange(-30., 30, 10.) # labels = [left,right,top,bottom] m.drawparallels(parallels, labels=[True, False, True, False]) meridians = np.arange(30., 120., 20.) m.drawmeridians(meridians, labels=[True, False, False, True]) #plt.xlabel('longitude') #plt.ylabel('latitude') #plt.colorbar(cs, shrink=0.7) plt.margins(tight=True) #fig.colorbar(cs, extend='both') #Steps for ploting SD ridge region------------------------------------------------------------------------------------------------------------------- m.drawgreatcircle(50, -10, 75, -10, linewidth=2, color='k') m.drawgreatcircle(75, -10, 75, -5, linewidth=2, color='k') m.drawgreatcircle(75, -5, 50, -5, linewidth=2, color='k') m.drawgreatcircle(50, -5, 50, -10, linewidth=2, color='k') m.drawcoastlines() m.fillcontinents() cbar_ax = fig.add_axes([.91, 0.2, 0.01, 0.6]) # (xstart,ystart,xend,yend) fig.colorbar(cs, cax=cbar_ax, extend='both') #plt.legend(loc='upper right') plt.ylabel('Depth (m)', fontsize=12) #Step for saving figure in .png format========================================================== plt.savefig( outputdir1 + 'access_om2_all_resoluvations_woa13__d20_isotherm_annal_mean_cosima_paper_2019.png' )
def _plt_show_tracks(img, bboxes, labels, ids, classes=None, thickness=1, font_scale=0.5, show=False, wait_time=0, out_file=None): """Show the tracks with matplotlib.""" assert bboxes.ndim == 2 assert labels.ndim == 1 assert ids.ndim == 1 assert bboxes.shape[0] == ids.shape[0] assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 if isinstance(img, str): img = plt.imread(img) else: img = mmcv.bgr2rgb(img) img_shape = img.shape bboxes[:, 0::2] = np.clip(bboxes[:, 0::2], 0, img_shape[1]) bboxes[:, 1::2] = np.clip(bboxes[:, 1::2], 0, img_shape[0]) if not show: matplotlib.use('Agg') plt.imshow(img) plt.gca().set_axis_off() plt.autoscale(False) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=None, wspace=None) plt.margins(0, 0) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.rcParams['figure.figsize'] = img_shape[1], img_shape[0] text_width, text_height = 12, 16 for bbox, label, id in zip(bboxes, labels, ids): x1, y1, x2, y2, score = bbox w, h = int(x2 - x1), int(y2 - y1) left_top = (int(x1), int(y1)) # bbox color = random_color(id) plt.gca().add_patch( Rectangle(left_top, w, h, thickness, edgecolor=color, facecolor='none')) # id text = str(id) width = len(text) * text_width plt.gca().add_patch( Rectangle((left_top[0], left_top[1]), width, text_height, thickness, edgecolor=color, facecolor=color)) plt.text(left_top[0], left_top[1] + text_height + 2, text, fontsize=5) if out_file is not None: plt.savefig(out_file, dpi=300, bbox_inches='tight', pad_inches=0.0) if show: plt.draw() plt.pause(wait_time / 1000.) else: plt.show() plt.clf() return img
narr_all_words # Single item of all the cleaned words for entire group as single string narrative_vocab # Single list of the vocabulary used throughout all narratives (i.e., omitting all redundancies from (4)). narr_as_string # Single item of all narratives as a string of the cleaned narratives. clean_ind_narr # Single list where each item in the list is a string of the participant narratives with only clean words. #%% from wordcloud import WordCloud import matplotlib.pyplot as plt #%% All words. wordcloud = WordCloud(width=500, height=500, background_color='white').generate(narr_all_words) plt.figure() plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.margins(x=0, y=0) plt.show() #%% Just top 50 words. wordcloud = WordCloud(width=500, height=500, background_color='white', max_words=50).generate(narr_all_words) plt.figure() plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.margins(x=0, y=0) plt.show() #%% Sentiment Analysis on the raw input from nltk.sentiment.vader import SentimentIntensityAnalyzer
# inspect the cumulative response if res is None: res = np.abs(h)**2 else: res += np.abs(h)**2 # check automatically that the cumulative response is (nearly) flat freq = w / np.pi * fs / 2 I = np.where( np.logical_and(freq > fc[0], freq < np.minimum(fc[-1], 0.95 * fs / 2)))[0] err = np.max(np.abs(10 * np.log10(res[I]))) print('The error is', err, '(tol is', tol, 'dB)') plt.semilogx(w / np.pi * fs / 2., 10 * np.log10(np.abs(res)), label='Sum') plt.title('Octave Filter Bank') plt.xlabel('Frequency [Hz]') plt.ylabel('Amplitude [dB]') plt.margins(0, 0.1) plt.grid(which='both', axis='both') plt.axvline(100, color='green') # cutoff frequency plt.ylim(-60, 3) plt.xlim(10., fs / 2.) plt.legend() plt.show() # run the automatic tests test_bandpass_filterbank()
#test_data = MPFM_data.groupby(np.arange(len(MPFM_data))//5).mean() #test_data2 = MPFM_data.loc[::5,:] ''' x = MPFM_data.loc[n:m,'comb_datetime'] plt.figure('Pressure vs dP vs Temp') plt.plot(x,MPFM_data.loc[n:m,'Pressure'],'b-') plt.plot(x,MPFM_data.loc[n:m,'dP'],'g-') plt.plot(x,MPFM_data.loc[n:m,'Temperature'],'r-') #plt.xticks(rotation='vertical') plt.xticks(rotation=45) # Pad margins so that markers don't get clipped by the axes plt.margins(0.05) plt.subplots_adjust(bottom=0.15) plt.legend('PDT') # plt.show plt.figure('Flow rate') plt.plot(x,MPFM_data.loc[n:m,'Std.OilFlowrate'],'k-') plt.plot(x,MPFM_data.loc[n:m,'GOR(std)'],'g-') plt.plot(x,MPFM_data.loc[n:m,'WaterFlowrate'],'b-') plt.xticks(rotation=45) plt.legend('Test') plt.figure('Gas rate and water cut') plt.plot(x,MPFM_data.loc[n:m,'Std.GasFlowrate'],'y-') plt.plot(x,MPFM_data.loc[n:m,'Std.Watercut'],'b-')
def show_gt(data, labels, class_nums, save_path, class_names=None, only_color=False): ''' 画出标签图的RGB图和假彩色图(任意非R,G,B波段的合成图,) :param data: loadData函数中返回的data(经过标准化后的) :param labels: 地物标签值 :param class_nums: 地物总类数 :param save_path: gt图的保存路径 :param class_names: 地物标签名 :return: None ''' number_of_rows = int(data.shape[0]) number_of_columns = int(data.shape[1]) data = data.reshape(-1, data.shape[-1]) gt = labels.reshape(-1, 1) # 拉成一列 colors = color_list(class_nums) # visualizing the indian pines dataset hyperspectral cube in RGB colors = sklearn.preprocessing.minmax_scale(colors, feature_range=(0, 1)) pixels_normalized = sklearn.preprocessing.minmax_scale(data, feature_range=(0, 1)) gt_thematic_map = np.zeros(shape=(number_of_rows, number_of_columns, 3)) rgb_hyperspectral_image = np.zeros(shape=(number_of_rows, number_of_columns, 3)) cont = 0 for i in range(number_of_rows): for j in range(number_of_columns): rgb_hyperspectral_image[i, j, 0] = pixels_normalized[cont, 29] # 10 rgb_hyperspectral_image[i, j, 1] = pixels_normalized[cont, 42] # 24 rgb_hyperspectral_image[i, j, 2] = pixels_normalized[cont, 89] # 44 gt_thematic_map[i, j, :] = colors[gt[cont, 0]] cont += 1 # fig = plt.figure(figsize=(15, 15)) if only_color == True: # 仅获取假彩色图像 plt.axis('off') height, width =labels.shape plt.gcf().set_size_inches(width / 100.0, height / 100.0) # 输出width*height像素 plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.subplots_adjust(top=1, bottom=0, right=0.999, left=0, hspace=0, wspace=0) plt.margins(0, 0) plt.xticks([]) # 关闭刻度 plt.yticks([]) plt.imshow(rgb_hyperspectral_image) # 假彩色 plt.savefig(save_path, dpi=300) return fig = plt.figure() columns = 2 rows = 1 ax1 = fig.add_subplot(rows, columns, 1) plt.xticks([]) # 关闭刻度 plt.yticks([]) ax1.set_xlabel("(a)", fontsize=15) plt.imshow(rgb_hyperspectral_image) # 假彩色 ax2 = fig.add_subplot(rows, columns, 2) fig.subplots_adjust(left=0.01, top=0.96, right=0.96, bottom=0.04, wspace=0.02, hspace=0.04) plt.xticks([]) # 关闭刻度 plt.yticks([]) ax2.set_xlabel("(b)", fontsize=15) plt.imshow(gt_thematic_map) # 标签图 patches = [mpatches.Patch(color=colors[i + 1], label=class_names[i]) for i in range(len(colors) - 1)] # plt.legend(handles=patches, loc=4, borderaxespad=0.) ax2.legend(handles=patches, bbox_to_anchor=(1.05, 0), loc=3, borderaxespad=0, frameon=False) # plt.tight_layout() fig.savefig(save_path, dpi=300, bbox_inches='tight') plt.show()
def vis_attr(image_url, attr_names, attr_outputs): # subplots with 2 columns fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={"width_ratios": [1, 2]}) fig.suptitle("The attributes for {}".format(image_url), fontsize="x-large") fig.set_size_inches(6, 3) # column 1 img = cv2.imread(image_url) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) img = cv2.resize(img, (256, 512), interpolation=cv2.INTER_LINEAR) ax1.imshow(img) ax1.axis("off") # column 2 attrs = sorted(zip(attr_names, attr_outputs), key=lambda x: x[1], reverse=True) logging.info(pprint.pformat(attrs)) x_name = [d[0] for d in attrs[:5]][::-1] x_value = [d[1] for d in attrs[:5]][::-1] x = np.arange(len(x_name)) * 2 rects = plt.barh(x, x_value, color="#1b71f1", align="center") ax2.xaxis.set_tick_params(labelsize=15) ax2.set_xticks([]) ax2.set_yticks(x) ax2.set_yticklabels(x_name) ax2.set_xlim(0, 1.3) ax2.set_xlabel("Score") ax2.set_ylabel("Attribute") rect_labels = [] for rect in rects: width = rect.get_width() rankStr = "{:.4f}".format(width) yloc = rect.get_y() + rect.get_height() / 2 label = ax2.annotate(rankStr, xy=(width, yloc), xytext=(5, 0), textcoords="offset points", ha="left", va="center", color="black", weight="bold", clip_on=True) rect_labels.append(label) for key, spine in ax2.spines.items(): if key in ["left", "right", "bottom", "top"]: spine.set_visible(False) # adjust margins plt.subplots_adjust(top=0.9, bottom=0.1, right=0.98, left=0.02, hspace=0, wspace=1.0) plt.margins(0, 0) # save figure save_url = image_url.split(".")[0] + "_attr.jpg" plt.savefig(save_url) logging.info("Visualization in {}".format(save_url))
unique_states = sorted_data['State'].unique() x = sorted_data.as_matrix(columns=['Time']) y = sorted_data.as_matrix(columns=['State']) summary = 'Unique States: ' + str(len(unique_states)) + '. ' + \ 'Observed Transitions: ' + str(len(x)) fig = plt.figure() plt.style.use(['dark_background', 'ggplot']) fig.suptitle(summary) plt.ylabel('State') plt.xlabel('Time') plt.step(x, y, marker='o', label='post', where='post', linewidth=1) plt.yticks(range(len(unique_states))) plt.grid(True) plt.margins(y=0.1, x=0.05) plt.show() elif example == 2: # # Step Plot of individual observations # data = pd.read_csv(dataset_path + 'synthetic_data4.csv') # State space constructed on the fly (grid lines) unique_states = data['State'].unique() # Identify unique ID's for data extraction unique_ids = data['ID'].unique() n = len(unique_ids)
def plot_and_format_results(defocus1_list, defocus2_list, defocus_angle_list, CCC_list, resolution_list, text_print_list, time, gctf_flag): print('\nCTF done\nI am plotting the results') mean_defocus1 = round(sum(defocus1_list) / len(defocus1_list), 1) mean_defocus2 = round(sum(defocus2_list) / len(defocus2_list), 1) mean_defocus_angle = round( sum(defocus_angle_list) / len(defocus_angle_list), 1) mean_CCC = round(sum(CCC_list) / len(CCC_list), 1) mean_resolution = round(sum(resolution_list) / len(resolution_list), 1) csfont = {'fontname': 'Sans-serif'} plt.style.use('ggplot') title_font_size = 12 summary_font_size = 10 ax = plt.axes([.65, .6, .4, .4]) plt.hist(resolution_list, range(2, 30), color='royalblue', alpha=0.75) #plt.hist(resolution_list, color='royalblue',alpha=0.75) plt.title('Estimated Resolution', size=title_font_size, y=1.05) ax.set_xlabel(r'$Resolution ( {\AA})$') ax.set_ylabel(r'$No.\ of\ Micrographs$') #plt.tick_params( axis='x', which='both', bottom='on', top='off', labelbottom='on') #plt.tick_params( axis='y', which='both', right='off', left='on', labelleft='on') #ax.xaxis.set_tick_params(labeltop='on') #ax.xaxis.set_tick_params(labelbottom='off') ax1 = plt.axes([1.2, .6, .4, .4]) plt.hist(CCC_list, color='crimson', alpha=0.75) plt.title('CTF Score', size=title_font_size, y=1.04) ax1.set_xlabel(r'$CCC$') ax1.set_ylabel(r'$No.\ of\ Micrographs$') with plt.style.context(('ggplot')): defocus1_list_in_micrometer = [x / 10000 for x in defocus1_list] defocus2_list_in_micrometer = [x / 10000 for x in defocus2_list] ax3 = plt.axes([0.1, 0.02, .4, .4]) ax3.set_xlabel(r'$Defocus-x ({\mu}m)$') ax3.set_ylabel(r'$Defocus-y ({\mu}m)$') plt.hist2d(defocus1_list_in_micrometer, defocus2_list_in_micrometer, bins=40, cmap=CM.Blues) plt.colorbar() plt.title('Defocus Spread', size=title_font_size, y=1.05) plt.grid(b=False) with plt.style.context(('ggplot')): ax = plt.axes([.1, 0.6, .4, .4]) y = list(reversed(np.arange(0, 1, 0.15))) #text = [time.strftime("%c"),\ text = ['Number of Micrographs : '+ str(len(micrographs_list)),\ 'Mean Resolution estimate : '+str(mean_resolution)+ ' '+ r'$ \AA $',\ 'Mean Defocus estimate : '+str(mean_defocus1)+' '+ r'$ \AA $', \ 'Pixel size : '+ text_print_list[0] + r'$\ \AA $', \ 'Magnification : '+ text_print_list[1],\ 'Microscope : '+ text_print_list[2] ] for i in range(6): plt.text(0.05, y[i], text[i], alpha=1, clip_on=True, fontsize=summary_font_size) plt.xticks(()) plt.yticks(()) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) #ax.spines['left'].set_visible(False) ax.set_axis_bgcolor([0.95, 0.95, 0.95, 1.0]) title = 'Summary: ' + time plt.title(title, size=title_font_size, y=1.05) from matplotlib.patches import Ellipse with plt.style.context(('ggplot')): ells = [] xlimit = max(defocus1_list) / 10000 + 0.5 ylimit = max(defocus2_list) / 10000 + 0.5 for i, j, k in zip(defocus1_list, defocus2_list, defocus_angle_list): ells.append( Ellipse(xy=[xlimit / 2, ylimit / 2], width=i / 10000, height=j / 10000, angle=k, linewidth=0.4)) ax = plt.axes([.7, 0.02, .3, .4]) for e in ells: ax.add_artist(e) e.set_clip_box(ax.bbox) e.set_alpha(0.1) e.set_facecolor('none') e.set_edgecolor([0.4, 0.4, 0.4]) ax.grid(b=False) ax.set_xlim(0, xlimit) ax.set_ylim(0, ylimit) ax.set_axis_bgcolor([0.9, 0.9, 0.9, 1.0]) ax.set_xlabel(r'$Defocus-x ({\mu}m)$') ax.set_ylabel(r'$Defocus-y ({\mu}m)$') plt.title('Beam Astigmatism', size=title_font_size, y=1.05) if (gctf_flag == 1): ax = plt.axes([1.2, 0.02, .4, .4]) scores_twenty_to_eight, scores_fifteen_to_six, scores_twelve_to_five, scores_ten_to_four, scores_eight_to_three = ( [] for i in range(5)) bins_list = ['20-08A', '15-06A', '12-05A', '10-04A', '08-03A'] if (float(drift_parameters['pixel_size']) * 2) < 3: bins_list = ['20-08A', '15-06A', '12-05A', '10-04A', '08-03A'] elif (float(drift_parameters['pixel_size']) * 2) < 4: bins_list = ['20-08A', '15-06A', '12-05A', '10-04A'] scores_eight_to_three.append(1) elif (float(drift_parameters['pixel_size']) * 2) < 5: bins_list = [ '20-08A', '15-06A', '12-05A', ] scores_eight_to_three.append(1) scores_ten_to_four.append(1) elif (float(drift_parameters['pixel_size']) * 2) < 6: bins_list = ['20-08A', '15-06A'] scores_eight_to_three.append(1) scores_ten_to_four.append(1) scores_twelve_to_five.append(1) for file in glob.glob("*_gctf.log"): for bin in bins_list: myfile = open(file, "r") if (bin == '20-08A'): a = ' '.join(grep(bin, myfile)) scores_twenty_to_eight.append((a.split()[-1])) elif (bin == '15-06A'): a = ' '.join(grep(bin, myfile)) scores_fifteen_to_six.append((a.split()[-1])) elif (bin == '12-05A'): a = ' '.join(grep(bin, myfile)) scores_twelve_to_five.append((a.split()[-1])) elif (bin == '10-04A'): a = ' '.join(grep(bin, myfile)) scores_ten_to_four.append((a.split()[-1])) elif (bin == '08-03A'): a = ' '.join(grep(bin, myfile)) scores_eight_to_three.append((a.split()[-1])) myfile.close() scores_twenty_to_eight = (np.array( list(map(int, scores_twenty_to_eight)))) scores_fifteen_to_six = (np.array(list(map(int, scores_fifteen_to_six)))) scores_twelve_to_five = (np.array(list(map(int, scores_twelve_to_five)))) scores_ten_to_four = (np.array(list(map(int, scores_ten_to_four)))) scores_eight_to_three = (np.array(list(map(int, scores_eight_to_three)))) scores_list = (scores_twenty_to_eight, scores_fifteen_to_six, scores_twelve_to_five, scores_ten_to_four, scores_eight_to_three) scores = [] for name in scores_list: for i in range(1, 6): scores.append(np.sum(name == i)) j = 1 x = [] y = [] for i in scores_list: x.extend([j] * len(i)) y.extend(i) j += 1 plt.hist2d(x, y, bins=5, cmap=CM.Blues) plt.grid(b=False) x = [1.4, 2.2, 2.9, 3.7, 4.5, 5.5] y = [1.3, 2.1, 2.9, 3.7, 4.5, 5.3] k = 0 for i in range(5): for j in range(5): plt.text(x[i], y[j], scores[k], alpha=1, clip_on=True, fontsize=8) k += 1 x = [1.4, 2.3, 3.0, 3.8, 4.6] y = [1, 2, 3, 4, 5] bin_names = ['20-8', '15-6', '12-5', '10-4', '8-3'] plt.xticks(x, bin_names) plt.yticks(x, y) ax.set_xlabel(r'$Resolution\ Bins\ ({\AA})$') ax.set_ylabel(r'$CTF\ \ Fitting\ \ Score$') #ax.set_xticks(np.arange(x[0])+0.5, minor=False) #ax.xaxis.set_tick_params(labeltop='on') #ax.xaxis.set_tick_params(labelbottom='off') plt.title('CTF Validation', size=title_font_size, y=1.05) #plt.axes().set_aspect('equal') plt.margins(0.2) plt.subplots_adjust(bottom=0.15) ax3 = plt.axes([0.1, -0.2, 1.2, .05]) ax3.text(0.01, 0.6, drift_parameters['input_commands'], alpha=1, clip_on=True, fontsize=10) plt.xticks(()) plt.yticks(()) ax3.spines['top'].set_visible(False) ax3.spines['right'].set_visible(False) ax3.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) ax3.set_axis_bgcolor([1, 1, 1, 1.0]) plt.savefig('try.pdf', bbox_inches='tight') plt.gcf().clear() drift_parameters['output_text'] = [ len(micrographs_list), mean_resolution, mean_defocus1, text_print_list[0], text_print_list[1], text_print_list[2] ]
def run(self): # For each of the lambda values... for lmbda in self.lambda_vals: print('lmbda = ', lmbda) #... and for each of the alpha values... for alpha in self.alpha_vals: print('alpha = ', alpha) # Go through each sequence in the training set... for sequence in self._my_training_sets.training_sets: self.weights = np.array([[.5, .5, .5, .5, .5]]).transpose() # for sequence in training_set. for set in sequence: dw = np.array([[0, 0, 0, 0, 0]]).transpose() errors = np.array([[0, 0, 0, 0, 0]]).transpose() # Stepping through each state... for step_cnt in range(set._x_matrix.shape[1]): current_step = np.array( [set._x_matrix[:, step_cnt]]).T # Update the error err_step = set._x_matrix[:, step_cnt].reshape(5, 1) errors = lmbda * errors + np.array(err_step) # If at G end, when reward is 1 if step_cnt == set._x_matrix.shape[1] - 1: dw = dw + alpha * (set.reward - np.dot(self.weights.transpose(), \ current_step)) * errors # Otherwise, if at internal state else: next_step = np.array( [set._x_matrix[:, step_cnt + 1]]).transpose() dw = dw + alpha * (np.dot(self.weights.transpose(), next_step) - \ np.dot(self.weights.transpose(), current_step)) * errors self.weights += dw self.rmse_vals[lmbda][alpha] += np.sqrt(np.mean((self.weights - \ np.array([1/6, 1/3, 1/2, 2/3, 5/6])) ** 2)) # Create dataframe for plot df = pd.DataFrame({'Alpha': self.alpha_vals}) for lmbda in [0, 0.3, 0.8, 1]: # Get the relevant data lst = list(self.rmse_vals[lmbda].items()) r_df = pd.DataFrame(lst, columns=["Alpha", "Lambda = " + str(lmbda)]) r_df[["Lambda = " + str(lmbda)]] = \ r_df[["Lambda = " + str(lmbda)]] / self.NO_TRAINING_SETS # ... them put it all together in a dataframe df = df.merge(r_df, on="Alpha", how="left") # Plot the graph sns.lineplot(y="Alpha", x="Lambda = 0", data=df, marker="o") sns.lineplot(y="Alpha", x="Lambda = 0.3", data=df, marker="o") sns.lineplot(y="Alpha", x="Lambda = 0.8", data=df, marker="o") sns.lineplot(y="Alpha", x="Lambda = 1", data=df, marker="o") plt.xlim(0., .7) plt.ylim(0., .8) plt.margins(x=.5, y=.2) plt.xlabel("Alpha") plt.ylabel("Error") plt.show()
def cli(sample, control, title, scolor, ccolor, bcolor, lcolor, dpi, ymax, out): """ Program to create the composite plots from signal and control tag pile up CDT data matrix. \b Colors are in Hexcode, no need to add '#' in front of the hexcode. Info: Hexcode is an alphanumeric value of length six. """ click.echo('\n' + '.' * 50) # reading the CDT file. try: signalData = pd.read_csv(sample, sep='\t', index_col=0) controlData = pd.read_csv(control, sep='\t', index_col=0) except IOError: print("\nUnable to OPEN input files !\n") sys.exit(1) print("signalData shape : {}".format(signalData.shape)) print("controlData shape : {}".format(controlData.shape)) # prepare PlotData for sample signalData = signalData.round(decimals=3) # Calculating the peak value index with respect to origin. mPeak = findRelativeMaxPoint(signalData) print(mPeak) # retrieve the row index from the dataframe rowIndex = list(signalData.index) # retrieve data for signal dataset sx = list(signalData.loc[rowIndex[0]]) # retrieve values for y axis and convert them to float sy = list(signalData.columns) sy = list(map(float, sy)) # retrieve the row index from the controlData dataframe rowIndex = list(controlData.index) # retrieve data for control dataset cx = list(controlData.loc[rowIndex[0]]) # retrieve values for y axis and convert them to float cy = list(controlData.columns) cy = list(map(float, cy)) # setting the font # matplotlib.rcParams['font.family'] = "Arial" # generating the figure fig, ax = plt.subplots() # plotting the signal data plt.plot(sy, sx, color="#" + scolor, label="Signal") # adding the background color d = numpy.zeros(len(sx)) plt.fill_between(sy, sx, where=sx >= d, interpolate=False, color="#" + bcolor) # plotting the control data plt.plot(cy, cx, color="#" + ccolor, label="Control") # adding the vertical line at midpoint plt.axvline(x=0, color="#" + lcolor, linestyle='--', linewidth=2) # adding yticks and label plt.yticks([0, max(int(ymax), math.ceil(mPeak[1]))], fontsize=18) plt.ylabel('Tags', fontsize=18) # setting the padding space between the y-axis label and the y-axis if math.ceil(mPeak[1]) < 10: ax.yaxis.labelpad = -16 else: ax.yaxis.labelpad = -25 # adding text to the composite plot. (the peak location.) # https://matplotlib.org/api/_as_gen/matplotlib.pyplot.text.html # changing the co-ordinates to position the value to the top right corner left, width = 0.28, .7 bottom, height = .28, .7 right = left + width top = bottom + height # position of the peak relative to 0 value = '(' + str(int(mPeak[0])) + ')' # plt.text(x=-480, y=(int(math.ceil(mPeak[1])-0.2)), s=value, fontsize=12) plt.text(right, top, value, horizontalalignment='right', verticalalignment='top', transform=ax.transAxes, fontsize=14) # setting the ylimits for yticks ax.set_ylim(0, max(int(ymax), math.ceil(mPeak[1]))) # removing the x-axis ticks plt.xticks([]) # adding the title and increase the spine width plt.title(title, fontsize=25) plt.setp(ax.spines.values(), linewidth=2) # referance for margins # https://matplotlib.org/api/_as_gen/matplotlib.pyplot.margins.html#matplotlib.pyplot.margins plt.margins(0) plt.tick_params(length=8, width=2) plt.savefig(out, facecolor=None, dpi=int( dpi), pad_inches=0.05, bbox_inches='tight') click.echo('\n' + '.' * 50)
barlist = plt.bar(range(len(numeric_representations)), feature_score_average[ids], yerr=feature_score_stddev[ids]) for f_id in ids: fname = feature_names[f_id] if fname in all_features_200.best_trial['misc'][ 'vals'] and all_features_200.best_trial['misc']['vals'][fname][0]: barlist[f_id].set_color('red') else: barlist[f_id].set_color('blue') plt.xticks(range(len(numeric_representations)), np.array(feature_names)[ids], rotation='vertical') plt.margins(0.2) # Tweak spacing to prevent clipping of tick-labels plt.subplots_adjust(bottom=0.5) plt.show() ids = np.argsort(feature_count * -1) fig, ax = plt.subplots() barlist = plt.bar(range(len(numeric_representations)), feature_count[ids]) for f_id in ids: fname = feature_names[f_id] if fname in all_features_200.best_trial['misc'][ 'vals'] and all_features_200.best_trial['misc']['vals'][fname][0]: barlist[f_id].set_color('red') else:
except ValueError: print(f'{date} missing data.') else: dates.append(date) highs.append(high) lows.append(low) # Plot data with matplotlib fig = plt.figure(dpi=110, figsize=(10, 6)) plt.plot(dates, highs, c='red', alpha=0.5, label='highs') plt.plot(dates, lows, c='blue', alpha=0.5, label='lows') plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1) title = 'Daily High & Low temperatures - 2020\nSan Francisco, California' plt.title(title, fontsize=20) plt.xlabel('', fontsize=13) plt.ylabel('Temperature (C)', fontsize=13) plt.ylim(-10, 45) plt.tick_params(axis='both', which='major', labelsize=13), plt.margins(x=0, y=0.1) fig.autofmt_xdate() plt.legend() plt.savefig('./downloading_data/images/high_low_san_fran.png', bbox_inches='tight') plt.show()
def plot_user(cgram, user, sigma, ylim=None): sns.reset_orig() mpl.rcParams['axes.linewidth'] = 0 mpl.rcParams['axes.unicode_minus'] = False F = len(cgram.mts.features) B = len(cgram.mts[user][1]) B = int(4 * 19.5 * 60) plt.figure(figsize=(20, 2. * F)) t = cgram.mts.feat_class plt.suptitle("User {} {} Chromatogram".format(user, t.title()), name='CMU Bright', size=30, weight='bold', y=1.02) plt.subplots_adjust(hspace=0.4) plt.margins(0) axs = [] for i in range(F): ax = plt.subplot(F, 1, i + 1) axs.append(ax) ax.set_xlim(0, B - 1) x_raw = cgram.mts[user][i][:B] if type(sigma) is list: s = sigma[i] else: s = sigma x_smooth = gaussian_filter1d(x_raw, sigma=s) ax.plot(x_raw, color='k', alpha=0.2, linewidth=3) ax.plot(x_smooth, color='k', alpha=1, linewidth=1.5) ax.yaxis.grid(False) if ylim: ax.set_ylim(ylim[i]) if i == F - 1: ax.set_yticks([ylim[i][1] / 2, ylim[i][1]]) else: ax.set_yticks([0, ylim[i][1] / 2, ylim[i][1]]) label = cgram.mts.features[i] if t == 'jupyter': label = JUP_LABELS[label] pad = [18, 15, 8, 12, 8] ax.set_ylabel(label, name='CMU Bright', size=26, labelpad=pad[i]) if i == F - 1: pass else: plt.setp(ax.get_xticklabels(), visible=False) plt.setp(ax.get_yticklabels(), name='CMU Bright', size=15) window_size = cgram.mts.word_shape[1] K = cgram.codebook.K cm = COLOR_MAP[t][0] cm = cmocean.tools.crop_by_percent(cm, COLOR_MAP[t][2], which=COLOR_MAP[t][3], N=None) values = list(range(K + 1)) if cgram.rendered: values = cgram.reorder_colors(values) colors = [cm((values[i]) / K) for i in range(K + 1)] uid = cgram.users.index(user) codes = cgram.chromatogram[uid] for i in range(len(codes)): cw = int(codes[i]) if cw == 0: break for ax in axs: start = i * (window_size // 2) stop = start + window_size ax.axvspan(start, stop, facecolor=colors[cw], edgecolor=None, alpha=0.7)
def create_plot(df, forecast_horizon, granularity, spatial_ids, save_address, plot_type, test_point): mpl.style.use('default') df = df.sort_values(by=['spatial id', 'temporal id']) x_axis_label = 'Target time point' if spatial_ids is None: spatial_ids = list(random.sample(list(df['spatial id']), 1)) temporal_ids = list(df['temporal id'].unique()) plt.rc('font', size=60) number_of_temporal_ids = len(temporal_ids) + 2 fig = plt.figure() for index, spatial_id in enumerate(spatial_ids): stage = 'training' if plot_type == 'test' else 'forecast' if test_point is not None: save_file_name = '{0}{2} stage for test point #{3}.pdf'.format( save_address, spatial_id, stage, test_point + 1) else: save_file_name = '{0}{2} stage.pdf'.format(save_address, spatial_id, stage) ax = fig.add_subplot(len(spatial_ids), 1, index + 1) # add the curve of real values of the target variable temp_df = df[df['spatial id'] == spatial_id] ax.plot(list(temp_df['temporal id']), list(temp_df['real']), label='Real values', marker='o', markersize=20, linewidth=3.0, color='navy') # add the curve of predicted values of the target variable in the training, validation and testing set if plot_type != 'future': temp_train_df = temp_df[temp_df['sort'] == 'train'] ax.plot(list(temp_train_df['temporal id']), list(temp_train_df['prediction']), label='Training set predicted values', marker='o', markersize=20, linewidth=3.0, color='green') temp_val_df = temp_df[temp_df['sort'] == 'validation'] ax.plot(list(temp_val_df['temporal id']), list(temp_val_df['prediction']), label='validation set predicted values', marker='o', markersize=20, linewidth=3.0, color='orange') temp_test_df = temp_df[temp_df['sort'] == 'test'] ax.plot(list(temp_test_df['temporal id']), list(temp_test_df['prediction']), label='Testing set predicted values', marker='o', markersize=20, linewidth=3.0, color='crimson') if plot_type == 'future': temp_test_df = temp_df[temp_df['sort'] == 'future'] ax.plot(list(temp_test_df['temporal id']), list(temp_test_df['prediction']), label='Predicted values', marker='o', markersize=20, linewidth=3.0, color='orangered') ax.grid() plt.ylabel('Target') if index < len(spatial_ids) - 1: ax.tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False) if index == 0: plt.legend() ttl = plt.title('spatial id ' + str(spatial_id)) ttl.set_position([.5, 1.05]) plt.xlabel(x_axis_label, labelpad=20) plt.xticks(rotation=90) # set the size of plot base on number of temporal units and lable fonts plt.gca().margins(x=0.002) plt.gcf().canvas.draw() tl = plt.gca().get_xticklabels() maxsize = max([t.get_window_extent().width for t in tl]) inch_margin = 0.5 # inch margin xtick_size = maxsize / plt.gcf().dpi * number_of_temporal_ids + inch_margin margin = inch_margin / plt.gcf().get_size_inches()[0] plt.gcf().subplots_adjust(left=margin, right=1. - margin) plt.gcf().set_size_inches( xtick_size, plt.gcf().get_size_inches()[1] * 5 * (len(spatial_ids))) plt.subplots_adjust(hspace=.5) plt.tight_layout() plt.margins(x=0.01) try: if not os.path.exists(save_address): os.makedirs(save_address) plt.savefig(save_file_name, bbox_inches='tight', pad_inches=1) plt.close() except FileNotFoundError: print("The address '{0}' is not valid.".format(save_address))
def single_frame(num, max_pixel, nframes): snap = "%04d" % num # Define path path = '/cosma/home/dp004/dc-rope1/cosma7/SWIFT/DMO_1380_data/ani_hydro_' + snap + ".hdf5" snap = "%05d" % num data = load(path) meta = data.metadata boxsize = meta.boxsize[0] z = meta.redshift print(boxsize, z) # Define centre cent = np.array([boxsize / 2, boxsize / 2, boxsize / 2]) # Define targets targets = [ [0, 0, 0], ] # Define anchors dict for camera parameters anchors = {} anchors['sim_times'] = [ 0.0, 'same', 'same', 'same', 'same', 'same', 'same', 'same' ] anchors['id_frames'] = np.linspace(0, nframes, 8, dtype=int) anchors['id_targets'] = [ 0, 'same', 'same', 'same', 'same', 'same', 'same', 'same' ] anchors['r'] = [ 0.1, 'same', 'same', 'same', 'same', 'same', 'same', 'same' ] anchors['t'] = [5, 'same', 'same', 'same', 'same', 'same', 'same', 'same'] anchors['p'] = [ -50, 'same', 'same', 'same', 'same', 'same', 'same', 'same' ] anchors['zoom'] = [ 1., 'same', 'same', 'same', 'same', 'same', 'same', 'same' ] anchors['extent'] = [ 10, 'same', 'same', 'same', 'same', 'same', 'same', 'same' ] # Define the camera trajectory cam_data = camera_tools.get_camera_trajectory(targets, anchors) poss = data.dark_matter.coordinates.value hsmls = data.dark_matter.softenings.value / (1 + z) print(hsmls) poss -= cent poss[np.where(poss > boxsize.value / 2)] -= boxsize.value poss[np.where(poss < -boxsize.value / 2)] += boxsize.value poss /= (1 + z) tree = cKDTree(poss) dists, _ = tree.query(np.array([0, 0, 0]), k=poss.shape[0]) v = cosmo.H(z) * dists print(cosmo.H(z)) print(v) # Get images rgb_DM, ang_extent = getimage(cam_data, poss, hsmls, num, z, v) i = cam_data[num] extent = ang_extent print(ang_extent, extent) dpi = rgb_DM.shape[0] print(dpi, rgb_DM.shape) fig = plt.figure(figsize=(1, 1.77777777778), dpi=dpi) ax = fig.add_subplot(111) ax.imshow(rgb_DM, extent=ang_extent, origin='lower') ax.tick_params(axis='both', left=False, top=False, right=False, bottom=False, labelleft=False, labeltop=False, labelright=False, labelbottom=False) ax.text(0.975, 0.05, "$t=$%.1f Gyr" % cosmo.age(z).value, transform=ax.transAxes, verticalalignment="top", horizontalalignment='right', fontsize=1, color="w") ax.plot([0.05, 0.15], [0.025, 0.025], lw=0.1, color='w', clip_on=False, transform=ax.transAxes) ax.plot([0.05, 0.05], [0.022, 0.027], lw=0.15, color='w', clip_on=False, transform=ax.transAxes) ax.plot([0.15, 0.15], [0.022, 0.027], lw=0.15, color='w', clip_on=False, transform=ax.transAxes) # ax.plot([0.05, 0.15], [0.105, 0.105], lw=0.1, color='w', clip_on=False, # transform=ax.transAxes) # # ax.plot([0.05, 0.05], [0.102, 0.107], lw=0.15, color='w', clip_on=False, # transform=ax.transAxes) # ax.plot([0.15, 0.15], [0.102, 0.107], lw=0.15, color='w', clip_on=False, # transform=ax.transAxes) axis_to_data = ax.transAxes + ax.transData.inverted() left = axis_to_data.transform((0.05, 0.075)) right = axis_to_data.transform((0.15, 0.075)) dist = extent[1] * (right[0] - left[0]) / (ang_extent[1] - ang_extent[0]) print(left, right, (right[0] - left[0]) / (ang_extent[1] - ang_extent[0]), dist) if dist > 0.1: ax.text(0.1, 0.065, '%.1f $^\circ$' % dist, transform=ax.transAxes, verticalalignment="top", horizontalalignment='center', fontsize=1, color="w") # elif 100 > dist * 10**3 > 1: # ax.text(0.1, 0.065, "%.1f pkpc" % dist * 10**3, # transform=ax.transAxes, verticalalignment="top", # horizontalalignment='center', fontsize=1, color="w") # else: # ax.text(0.1, 0.065, "%.1f pkpc" % dist * 10**6, # transform=ax.transAxes, verticalalignment="top", # horizontalalignment='center', fontsize=1, color="w") plt.margins(0, 0) fig.savefig('plots/Ani/Recession/DM_recession_animation_' + snap + '.png', bbox_inches='tight', pad_inches=0) plt.close(fig)
def KeyTimeHisto(Db, Code, Key=[], Year0=[], Year1=[], Delta=5, Threshold=0, OutFile=[]): if not Year0: Year0 = min(Db.Extract('Year')) if not Year1: Year1 = max(Db.Extract('Year')) YBins = np.arange(Year0, Year1 + Delta, Delta) ItL, ItD = Db.KeyStat(Code) # Filter by threshold ItL = [K for K in ItL if ItD[K] >= Threshold] ItD = {K: V for (K, V) in ItD.items() if V >= Threshold} # Filter by key if Key: ItL = [K for K in ItL if K in Key] ItD = {K: ItD[K] for K in ItL} for N, Agn in enumerate(ItL): DbA = Db.Filter(Code, Agn, Owrite=0) YearArray = DbA.Extract('Year') NewRow = np.histogram(YearArray, YBins) if N == 0: Histo = NewRow[0] else: Histo = np.vstack([Histo, NewRow[0]]) # Plot time histogram fig = plt.figure(figsize=(8, 5)) X = YBins Y = np.arange(0, len(ItL) + 1) Z = np.log(Histo.clip(min=1E-10)) plt.pcolor(X, Y, Z, cmap='Purples', vmin=0, vmax=np.max(Z)) plt.xticks(X, map(str, X), rotation='45') plt.yticks(Y + 0.5, ItL, rotation='horizontal') plt.margins(0) plt.gca().yaxis.tick_right() plt.axes().yaxis.grid(True) plt.gca().xaxis.set_ticks_position('none') plt.gca().yaxis.set_ticks_position('none') plt.xlabel('Year', fontsize=14, fontweight='bold') plt.ylabel('Agency Code', fontsize=14, fontweight='bold') plt.tight_layout() plt.show(block=False) if OutFile: plt.savefig(OutFile, bbox_inches='tight', dpi=150)