def plot_throughput_vary_size(exp_idxs, labels, tx, sizes, rp, num_of_server, fig_num): assert len(exp_idxs) == len(labels) alo_lists = [ get_throughput_vary_size(idx, tx, sizes, rp) for idx in exp_idxs ] fig, axs = plt.subplots(figsize=fig_size) for j, alo_list in enumerate(alo_lists): axs.plot(sizes, alo_list, colors[j], marker=markers[j], label=labels[j], linestyle=linestyle, linewidth=linewidth, markersize=markersize) # axs.legend(loc='upper center', bbox_to_anchor=(0.5, 1), # ncol=4, fancybox=True, shadow=True, prop={'size': 8}) ## axs.legend(loc='upper left', ncol=legend_ncol, prop=legend_prop) ## axs.set_xlabel(vary_szs_x_label(num_of_server, tx, rp), fontsize=fontsize) plt.xscale('log') plt.xticks(sizes, tuple(sizes)) plt.minorticks_off() plt.ylim(0, 5000) axs.set_ylabel("throughput (ops/sec)", fontsize=fontsize) plt.grid(color='tab:gray', linestyle=':', linewidth=0.8) plt.tight_layout() plt.savefig(f"LAN_{fig_num}_throughput,_vary_size,_{num_of_server}_svrs") plt.show()
def plotMatches(listofaValues, listofNValues, errors, fileName = "images/scalar_effect_of_n.pdf"): fig, ax = plt.subplots() fig.suptitle("Match probability for sparse vectors") ax.set_xlabel("Dimensionality (n)") ax.set_ylabel("Frequency of matches") ax.set_yscale("log") ax.plot(listofNValues, errors[0,:], 'k:', label="a=64 (predicted)", marker="o", color='black') ax.plot(listofNValues, errors[1,:], 'k:', label="a=128 (predicted)", marker="o", color='black') ax.plot(listofNValues, errors[2,:], 'k:', label="a=256 (predicted)", marker="o", color='black') ax.plot(listofNValues, errors[3,:], 'k:', label="a=n/2 (predicted)", marker="o", color='black') ax.annotate(r"$a = 64$", xy=(listofNValues[3]+100, errors[0,3]), xytext=(-5, 2), textcoords="offset points", ha="left", color='black') ax.annotate(r"$a = 128$", xy=(listofNValues[3]+100, errors[1,3]), ha="left",color='black') ax.annotate(r"$a = 256$", xy=(listofNValues[3]+100, errors[2,3]), ha="left",color='black') ax.annotate(r"$a = \frac{n}{2}$", xy=(listofNValues[3]+100, errors[3,3]), ha="left",color='black') plt.minorticks_off() plt.grid(True, alpha=0.3) plt.savefig(fileName) plt.close()
def plot_half_log_fit(R, T, fig, R_type): plt.figure(fig) [log_base, log_coefficient], errors = fit_line(T, np.log(R)) base, coefficient = np.exp([log_base, log_coefficient]) coefficient_low_err = coefficient - np.exp(log_coefficient - errors[0]) coefficient_high_err = np.exp(log_coefficient + errors[0]) - coefficient base_low_err = base - np.exp(log_base - errors[1]) base_high_err = np.exp(log_base + errors[1]) - base plot_range = (min(T), max(T)) plot_func(lambda x: coefficient * np.power(base, x), plot_range) plt.scatter(T, R, c='g') plt.semilogy() plt.minorticks_off() plt.yticks(np.linspace(round(0.9 * min(R), 0), round(1.05 * max(R), 0), 5)) plt.xticks(np.linspace(round(plot_range[0], 0), round(plot_range[1], 0), 5)) plt.grid(True, which='both') # coefficient_err_str = '$^{' + f'{coefficient_high_err:.2f}' + '}_{' + f'{coefficient_low_err:.2f}' + '}$' # base_err_str = '$^{' + f'{base_high_err:.2f}' + '}_{' + f'{base_low_err:.2f}' + '}$' plt.xlabel('$\mathbb{\Delta}$T[°C]') plt.ylabel('R[$\mathbb{\Omega}$]')
def draw_lines(line_data, path, y_min, y_max, is_log=False, flag=0): plt.figure(figsize=(7, 4.2)) for i in range(0, len(line_data)): plt.plot(x, line_data[i], label=line_legends[i], color=line_colors[i], marker=line_markers[i], markersize=10, fillstyle='none', linestyle='--') if is_log: plt.yscale('log') plt.minorticks_off() plt.xlabel(x_label, fontsize=18) plt.ylabel(y_label[flag], fontsize=18) plt.xticks(x, x_tick_labels, fontsize=18) plt.yticks(fontsize=18) plt.ylim(y_min, y_max) plt.xlim(min(x) - 0.25, max(x) + 0.25) plt.legend(loc=0, ncol=3, fontsize=18, frameon=False, handletextpad=0.1, columnspacing=1.0, handlelength=1.5) plt.grid() plt.tight_layout() # plt.show() plt.savefig(path) plt.close()
def plotLog(filename, watch=False, **kargs): fig, ax = plt.subplots(1,1) fig.subplots_adjust(hspace=0) plt.show(block=False) while True: with open(filename, 'r') as f: names = f.readline().rstrip().split('\t') df = pd.read_csv(filename, skiprows=1, delimiter='\t', parse_dates=[0], na_values="<undefined>", names=names) #df = df.dropna() ax2 = df.plot("Time", subplots=True, ax=ax, sharex=True) dt = df.iloc[-1,0]-df.iloc[0,0] for a in ax2: if dt.seconds < 15*60: a.xaxis.set_major_locator(mpl.dates.MinuteLocator(interval=1)) elif dt.seconds < 3*60*60: a.xaxis.set_major_locator(mpl.dates.MinuteLocator(interval=5)) else: a.xaxis.set_major_locator(mpl.dates.MinuteLocator(interval=15)) a.xaxis.set_major_formatter(mpl.dates.DateFormatter('%H:%M')) a.grid() plt.minorticks_off() if watch: mypause(3) else: plt.show() if not watch: break
def plotLog(filename, watch=False, **kargs): fig, ax = plt.subplots(1,1) fig.subplots_adjust(hspace=0) plt.show(block=False) while True: with open(filename, 'r') as f: names = f.readline().rstrip().split('\t') df = pd.read_csv(filename, skiprows=1, delimiter='\t', parse_dates=[0], na_values="<undefined>", names=names) #df = df.dropna() ax2 = df.plot("Time", subplots=True, ax=ax, sharex=True) dt = df.iloc[-1,0]-df.iloc[0,0] for a in ax2: if dt.seconds < 15*60: a.xaxis.set_major_locator(mpl.dates.MinuteLocator(interval=1)) elif dt.seconds < 3*60*60: a.xaxis.set_major_locator(mpl.dates.MinuteLocator(interval=5)) else: a.xaxis.set_major_locator(mpl.dates.MinuteLocator(interval=15)) a.xaxis.set_major_formatter(mpl.dates.DateFormatter('%H:%M')) a.grid() plt.minorticks_off() if watch: mypause(3) else: plt.show()
def plotMatches(listofaValues, listofNValues, errors, fileName="images/scalar_effect_of_n.pdf"): fig, ax = plt.subplots() fig.suptitle("Match probability for sparse vectors") ax.set_xlabel("Dimensionality (n)") ax.set_ylabel("Frequency of matches") ax.set_yscale("log") ax.plot(listofNValues, errors[0, :], 'k:', label="a=64 (predicted)", marker="o", color='black') ax.plot(listofNValues, errors[1, :], 'k:', label="a=128 (predicted)", marker="o", color='black') ax.plot(listofNValues, errors[2, :], 'k:', label="a=256 (predicted)", marker="o", color='black') ax.plot(listofNValues, errors[3, :], 'k:', label="a=n/2 (predicted)", marker="o", color='black') ax.annotate(r"$a = 64$", xy=(listofNValues[3] + 100, errors[0, 3]), xytext=(-5, 2), textcoords="offset points", ha="left", color='black') ax.annotate(r"$a = 128$", xy=(listofNValues[3] + 100, errors[1, 3]), ha="left", color='black') ax.annotate(r"$a = 256$", xy=(listofNValues[3] + 100, errors[2, 3]), ha="left", color='black') ax.annotate(r"$a = \frac{n}{2}$", xy=(listofNValues[3] + 100, errors[3, 3]), ha="left", color='black') plt.minorticks_off() plt.grid(True, alpha=0.3) plt.savefig(fileName) plt.close()
def draw(data, name): for i, d in enumerate(data): fig = plt.figure(figsize=(2, 10)) ax = plt.subplot(111) ax.set_ylim((-70, 70)) bars = ax.bar([0], [d], width=1, color='r' if d < 0 else 'g') ax.set_yscale('symlog') for rect in bars: height = rect.get_height() plt.text(rect.get_x() + rect.get_width() / 2.0, height, ('' if height < 0 else '+') + '%d' % int(height), ha='center', va='bottom', size=36, color='w' if height < 0 else 'g') plt.gca().get_xaxis().set_ticks([]) plt.gca().get_yaxis().set_ticks([]) plt.minorticks_off() # plt.majorticks_off() plt.ylabel('Net Resource Collected', fontsize=36) plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.gca().spines['bottom'].set_visible(False) plt.gca().spines['left'].set_visible(False) ax.axhline(y=0, color='k') plt.tight_layout(pad=0.4) plt.savefig('img/explanatory_sep/' + name + '-' + str(i + 1) + '.pdf', transparent=True)
def plot_export_txt(df: pd.DataFrame, file_name: str, config, timestamps): print('Creating plot for', file_name) df = df.query(config.query) axes = df.plot(subplots=True, sharex=True, figsize=(60, 15), title=file_name) # specify date format and tick interval date_format = '%H:%M:%S' tick_interval = 1500 # select ticks from index ticks_to_use = df.index[::tick_interval] # create labels by reformatting the selected indices labels = [i.strftime(date_format) for i in ticks_to_use] ax = axes[0] # First subplot, alternative = ax = plt.gca() # apply indices and matching labels to the plot ax.set_xticks(ticks_to_use) ax.set_xticklabels(labels, rotation=45) # rename x axis plt.xlabel('Zeitpunkt') # disable minor ticks that would interfere with new ticks plt.minorticks_off() for i in range(len(timestamps)): c = 'g' if i % 2 == 0 else 'r' plt.axvline(x=timestamps[i], linewidth=1, color=c, ls='--') # plt.axvline(x=row['ts_end'], linewidth=0.5, color='r', ls='--') plt.show()
def set_yticks(self, yticks, index=None, rotation='horizontal'): if index == None: plt.yticks(np.arange(len(yticks)), yticks, rotation=rotation) else: plt.yticks(index, yticks, rotation=rotation) #plt.locator_params(numticks=len(index)) plt.minorticks_off()
def plot_oligo_rate_histo(norm_data): # norm_data = [] bins_bound = np.linspace(0, 5, 36) # n, bins, patches = plt.hist(slopes, bins_bound, normed=True, stacked = True) # total = sum(n) # for i in range(len(n)): # norm = (n[i] / total) # print norm, "=norm" # norm_data.append(norm) # print norm_data, sum(norm_data) plt.hist(norm_data, bins=bins_bound, color='black') plt.xlabel('Rate of BAX Oligomerization\n(RFU min^-1)', size=16) plt.ylabel('Frequency\n(number of mitochondria)', size=16) plt.legend() #plt.title('Rate of Aggregation per Mitochondria of One Cell ') #plt.ylim(ymax = 2) plt.xlim(xmax=1) #plt.axes(frameon = False) #ax1.get_xaxis().tick_bottom() #ax1.get_yaxis().tick_left() #plt.rcParams['axes.linewidth'] = 2 plt.axhline(y=0, linewidth=3, color='black') plt.axvline(x=0, linewidth=3, color="black") plt.xticks(size=14) plt.yticks(size=14) plt.minorticks_off() plt.tick_params('both', length=8, width=2, which='major') plt.show()
def draw_distances(statistics, sample_types, normalize, output_filename, ylim=(2**-3, 2**1)): plt.figure(figsize=(8, 5)) fontsize = 30 xs = [1000, 3000, 10000, 30000] for sample_type_id, sample_type in enumerate(sample_types): ys_main_median = [] ys_main_min = [] ys_main_max = [] for x in xs: main_distances = statistics[x][sample_type] y_median = np.median(main_distances) y_min = np.min(main_distances) y_max = np.max(main_distances) if normalize: normalizer = np.mean(statistics[x]["synthetic"]) else: normalizer = 1.0 ys_main_median.append(y_median / normalizer) ys_main_min.append(y_min / normalizer) ys_main_max.append(y_max / normalizer) markersize_coef = 0.5 label = sample_type_to_label[sample_type] plt.plot(xs, ys_main_median, color=sample_type_to_color[sample_type], marker='o', markersize=fontsize * markersize_coef, label=f"{label}") plt.fill_between(xs, ys_main_min, ys_main_max, color=sample_type_to_color[sample_type], linestyle="--", alpha=0.1) plt.xlabel("# sequencing reads", fontsize=fontsize) plt.ylabel(f"{'Normalized' if normalize else 'Explicit'} JSD", fontsize=fontsize) plt.xscale("log") ax = plt.gca() ax.set_xticks(xs) ax.get_xaxis().set_major_formatter(mpl.ticker.ScalarFormatter()) plt.ylim(ylim) plt.setp(plt.gca().get_xticklabels(), fontsize=fontsize * 0.8) plt.setp(plt.gca().get_yticklabels(), fontsize=fontsize * 0.8) plt.minorticks_off() plt.tight_layout() plt.savefig(output_filename, format="png") plt.show()
def plot3_regime(vars1, vars2, vars3, plotname, shem=True, logplot=True, add_ss=True): """Plot 3 sets of psi edge loc vs psi max on the same axis for comparison""" plot_regime(vars1, shem=shem) plot_regime(vars2, varcolor='b', shem=shem) plot_regime(vars3, varcolor='r', shem=shem) if add_ss: plt.plot(vars_90[0], -1.*vars_90[1], 'sk', ms=10) plt.plot(vars_91[0], -1.*vars_91[1], 'sk', ms=10) plt.plot(vars_92[0], -1.*vars_92[1], 'sk', ms=10) plt.plot(vars_95[0], -1.*vars_95[1], 'sk', ms=10) plt.plot(vars_100[0], -1.*vars_100[1], 'sk', ms=10) plt.plot(vars_105[0], -1.*vars_105[1], 'sk', ms=10) plt.xlabel('Cell edge') plt.ylabel('Max 500 hPa Mass Streamfunction') plt.grid(True,linestyle=':') if logplot: plt.xscale('log') plt.yscale('log') plt.minorticks_off() plt.xlim(0.625,40) plt.ylim(100,600) plt.xticks([1.25,2.5,5,10,20,40]) plt.yticks([75,150,300,600]) ax=plt.gca() ax.get_xaxis().set_major_formatter(tk.ScalarFormatter()) ax.get_yaxis().set_major_formatter(tk.ScalarFormatter()) plt.tight_layout() else: plt.ylim([0,600]) plt.xlim([0,40]) plt.savefig('/scratch/rg419/plots/seasons/regimefig_' + plotname +'.pdf', format='pdf') plt.close()
def plot_AMI(): ax = plt.gca() ax.autoscale(enable=True, axis='x', tight=True) plot_data_at_index(ax=ax, filename='./data/house_ami_no_self.csv', x_idx=0, y_idx=1, ls='-', label='House', regression=True, marker='x') plot_data_at_index(ax=ax, filename='./data/senate_ami_no_self.csv', x_idx=0, y_idx=1, ls='-', label='Senate', regression=True, marker='o') plot_top_presidents(ax) plot_color_bands(ax) ax.legend() ax.set_xlabel('Congress') ax.set_ylabel('AMI') ax.set_ylim([0, 1]) ax.grid() plt.minorticks_off() plt.savefig('./plots/main_ami_color_no_self', bbox_inches='tight', pad_inches=0.1) plt.show()
def main(): citation_graph = load_graph(CITATION_URL) print compute_in_degrees(citation_graph) start_time = timeit.default_timer() dist = in_degree_distribution(citation_graph) print 'dist =', dist total = sum(dist.itervalues()) normalized = {key: float(value)/total for key, value in dist.items()} print(timeit.default_timer() - start_time) x = normalized.keys() y = normalized.values() print len(y) plt.loglog(x, y, 'ro') plt.yscale('log') plt.xscale('log') plt.minorticks_off() plt.xlabel('In-degree distribution') plt.ylabel('Normalized In-degree distribution') plt.title('Graph of Citations') plt.grid(True) plt.savefig('citations-q1.png') plt.show()
def save_image(self, path): plt.minorticks_off() self.fig.savefig(path, bbox_inches='tight') self.fig.savefig(path + '.eps', bbox_inches='tight', format='eps', dpi=600)
def drawdata1(a, b): font = {'fontname': 'Arial', 'weight': 'bold', 'size': 15} a = np.pi*a plt.plot( a, b, 'ks', label='LBE', markersize=7, linewidth=2.0) a1, b1 = np.polyfit((np.sin(2*a))**2.0, b, 1) fit = a1*(np.sin(2*a))**2.0+b1 plt.plot(a, fit, '-k', label=r'$0.35906\sin(2\theta)^2+0.061074$', linewidth=2.0) plt.minorticks_off() plt.xlabel(r'$\theta$', font, fontsize=20) #plt.ylabel(r'$\frac{K}{L^{2}_p}$', font, fontsize=20) plt.ylabel(r'$F_2$', font, fontsize=20) plt.legend(frameon=False, numpoints=1, loc=9) ax = plt.gca() setupfunc(ax) plt.tick_params(axis='x', which='major', length=5, width=2) plt.tick_params(axis='x', which='minor', length=3) plt.tick_params(axis='y', which='major', length=5, width=2) plt.tick_params(axis='y', which='minor', length=3) # ax.spines['top'].set_visible(False) # ax.spines['right'].set_visible(False) #plt.tick_params(top='off', right='off') savefig(r'4-3permeability.eps', bbox_inches='tight') plt.show()
def plot_sv_diagram(x, cn, fusions, outfile, **kwargs): """Plots a Campbell-gram with default-y settings. Key word arguments are aesthetic options which can be safely left blank: xmin, xmax, ymin, ymax, xticks, yticks, logbase, xlabel, ylabel """ # X axis is in Mb x = x / 1e6 fig = setup_figure() cn_axes, fusion_axes = sv_diagram_axes() # Copy number plot_cn(cn_axes, x, cn) set_cn_axes_options(cn_axes, x, cn, kwargs) set_cn_axes_aesthetics(cn_axes) plt.minorticks_off() # Fusions setup_fusion_axes(fusion_axes, min(x), max(x)) for fusion in fusions: plot_fusion(cn_axes, fusion_axes, fusion) # Ensure everything fits plt.tight_layout() # Output fig.savefig(outfile) plt.close(fig)
def plot_auditor_time_vs_stmt_len(plt, expr_dict, num_servers): import copy num_bits = num_bits_vec[0] x = np.array(num_clients_vec) y_vals = [] for count in range(10): y = [] x_new = [] for i, x_elem in enumerate(x): if expr_dict[(num_bits, num_servers, x_elem, count)] is not None: print(x_elem, num_servers) y.append(expr_dict[(num_bits, num_servers, x_elem, count)].auditer_avg_time / 10**6) y_vals.append(y) y_vals = np.array(y_vals) errors_bar_y = 1.95 * np.std(y_vals, axis=0) / ((len(y_vals) * 10)**0.5) y_vals_avg = np.average(y_vals, axis=0) print(y_vals) print(errors_bar_y) plt.minorticks_off() plt.errorbar( x, y_vals_avg, yerr=errors_bar_y, # marker=matplotlib.markers.CARETDOWNBASE, label=str(num_servers) + " servers", )
def save(self): if self.log_path is None: return ar = np.array(self.buffer) logger.info( "Minimal action: {}, max action: {}, unique items: {} in {} items". format(ar.min(), ar.max(), len(np.unique(ar)), ar.shape)) with open(self.log_path, self.file_mode) as f: w = csv.writer(f) for row in self.buffer: w.writerow(row) item_id, values = list(zip(*self.counter.items())) item_id, values = np.array(item_id), np.array(values) ind_sort = np.argsort(item_id) item_id = item_id[ind_sort] values = values[ind_sort] plt.hist(item_id, weights=values, bins=100, log=True) plt.xlabel("Item ID") plt.ylabel("Predictions") plt.minorticks_off() plt.savefig(self.log_path.parent / "prediction_distribution.png") with open(self.log_path.parent / "test_prediction_counter.pkl", "wb") as f: pickle.dump(self.counter, f) plt.clf()
def loss_boxplot(expt, dataset_name='test1', ax=None): # Generate loss data, and set labels losses = expt.compute_losses(dataset_name) labels = [ r"$\mathcal{L}_1$", r"$\mathcal{L}_2$", r"$\mathcal{L}_3$", r"$\mathcal{L}_4$", r"$\mathcal{L}_5$", r"$\mathcal{L}_6$" ] # Create a figure or instantiate new axes: set_axes(ax) # Plot the losses, give the labels plt.boxplot(losses, labels=labels, showfliers=False, medianprops={'color': 'black'}) # Format the Y-Axis plt.ylabel("Relative Error") plt.ylim([1e-6, 1e-3]) plt.yscale('log') ax = plt.gca() ax.set_yticks([1e-6, 1e-5, 1e-4, 1e-3]) ax.set_yticklabels( [r"$10^{-6}$", r"$10^{-5}$", r"$10^{-4}$", r"$10^{-3}$"]) plt.minorticks_off()
def gen_depthwise(): gtx_ = np.asarray(gtx[:, 0:2]).transpose() tx1_ = np.asarray(tx1[:, 0:2]).transpose() tk1_ = np.asarray(tk1[:, 0:2]).transpose() raspi_ = np.asarray(raspi[:, 0:2]).transpose() out = [gtx_, tx1_, tk1_, raspi_] for i in range(len(platform_dd)): plt.ylabel('log time per image(ms)') plt.yscale('log') plt.ylim([0.5, 300 * 10**(i + 1) + 8**(i + 1)]) #manipulating axis plt.xlabel('batch size') plt.xscale('log') plt.xlim([0.5, 256]) plt.xticks(x_dd, x_dd) plt.figtext(.5, .93, platform_dd[i], fontsize=18, ha='center') plt.figtext(.5, .9, 'mobilenet improvement by depthwise convolution', fontsize=10, ha='center') plt.minorticks_off() line = plt.plot(x_dd, out[i][0], '--o', label='mobilenet') line1 = plt.plot(x_dd, out[i][1], '--o', label='mobilenet depthwise') plt.legend() #plt.show() plt.savefig('mobilenet_' + platform_dd[i] + '.png', bbox_inches='tight') plt.clf() plt.close()
def plot1d(func, lb, ub, step, stop_val=None, stop_iter=50, svname='', show=False, esp=1e-7, adaptive=False): x = np.arange(lb, ub, step) prog = Progress(len(x), label='plotting', response_time=60) if adaptive: y = [] stop_cnt = 0 for xval in x: prog.count() yval = func(xval) y.append(yval) if abs(yval - stop_val) < esp: stop_cnt += 1 if stop_cnt > stop_iter: break else: stop_cnt = 0 x = x[:len(y)] else: y = fp.lmap(func, x) fig = plt.figure(1) plt.minorticks_off() plt.plot(x, y, 'k', color='b') if svname != '': plt.savefig(svname) if show: plt.show() plt.close(fig) return len(x)
def plot_latency_vary_size(exp_idxs, labels, tx, sizes, rp, num_of_server, tag, fig_num): assert len(exp_idxs) == len(labels) alo_lists = [get_latency_vary_size(idx, tx, sizes, rp, tag) for idx in exp_idxs] fig, axs = plt.subplots(figsize=fig_size) for j, alo_list in enumerate(alo_lists): axs.plot(sizes, alo_list, colors[j], marker=markers[j], label=labels[j], linestyle=linestyle, linewidth=linewidth, markersize=markersize) axs.legend(loc='upper left', ncol=legend_ncol, prop=legend_prop) ## axs.set_xlabel(vary_szs_x_label(num_of_server, tx, rp), fontsize=fontsize) plt.xscale('log') plt.xticks(sizes, tuple(sizes)) plt.minorticks_off() # plt.yticks(np.arange(0, 2500, step=500)) # plt.yticks(np.arange(0, 4500, step=500)) plt.yticks(np.arange(0, 6500, step=500)) tag = ' '.join(tag.split("_")) axs.set_ylabel(f"{tag} latency (µs)", fontsize=fontsize) plt.grid(color='tab:gray', linestyle=':', linewidth=0.8) plt.tight_layout() tag = '_'.join(tag.split(" ")) plt.savefig(f"LAN_{fig_num}_latency,_vary_size,_{num_of_server}_svrs,_{tag}") plt.show()
def metrics_progress_plot(df, path, sliding_window=5, y="Return", df_type="train"): fig = plt.figure(figsize=[6.4, 4.8]) df = df[df.Epoch >= 20] rolling_metric = df.groupby( ["Method", "Seed", "Dataset"])[y].apply(lambda x: x.ewm(span=sliding_window).mean()) df.loc[:, y] = rolling_metric df.Epoch = df.Epoch // 89 print(df.iloc[0]) ax = sns.lineplot(x="Epoch", y=y, seed=16, hue="Method", data=df) plt.xlabel("Iteration") handles, labels = ax.get_legend_handles_labels() try: index_of = labels.index("Method") handles.pop(index_of) labels.pop(index_of) except ValueError: pass ax.legend_.remove() # sort both labels and handles by labels labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0])) ax.legend(handles, labels) # , bbox_to_anchor=(1,0.7)) plt.minorticks_off() plt.tight_layout() save_plot(path, "{}_{}".format(df_type, y.lower()))
def plot_temps(self): """ Plot NAND temps vs Ambient Temp :return: """ self._get_max_bw() try: plot_df = self.comb_df[[ "Drive Temperature.1", "Drive Temperature.2", "Drive Temperature.3", "Drive Temperature.4", "Ambient" ]].copy() cols_to_plot = [ "Drive Temperature.1", "Drive Temperature.2", "Drive Temperature.3", "Drive Temperature.4", "Ambient" ] except KeyError as e: logging.warning( "KeyError {}, trying other configuration.".format(e)) plot_df = self.comb_df[[ "Drive Temperature", "Drive Temperature.1", "Drive Temperature.2", "Drive Temperature.3", "Ambient" ]].copy() cols_to_plot = [ "Drive Temperature", "Drive Temperature.1", "Drive Temperature.2", "Drive Temperature.3", "Ambient" ] # Drop all rows that have an NaN value plot_df = plot_df.dropna(axis=0, how="any") plot_df = plot_df.drop_duplicates() fig, ax1 = plt.subplots(figsize=self.figsize) # plot all columns in cols_to_plot for col in cols_to_plot: ax1.plot(plot_df.index, plot_df[col], label=col) # ax1.scatter(plot_df.index, plot_df[col], label = col) plt.minorticks_off() plt.tick_params(axis='x', which='minor', bottom=False) plt.grid(axis='y') # Set y axis ticks at every 1 degree ax1.yaxis.set_major_locator(ticker.MultipleLocator(1)) ax1.set_ylabel("Temperature (C)") ax1.legend() plt.title("Drive Temperature vs Ambient Temperature") save_path = os.path.join(self.dirpath, "Drive_Temp_vs_Ambient.png") self.log_q.put("Saving Drive_Temp_vs_Ambient at {}".format(save_path)) logging.info("Saving drive_temp_vs_ambient at {}".format(save_path)) plt.savefig(save_path, dpi='figure')
def grid(b=True, which="major"): # This is consistent with `ideplot.grid`, not with `matplotlib.pyplot.grid`. if which in ["minor", "both"]: if b: plt.minorticks_on() else: plt.minorticks_off() plt.grid(b, which)
def bins_labels(bins, **kwargs): """ Center bins for histogram (see: https://stackoverflow.com/a/42264525/1679629) """ bin_w = (max(bins) - min(bins)) / (len(bins) - 1) plt.xticks(numpy.arange(min(bins) + bin_w / 2, max(bins) + 1, bin_w), bins, **kwargs) plt.xlim(bins[0], bins[-1]) plt.minorticks_off()
def make_plot(show=False): # Set up figure fig = plot.figure(figsize=(10, 6), dpi=dpi) ax = fig.add_subplot(111) ### Plot ### x = radii y = freq result = plot.pcolormesh(x, y, np.abs(fft_data), cmap=cmap) result.set_clim(clim) plot.colorbar() # Reference Line xref = radii yref1 = np.power(radii, -1.5) * (np.sqrt(5) - 2.0) yref2 = np.power(radii, -1.5) ref_color = "deepskyblue" plot.plot(xref, yref1, c=ref_color, linewidth=2, linestyle="--") plot.plot(xref, yref2, c=ref_color, linewidth=2) # Axes plot.xlim(radii[0], radii[-1]) plot.ylim(min(freq[freq > 0]), max(freq)) plot.xscale("log") plot.yscale("log") xticks = np.concatenate( (np.logspace(np.log10(0.5), np.log10(1.0), 3), np.logspace(np.log10(1.0), np.log10(2.5), 5)[1:])) plot.minorticks_off( ) # Fixes known bug where xticks aren't removed if scale is log plot.xticks(xticks, ['%.2f' % xtick for xtick in xticks]) yticks = np.logspace(np.log10(1.0 * cadence / len(frames) / rate), np.log10(0.5 * cadence / rate), 10) plot.yticks(yticks, ['%.3f' % ytick for ytick in yticks]) # Annotate Axes radius_unit = r'$r_\mathrm{p}$' plot.xlabel("Radius [%s]" % radius_unit, fontsize=fontsize) plot.ylabel(r"$\omega / \Omega_\mathrm{0}$", fontsize=fontsize) # Save, Show, and Close directory_name = os.getcwd().split("/")[-1] if version is None: save_fn = "%s/%s_FFTverticalVelocityMap_%04d_%04d_%04d.png" % ( save_directory, directory_name, frames[0], frames[-1], args.rate) else: save_fn = "%s/v%04d_%s_FFTverticalVelocityMap_%04d_%04d_%04d.png" % ( save_directory, version, directory_name, frames[0], frames[-1], args.rate) plot.savefig(save_fn, bbox_inches='tight', dpi=dpi, pad_inches=0.2) if show: plot.show() plot.close(fig) # Close Figure (to avoid too many figures)
def plot_curve(filename): with open(filename) as csvfile: r = csv.DictReader(csvfile) layers = [] #errors_49 = [] errors_68 = [] for row in r: _layers = row["layers"].replace("[", "").replace("]", "") if "," in _layers: continue neurons = int(_layers) if neurons < 48: continue #if neurons not in [2**i for i in range(12)]: # continue layers.append(neurons) #errors_49.append(float(row["test_error_49"])) #errors_68.append(float(row["test_error_68"])) errors_68.append( (float(row["best_e68"]) + float(row["best_h68"])) / 2) combined = list(zip(layers, errors_68)) combined = sorted(combined, key=lambda x: x[0]) layers = [x[0] for x in combined] #errors_49 = [x[1] for x in combined] errors_68 = [x[1] for x in combined] ax = plt.gca() #ax.set_aspect(2) plt.xticks([1, 32, 64, 128, 192, 256, 384, 512], rotation=0) ax.set_yscale("log", basey=10) ax.set_yticks([ min(errors_68), 0.2, 0.4, 0.6, 0.8, 1.0, 1.5, 2.0, 2.5, 3.0, max(errors_68) ]) plt.minorticks_off() #plt.grid() ax.xaxis.set_minor_formatter(mticker.ScalarFormatter()) #ax.yaxis.set_minor_formatter(mticker.ScalarFormatter()) ax.yaxis.set_major_formatter(mticker.ScalarFormatter()) #plt.plot(layers, errors_49, '--ro', label="test error (49 landmarks)") plt.plot(layers, errors_68, '--bo', label="test error (68 landmarks)") #plt.legend() #plt.subplots_adjust(bottom=0.15) plt.xlabel("latent vector dimension") plt.ylabel("IOD normalized RMSE in % (log scale)") #plt.savefig("/tmp/pdm_reconstruction_error.png", bbox_inches='tight') plt.show() plt.clf()
async def gen_plot(ctx, tmf: typing.Optional[str] = '24'): user_range = 0 if tmf.lower() in ['w', 'week', 'weeks']: user_range = 168 - 1 interval = 24 date_format = '%m/%d' timedo = 'week' description = 'Players online in the past ' + timedo + ':' elif tmf.lower() in ['12', '12hrs', '12h', '12hr']: user_range = 12 - 0.15 interval = 1 date_format = '%H' timedo = '12hrs' description = 'Players online in the past ' + timedo + ':' else: user_range = 24 - 0.30 interval = 2 date_format = '%H' timedo = '24hrs' description = 'Players online in the past ' + timedo + ':' #Get data from csv df = pd.read_csv('csv/playerstats.csv', header=None, usecols=[0, 1], parse_dates=[0], dayfirst=True) lastday = datetime.now() - timedelta(hours = user_range) last24 = df[df[0]>=(lastday)] # Plot formatting / styling matplotlib plt.style.use('seaborn-pastel') plt.minorticks_off() fig, ax = plt.subplots() ax.grid(b=True, alpha=0.2) ax.set_xlim(lastday, datetime.now()) # ax.set_ylim(0, 10) Not sure about this one yet for axis in [ax.xaxis, ax.yaxis]: axis.set_major_locator(ticker.MaxNLocator(integer=True)) ax.xaxis.set_major_formatter(md.DateFormatter(date_format)) ax.xaxis.set_major_locator(md.HourLocator(interval=interval)) for spine in ax.spines.values(): spine.set_visible(False) for tick in ax.get_xticklabels(): tick.set_color('gray') for tick in ax.get_yticklabels(): tick.set_color('gray') #Plot and rasterize figure plt.gcf().set_size_inches([5.5,3.0]) plt.plot(last24[0], last24[1]) plt.tick_params(axis='both', which='both', bottom=False, left=False) plt.margins(x=0,y=0,tight=True) plt.tight_layout() fig.savefig('temp.png', transparent=True, pad_inches=0) # Save and upload Plot image = discord.File('temp.png', filename='temp.png') plt.close() embed = discord.Embed(title=server_name, description=description, colour=12320855) embed.set_image(url='attachment://temp.png') await ctx.send(file=image, embed=embed)
def main(): # plot cost ticks_x, costmatrix = generate_costlist(total_torsions=20) print("t r") for x, y in zip(ticks_x, costmatrix): print(*x, y) ticks_x = ["{:},{:}".format(*x) for x in ticks_x] ticks_x = np.asarray(ticks_x) xticks = list(range(len(ticks_x))) xticks = np.asarray(xticks) max_cost = 10**7 idx = np.where(costmatrix < max_cost) ticks_x = ticks_x[idx] xticks = xticks[idx] costmatrix = costmatrix[idx] plt.figure(figsize=(15, 5)) plt.plot(costmatrix, 'k.-', markersize=10, markeredgewidth=1.5, markeredgecolor='w') plt.xticks(xticks, ticks_x, rotation=-45) plt.xlabel("(Torsions, Clockwork)") plt.yscale("log") plt.grid(True, axis="y", color="k") ax = plt.gca() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(True) ax.spines['left'].set_visible(False) # Add extra lines for total_torsions in [10, 30, 40, 50, 60, 70]: print("add extra") ticks_x, costmatrix = generate_costlist(total_torsions=total_torsions) idx = np.where(costmatrix < max_cost) costmatrix = costmatrix[idx] plt.plot(costmatrix, '.-', markersize=10, markeredgewidth=1.5, markeredgecolor='w') plt.minorticks_off() plt.savefig("linearcost", bbox_inches="tight") plt.clf()
def relative_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 rel_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 + ' relative 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 relative ax1.plot(timepoints, rel_com['gr L 1'], color='brown', label='gr') ax1.plot(timepoints, rel_com['het L 1'], color='k', label='het') ax1.set_title('Incubation Time 1h', fontsize=10) ax1.set_ylabel('Light, Distance (mm)') ax2.plot(timepoints, rel_com['gr L 24'], color='brown', label='gr') ax2.plot(timepoints, rel_com['het L 24'], color='k', label='het') 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, rel_com['gr D 1'], color='brown', label='gr') ax3.plot(timepoints, rel_com['het D 1'], color='k', label='het') ax3.set_ylabel('Speed (mm/s)') ax3.set_xlabel('Concentration (uM)') ax3.set_ylabel('Dark, Distance (mm)') ax4.plot(timepoints, rel_com['gr D 24'], color='brown', label='gr') ax4.plot(timepoints, rel_com['het D 24'], color='k', label='het') ax4.set_xlabel('Concentration (uM)') f.savefig(savename, bbox_inches='tight') plt.close('all')
def figplot(clrs, x, y, xlab, ylab, fig, n): fig.add_subplot(2, 2, n) plt.xscale('log') if n == 1: plt.yscale('log', subsy=[1, 2]) plt.yscale('log') plt.minorticks_off() d = pd.DataFrame({'x': np.log10(x)}) d['y'] = np.log10(y) f = smf.ols('y ~ x', d).fit() m, b, r, p, std_err = stats.linregress(np.log10(x), np.log10(y)) st, data, ss2 = summary_table(f, alpha=0.05) fitted = data[:, 2] mean_ci_low, mean_ci_upp = data[:, 4:6].T ci_low, ci_upp = data[:, 6:8].T x, y, fitted, ci_low, ci_upp, clrs = zip( *sorted(zip(x, y, fitted, ci_low, ci_upp, clrs))) x = np.array(x) y = np.array(y) fitted = 10**np.array(fitted) ci_low = 10**np.array(ci_low) ci_upp = 10**np.array(ci_upp) if n == 1: lbl = r'$rarity$' + ' = ' + str(round( 10**b, 1)) + '*' + r'$N$' + '$^{' + str(round(m, 2)) + '}$' elif n == 2: lbl = r'$Nmax$' + ' = ' + str(round( 10**b, 1)) + '*' + r'$N$' + '$^{' + str(round(m, 2)) + '}$' elif n == 3: lbl = r'$Ev$' + ' = ' + str(round( 10**b, 1)) + '*' + r'$N$' + '$^{' + str(round(m, 2)) + '}$' elif n == 4: lbl = r'$S$' + ' = ' + str(round( 10**b, 1)) + '*' + r'$N$' + '$^{' + str(round(m, 2)) + '}$' plt.scatter(x, y, s=sz, color=clrs, linewidths=0.0, edgecolor=None) plt.fill_between(x, ci_upp, ci_low, color='0.5', lw=0.1, alpha=0.2) plt.plot(x, fitted, color='k', ls='--', lw=0.5, label=lbl) if n == 3: plt.legend(loc=3, fontsize=8, frameon=False) else: plt.legend(loc=2, fontsize=8, frameon=False) plt.xlabel(xlab, fontsize=10) plt.ylabel(ylab, fontsize=10) plt.tick_params(axis='both', labelsize=6) if n in [2, 4]: plt.ylim(min(y), max(y)) elif n == 1: plt.ylim(min(ci_low), max(ci_upp)) elif n == 3: plt.ylim(0.1, 1.1) return fig
def saveBitmap(outputFileName, inputFileName, imageData, size, colorMapName, caption): """Makes a bitmap image from an image array; the image format is specified by the filename extension. (e.g. ".jpg" =JPEG, ".png"=PNG). @type outputFileName: string @param outputFileName: filename of output bitmap image @type imageData: numpy array @param imageData: image data array @type cutLevels: list @param cutLevels: sets the image scaling - available options: - pixel values: cutLevels=[low value, high value]. - histogram equalisation: cutLevels=["histEq", number of bins ( e.g. 1024)] - relative: cutLevels=["relative", cut per cent level (e.g. 99.5)] - smart: cutLevels=["smart", cut per cent level (e.g. 99.5)] ["smart", 99.5] seems to provide good scaling over a range of different images. @type size: int @param size: size of output image in pixels @type colorMapName: string @param colorMapName: name of a standard matplotlib colormap, e.g. "hot", "cool", "gray" etc. (do "help(pylab.colormaps)" in the Python interpreter to see available options) """ import time start = time.time() scale=zscale.zscale(imageData) anorm = matplotlib.colors.Normalize(scale[0],scale[1]) cut = {'image': imageData, 'norm': anorm} # Make plot aspectR=float(cut['image'].shape[0])/float(cut['image'].shape[1]) fig = pyplot.figure(figsize=(10,10*aspectR)) xPix = size yPix = size dpi = 80 xSizeInches = size/dpi ySizeInches = xSizeInches fig.set_size_inches(xSizeInches,ySizeInches) pyplot.axes([0,0,1,1]) pyplot.minorticks_off() try: colorMap=matplotlib.cm.get_cmap(colorMapName) except AssertionError: raise Exception, colorMapName+" is not a defined matplotlib colormap." pyplot.imshow(cut['image'], interpolation="bilinear", norm=cut['norm'], origin='lower', cmap=colorMap) pyplot.axis("off") xmin,xmax = fig.gca().get_xlim() ymin,ymax = fig.gca().get_ylim() pyplot.text(xmin+1,ymin+35,caption,color="red",fontsize=20,fontweight=500,backgroundcolor='white') pyplot.savefig(outputFileName,format="png",dpi=dpi) pyplot.close() return inputFileName,outputFileName
def figplot(clrs, x, y, xlab, ylab, fig, n): fig.add_subplot(2, 2, n) plt.xscale('log') if n == 1: plt.yscale('log', subsy=[1, 2]) plt.yscale('log') plt.minorticks_off() d = pd.DataFrame({'x': np.log10(x)}) d['y'] = np.log10(y) f = smf.ols('y ~ x', d).fit() m, b, r, p, std_err = stats.linregress(np.log10(x), np.log10(y)) st, data, ss2 = summary_table(f, alpha=0.05) fitted = data[:,2] mean_ci_low, mean_ci_upp = data[:,4:6].T ci_low, ci_upp = data[:,6:8].T x, y, fitted, ci_low, ci_upp, clrs = zip(*sorted(zip(x, y, fitted, ci_low, ci_upp, clrs))) x = np.array(x) y = np.array(y) fitted = 10**np.array(fitted) ci_low = 10**np.array(ci_low) ci_upp = 10**np.array(ci_upp) if n == 1: lbl = r'$rarity$'+ ' = '+str(round(10**b,1))+'*'+r'$N$'+'$^{'+str(round(m,2))+'}$' elif n == 2: lbl = r'$Nmax$'+ ' = '+str(round(10**b,1))+'*'+r'$N$'+'$^{'+str(round(m,2))+'}$' elif n == 3: lbl = r'$Ev$'+ ' = '+str(round(10**b,1))+'*'+r'$N$'+'$^{'+str(round(m,2))+'}$' elif n == 4: lbl = r'$S$'+ ' = '+str(round(10**b,1))+'*'+r'$N$'+'$^{'+str(round(m,2))+'}$' plt.scatter(x, y, s = sz, color=clrs, linewidths=0.0, edgecolor=None) plt.fill_between(x, ci_upp, ci_low, color='0.5', lw=0.1, alpha=0.2) plt.plot(x, fitted, color='k', ls='--', lw=0.5, label = lbl) if n == 3: plt.legend(loc=3, fontsize=8, frameon=False) else: plt.legend(loc=2, fontsize=8, frameon=False) plt.xlabel(xlab, fontsize=10) plt.ylabel(ylab, fontsize=10) plt.tick_params(axis='both', labelsize=6) if n in [2, 4]: plt.ylim(min(y), max(y)) elif n == 1: plt.ylim(min(ci_low), max(ci_upp)) elif n == 3: plt.ylim(0.1, 1.1) return fig
def hists(df, keys=['m1'], nbins=100): print 'fits from {0} bootstraps'.format(df['bi'].max()) fig = plt.figure() # ax = plt.gca() axs = [fig.add_subplot(2, 1, i+1) for i in range(2)] # for each dotmode clrs = [[0.0, 0.7, 0.0], [0.7, 0.0, 0.0]] # bins = np.linspace(df[keys].min().min(), df[keys].max().max(), nbins) bins = np.linspace(0.0, 1.0, nbins) ymx = 10.0 for i, (dotmode, dfc) in enumerate(df.groupby('dotmode', as_index=False)): for key in keys: dfc[key].hist(bins=bins, alpha=1.0, normed=True, color=clrs[i], histtype='stepfilled', lw=2, ax=axs[i]) # dfc[key].hist(bins=bins, alpha=1.0, normed=True, color=clrs[i], histtype='step', ax=axs[i]) axs[i].set_xlim(min(bins), max(bins)) axs[i].set_ylim(0.0, ymx) axs[i].grid(False) axs[i].plot([0.3, 0.3], [0.0, ymx], '--k', lw=3, alpha=0.4, label='probability summation') axs[i].plot([0.5, 0.5], [0.0, ymx], '-k', lw=3, alpha=0.4, label='perfect integration') # axs[i].plot([0.3, 0.3], [0.0, ymx], ':k', lw=2, label='probability summation') # axs[i].plot([0.5, 0.5], [0.0, ymx], '--k', lw=2, label='perfect integration') leg = axs[-1].legend(loc='lower right', prop={'size': 14}) leg.get_frame().set_linewidth(1.5) axs[-1].invert_yaxis() plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.0, hspace=0.0) plt.minorticks_off() axs[-1].tick_params(top='off') axs[-1].tick_params(right='off') axs[-1].tick_params(axis='x', direction='out', width=1.5, length=5) axs[-1].tick_params(axis='y', direction='out', width=1.5, length=5) axs[-1].patch.set_facecolor('white') for axis in ['bottom', 'left']: axs[-1].spines[axis].set_linewidth(lw) for axis in ['top', 'right']: axs[-1].spines[axis].set_linewidth(0) axs[0].tick_params(top='off') axs[0].tick_params(right='off') axs[0].tick_params(axis='x', direction='out', width=1.5, length=5) axs[0].tick_params(axis='y', direction='out', width=1.5, length=5) axs[0].patch.set_facecolor('white') for axis in ['bottom', 'left']: axs[0].spines[axis].set_linewidth(lw) for axis in ['top', 'right']: axs[0].spines[axis].set_linewidth(0) plt.show()
def stem_event_count(self, stream): key = (stream.room, stream.sid, stream.typo) interval = stream._interval stream.set_interval(interval) yres = stream.apply_func(lambda i, v: len(v)) xres = stream.get_intervals() fig = plt.figure() ax = fig.add_subplot(111, axisbg='#F8F8F8') markerline, stemlines, baseline = ax.stem(xres, yres) plt.xticks(rotation=50) plt.title('Number of events produced ' + _key_to_title( key), fontdict=self._override) plt.yticks(fontsize=16) plt.xticks(fontsize=11) ax.yaxis.labelpad = 18 ax.xaxis.labelpad = 18 plt.ylabel('Number of events', fontdict=self._override) if isinstance(interval, off.Day): delta = timedelta(days=1) ax.xaxis.set_major_locator(mpd.DayLocator(interval=1)) ax.xaxis.set_major_formatter(mpd.DateFormatter('%d/%m')) elif isinstance(interval, off.Hour): delta = timedelta(hours=1) ax.xaxis.set_major_locator(mpd.HourLocator(interval=3)) ax.xaxis.set_major_formatter(mpd.DateFormatter('%d/%m %H:%M')) elif isinstance(interval, off.Minute): delta = timedelta(hours=1) ax.xaxis.set_major_locator(mpd.HourLocator(interval=20)) ax.xaxis.set_major_formatter(mpd.DateFormatter('%d/%m %H:%M')) else: raise ValueError('Interval type is unmanageable') ax.set_xlim(xres[0] - delta, xres[-1] + delta) miny = min(yres) maxy = max(yres) ax.set_ylim(0.9 * miny, 1.1 * maxy) plt.minorticks_off() #ax.set_aspect('0.04') self._save_current_figure(fig, suffix='_count')
def set_cn_axes_options(cn_axes, x, cn, kwargs): """Sets various options for the CN axes, including labels, whether to log-transform the y-axis, etc. Could add zebra stripe options ...""" # xmin, xmax, ymin, ymax, xticks, yticks, xlabel, ylabel if kwargs.get("logbase", None) != None: cn_axes.set_yscale("log", basey=kwargs["logbase"]) cn_axes.yaxis.set_major_formatter(ScalarFormatter()) plt.minorticks_off() if (kwargs.get("xmin", None) != None) and (kwargs.get("xmax", None) != None): cn_axes.set_xlim(kwargs["xmin"], kwargs["xmax"]) cn_axes.spines["bottom"].set_bounds(kwargs["xmin"], kwargs["xmax"]) else: cn_axes.set_xlim(min(x), max(x)) cn_axes.spines["bottom"].set_bounds(min(x), max(x)) if kwargs.get("ymin", None) != None: cn_axes.set_ylim(bottom=kwargs["ymin"]) if kwargs.get("ymax", None) != None: cn_axes.set_ylim(top=kwargs["ymax"]) if kwargs.get("xticks", None) != None: cn_axes.set_xticks(kwargs["xticks"]) cn_axes.set_xticklabels(kwargs["xticks"]) if kwargs.get("yticks", None) != None: yticks = kwargs["yticks"] cn_axes.set_yticks(yticks) cn_axes.set_yticklabels(yticks) cn_axes.spines["left"].set_bounds(min(yticks), max(yticks)) if kwargs.get("xlabel", None) != None: cn_axes.set_xlabel(kwargs["xlabel"]) else: cn_axes.set_xlabel("Position on chromosome ?? (Mb)") if kwargs.get("ylabel", None) != None: cn_axes.set_ylabel(kwargs["ylabel"]) else: cn_axes.set_ylabel("Estimated copy number")
def displayData(X, nrows=10, ncols=10): # set up array fig, axarr = plt.subplots(nrows=nrows, ncols=ncols, figsize=(nrows, ncols)) nblock = int(np.sqrt(X.shape[1])) # loop over randomly drawn numbers ct = 0 for ii in range(nrows): for jj in range(ncols): # ind = np.random.randint(X.shape[0]) tmp = X[ct, :].reshape(nblock, nblock, order='F') axarr[ii, jj].imshow(tmp, cmap='gray') plt.setp(axarr[ii, jj].get_xticklabels(), visible=False) plt.setp(axarr[ii, jj].get_yticklabels(), visible=False) plt.minorticks_off() ct += 1 fig.subplots_adjust(hspace=0, wspace=0)
def PlotDET(results, eerDcf, title): """ Plots a DET curve with false positive and false negative rates (results). marks minDCF value (from eerDcf) for each curve. Some code based on https://jeremykarnowski.wordpress.com/2015/08/07/detection-error-tradeoff-det-curves/ """ axis_min = min(min(results)) fig,ax = plt.subplots() for r in range(len(results)): plt.plot(results[r][0], results[r][1], linewidth = 2, label = results[r][2]) if r == len(results)-1: plt.plot(eerDcf[r][2][0], eerDcf[r][2][1], 'kD', markersize=6, label = "DCF minimipisteet") else: plt.plot(eerDcf[r][2][0], eerDcf[r][2][1], 'kD', markersize=6) plt.yscale('logit') plt.xscale('logit') majorTicks = [0.01,0.02,0.05,0.1,0.2,0.4,0.6,0.8,0.9] ax.xaxis.set_major_formatter(ScalarFormatter()) ax.yaxis.set_major_formatter(ScalarFormatter()) ax.set_xticks(majorTicks) ax.set_yticks(majorTicks) plt.axis([0.005,0.9,0.005,0.9]) plt.title(title) plt.xlabel(u"Väärät hyväksymiset (%)") plt.ylabel(u"Väärät hylkäykset (%)") plt.legend(loc = 0) plt.minorticks_off() def ToPercent(y, position): return int(y * 100) formatter = FuncFormatter(ToPercent) plt.gca().yaxis.set_major_formatter(formatter) plt.gca().xaxis.set_major_formatter(formatter) ax.grid(True) plt.show()
def plot_svd_components(tup, n=4, from_t = None): wl, t, d = tup.wl, tup.t, tup.data if from_t: idx = dv.fi(t, from_t) t = t[idx:] d = d[idx:, :] u, s, v = np.linalg.svd(d) ax1: plt.Axes = plt.subplot(311) ax1.set_xlim(-1, t.max()) lbl_trans() plt.minorticks_off() ax1.set_xscale('symlog') ax2 = plt.subplot(312) lbl_spec() plt.ylabel('') for i in range(n): ax1.plot(t,u.T[i], label=str(i) ) ax2.plot(wl,v[i] ) ax1.legend() plt.subplot(313) plot_singular_values(d) plt.tight_layout()
def plotEigPowerRec(angFreq, eigValGen, powerSample, powerRec, powerAna=None, eigAna=None, markersize=None, condition=None, xlabel=None, ylabel=None, zlabel=None, xlim=None, ylim=None, zlim=None, xticks=None, yticks=None, zticks=None): if condition is None: condition = 'b' # Create axis for eigenvalue and power spectrum panels nullfmt = plt.NullFormatter() left, width = 0.1, 0.6 bottom, height = 0.1, 0.9 leftPow = left + width + 0.01 widthPow = 0.25 rectEig = [left, bottom, width, height] rectPow = [leftPow, bottom, widthPow, height] ratio = height / width defaultFigHeight = plt.rcParams['figure.figsize'][1] fig = plt.figure(figsize=(defaultFigHeight*ratio, defaultFigHeight)) axEig = plt.axes(rectEig) axPow = plt.axes(rectPow) # Plot eigenvalues if markersize is not None: axEig.scatter(eigValGen.real, eigValGen.imag, s=msize*2, marker='+', c=condition, cmap=cm.Greys_r) else: markersize=20 axEig.scatter(eigValGen.real, eigValGen.imag, s=markersize, marker='o', edgecolors='face', c=condition, cmap=cm.Greys_r) if eigAna is not None: axEig.scatter(eigAna.real, eigAna.imag, c='k', s=int(markersize*2.), marker='x') if xlim is not None: axEig.set_xlim(xlim) if ylim is not None: axEig.set_ylim(ylim) axEig.set_xlabel(xlabel, fontsize=fs_default) axEig.set_ylabel(ylabel, fontsize=fs_default) if xticks is not None: axEig.set_xticks(xticks) if yticks is not None: axEig.set_yticks(yticks) plt.setp(axEig.get_xticklabels(), fontsize=fs_xticklabels) plt.setp(axEig.get_yticklabels(), fontsize=fs_yticklabels) axEig.grid() # Plot power spectra axPow.plot(powerSample, angFreq, 'k-') if powerAna is not None: axPow.plot(powerAna, angFreq, 'b-') axPow.plot(powerRec, angFreq, '--r', linewidth=2) axPow.set_xscale('log') if zlim is not None: axPow.set_xlim(zlim) if ylim is not None: axPow.set_ylim(ylim) axPow.set_xlabel(zlabel, fontsize=fs_default) axPow.yaxis.set_major_formatter(nullfmt) if yticks is not None: axPow.set_yticks(yticks) if zticks is not None: axPow.set_xticks(zticks) elif zlim is not None: zticks = np.logspace(np.log10(zlim[0]), np.log10(zlim[1]), int(np.round(np.log10(zlim[1]/zlim[0]) + 1))) axPow.set_xticks(zticks) zticklabels = [''] for k in np.arange(1, zticks.shape[0]): zticklabels.append(r'$10^{%d}$' % int(np.round(np.log10(zticks[k])))) axPow.set_xticklabels(zticklabels) plt.setp(axPow.get_xticklabels(), fontsize='large') plt.minorticks_off() axPow.grid()
# Import necessary modules import matplotlib.pyplot as plt import numpy as np # Name output file psname = 'python_plot.ps' # Create data and labels for the bar plot. data1 = [1, 0.6, 0.8] data2 = [0.85, 0.4, 0.95] labels = ['0.4 Z$_\odot$', '1 Z$_\odot$', '2.5 Z$_\odot$'] # Set up plot fig, ax = plt.subplots(figsize=(6, 6)) ax.set_xlim(-0.1, 2.7, plt.minorticks_off()) ax.set_ylim(0., 1.2, plt.minorticks_on()) ind = np.arange(len(data1)) width = .3 # Plot bars ax.bar(ind, data1, width, color='darkslateblue', ec='white') ax.bar(ind+width, data2, width, color='royalblue', ec='white') # Add axes labels and titles (x), and annotations ax.set_xlabel('Metallicity', size=18) ax.set_ylabel('Normalized Frequency', size=18) ax.set_xticks(ind+width)
def main(): if len(sys.argv) != 2: print "Usage: ", sys.argv[0], "<simout directory>" exit(-1) try: stats = open(sys.argv[1] + "/stats.txt", "r") except IOError: print "Failed to open ", sys.argv[1] + "/stats.txt", " for reading" exit(-1) try: simout = open(sys.argv[1] + "/simout", "r") except IOError: print "Failed to open ", sys.argv[1] + "/simout", " for reading" exit(-1) # Get the address ranges got_ranges = False ranges = [] iterations = 1 for line in simout: if got_ranges: ranges.append(int(line) / 1024) match = re.match("lat_mem_rd with (\d+) iterations, ranges:.*", line) if match: got_ranges = True iterations = int(match.groups(0)[0]) simout.close() if not got_ranges: print "Failed to get address ranges, ensure simout is up-to-date" exit(-1) # Now parse the stats raw_rd_lat = [] for line in stats: match = re.match(".*readLatencyHist::mean\s+(.+)\s+#.*", line) if match: raw_rd_lat.append(float(match.groups(0)[0]) / 1000) stats.close() # The stats also contain the warming, so filter the latency stats i = 0 filtered_rd_lat = [] for l in raw_rd_lat: if i % (iterations + 1) == 0: pass else: filtered_rd_lat.append(l) i = i + 1 # Next we need to take care of the iterations rd_lat = [] for i in range(iterations): rd_lat.append(filtered_rd_lat[i::iterations]) final_rd_lat = map(lambda p: min(p), zip(*rd_lat)) # Sanity check if not (len(ranges) == len(final_rd_lat)): print "Address ranges (%d) and read latency (%d) do not match" % (len(ranges), len(final_rd_lat)) exit(-1) for (r, l) in zip(ranges, final_rd_lat): print r, round(l, 2) # lazy version to check if an integer is a power of two def is_pow2(num): return num != 0 and ((num & (num - 1)) == 0) plt.semilogx(ranges, final_rd_lat) # create human readable labels xticks_locations = [r for r in ranges if is_pow2(r)] xticks_labels = [] for x in xticks_locations: if x < 1024: xticks_labels.append("%d kB" % x) else: xticks_labels.append("%d MB" % (x / 1024)) plt.xticks(xticks_locations, xticks_labels, rotation=-45) plt.minorticks_off() plt.xlim((xticks_locations[0], xticks_locations[-1])) plt.ylabel("Latency (ns)") plt.grid(True) plt.show()
rects = plt.bar(index, values_lme, bar_width, alpha=0.3, color='r', label='LME', linewidth=0) for i, rect in enumerate(rects): height = rect.get_height() plt.text(rect.get_x() + rect.get_width() / 2., -1 * height, 'LME', ha='center', va='bottom', fontsize=4, rotation=90) rects = plt.bar(index + bar_width, values_iflux, bar_width, alpha=0.3, color='b', label='CF', linewidth=0) for i, rect in enumerate(rects): height = rect.get_height() plt.text(rect.get_x() + rect.get_width() / 2., -1 * height, 'CF', ha='center', va='bottom', fontsize=4, rotation=90) rects = plt.bar(index + bar_width * 2, values_mem, bar_width, alpha=0.3, color='k', label='CF', linewidth=0) for i, rect in enumerate(rects): height = rect.get_height() plt.text(rect.get_x() + rect.get_width() / 2., -1 * height, 'CF-Mem', ha='center', va='bottom', fontsize=4, rotation=90) plt.xticks(index + bar_width, (r'Yes 50', r'Yes 100', r'F.Sq 10')) ax = plt.gca() ax.tick_params(direction='out', pad=0.3) ax.set_ylim((-5, -8)) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') plt.minorticks_off() plt.ylabel('Avg. LL (Test Set)', labelpad=0) #plt.legend(frameon=False, loc='upper left') plt.tight_layout(pad=0.2) plt.savefig('ll_lme.pdf')
def base_fraction_plot( base_count_position_list_dict, flank_size=10, normalize_to_GC_contents=1, overall_GC_content=0.5, genome_info="", add_markers=True, ytick_scale=1, bases_plotstyles={"A": "g^-", "T": "rv-", "C": "bs-", "G": "yo-"}, ): """ Plot the base fractions at each position, with given flanksize, normalized to GC content or not. Base_count_position_list_dict should be the output of base_count_dict. Normalize_to_GC_contents can be: - 0 - no normalization (show raw base fractions) - 1 - difference between real and expected base contents (so the difference between 0.1 and 0.3, and between 0.3 and 0.5, will be the same 0.2), - 2 - ratio between real and expected base contents (the 0.1 to 0.3 ratio is same as 0.3 to 0.9, bigger than 0.3 to 0.5) - 3 - ratio on a log-scale (so that ratios of 1/4, 1/2, 1, 2, 4 are all equidistant, which makes sense, instead of 1, 2, 3 being equidistant and 1/2, 1/3, ..., 1/10 being all squished between 0 and 1 like on the linear scale) Ytick_scale is only applicable when normalize_to_GC_contents is 3: - if it's 1, the ticks will be ..., 2, 1, 1/2, ... - if it's 2, the ticks will be ..., 2, 3/2, 1, 3/2, 1/2, ... - if it's 3, the ticks will be ..., 2, 5/3, 4/3, 1, 3/4, 3/5, 1/2, ... """ if not 0 <= normalize_to_GC_contents <= 3: raise Exception("normalize_to_GC_contents must be 0/1/2/3, not %s!" % normalize_to_GC_contents) real_base_fraction_list_dict = base_fraction_dict_from_count_dict(base_count_position_list_dict) pos_after_insertion = int(len(real_base_fraction_list_dict["A"]) / 2) expected_base_fractions = base_fractions_from_GC_content(overall_GC_content) all_plot_data = [] for base in NORMAL_DNA_BASES: raw_plot_data = real_base_fraction_list_dict[base][ pos_after_insertion - flank_size : pos_after_insertion + flank_size ] assert len(raw_plot_data) == flank_size * 2 if normalize_to_GC_contents == 0: plot_data = raw_plot_data elif normalize_to_GC_contents == 1: plot_data = [x - expected_base_fractions[base] for x in raw_plot_data] else: plot_data = [x / expected_base_fractions[base] for x in raw_plot_data] all_plot_data.extend(plot_data) if add_markers: mplt.plot(plot_data, bases_plotstyles[base], label=base, markeredgecolor="none") else: mplt.plot(plot_data, bases_plotstyles[base][0], label=base) mplt.legend(loc=2, prop=FontProperties(size="smaller")) ylabel = "fraction of bases in given position" if normalize_to_GC_contents == 0: ylabel = "raw " + ylabel elif normalize_to_GC_contents == 1: ylabel += ",\nas a difference from %s GC content" % genome_info else: ylabel += ",\nas a ratio to %s GC content" % genome_info if normalize_to_GC_contents == 3: ylabel += " (log scale)" # make y logscale if desired; in that case I have to do the min/max/ticks sort of by hand... if normalize_to_GC_contents == 3: if min(all_plot_data) <= 0: raise ValueError("some bases have 0 fraction - can't plot log-scale!") # MAYBE-TODO plot it symlog if needed? But then all my work with limits/ticks needs to be redone... mplt.yscale("log") y_max = int(max(scipy.ceil(max(all_plot_data)), scipy.ceil(1 / min(all_plot_data)))) mplt.ylim(1 / y_max, y_max) half_yticks_x = [x for x in range(ytick_scale + 1, ytick_scale * y_max + 1)] yticks = [ytick_scale / x for x in half_yticks_x] + [1] + [x / ytick_scale for x in half_yticks_x] yticklabels = ( [Fraction(ytick_scale, x) for x in half_yticks_x] + [1] + [Fraction(x, ytick_scale) for x in half_yticks_x] ) mplt.yticks(yticks, yticklabels) mplt.minorticks_off() # change the xticks to use -1 before the insertion position and 1 after, no 0 xticks = range(flank_size * 2) mplt.xlim(0, flank_size * 2 - 1) mplt.xticks(xticks, [_relative_position_vs_cut(x, flank_size) for x in xticks]) mplt.xlabel("relative genome position (dotted line is the insertion position)") mplt.ylabel(ylabel, ha="right") # put a dashed line at the insertion position ylim = mplt.ylim() mplt.vlines(flank_size - 0.5, *ylim, linestyles="dashed") mplt.ylim(*ylim)
def plot(df0a, df1a, df2a, dotmode, colorA, colorB): df0 = df0a[df0a['dotmode'] == dotmode] df1 = df1a[df1a['dotmode'] == dotmode] df2 = df2a[df2a['dotmode'] == dotmode] df = df0.groupby('dur', as_index=False)['sens'].agg([np.mean, np.std])['sens'].reset_index() if dotmode == '3d': df = df[df['dur'] > df['dur'].min()] df = df[df['dur'] > df['dur'].min()] ptA = df1[['x0','x1','b0','b1','m0','m1']].median() df = df[df['dur'] <= np.exp(ptA['x1'])] # ignore higher durs since line there is flat df['y1'] = df['dur']**ptA['m0']*np.exp(ptA['b0']) x1 = df['dur'] > np.exp(ptA['x0']) df['y1'][x1] = df['dur'][x1]**ptA['m1']*np.exp(ptA['b1']) ptB = df2[['b0','m0']].median() df['y2'] = df['dur']**ptB['m0']*np.exp(ptB['b0']) sz = 80 lw1 = 1 lw2 = 2 df['y1e'] = (df['mean'] - df['y1'])/df['std'] df['y2e'] = (df['mean'] - df['y2'])/df['std'] # plt.plot(df['dur'], df['y2e'], lw=lw1, c='k', zorder=4) # plt.plot(df['dur'], df['y1e'], lw=lw1, c='k', zorder=4) plt.gca().fill_between(df['dur'], 0.0, df['y2e'], lw=0, color=colorB, facecolor=colorB, alpha=0.6, zorder=3) plt.gca().fill_between(df['dur'], 0.0, df['y1e'], lw=0, color=colorA, facecolor=colorA, alpha=1.0, zorder=3) plt.scatter(df['dur'], df['y2e'], sz, c=colorB, lw=1.5, label='bi-limb fit', zorder=5) plt.scatter(df['dur'], df['y1e'], sz, c=colorA, lw=1.5, label='tri-limb fit', zorder=5) plt.plot([np.exp(ptA['x0']), np.exp(ptA['x0'])], [-29.5, 8.5], '--', lw=lw2, c=colorA, zorder=1) # plt.plot([0.01, df['dur'].max() + 50], [0, 0], '-', lw=lw2, c='k', zorder=1) # plt.plot(df['dur'], df['mean']) # plt.scatter(df['dur'], df['y1'], c='g') # plt.scatter(df['dur'], df['y2'], c='r') # plt.yscale('log') ys = np.hstack([df['y1e'].values, df['y2e'].values]) plt.xscale('log') # plt.xlim(df0a['dur'].min()-2, df['dur'].max()+30) # plt.ylim(ys.min()-1, ys.max()+1) plt.xlabel('Duration (msec)') plt.ylabel('Motion sensitivity residual') leg = plt.legend(loc='upper left', prop={'size': LegendFontSize}) leg.get_frame().set_linewidth(1.5) xtcks = np.array([33, 200, 1000]) # xtcks = xtcks[xtcks >= df['dur'].min()] plt.xticks(xtcks, xtcks) # formatting plt.minorticks_off() plt.gca().tick_params(top='off') plt.gca().tick_params(right='off') plt.gca().tick_params(axis='x', direction='out', width=1.5, length=5) plt.gca().tick_params(axis='y', direction='out', width=1.5, length=5) plt.gcf().patch.set_facecolor('white') for axis in ['bottom', 'left']: plt.gca().spines[axis].set_linewidth(lw2) for axis in ['top', 'right']: plt.gca().spines[axis].set_linewidth(0)
def fillActivityTab(self): """ Fills and displays the bar graph in the activity tab. """ taskID = self.activityTaskID # First get the absolute value of today. today = datetime.date.today() delta = datetime.timedelta(days=1) # Now construct shiftable intervals. beginInt = datetime.datetime endInt = datetime.datetime # Default scale (days): Begin interval at midnight of today. beginInt = datetime.datetime(today.year, today.month, today.day, 0, 0, 0) shiftDelta = delta if self.intervalScale == 2: # Scale: Weeks # Begin interval at midnight of this weeks monday. weekDayNum = calendar.weekday(today.year, today.month, today.day) beginInt = beginInt - delta * weekDayNum shiftDelta = 7 * delta elif self.intervalScale == 1: # Scale: Months # Begin interval at midnight of the first day of the month. beginInt = datetime.datetime(today.year, today.month, 1, 0, 0, 0) shiftDelta = 30 * delta self.shiftDelta = shiftDelta # Get the data of the last units since today. units = list() values = list() size = 6 + self.intervalScale for i in range(size): # Shift offset = size - i - 1 - self.intervalShift shiftedBegin = beginInt - offset * shiftDelta # When scaled to months, an arithmetical shift is not practical. if self.intervalScale == 1: yearDiff, monDiff = divmod(offset, 12) newMon = beginInt.month - monDiff if newMon < 0: newMon = 12 + newMon yearDiff += 1 if newMon == 0: newMon = 12 yearDiff += 1 shiftedBegin = datetime.datetime(beginInt.year - yearDiff, newMon, 1, 0, 0, 0) shiftedEnd = datetime.datetime if self.intervalScale == 3: units.append(str(shiftedBegin.month) + "/" + str(shiftedBegin.day)) shiftedEnd = datetime.datetime(shiftedBegin.year, shiftedBegin.month, shiftedBegin.day, 23, 59, 59) elif self.intervalScale == 2: units.append(shiftedBegin.strftime("CW %W")) shiftedEnd = datetime.datetime(shiftedBegin.year, shiftedBegin.month, shiftedBegin.day, 23, 59, 59) shiftedEnd = shiftedEnd + delta * 6 else: units.append(shiftedBegin.strftime("%b %y")) lastDay = calendar.monthrange(shiftedBegin.year, shiftedBegin.month)[1] shiftedEnd = datetime.datetime(shiftedBegin.year, shiftedBegin.month, lastDay, 23, 59, 59) timeInt = [int(shiftedBegin.timestamp()), int(shiftedEnd.timestamp())] values.append(self.pomo.pomoData.getPomoCount(timeInt, taskID)) # Disable left buttons once we scrolled far enough if self.pomo.pomoData.firstPomo != 0: shiftedBegin = beginInt - (size - 1 - self.intervalShift) * shiftDelta self.shiftedBegin = shiftedBegin if shiftedBegin.timestamp() <= self.pomo.pomoData.firstPomo: self.leftButtonHandle.setDisabled(True) self.farLeftButtonHandle.setDisabled(True) else: self.leftButtonHandle.setDisabled(False) self.farLeftButtonHandle.setDisabled(False) # Create a new subplot. ax = self.figure.add_subplot(111) ax.hold(False) # Create the bar graphs bars = ax.bar(list(range(1, size + 1)), values, width=0.4, align="center", color="#E04B3F") for bar in bars: height = bar.get_height() plt.text( bar.get_x() + bar.get_width() / 2.0, height + 0.08, "%d" % int(height), ha="center", va="bottom", weight="medium", ) plt.xticks(list(range(1, size + 1)), units) # y-Limit of the graph depends on the maximum bar height. yLim = self.highestPomoCount[self.intervalScale - 1] * 1.24 # To avoid rescaling the graph when changing the task, we lock the # y-Limit to the first one generated after startup. if self.lockYLim is False: if yLim == 0: # When no pomodoros have been done, use a constant y-Limit. yLim = 15 else: self.lockYLim = True self.yLim = yLim else: # Update the y-Limit when it exceeds the saved one. if yLim > self.yLim: self.yLim = yLim # Set the graph limits. ax.set_ylim([0, self.yLim]) ax.set_xlim([0.5, size + 0.5]) # Additional plot and graph settings. plt.subplots_adjust(left=0, right=0.99, top=1, bottom=0.087) ax.get_yaxis().set_visible(False) plt.minorticks_off() for tick in ax.get_xticklines(): tick.set_visible(False) # Write currently viewed month and year in the upper right corner, # when zoomed out, only display the year. if self.intervalScale != 1: tempDate = beginInt - (size - 1 - self.intervalShift) * shiftDelta dateString = tempDate.strftime("%b %Y") if self.intervalScale != 3: dateString = tempDate.strftime("%Y") plt.text( 0.99, 0.937, dateString, horizontalalignment="right", verticalalignment="center", transform=ax.transAxes, weight="bold", ) # Show. self.canvas.draw()
def main(trace_fpath, lat_long_fpath, leaveout=0.3): initialize_matplotlib() leaveout = float(leaveout) lat_long_dict = {} with open(lat_long_fpath) as lat_long_file: for line in lat_long_file: loc, lat, long_ = line.split('\t') lat_long_dict[loc] = (float(lat), float(long_)) df = pd.read_csv(trace_fpath, sep='\t', names=['dt', 'u', 's', 'd']) num_lines = len(df) to = int(num_lines - num_lines * leaveout) df_train = df[:to] df_test = df[to:] pop_df = df_train.groupby(['d']).count()['u'] pop_dict = dict(zip(pop_df.index, pop_df.values)) answer_df = df_train.groupby(['s', 'd']).count()['u'] answer_dict = dict(zip(answer_df.index, answer_df.values)) X = [] y = [] for row in df_train[['s', 'd']].values: s, d = row if s in pop_dict and d in pop_dict and \ str(s) in lat_long_dict and str(d) in lat_long_dict: dist = distance(str(s), str(d), lat_long_dict) if dist == 0: #different ids, same loc, ignore continue X.append([1.0, np.log(pop_dict[s]), np.log(pop_dict[d]), -np.log(dist)]) y.append(answer_dict[s, d]) answer_df_test = df_test.groupby(['s', 'd']).count()['u'] answer_dict_test = dict(zip(answer_df_test.index, answer_df_test.values)) #This is future information, should not be exploited for likelihood pop_df_test = df_test.groupby(['d']).count()['u'] pop_dict_test = dict(zip(pop_df_test.index, pop_df_test.values)) X_test_ll = [] X_test_pred = [] y_test = [] for row in df_test[['s', 'd']].values: s, d = row if s in pop_dict and d in pop_dict and \ str(s) in lat_long_dict and str(d) in lat_long_dict: dist = distance(str(s), str(d), lat_long_dict) if dist == 0: #different ids, same loc, ignore continue X_test_ll.append([1.0, np.log(pop_dict[s]), np.log(pop_dict[d]), \ -np.log(dist)]) X_test_pred.append([1.0, np.log(pop_dict_test[s] if s in pop_dict_test else 0), \ np.log(pop_dict_test[d]), -np.log(dist)]) y_test.append(answer_dict_test[s, d]) X_train = np.asarray(X) y_train = np.asarray(y) X_test_ll = np.asarray(X_test_ll) X_test_pred = np.asarray(X_test_pred) y_test = np.asarray(y_test) import time print('training', time.localtime()) model = sm.GLM(y_train, X_train, family=sm.families.Poisson()) results = model.fit() print('done', time.localtime()) print(results.summary()) y_pred = np.array(results.predict(X_test_pred)) print(np.abs(y_test - y_pred).mean()) plt.plot(y_pred, y_test, 'wo', rasterized=True, markersize=2) plt.plot(y_pred, y_pred, 'r-', rasterized=True) plt.minorticks_off() ax = plt.gca() ax.tick_params(direction='out', pad=0.3) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') plt.ylabel(r'True value ($n_{ds}$)', labelpad=0.2) plt.xlabel(r'Predicted value ($\tilde{n_{ds}}$)', labelpad=0.3) plt.tight_layout(pad=0.1) _, _, r, _, _ = linregress(y_pred, y_test) plt.title('MAE = %.3f ; R2 = %.3f ' %(np.abs(y_test - y_pred).mean(), r**2), y=0.8) plt.savefig('pred.pdf') #Likelihood on test set (adapted from glm code on train set, #no method for test set exists) lin_pred = np.dot(X_test_ll, results.params) + model._offset_exposure expval = model.family.link.inverse(lin_pred) llr = model.family.loglike(expval, y_test, results.scale) llr = llr print(llr, llr / X_test_ll.shape[0])
def plot_t_wf_a(wf, save="", range_x=None, range_y=None): """ Plot wavefront in Q-space. :params: wf: wavefront structure :params: save: Whether to save the figure on disk :type: string for filename. Empty string '' means don't save. :default: '', do not save the figure. :params: range_x: x-axis range. :type: float :default: None, take entire x range. :params: range_y: y-ayis range. :type: float :default: None, take entire y range. """ import matplotlib.pyplot as plt wf_intensity = wf.get_intensity().sum(axis=-1) plt.figure(figsize=(10, 10), dpi=100) plt.axis("tight") try: plt.set_cmap("viridis") except: plt.set_cmap("YlGnBu_r") profile = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2) xmin, xmax, ymax, ymin = wf.get_limits() profile.imshow(wf_intensity, extent=[xmin * 1.0e6, xmax * 1.0e6, ymax * 1.0e6, ymin * 1.0e6]) profile.set_aspect("equal", "datalim") # [LS:2016-03-17] # change shape dimension, otherwise, in case nx!=ny , # 'x, y should have the same dimension' error from py plot # x = numpy.linspace(xmin*1.e6,xmax*1.e6,wf_intensity.shape[0]) # y = numpy.linspace(ymin*1.e6,ymax*1.e6,wf_intensity.shape[1]) x = numpy.linspace(xmin * 1.0e6, xmax * 1.0e6, wf_intensity.shape[1]) y = numpy.linspace(ymin * 1.0e6, ymax * 1.0e6, wf_intensity.shape[0]) profile.set_xlabel(r"$\mu$rad", fontsize=12) profile.set_ylabel(r"$\mu$rad", fontsize=12) x_projection = plt.subplot2grid((3, 3), (0, 0), sharex=profile, colspan=2) print(x.shape, wf_intensity.sum(axis=0).shape) x_projection.plot(x, wf_intensity.sum(axis=0), label="x projection") if range_x is None: profile.set_xlim([xmin * 1.0e6, xmax * 1.0e6]) else: profile.set_xlim([-range_x / 2.0, range_x / 2.0]) y_projection = plt.subplot2grid((3, 3), (1, 2), rowspan=2, sharey=profile) y_projection.plot(wf_intensity.sum(axis=1), y, label="y projection") # Hide minor tick labels. plt.minorticks_off() if range_y is None: profile.set_ylim([ymin * 1.0e6, ymax * 1.0e6]) else: profile.set_ylim([-range_y / 2.0, range_y / 2.0]) if save != "": plt.savefig(save) else: plt.show()
def plot_t_wf(wf, save="", range_x=None, range_y=None): """ Plot wavefront in R-space. :params: wf: wavefront structure :params: save: Whether to save the figure on disk :type: string for filename. Empty string '' means don't save. :default: '', do not save the figure. :params: range_x: x-axis range. :type: float :default: None, take entire x range. :params: range_y: y-ayis range. :type: float :default: None, take entire y range. """ import matplotlib.pyplot as plt # Get the wavefront and integrate over time. wf_intensity = wf.get_intensity().sum(axis=-1) # Get average and time slicing. average = averaged_intensity(wf, bPlot=True) nslices = wf.params.Mesh.nSlices dt = (wf.params.Mesh.sliceMax - wf.params.Mesh.sliceMin) / (nslices - 1) t0 = dt * nslices / 2 + wf.params.Mesh.sliceMin # Setup a figure. figure = plt.figure(figsize=(10, 10), dpi=100) plt.axis("tight") # Set colormap. Inferno is not available in all matplotlib versions, fall # back to gnuplot. try: plt.set_cmap("viridis") except: plt.set_cmap("YlGnBu_r") # Profile plot. profile = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2) # Get limits. xmin, xmax, ymax, ymin = wf.get_limits() # Plot profile as 2D colorcoded map. profile.imshow(wf_intensity, extent=[xmin * 1.0e3, xmax * 1.0e3, ymax * 1.0e3, ymin * 1.0e3]) profile.set_aspect("equal", "datalim") # Get x and y ranges. # [LS:2016-03-17] # change shape dimension, otherwise, in case nx!=ny , # 'x, y should have the same dimension' error from py plot # x = numpy.linspace(xmin*1.e3,xmax*1.e3,wf_intensity.shape[0]) # y = numpy.linspace(ymin*1.e3,ymax*1.e3,wf_intensity.shape[1]) x = numpy.linspace(xmin * 1.0e3, xmax * 1.0e3, wf_intensity.shape[1]) y = numpy.linspace(ymin * 1.0e3, ymax * 1.0e3, wf_intensity.shape[0]) # Labels. profile.set_xlabel("$mm$", fontsize=12) profile.set_ylabel("$mm$", fontsize=12) # x-projection plots above main plot. x_projection = plt.subplot2grid((3, 3), (0, 0), sharex=profile, colspan=2) print(x.shape, wf_intensity.sum(axis=0).shape) x_projection.plot(x, wf_intensity.sum(axis=0), label="x projection") # Set range according to input. if range_x is None: profile.set_xlim([xmin * 1.0e3, xmax * 1.0e3]) else: profile.set_xlim([-range_x / 2.0, range_x / 2.0]) # Set title. x_projection.set_title( "relative intensity={:03.3g}, t0={:03.2f} fs".format(wf_intensity.sum() / average, t0 * 1.0e15) ) # y-projection plot right of main plot. y_projection = plt.subplot2grid((3, 3), (1, 2), rowspan=2, sharey=profile) y_projection.plot(wf_intensity.sum(axis=1), y, label="y projection") # Hide minor tick labels, they disturb here. plt.minorticks_off() # Set range according to input. if range_y is None: profile.set_ylim([ymin * 1.0e3, ymax * 1.0e3]) else: profile.set_ylim([-range_y / 2.0, range_y / 2.0]) # If requested, save to disk, otherwise show in interactive window. if save != "": # Add parameters. plt.savefig(save) else: plt.show()
def despine(): ax = py.gca() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) py.tick_params(axis='both', bottom='on', top='off', left='on', right='off') py.minorticks_off()
def MultiDimNewtRaph(f,x0,dx=1e-6,args=(),ytol=1e-4,w=1.0,JustOneStep=False): """ A Newton-Raphson solver where the Jacobian is always re-evaluated rather than re-using the information as in the fsolve method of scipy.optimize """ x=np.array(x0) error=999 J=np.zeros((len(x),len(x))) error_list=[] iteration_list = [] #If a float is passed in for dx, convert to a numpy-like list the same shape #as x if isinstance(dx,float): dx=dx*np.ones_like(x) r0=array(f(x,*args)) while abs(error)>ytol: #Build the Jacobian matrix by columns for i in range(len(x)): epsilon=np.zeros_like(x) epsilon[i]=dx[i] J[:,i]=(array(f(x+epsilon,*args))-r0)/epsilon[i] v=np.dot(-inv(J),r0) x=x+w*v #Calculate the residual vector at the new step r0=f(x,*args) error = np.max(np.abs(r0)) iteration=my_counter_2.next() error_list.append(error) iteration_list.append(iteration) #print error #Just do one step and stop if JustOneStep==True: return x error_convergence = np.array(error_list) iteration_convergence = np.array(iteration_list) print '---MultiDimNewtRaph---' print iteration_convergence print error_convergence matplotlib.rc('text', usetex=True) plt.rc('font', family='serif') fig=figure(figsize=(8,6)) ax=fig.add_subplot(1,1,1) plot(iteration_convergence, error_convergence,'bs-',linewidth=2.0,markersize = 10,markerfacecolor="blue", markeredgewidth=2, markeredgecolor="black") plt.axhline(y=1e-5,color='r',ls='dashed', linewidth=2.5) xlabel('Iteration [-] ',fontsize=18) ylabel(r'$|Resid|$',fontsize=18) title('Overall Cycle Convergence',fontsize=18) plt.yscale('log') plt.minorticks_off() for tickx in ax.xaxis.get_major_ticks(): tickx.label.set_fontsize(20) for ticky in ax.yaxis.get_major_ticks(): ticky.label.set_fontsize(20) show() fig.savefig('MultiDimNewtRaph.png',dpi=300) return x
def setupPlot(minZ, zMed, maxZ, imSize, imSizePlot, minR, maxR, minSM, maxSM, morph, thisPanel, nPanels, legendFlag): # setup plot of SM vs R with colorbar if(nPanels==1): lmarg=0.03 rmarg=0.08 bmarg=0.1 tmarg=0.02 nCols=1 nRows=1 rect=np.array([lmarg,bmarg,(1.-rmarg-lmarg),(1.-tmarg-bmarg)]) tickLabelSize=14 figSize=(8,6) zFontSize='medium' barFontSize='x-small' morphFontSize='medium' else: lmarg=0.1 rmarg=0.01 bmarg=0.2 tmarg=0.01 nCols=np.min([3,nPanels]) nRows=int(np.ceil(float(nPanels)/nCols)) thisCol=int(np.remainder(thisPanel,nCols)) thisRow=int(np.floor(float(thisPanel)/nCols)) panelWidth=(1.-rmarg-lmarg)/nCols panelHeight=(1.-tmarg-bmarg)/nRows rect=np.array([lmarg+thisCol*panelWidth,bmarg+(nRows-thisRow-1)*panelHeight,panelWidth,panelHeight]) tickLabelSize=14 figSize=(8,9) zFontSize=10 barFontSize=7 morphFontSize=9 if(thisPanel==0): # use helvetica and latex plt.rc('font',**{'family':'sans-serif','sans-serif':['Helvetica'],'size':20}) plt.rc('text', usetex=True) plt.rc('axes',linewidth=1.5) plt.rc('xtick',labelsize=tickLabelSize) plt.rc('ytick',labelsize=tickLabelSize) xStr=r'Distance from Group Center [R/R$_{200{\rm c}}$]' yStr=r'Stellar Mass [log(M$_{\star}/$M$_{\odot}$)]' # start the plot with axes fig=plt.figure(1,figsize=figSize) # there will be a main figure with the galaxies and a second for the colorbar # a set of axes will be created for each panel and for the colorbar if(nPanels==1): ax1=plt.axes(rect) plt.xlabel(xStr,fontsize='medium') plt.ylabel(yStr,fontsize='medium') else: ax1=plt.axes(rect) if(thisPanel==0): fig.text(lmarg+0.5*(1.-lmarg-rmarg),0.75*bmarg,xStr,fontsize='medium',horizontalalignment='center',verticalalignment='center',rotation='horizontal') fig.text(0.22*lmarg,bmarg+0.5*(1.-bmarg-tmarg),yStr,fontsize='medium',horizontalalignment='center',verticalalignment='center',rotation='vertical') if(thisCol != 0): plt.setp(ax1.get_yticklabels(),visible=False) if((thisRow != nRows) & (thisPanel+nCols < nPanels)): plt.setp(ax1.get_xticklabels(),visible=False) if(legendFlag): # overlay morph category text yMorph=0.97 oplotMorphText(plt,yMorph,morphFontSize) plt.setp(ax1.get_xticklines(),visible=False) plt.setp(ax1.get_yticklines(),visible=False) plt.minorticks_off() axThick=5 axColor='black' ax1.axhline(linewidth=axThick,color=axColor) ax1.axvline(linewidth=axThick,color=axColor) ax1.axhline(y=1,linewidth=axThick,color=axColor) ax1.axvline(x=1,linewidth=axThick,color=axColor) else: # z range text (upper right) xText=0.95 yText=0.95 oplotZRange(plt, xText, yText, minZ, maxZ, zFontSize) # scale bar (below z range) xBar=xText yBar=0.82 oplotScaleBar(plt, xBar, yBar, zMed, imSize, imSizePlot, barFontSize) # print morph type (title) # setMorphTitle(plt, morph) # bottom shaded region for SM limit smLimit=getSMLimit(maxZ) oplotSMLimit(plt, smLimit, minSM, maxSM) # left shaded region for centrals oplotCentralRegion(plt, minR, maxR, minSM, maxSM, smLimit) plt.minorticks_on() # axes plt.xlim((0,1)) plt.ylim((0,1)) xticks=np.linspace(0,1,num=6) plt.xticks((xticks-minR)/(maxR-minR),xticks) yticks=np.linspace(10,12,num=3) plt.yticks((yticks-minSM)/(maxSM-minSM),yticks) marg=np.array([lmarg,rmarg,bmarg,tmarg]) return (plt,ax1,marg)