def fancy_dendrogram(*args, **kwargs): ''' Source: https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/ ''' from scipy.cluster import hierarchy import matplotlib.pylab as plt max_d = kwargs.pop('max_d', None) if max_d and 'color_threshold' not in kwargs: kwargs['color_threshold'] = max_d annotate_above = kwargs.pop('annotate_above', 0) ddata = hierarchy.dendrogram(*args, **kwargs) if not kwargs.get('no_plot', False): plt.title('Hierarchical Clustering Dendrogram (truncated)') plt.xlabel('sample index or (cluster size)') plt.ylabel('distance') for i, d, c in zip(ddata['icoord'], ddata['dcoord'], ddata['color_list']): x = 0.5 * sum(i[1:3]) y = d[1] if y > annotate_above: plt.plot(x, y, 'o', c=c) plt.annotate("%.3g" % y, (x, y), xytext=(0, -5), textcoords='offset points', va='top', ha='center') if max_d: plt.axhline(y=max_d, c='k') return ddata
def results_plot(tpr_vals, fdr_vals, names, fdr_threshold): import pandas as pd plt.close() with sns.axes_style('white', {'legend.frameon': True}): plt.rc('font', weight='bold') plt.rc('grid', lw=3) plt.rc('lines', lw=2) plt.rc('axes', lw=2) plt.figure(figsize=(12,5)) rates = [] labels = [] models = [] for t, f, n in zip(tpr_vals, fdr_vals, names): rates.extend(t) rates.extend(f) labels.extend(['TPR']*len(t)) labels.extend(['FDR']*len(f)) models.extend([n]*(len(t)+len(f))) df = pd.DataFrame({'value': rates, 'Rate': labels, 'Model': models}) ax = sns.boxplot(x='Model', y='value', hue='Rate', data=df) # RUN PLOT plt.xlabel('', fontsize=18, weight='bold') plt.ylabel('Power and FDR', fontsize=18, weight='bold') plt.axhline(fdr_threshold, color='red', lw=2, ls='--') # ax.tick_params(labelsize=10) plt.legend(loc='upper right') sns.despine(offset=10, trim=True)
def violin_plot(df, runs_name, attr, tot_same_repeats, yscale='linear'): savefolder, colors, legend_elements = generate_common_plotting_params(runs_name) plt.rcParams.update({'font.size': 22}) violin_colors = create_violin_colors(colors, repeat=tot_same_repeats) plt.figure(figsize=(25, 10)) if attr == 'avg_energy': plt.axhline(y=2, linewidth=1, color='r') elif attr== 'avg_velocity': plt.axhline(y=0.05, linewidth=1, color='r') chart = sns.violinplot(data=df, width=0.8, inner='quartile', scale='width', linewidth=0.05, palette=violin_colors) # inner='quartile' df.mean().plot(style='_', c='black', ms=30) chart.set_xticklabels(chart.get_xticklabels(), rotation=70) plt.yscale(yscale) #plt.gca().set_ylim(top=20) plt.legend(handles=legend_elements) plt.title(attr) plt.savefig('{}violin_{}.png'.format(savefolder, attr), dpi=300, bbox_inches='tight') plt.show()
def compute_autocorrelation(psd_x, wavenumber_x, x_zero_crossing, display=True): """ :param psd_x: :param wavenumber_x: :param x_zero_crossing: :param display: :return: """ autocorrelation = np.fft.ifft(psd_x).real autocorrelation /= autocorrelation[0] delta_f = 1 / (wavenumber_x[-1] - wavenumber_x[-2]) distance = np.linspace(0, int(delta_f/2), int(autocorrelation.size /2)) corr_model = (1 + factor_a * x / x_zero_crossing + one_six * (factor_a * x / x_zero_crossing) ** 2 - one_six * (factor_a * x / x_zero_crossing) ** 3) * \ np.exp(-np.abs(factor_a * x / x_zero_crossing)) if display: plt.plot(distance, autocorrelation[:int(autocorrelation.size/2)], lw=2, color='b', label='autocorrelation') plt.plot(x, corr_model, lw=2, color='r', label='MODEL') plt.legend() plt.xlabel("DISTANCE (km)") plt.ylabel("AUTOCORRELATION") plt.axhline(y=0., color='k', lw=2) plt.xlim(0, zero_crossing*3) plt.title("AutoCorrelation from spectrum Ahran model for zero crossing = %s" % str(zero_crossing)) plt.show() return autocorrelation[:int(autocorrelation.size/2)], distance
def showKernel(dataOrMatrix, fileName = None, useLabels = True, **args) : labels = None if hasattr(dataOrMatrix, 'type') and dataOrMatrix.type == 'dataset' : data = dataOrMatrix k = data.getKernelMatrix() labels = data.labels else : k = dataOrMatrix if 'labels' in args : labels = args['labels'] import matplotlib if fileName is not None and fileName.find('.eps') > 0 : matplotlib.use('PS') from matplotlib import pylab pylab.matshow(k) #pylab.show() if useLabels and labels.L is not None : numPatterns = 0 for i in range(labels.numClasses) : numPatterns += labels.classSize[i] #pylab.figtext(0.05, float(numPatterns) / len(labels), labels.classLabels[i]) #pylab.figtext(float(numPatterns) / len(labels), 0.05, labels.classLabels[i]) pylab.axhline(numPatterns, color = 'black', linewidth = 1) pylab.axvline(numPatterns, color = 'black', linewidth = 1) pylab.axis([0, len(labels), 0, len(labels)]) if fileName is not None : pylab.savefig(fileName) pylab.close()
def my_lines(ax, pos, *args, **kwargs): if ax == 'x': for p in pos: plt.axvline(p, *args, **kwargs) else: for p in pos: plt.axhline(p, *args, **kwargs)
def results_plot(tpr_vals, fdr_vals, names, fdr_threshold): import pandas as pd plt.close() with sns.axes_style("white", {"legend.frameon": True}): plt.rc("font", weight="bold") plt.rc("grid", lw=3) plt.rc("lines", lw=2) plt.rc("axes", lw=2) plt.figure(figsize=(12, 5)) rates = [] labels = [] models = [] for t, f, n in zip(tpr_vals, fdr_vals, names): rates.extend(t) rates.extend(f) labels.extend(["TPR"] * len(t)) labels.extend(["FDR"] * len(f)) models.extend([n] * (len(t) + len(f))) df = pd.DataFrame({"value": rates, "Rate": labels, "Model": models}) ax = sns.boxplot(x="Model", y="value", hue="Rate", data=df) # RUN PLOT plt.xlabel("", fontsize=18, weight="bold") plt.ylabel("Power and FDR", fontsize=18, weight="bold") plt.axhline(fdr_threshold, color="red", lw=2, ls="--") # ax.tick_params(labelsize=10) plt.legend(loc="upper right") sns.despine(offset=10, trim=True)
def plot_time(iterations): iteration = [] time = [] timer = Timer() mean_time = 0 for i in range(iterations): individuals = 10 first_operation = Operator(lambda x, y: x + y, '+') second_operation = Operator(lambda x, y: x * y, '*') operations = [first_operation, second_operation] values = [25, 7, 8, 100, 4, 2] constants = [] for value in values: constants.append(Constant(value)) depth = 3 goal = 459 timer.start() build_programs(individuals, operations, constants, depth, goal, 1000) value = timer.stop() time.append(value) mean_time += value iteration.append(i + 1) mean_time = mean_time / iterations plt.figure() plt.title("Tiempo Tomado en 1000 Generaciones", fontsize=20) plt.xlabel('Iteración') plt.ylabel('Tiempo (segundos)') plt.scatter(iteration, time, color='blue') plt.axhline(y=mean_time, color='r', linestyle='-') plt.show()
def vis_modematching(self): a = Listizing(self.R1, self.R2, self.L, self.wl, self.fl, self.w1, self.w2, self.t, self.d1, self.d2) wlist = np.array(a.get_cavity_w()) wlist_rev = -1 * self.wlist cavR = a.get_cavity_R() fullw = a.get_full_w() fullR = a.get_full_R() #plotting graphs fig = plt.figure(figsize=(15, 10)) fig.subplots_adjust(hspace=0.8) data1 = fig.add_subplot(3, 2, 1) plt.figure('Resonator') plt.title('Beam Contour in Resonator') plt.xlabel('distance(mm)') plt.ylabel('beam size(µm)') plt.plot(self.wlist, '-r') plt.plot(self.wlist_rev, '-r') plt.axhline(0, color='yellow') plt.xlabel('Time(µs)') plt.ylabel('Amplitude') plt.title('Signal of monochromatic wave') data1.plot(time, result, color='red', label='Monochromatic wave') plt.legend() plt.minorticks_on() data2 = fig.add_subplot(3, 2, 2) plt.xlabel('Time(µs)') plt.ylabel('Amplitude') plt.title('Signal with %s kHz Lineshape' % (self.e8.get())) data2.plot(time, result2, color='blue', linestyle='solid', label='with Lineshape', linewidth=1.5) plt.legend() plt.minorticks_on() data3 = fig.add_subplot(3, 1, 2) plt.xlabel('Time(µs)') plt.ylabel('Amplitude') plt.title('Build up, Ringdown Signal') data2.plot(time, result3, color='green', linestyle='solid', label='ringdown signal', linewidth=1.5) plt.legend() plt.minorticks_on() data4 = fig.add_subplot(3, 1, 3) plt.show()
def show_results(res1, res2, res3, res4, param): fig, axes = plt.subplots() x2 = [i for i in range(1000)] #axes.plot(x2, res1, 'gold', label="20x20") #axes.plot(x2, res2, 'orange', label="40x40") #axes.plot(x2, res3, 'r', label="80x80") axes.plot(x2, res4, 'k', label=r"$\ell = 3 \; (160\times160)$") if param != None: plt.axhline( y=param[0], linestyle='--', color='k', label= r"$\mathrm{\mathbb{E}} \left[\Vert u\Vert^2_{L^2} \right]_{MLMC}$") #axes.hist(solutions, bins = 40, color = 'blue', edgecolor = 'black') plt.style.use('classic') axes.legend(loc="best", prop={'size': 13}) axes.set_ylabel( r'$\mathrm{\mathbb{E}} \left[\Vert u \Vert^2_{L^2} \right]$', fontsize=14) axes.set_xlabel(r'Repetitions, $N$', fontsize=13) plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) axes.tick_params(axis="y", direction='in', which='both') axes.tick_params(axis="x", direction='in', which='both') plt.tight_layout() plt.show()
def create_fix_speedup(results, fixspeedupFile): parse_functions.removing_existing_file(fixspeedupFile) print("Creating fix speedup file: " + fixspeedupFile) import matplotlib.pylab as plt seg = config_generator.fixspeedup_seg fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.set_ylim([0, 9]) for i in range(1,10,1): plt.axhline(y = i, color ="black", linestyle ="--", linewidth=0.1, dashes=(5, 10)) for entry in results: plt.plot(results[entry][seg][0], results[entry][seg][1], config_generator.fixspeedupSymbols[entry], label=config_generator.fixspeedupLabels[entry]) plt.legend() # show line names plt.ylabel('Speedup') plt.xlabel('Array Lenght') plt.xticks(rotation=30) # rotate plt.subplots_adjust(bottom=0.2) # increment border plt.show() plt.savefig(fixspeedupFile, format='eps')
def mark_cross(center, **kwargs): """Mark a cross. Correct for matplotlib imshow funny coordinate system. """ N = 20 plt.hold(1) plt.axhline(y=center[1] - 0.5, **kwargs) plt.axvline(x=center[0] - 0.5, **kwargs)
def create_fix_times(fixTimes, fixFile): parse_functions.removing_existing_file(fixFile) print("Creating Fix file: " + fixFile) seg = config_generator.fixtimes_seg import matplotlib.pylab as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.set_ylim([0, 80]) for i in range(10,90,10): plt.axhline(y = i, color ="black", linestyle ="--", linewidth=0.1, dashes=(5, 10)) for strategy in fixTimes: plt.plot(fixTimes[strategy][seg][0], fixTimes[strategy][seg][1], label=config_generator.fixpassLabels[strategy]) plt.ylabel('Execution Time (ms)') plt.xlabel('Array lenght') plt.xticks(rotation=30) # rotate plt.subplots_adjust(bottom=0.2) # increment border plt.legend() #plt.subplots_adjust(bottom=0.2, right=0.75) # increment border #plt.xticks([]) # hide axis x #plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)) # show line names plt.savefig(fixFile, format='eps')
def make_axis(a): a.spines['top'].set_color('none') a.spines['right'].set_color('none') a.tick_params(axis='x', which='both', bottom="off", top="off", labelbottom="on") a.tick_params(axis="y", which="both", bottom="off", top="off", labelbottom="off", left="off", right="off", labelleft="on") plt.axvline(x=0, color='k') plt.ylabel("Correlation", fontsize=12) plt.ylim([-0.65, 1.]) plt.yticks([-0.6, -0.4, -0.2, 0., 0.2, 0.4, 0.6, 0.8, 1.], fontsize=12) plt.axhline(y=0., color='k', linestyle='-', linewidth=2.) plt.xlim([-3., 3.]) plt.xlabel("Lag [years]", fontsize=12) return 0
def process_mmd_experiment(width_class): results_file_name = mmd_experiment.results_file_stub + "_" + width_class + ".pickle" results = pickle.load( open(results_file_name,'rb' ) ) callibration_mmds = np.loadtxt('results/callibration_mmds.csv') mean_callibration = np.mean(callibration_mmds) mmd_squareds = results['mmd_squareds'] hidden_layer_numbers = results['hidden_layer_numbers'] hidden_unit_numbers = results['hidden_unit_numbers'] num_repeats = mmd_squareds.shape[2] mean_mmds = np.mean( mmd_squareds, axis = 2 ) std_mmds = np.std( mmd_squareds, axis = 2 ) / np.sqrt(num_repeats) plt.figure() for hidden_layer_number, index in zip(hidden_layer_numbers,range(len(hidden_layer_numbers))): if hidden_layer_number==1: layer_string = ' hidden layer' else: layer_string = ' hidden layers' line_name = str(hidden_layer_number) + layer_string plt.errorbar( hidden_unit_numbers, mean_mmds[:,index], yerr = 2.*std_mmds[:,index], label = line_name) plt.xlabel('Number of hidden units per layer') plt.xlim([0,60]) plt.ylabel('MMD SQUARED(GP, NN)') plt.ylim([0.,0.02]) plt.axhline(y=mean_callibration, color='r', linestyle='--') plt.legend() output_file_name = "../figures/mmds_" + width_class + ".pdf" plt.savefig(output_file_name) embed() plt.show()
def drawGammaR_1(poly,r=1,res=314): # Draws a curve gamma_r for a given complex polynomial # on the C plane. # The function maps points on the unit circle # to the complex plane using p: C --> C # generate slices of the interval [0,1] tspace = np.linspace(0,1,res+1) Zs = [] pZ = [] # map t to the function r = 1 for t in tspace: Zs.append(r*np.exp(2*math.pi*1j*t)) for z in Zs: pZ.append(poly(z)) X = [x.real for x in pZ] Y = [x.imag for x in pZ] # plot the figure ax = plt.subplot() ax.plot(X,Y) ax.set_xlabel('$Re(z)$') ax.set_ylabel('$Im(z)$') plt.axhline(color='black') plt.axvline(color='black') ax.minorticks_on() ax.grid(which='minor', alpha=0.5) ax.grid(which='major', alpha=0.5) ax.set_title(poly.latex_label) plt.show()
def testData(symbols, periods, low, high, portfolio, tradeSize, tradeFee, firstOnly=0, reverse=True, buyOnly=True, plot=False): numSym = len(symbols) ress = [[] for sym in symbols] for i, sym in enumerate(symbols): data = pandas.read_csv(sym+".csv") if reverse: data = data[::-1] if firstOnly>0: res = data[:firstOnly].copy() else: res = data.copy() ress[i] = res rsiCompute(res, periods) buyOrSell(res, low, high) tp, nt, nb, ns = computeProfit(ress, symbols, portfolio, tradeSize, tradeFee, buyOnly) print("tp = ",tp,", nt = ",nt,", nb = ",nb,", ns = ",ns) print("profit % = ", tp/portfolio*100) # print(res['rsi']) if plot and numSym == 1: res = ress[0] n = len(res) rcParams['figure.figsize'] = 60, 12 bs = np.array(res['bs']) x = np.arange(n) markers_onB = x[bs=='B'] markers_onS = x[bs=='S'] plt.plot(x, res["Adj Close"], '-bo', label='spy', markevery=markers_onB) plt.plot(x, res["rsi"], '-ro', label='RSI', markevery=markers_onS) plt.axhline(low) plt.axhline(high) plt.xlabel('Date') plt.ylabel('Price') plt.show()
def transform(gameData): # refHistory= { ref: { team : [total, games] } winData = transformWinLoss(gameData) collapsedData = winData.groupby([winData.referee,winData.team]).sum() collapsedData['winPercentage'] = collapsedData["teamWin"]/collapsedData["teamPlay"] totalData = winData.groupby(winData.team).sum() totalData['winPercentage'] = totalData["teamWin"]/totalData["teamPlay"] print totalData.sort_index(by='winPercentage', ascending=False) plt.figure(figsize=(8, 8)) #totalData['winPercentage'].plot(kind='bar', alpha=0.5) #plt.show() #plt.clf() print totalData.index.values for team in totalData.index.values: print "doing %s" % team teamData = winData.loc[winData['team'] == team ].groupby(winData.referee).sum() teamData = teamData[teamData['teamPlay'] > 6] if teamData.empty: continue print teamData teamData['winPercentage'] = teamData["teamWin"]/teamData["teamPlay"] teamData['winPercentage'].plot(kind='bar', alpha=0.5) teamAvg = totalData['winPercentage'][team] print teamAvg plt.ylim([0,1.0]) plt.axhline(teamAvg, color='k') plt.savefig('%s.png' % team) plt.clf()
def plot_ra(s1, s2, idxs=None, epsilon=0.25, fig=None): """Computes the RA plot of two groups of samples""" ## compute log2 values l1 = np.log2(s1 + epsilon) l2 = np.log2(s2 + epsilon) ## compute A and R r = l1 - l2 a = (l1 + l2) * 0.5 fig = pl.figure() if fig is None else fig pl.figure(fig.number) if idxs is None: pl.plot(a, r, '.k', markersize=2) else: pl.plot(a[~idxs], r[~idxs], '.k', markersize=2) pl.plot(a[idxs], r[idxs], '.r') pl.axhline(0, linestyle='--', color='k') pl.xlabel('(log2 sample1 + log2 sample2) / 2') pl.ylabel('log2 sample1 - log2 sample2') pl.tight_layout()
def get_OH(OIII4363,OIII4959,OIII5007,Hb): Te = np.arange(5000,20000,1) t3 = Te/1e4 ne = 100 # cm^-3 x = 1e-4*ne*t3**(-0.5) C_T = (8.44-1.09*t3+0.5*t3**2.-0.08*t3**3.)*(1.+0.0004*x)/(1.+0.044*x) log_OIII_ratio = 1.432/t3+np.log10(C_T) log_OIII_ratio_obs = np.log10((OIII4959+OIII5007)/OIII4363) Te_obs = [] plt.clf() plt.plot(Te,log_OIII_ratio,color='black',marker='.',linestyle='none') plt.yscale('log') for i in range(len(OIII4363)): plt.axhline(log_OIII_ratio_obs[i],linestyle='--') d_ratio = abs(log_OIII_ratio_obs[i]-log_OIII_ratio) min_d_ratio = min(d_ratio) min_sub = list(d_ratio).index(min_d_ratio) Te_obs.append(Te[min_sub]) plt.xlim(10000,20000) plt.ylim(1.,3) plt.xlabel('Te') plt.ylabel('(OIII4959+5007)/OIII4363') Te_obs = np.array(Te_obs) t3_obs = Te_obs/1e4 logOIIIH = np.log10((OIII4959+OIII5007)/Hb)+6.200+1.251+1.251/t3_obs - \ 5*np.log10(t3_obs)-0.014*t3_obs t2_obs = -0.577+t3*(2.065-0.498*t3) logOIIH = np.log10(OII3727/Hb)+5.961+1.676/t2_obs-0.4*np.log10(t2_obs) - \ 0.034*t2_obs+np.log10(1+1.35*x) OH = 10**(logOIIIH-12.)+10**(logOIIIH-12.) logOH = 12 + np.log10(OH) return Te_obs,logOIIH,logOIIIH,logOH
def plotPDF(data_samples, xlabel_str, ylabel_str, fig_name='pdf.pdf', xais_range=None, N=1000): # this create the kernel, given an array it will estimate the probability over that values kde = gaussian_kde(data_samples) # these are the values over wich your kernel will be evaluated dist_space = np.linspace(min(data_samples), max(data_samples), N) # plot the results fig = plt.figure(figsize=(8, 6)) x = dist_space y = kde(dist_space) plt.plot(x, y, markersize=0) # pdf line with no markers if xais_range is not None: plt.xlim(xais_range[0], xais_range[1]) # plot a vertical line and a honrizontal line i = np.argmax(y) xline = x[i] yline = y[i] plt.axvline(x=xline, linewidth=1, markersize=0, color='b', linestyle='--') plt.axhline(y=yline, linewidth=1, markersize=0, color='b', linestyle='--') # plt.xticks(list(plt.xticks()[0]) + [xline]) # plt.yticks(list(plt.yticks()[0]) + [yline]) plt.xticks(replaceNearestElement(plt.xticks()[0], xline)) plt.yticks(replaceNearestElement(plt.yticks()[0], yline)) plt.xlabel(xlabel_str) plt.ylabel(ylabel_str) plt.savefig(join(WORK_PATH, fig_name)) # plt.savefig(fig_name) plt.close(fig)
def plot_time_vs_epochs(train_data, test_data): timer = Timer() number_epochs = [] time = [] total_time = 0 # Build Neural Network neural_network = build_network() # 200 runs of 100 epochs each for i in range(100): number_epochs.append(i) timer.start() for j in range(1000): for data in test_data: neural_network.feed(data) this_time = timer.stop() time.append(this_time) total_time += this_time mean_time = total_time / len(number_epochs) # Plot plt.figure() plt.title("Time Taken in 1000 Epochs", fontsize=20) plt.xlabel('Number of Experiment') plt.ylabel('Time (seconds)') plt.scatter(number_epochs, time, color='blue') plt.axhline(y=mean_time, color='r', linestyle='-') plt.show()
def plotNLLAroundMin(self, NLLEval, minParams, errPosParams, errNegParams): numOfXPoints = 200 minTau = minParams[0] tauErrPos = errPosParams[0] tauErrNeg = errNegParams[0] minNLL = NLLEval([minTau]) tauWidth = 2 * tauErrPos minPlotTau = minTau - tauWidth maxPlotTau = minTau + tauWidth tauRange = np.linspace(minPlotTau, maxPlotTau, numOfXPoints) #stores y values NLLVals = [] for tau in tauRange: NLLVals.append(NLLEval([tau]) - minNLL) #centers tauRange around tauMin tauRange -= minTau pl.title(r"$\Delta \tau$ vs. $\Delta NLL$ about minimum") pl.xlabel(r"$\Delta \tau$") pl.ylabel("$\Delta NLL$") pl.axvline(x=tauErrPos, linestyle=':', color='r') pl.axvline(x=-tauErrNeg, linestyle=':', color='r') pl.axhline(y=0.5, linewidth=0.5, linestyle='--', color='dimgray') pl.plot(tauRange, NLLVals) pl.show()
def graficos(): plt.figure() for i in range(3): plt.subplot(3, 3, 1 + i) plt.grid(True) plt.plot(t / hr, x[:, i]) plt.figure() plt.grid(True) plt.plot(t / hr, Alt / km) plt.axhline(80., c="c") plt.axhline(0., c="green") plt.figure() plt.grid(True) plt.plot(x[:, 0], x[:, 1]) th = np.linspace(0, 2 * np.pi, 500) plt.plot(Rtierra * np.cos(th), Rtierra * np.sin(th)) plt.axis("equal") fig = plt.figure() ax = plt.axes(projection='3d') ax.plot3D(x[:, 0], x[:, 1], x[:, 2]) plt.show()
def create_fix_steps(results, fixstepsFile): parse_functions.removing_existing_file(fixstepsFile) print("Creating fix relation file: " + fixstepsFile) import matplotlib.pylab as plt import matplotlib.ticker as mtick seg = config_generator.fixsteps_seg fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.set_ylim([0, 70]) ax.yaxis.set_major_formatter(mtick.PercentFormatter()) for i in range(10,80,10): plt.axhline(y = i, color ="black", linestyle ="--", linewidth=0.1, dashes=(5, 10)) for entry in results: plt.plot(results[entry][seg][0], results[entry][seg][1], config_generator.fixstepsSymbols[entry], label=config_generator.fixstepsLabels[entry]) plt.legend() # show line names plt.ylabel('Percentage') plt.xlabel('Array Lenght') plt.xticks(rotation=30) # rotate plt.subplots_adjust(bottom=0.2) # increment border plt.show() plt.savefig(fixstepsFile, format='eps')
def plot_genetic_algorithm_time(): timer = Timer() experiments = [] time = [] total_time = 0 # Build Neural Network genetic_algorithm = GeneticFixedTopology(100, 1000) # 20 runs of 1000 generations each for i in range(20): experiments.append(i) timer.start() genetic_algorithm.run() this_time = timer.stop() time.append(this_time) genetic_algorithm = GeneticFixedTopology(100, 1000) total_time += this_time mean_time = total_time / len(experiments) # Plot plt.figure() plt.title("Time Taken in 1000 Generations", fontsize=20) plt.xlabel('experimento') plt.ylabel('tiempo (segundos)') plt.scatter(experiments, time, color='blue') plt.axhline(y=mean_time, color='r', linestyle='-') plt.show()
def test(args): data = multivariate_normal([0, 0], [[1, 2], [2, 5]], int(args[1])) print(data) # PCA result = pca(data, base_num=int(args[2])) pc_base = result[0] print(pc_base) # Plotting fig = plt.figure() fig.add_subplot(1, 1, 1) plt.axvline(x=0, color="#000000") plt.axhline(y=0, color="#000000") # Plot data plt.scatter(data[:, 0], data[:, 1]) # Draw the 1st principal axis pc_line = sp.array([-3.0, 3.0]) * (pc_base[1] / pc_base[0]) plt.arrow(0, 0, -pc_base[0] * 2, -pc_base[1] * 2, fc="r", width=0.15, head_width=0.45) plt.plot([-3, 3], pc_line, "r") # Settings plt.xticks(size=15) plt.yticks(size=15) plt.xlim([-3, 3]) plt.tight_layout() plt.show() plt.savefig("image.png") return 0
def printAccCurve(regType='OLS'): for time in times: curveData = np.array( list( csv.reader( open( './5-accCurveNoSameTake-' + time + '_' + regType + '.csv', 'r')))).astype(float) CIi = [] CIa = [] for l in curveData: CIi.append(l[0] - (1.96 * (l[1] / math.sqrt(l[5])))) CIa.append(l[0] + (1.96 * (l[1] / math.sqrt(l[5])))) #plt.axhline(1.2) # Set up dashed lines for y in np.arange(0.1, 1.11, 0.1): plt.axhline(y, color='k', ls='--', alpha=0.25) Xs = range(5, 121, 5) plt.plot(Xs, curveData[:, 4], label='Maximum') plt.plot(Xs, curveData[:, 2], label='Median') plt.plot(Xs, curveData[:, 0], label='Mean') plt.plot(Xs, CIi, 'k', alpha=0.2, label='95% Confidence Interval') plt.plot(Xs, CIa, 'k', alpha=0.2) plt.fill_between(Xs, CIi, CIa, facecolor='k', alpha=0.1) plt.plot(Xs, curveData[:, 3], label='Minimum') plt.title('Accuracy Curves for ' + time + ',' + regType) plt.legend(loc='best') plt.xlabel('Time Points') plt.ylabel('Accuracy') plt.show()
def standard_plot(self): s = [x for x in self.base.steady_state] self.base.enz[E_DAGK].v *= MUTANT_DEPLETION self.base.attain_steady_state() mt_rdga = [x for x in self.base.steady_state] self.base.enz[E_DAGK].v /= MUTANT_DEPLETION self.base.enz[E_LAZA].v *= MUTANT_DEPLETION self.base.attain_steady_state() mt_laza = [x for x in self.base.steady_state] self.base.enz[E_LAZA].v /= MUTANT_DEPLETION ax = self.figure.add_subplot(111) ind = np.arange(4) # the x locations for the groups width = 0.35 # the width of the bars exp_observations = [1, 1, 1, 2.5] val1 = get_ratios(s, mt_rdga) val2 = get_ratios(s, mt_laza) vals = val1[0], val1[1], val2[0], val2[1] legends = ["DAG/PI\n(rdga3)", "PA/PI\n(rdga3)", "DAG/PI\n(laza22)", "PA/PI\n(laza22)"] ax.bar(ind, exp_observations, width, color='#3c6df0', label="WT") ax.bar(ind + width, vals, width, color='#fe6100', label="MT") ax.set_xticks(ind + width / 2) plt.axhline(1, linestyle="--") plt.axhline(2.5, linestyle="--") ax.set_xticklabels(legends) plt.legend(loc=0)
def plot_equilibration(temperature_next, strain_lst, nve_run_time_steps, project_parameter, debug_plot=True): if debug_plot: for strain in strain_lst: job_name = get_nve_job_name( temperature_next=temperature_next, strain=strain, steps_lst=project_parameter['nve_run_time_steps_lst'], nve_run_time_steps=nve_run_time_steps) ham_nve = project_parameter['project'].load(job_name) plt.plot(ham_nve['output/generic/temperature'], label='strain: ' + str(strain)) plt.axhline(np.mean(ham_nve['output/generic/temperature'][-20:]), linestyle='--', color='red') plt.axvline(range(len(ham_nve['output/generic/temperature']))[-20], linestyle='--', color='black') plt.legend() plt.xlabel('timestep') plt.ylabel('Temperature K') plt.legend() plt.show()
def test_cumulative(plot=False): # arrange endpoints = (44, 666) norm_factor = np.pi spectrum = spectra.TopHat(norm_factor=norm_factor, endpoints=endpoints) x = np.linspace(0, 1000) # act y = spectrum.cumulative(x) # plot if plot: pylab.axhline(0) pylab.axhline(norm_factor) pylab.axvline(endpoints[0]) pylab.axvline(endpoints[1]) pylab.plot(x, y, color='red') pylab.xlim(x[0], x[-1]) pylab.grid() pylab.show() # assert for point in y: assert 0 <= point <= norm_factor assert y[-1] == norm_factor hy, hx = np.histogram(np.diff(y) / np.diff(x)) assert hx[0] == 0 np.testing.assert_approx_equal( hx[-1], norm_factor / (endpoints[1] - endpoints[0])) assert np.sum(hy[1:-1]) == 2
def create_scurve(scurves, scurveFile): parse_functions.removing_existing_file(scurveFile) print("Creating scurve file: " + scurveFile) import matplotlib.pylab as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.set_ylim([0, 9]) for i in range(1,10,1): plt.axhline(y = i, color ="black", linestyle ="--", linewidth=0.1, dashes=(5, 10)) for strategy in scurves: length = len(scurves[strategy]) middle = int(length/2-1) markers_on = [0, middle, length-1] #plt.plot(scurves[strategy], config_generator.symbols[strategy], color=config_generator.colors[strategy], markevery=marks, label=config_generator.abbreviations[strategy]) plt.plot(scurves[strategy], config_generator.symbols[strategy], color=config_generator.colors[strategy], markevery=markers_on, label=config_generator.abbreviations[strategy], dashes=(5, 5)) plt.ylabel('Normalized Times') plt.locator_params(nbins=3) plt.xticks([]) # hide axis x plt.legend() # show line names plt.savefig(scurveFile, format='eps')
def mark_cross(center, **kwargs): """Mark a cross. Correct for matplotlib imshow funny coordinate system. """ N = 20 plt.hold(1) plt.axhline(y=center[1]-0.5, **kwargs) plt.axvline(x=center[0]-0.5, **kwargs)
def create_hou_curve(houCurve, houFile): parse_functions.removing_existing_file(houFile) print("Creating Hou file: " + houFile) strategy = 'bbsegsort' import matplotlib.pylab as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.set_ylim([0, 1.5]) for i in numpy.arange(0.25, 1.75, 0.25): plt.axhline(y = i, color ="black", linestyle ="--", linewidth=0.1, dashes=(5, 10)) for length in houCurve: if(length > 530000): continue; plt.plot(houCurve[length][0], houCurve[length][1], label=str(length)) #for seg in houCurve: # plt.plot(houCurve[seg], config_generator.symbols[strategy], color=config_generator.colors[strategy], label=config_generator.abbreviations[strategy]) plt.ylabel('Execution Time (ms)') plt.xlabel('Number of Segments') plt.xticks(rotation=30) # rotate plt.subplots_adjust(bottom=0.2) # increment border #plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)) # show line names #plt.subplots_adjust(right=0.75) # increment border plt.legend() plt.savefig(houFile, format='eps')
def individual_plot(self): s = [x for x in self.base.steady_state] s.append( self.base.steady_state[I_PMPA] + self.base.steady_state[I_ERPA]) s.append( self.base.steady_state[I_PMPI] + self.base.steady_state[I_ERPI]) if self.mutant_enzyme != E_NONE: self.base.enz[self.mutant_enzyme].v *= MUTANT_DEPLETION self.base.attain_steady_state() mt = [x for x in self.base.steady_state] mt.append( self.base.steady_state[I_PMPA] + self.base.steady_state[I_ERPA]) mt.append( self.base.steady_state[I_PMPI] + self.base.steady_state[I_ERPI]) ax = self.figure.add_subplot(111) ind = np.arange(len(LIPID_NAMES) + 2) # the x locations for the groups width = 0.35 # the width of the bars ax.bar(ind, np.asanyarray(mt) / np.asanyarray(s), width) ax.set_xticks(ind) plt.axhline(1, linestyle="--") # plt.yscale("log") nm = [L_PMPI, L_PI4P, L_PIP2, L_DAG, L_PMPA, L_ERPA, L_CDPDAG, L_ERPI, "PA", "PI"] ax.set_xticklabels(nm) if self.mutant_enzyme != E_NONE: self.base.enz[self.mutant_enzyme].v /= MUTANT_DEPLETION
def plot_deviations(self): '''Plots the p-value and deviation level over time. ''' plt.scatter(self.T, self.P, marker=".") plt.plot(self.T, self.M) plt.axhline(y=self.dev_threshold, color='r', linestyle='--') plt.show()
def plot_eigen_spectrum(eig, laser_freq_eV=0.0, file_to_save='eig.png', figuresize=(10,10)): plt.figure(figsize=figuresize) # in inches! plt.plot(eig.real, 'o', ms=2) plt.ylabel(r'$E, eV$', fontsize=26) plt.axhline(y=laser_freq_eV, color='r', ls='--') plt.axhline(y=0, color='r', ls='--') if file_to_save: plt.savefig(file_to_save) plt.close() return None
def epi_vs_gain_volcano_plot(filtered_gain_snps, filtered_epi_snps, gain_vals, epi_vals, max_p, min_I3): gain_I3 = [] gain_log_p = [] for snps in filtered_gain_snps: gain_I3.append(gain_vals[snps]) order = switch_snp_key_order(snps) if epi_vals.has_key(order[0]): gain_log_p.append(epi_vals[order[0]]) elif epi_vals.has_key(order[1]): gain_log_p.append(epi_vals[order[1]]) gain_log_p = -1 * np.log10(gain_log_p) epi_I3 = [] epi_log_p = [] for snps in filtered_epi_snps: order = switch_snp_key_order(snps) if gain_vals.has_key(order[0]): epi_I3.append(gain_vals[order[0]]) elif gain_vals.has_key(order[1]): epi_I3.append(gain_vals[order[1]]) epi_log_p.append(epi_vals[snps]) epi_log_p = -1 * np.log10(epi_log_p) mp.figure(1) mp.xlabel("I3") mp.ylabel("-log10(P)") mp.title("Volcano plot - EPISTASIS and GAIN") mp.plot(epi_I3, epi_log_p, "bo") mp.plot(gain_I3, gain_log_p, "ro") mp.axhline(y=(-1 * np.log10(max_p)), linewidth=2, color="g") mp.axvline(x=min_I3, linewidth=2, color="g") # label max point max_x = np.max(gain_I3) max_y = np.max(gain_log_p) best_connection = "" # label best edge for snps in epi_vals: if -1 * np.log10(epi_vals[snps]) == max_y: best_connection = str(snps) mp.text( np.max(gain_I3), np.max(gain_log_p), best_connection, fontsize=10, horizontalalignment="center", verticalalignment="center", ) mp.show() print
def plot_episode(args): """Plot an episode plucked from the large h5 database""" print "plot_episode" # load the data file tblfilename = "bf_optimize_mavlink.h5" h5file = tb.open_file(tblfilename, mode = "a") # get the table handle table = h5file.root.v2.evaluations # selected episode episode_row = table.read_coordinates([int(args.epinum)]) # compare episodes episode_row_1 = table.read_coordinates([2, 3, 22, 46]) # bad episodes print "row_1", episode_row_1.shape # episode_row = table.read_coordinates([3, 87]) episode_target = episode_row["alt_target"] episode_target_1 = [row["alt_target"] for row in episode_row_1] print "episode_target_1.shape", episode_target_1 episode_timeseries = episode_row["timeseries"][0] episode_timeseries_1 = [row["timeseries"] for row in episode_row_1] print "row", episode_timeseries.shape print "row_1", episode_timeseries_1 sl_start = 0 sl_end = 2500 sl_len = sl_end - sl_start sl = slice(sl_start, sl_end) pl.plot(episode_timeseries[sl,1], "k-", label="alt", lw=2.) print np.array(episode_timeseries_1)[:,:,1] pl.plot(np.array(episode_timeseries_1)[:,:,1].T, "k-", alpha=0.2) # alt_hold = episode_timeseries[:,0] > 4 alt_hold_act = np.where(episode_timeseries[sl,0] == 11) print "alt_hold_act", alt_hold_act[0].shape, sl_len alt_hold_act_min = np.min(alt_hold_act) alt_hold_act_max = np.max(alt_hold_act) print "min, max", alt_hold_act_min, alt_hold_act_max, alt_hold_act_min/float(sl_len), alt_hold_act_max/float(sl_len), # pl.plot(episode_timeseries[sl,0] * 10, label="mode") pl.axhspan(-100., 1000, alt_hold_act_min/float(sl_len), alt_hold_act_max/float(sl_len), facecolor='0.5', alpha=0.25) pl.axhline(episode_target, label="target") pl.xlim((0, sl_len)) pl.xlabel("Time steps [1/50 s]") pl.ylabel("Alt [cm]") pl.legend() if args.plotsave: pl.gcf().set_size_inches((10, 3)) pl.gcf().savefig("%s.pdf" % (sys.argv[0][:-3]), dpi=300, bbox_inches="tight") pl.show()
def diff_plot_bar(lists, list_ids, xticks, rotation=0, xlabel='', ylabel='Accuracy', hline_at=None, legend_title='Method', **kwargs): """ Compare the scores of paired of experiment ids and plot a bar chart or their accuracies. :param list1, list2: [1,2,3], [4,5,6] means exp 1 is compared to exp 4, etc ... :param list1_id, list2_id: name for the first/ second group of experiments, will appear in legend :param xticks: labels for the x-axis, one per pair of experiments, e.g. list('abc') will label the first pair 'a', etc. Will appear as ticks on x axis. If only two lists are provided a significance test is run for each pair and a * is added if pair is significantly different :param rotation: angle of x axis ticks :param hline_at: draw a horizontal line at y=hline_at. Useful for baselines, etc :param kwargs: extra arguments for sns.factorplot """ assert len(set(map(len, lists))) == 1 assert len(list_ids) == len(lists) df_scores, df_reps, df_groups, df_labels = [], [], [], [] if len(lists) == 2: for i, (a, b) in enumerate(zip(*lists)): significance_df, names, mean_scores = get_demsar_params([a, b], name_format=['id', 'expansions__vectors__id', 'expansions__vectors__composer', 'expansions__vectors__algorithm', 'expansions__vectors__dimensionality']) if significance_df is None: continue if significance_df.significant[0]: xticks[i] += '*' for i, exp_ids in enumerate(zip(*lists)): data, folds = get_cv_scores_many_experiment(exp_ids) df_scores.extend(data) df_reps.extend(folds) df_labels.extend(len(folds) * [xticks[i]]) for list_id in list_ids: df_groups.extend(len(folds) // len(lists) * [list_id]) df = pd.DataFrame(dict(Accuracy=df_scores, reps=df_reps, Method=df_groups, labels=df_labels)) df.rename(columns={'Method': legend_title}, inplace=True) g = sns.factorplot(y='Accuracy', hue=legend_title, x='labels', data=df, kind='bar', aspect=1.5, **kwargs); g.set_xticklabels(rotation=rotation); # remove axis labels for ax in g.axes.flat: ax.set(xlabel=xlabel, ylabel=ylabel) if hline_at is not None: plt.axhline(hline_at, color='black')
def plot_moving_correlation(series, chrons, start, end, crit=None, window=get_window('boxcar', 31)): w = len(window) / 2 t = range(start + w, end - w + 1) p = series.ix[start:end].values.flatten() for col in chrons: c = chrons[col].ix[start:end].values corrs, pvals = moving_correlation(p, c, window) plt.plot(t, corrs, label=col.upper(), markevery=(t[-1]-t[0])/12, **pens[col.upper()]) if crit is not None: plt.axhline(y=crit, linestyle='--', color='black')
def plot_all_pair_correlation(dirname=".",target="g_tilde"): handles=[] global folders plt.figure(figsize=(20,10)) global folders # plot all energies print "Pair correlations" for f in folders: label=get_label(target,f) handles.append(anal.plot_file(f.dir_path+"/pair_correlation_t.dat",label=label,error=True )) plt.legend(handles=handles) plt.axhline(y=1, xmin=0,hold=True) handles=[]
def plot_weather(p, ranges, plot_happiness = False, threshold=40.0): plts = [] for date_start, date_end in ranges: print "Getting weather data for %s - %s..." % (date_start, date_end) station_info, observations = p.get_weather_hourly(94846, date_start, date_end) observations = Plenario.get_weather_observations_list(observations, "datetime", ["drybulb_fahrenheit"]) observations.reverse() dates = [x[0] for x in observations] datesnorm = normalize_dates(dates) temps = [x[1] for x in observations] fill_missing_values(temps) label = "%s - %s" % (date_start, date_end) if plot_happiness: Ys = gen_happiness(temps, threshold) else: Ys = temps w_plt, = plt.plot(datesnorm, Ys, label=label) plts.append(w_plt) if plot_happiness: hline = 0 title = "Weather-related Happiness in Chicago" ylabel = "Happiness" else: hline = 32 title = "Temperatures in Chicago" ylabel = u"Temperature (°F)" xlabel = "Week" nweeks = (int(datesnorm[-1]) / 604800) + 1 plt.xticks([w*604800 for w in range(nweeks)], [w for w in range(nweeks)]) plt.axhline(hline, color="gray", linestyle="--") plt.legend(handles=plts, loc="lower left") plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) plt.show()
def draw_cost_history(self, filename='output/cost.png', compare_dict=None): colors = ['blue', 'red', 'yellow', 'green', 'pink'] f, ax = plt.subplots() ax.plot(self.cost_history, lw=1, c='black', label='cost', alpha=0.8) df = pd.DataFrame(columns=['rolling mean'], data=self.cost_history) df['rolling mean'] = pd.rolling_mean(df['rolling mean'], window=100) df.plot(ax=ax, lw=3, c='green') if compare_dict is not None: for idx, (name, vals) in enumerate(compare_dict.iteritems()): c = colors[idx % len(colors)] if hasattr(vals, '__iter__'): plt.plot(vals, lw=2, label=name, c=c, alpha=0.6) else: plt.axhline(y=vals, lw=2, label=name, c=c, alpha=0.6) plt.legend(loc='best') plt.savefig(filename, dpi=150) plt.close('all')
def plotDifference(bst, dtest, target = None): p = bst.predict(dtest) l = 0 if (target is None): l = dtest.get_label() else: l = target res = np.abs((l-p)/l) res = res[~np.isnan(res)] res = res[~np.isinf(res)] xmax = 0.2 for y in np.arange(0,xmax,xmax/8): plt.axvline(y, color='g', linestyle='dashed') plt.hist(res, range=(0,xmax), bins=50, color='red') plt.axvline(res.mean(), color='c', linestyle='dashed', linewidth=2) for y in range(200,1600,200): plt.axhline(y, color='g', linestyle='dashed')
def view_design(g, t, arange): """Plot the scaling and wavelet kernel. Plot the input scaling function and wavelet kernels, indicates the wavelet scales by legend, and also shows the sum of squares G and corresponding frame bounds for the transform. Parameters ---------- g : list of function handles for scaling function and wavelet kernels t : array of wavelet scales corresponding to wavelet kernels in g arange : approximation range Returns ------- h : figure handle """ x = np.linspace(arange[0], arange[1], 1e3) h = plt.figure() J = len(g) G = np.zeros(x.size) for n in range(J): if n == 0: lab = 'h' else: lab = 't=%.2f' % t[n-1] plt.plot(x, g[n](x), label=lab) G += g[n](x)**2 plt.plot(x, G, 'k', label='G') (A, B, _, _) = framebounds(g, arange[0], arange[1]) plt.axhline(A, c='m', ls=':', label='A') plt.axhline(B, c='g', ls=':', label='B') plt.xlim(arange[0], arange[1]) plt.title('Scaling function kernel h(x), Wavelet kernels g(t_j x) \n' 'sum of Squares G, and Frame Bounds') plt.yticks(np.r_[0:4]) plt.ylim(0, 3) plt.legend() return h
def simSeries(numSeries): prob = 0.5 fracWon = [] probs = [] while prob <= 1.0: seriesWon = 0 for i in range(numSeries): if playSeries(7, prob): seriesWon += 1 fracWon.append(seriesWon/numSeries) probs.append(prob) prob += 0.01 plt.plot(probs, fracWon, linewidth=3) plt.xlabel("Probability of Winning a Game") plt.ylabel("Probability of Winning a Series") plt.axhline(0.95) plt.ylim(0.5, 1.1) plt.title(str(numSeries) + " Seven-Game Series")
def fplot(x0): """plot f(x) in the neighborhood of the initial guess""" #build x and f vectors points = 200 xmin = x0 - 5.0 xmax = x0 + 5.0 xvec = linspace(xmin,xmax, num=points,endpoint = True) fvec = matrix (0, (points, 1), 'd') for item, x in enumerate(xvec): fvec[item] = Function.fcall(x) # graphical commands fig = pyplot.figure() pyplot.hold(True) pyplot.plot(xvec, fvec, 'k') pyplot.axhline(linestyle=':',color = 'k') pyplot.axvline(linestyle=':',color = 'k') pyplot.xlabel('$x$') pyplot.ylabel('$f(x)$') pyplot.savefig('zeroplot.eps',format='eps') pyplot.show()
def findSeriesLength(teamProb): numSeries = 200 maxLen = 2500 step = 10 def fracWon(teamProb, numSeries, seriesLen): won = 0 for series in range(numSeries): if playSeries(seriesLen, teamProb): won += 1 return won/numSeries winFrac = [] xVals = [] for seriesLen in range(1, maxLen, step): xVals.append(seriesLen) winFrac.append(fracWon(teamProb, numSeries, seriesLen)) plt.plot(xVals, winFrac, linewidth=3) plt.xlabel("Length of Series") plt.ylabel("Probability of Winning Series") plt.title(str(round(teamProb, 4))+ " Probability of Better Team Winning a Game") plt.axhline(0.95) plt.ylim(0.5, 1.1)
def plot_rough(data=None, use_fb=True, levels=20, colorbars=False): if data is None: data = read_rough('rough') (x, y) = ('fb_x', 'fb_y') if use_fb else ('x', 'y') fig = pl.figure() #pos = np.genfromtxt("rough_pos") pos = None print pos for n,signal in enumerate(['psd_x', 'psd_y', 'sum_x', 'sum_y']): pl.subplot(2,2,n+1) pl.contourf(data[x][:,:,0], data[y][:,:,0], data[signal][:,:,0], levels) pl.title(signal) pl.axis('equal') pl.axis('tight') if colorbars: pl.colorbar() if pos is not None: pl.axvline(pos[0], alpha=0.3) pl.axhline(pos[1], alpha=0.3) fig.show()
def drawxint(): df = pd.read_csv('out/ana_bd.dat') # dftmp = df[['W', 'Q2', 'xsect', 'err', 'fsigchi2']][ # (df['fsigchi2'] < 3) & (df['fsigchi2'] > 0) & (df['weightsb'] > 0) & (df['weight3s'] > 0)] q2groups = df[(df['fsigchi2'] < 3) & (df['fsigchi2'] > 0.5) & ( df['fsigstat'] == 'CONVERGED ') & (df['weightsb'] > 0) & (df['weight3s'] > 0) & (df['fsigpar2'] > 0.008) & (df['fsigpar2'] < 0.035)].groupby(df.Q2) # xerrs = np.array(48 * [0.0]) gtitle = '$Q^2 = %.3f$ $GeV^2$' for q2, grp in q2groups: fig = plt.figure() ax1 = fig.add_subplot(111) ax1.errorbar(grp.W, grp.xsect, grp.err, ls='none', marker='o', ms=4, color='black') ax1.set_ylim(-50, ax1.get_ylim()[1]) ax1.set_xlim((1.6, 3.0)) ax1.set_title(gtitle % q2, fontsize=20, y=1.025) ax1.set_xlabel('W (GeV)', fontsize=16) ax1.set_ylabel('$\sigma$ ($nb^{-1}$)', fontsize=20) plt.grid(b=True, which='major', color='black', ls='-') plt.grid(b=True, which='minor', color='black', ls='dotted') ax1.minorticks_on() plt.axhline(0, color='blue', ls='-', linewidth=1.25) ax2 = ax1.twinx() ax2.set_ylabel('$\~{\chi}^{2}$', color='r', fontsize=20) ax2.set_ylim(0, 3) ax2.plot(grp.W, grp.fsigchi2, marker='o', ms=4, ls='none', color='r') for tickl in ax2.get_yticklabels(): tickl.set_color('r') # ax2 = ax1.twinx() # ax2.set_ylabel('$\sigma_{g}$', color='r') # ax2.set_ylim(0.008, 0.035) # ax2.plot(grp.W, grp.fsigpar2, marker='o', ms=4, ls='none', color='r') # for tickl in ax2.get_yticklabels(): # tickl.set_color('r') # plt.tight_layout() plt.show() wait()
def plot_abc(self, uarg, param): """Plot a() and b() functions on the same plot. """ plt.figure(figsize=(8, 4)) plt.subplot(1, 3, 1) plt.plot(uarg, self.afun(uarg, param)) plt.axhline(0) plt.axvline(0) plt.ylabel('$a(u)$') plt.xlabel('$u$') plt.subplot(1, 3, 2) plt.plot(uarg, self.bfun(uarg, param)) plt.axhline(0) plt.axvline(0) plt.ylabel('$b(u)$') plt.xlabel('$u$') plt.subplot(1, 3, 3) plt.plot(uarg, self.cfun(uarg, param)) plt.axhline(0) plt.axvline(0) plt.ylabel('$c(u)$') plt.xlabel('$u$') plt.tight_layout() plt.show()
def plotMadMedian(self, events, figsize=(5, 5), save=False, figname='mam-median-evts', figtype='png'): """ Plots the median and the medan absolute value of the input array events **Parameters** events : double The spike events figsize : float A tuple of the sizes of the figure save : boolean Indicates if the figure will be saved figname : string The name of the saved figure figtype : string The type of the saved figure (supports png and pdf) """ events_median = np.apply_along_axis(np.median, 0, events) events_mad = np.apply_along_axis(mad, 0, events) plt.figure(figsize=figsize) plt.plot(events_median, color='red', lw=2) plt.axhline(y=0, color='black') for i in np.arange(0, 400, 100): plt.axvline(x=i, c='k', lw=2) for i in np.arange(0, 400, 10): plt.axvline(x=i, c='grey') plt.plot(events_median, 'r', lw=2) plt.plot(events_mad, 'b', lw=2) if save: if figtype == 'pdf': plt.savefig(figname+'pdf', dpi=90) else: plt.savefig(figname+'png')
def manhattan(subplot, p_values, p=0.05, threshold='bonferroni', title=None, colors=('r', 'g', 'b'), min_p_value=1e-16): '''Generate a Manahattan plot from a list of 22 p-value data sets. Entry data[i] corresponds to chromosome i-1, and should be tuple (snp_bp_coordinate, p_value).''' # Prepare plot subplot.set_yscale('log') if title: P.title(title) P.xlabel('Chromosome') P.ylabel(r'$p^{-1}$') P.hold(True) offset = genome_bp_offset()[:NUM_CHROMOSOMES] (xmin, xmax) = (np.inf, -np.inf) chr_label = [] for (index, (snp_chr, p_chr)) in enumerate(p_values): x = snp_chr + offset[index] color = colors[np.mod(index, len(colors))] P.scatter(x, 1.0 / np.maximum(min_p_value, p_chr), c=color, marker='o', edgecolor=color) xmin = min(xmin, np.min(x)) xmax = max(xmax, np.max(x)) chr_label.append('%s' % (index + 1,)) P.xlim((xmin, xmax)) # Set the locations and labels of the x-label ticks P.xticks(ndimage.convolve(offset, [0.5, 0.5]), chr_label) # Calculate significance threshold if threshold == 'bonferroni': # Bonferroni correction: divide by the number of SNPs we are considering num_snps = sum(len(chr_data[0]) for chr_data in p_values) threshold = p / num_snps elif isreal(threshold): # Custom threshold pass elif not threshold: raise ValueError('Unsupported threshold %s' % (threshold,)) if threshold is not None: # Draw threshold P.axhline(y=1.0 / threshold, color='red')
def graph(dates,pounds,filename): plt.xlabel("Date") plt.ylabel("Pound") fig = plt.figure(facecolor='white') ax1 = fig.gca() # Add zero line plt.axhline(linewidth=1, color='b') # Set font size of labels for axis in [ax1.axes.get_xticklabels(),ax1.axes.get_yticklabels()]: for xlabel_i in axis: xlabel_i.set_fontsize(8) right=0.94 lft=1-right fig.subplots_adjust(top=0.97,right=0.94,left=lft) # Set size of graph F = pylab.gcf() DefaultSize = F.get_size_inches() F.set_size_inches( (DefaultSize[0]*0.7, DefaultSize[1]*0.7) ) # Fill in graph ax1.fill_between(dates, pounds, facecolor='blue', alpha=0.5) # Plot dates ax1.plot_date(dates, pounds, color='blue', ls='-',marker='None', label='MA (200)') ax1.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%a %d\n%b %Y')) # Save to file ax1 = plt.gca() plt.savefig(filename) plt.close()
def run(self, filename): from dials.model.data import ReflectionList, Reflection from math import sqrt import cPickle as pickle import numpy rlist = pickle.load(open(filename, "rb")) print "read file" r_to_use = [r for r in rlist if r.is_valid() and r.bounding_box[1] - r.bounding_box[0] >= 5 and r.bounding_box[3] - r.bounding_box[2] >= 5 and r.intensity / sqrt(r.intensity_variance) > 10] print "Filtered list", len(r_to_use) px_prd = [r.image_coord_px for r in r_to_use] f_prd = [r.frame_number for r in r_to_use] px_obs = [r.centroid_position for r in r_to_use] x_prd, y_prd = zip(*px_prd) x_obs, y_obs, f_obs = zip(*px_obs) diff = [] for xp, yp, zp, xo, yo, zo in zip(x_prd, y_prd, f_prd, x_obs, y_obs, f_obs): diff.append(sqrt((xp - xo)**2 + (yp - yo)**2 + (zp - zo)**2)) x_diff = [p - o for p, o in zip(x_prd, x_obs)] y_diff = [p - o for p, o in zip(y_prd, y_obs)] f_diff = [p - o for p, o in zip(f_prd, f_obs)] from matplotlib import pylab pylab.subplot(3, 1, 1) pylab.scatter(x_prd, x_diff) pylab.axhline(numpy.mean(x_diff)) pylab.subplot(3, 1, 2) pylab.scatter(y_prd, y_diff) pylab.axhline(numpy.mean(y_diff)) pylab.subplot(3, 1, 3) pylab.scatter(f_prd, f_diff) pylab.axhline(numpy.mean(f_diff)) pylab.show() #from scipy.interpolate import griddata points = zip(y_prd, x_prd) pickle.dump((points, x_diff, y_diff), open("temp_data.pickle", "wb"))
nRuns = 5000; # The standard deviation in the random walk proposal stepSize = 0.10; # Run the PMH algorithm res = helpParam.pmh(y,initPar,par,nPart,T,x0,helpState.pf,nRuns,stepSize) ############################################################################## # Plot the results ############################################################################## # Plot the parameter posterior estimate # Solid black line indicate posterior mean plt.subplot(2,1,1); plt.hist(res[nBurnIn:nRuns], np.floor(np.sqrt(nRuns-nBurnIn)), normed=1, facecolor='#7570B3') plt.xlabel("par[0]"); plt.ylabel("posterior density estimate") plt.axvline( np.mean(res[nBurnIn:nRuns]), linewidth=2, color = 'k' ) # Plot the trace of the Markov chain after burn-in # Solid black line indicate posterior mean plt.subplot(2,1,2); plt.plot(np.arange(nBurnIn,nRuns,1),res[nBurnIn:nRuns],color = '#E7298A') plt.xlabel("iteration"); plt.ylabel("par[0]") plt.axhline( np.mean(res[nBurnIn:nRuns]), linewidth=2, color = 'k' ) ############################################################################## # End of file ##############################################################################