def plot(data, name, save): labels = list() means = list() std = list() i = 1 for label, values in sorted(data.iteritems(), key=lambda x: x[1][0], reverse=True): labels.append('user %d' % i) i += 1 means.append(100*values[0]) std.append(100*values[1]) ind = np.arange(len(labels)) # the x locations for the groups width = 0.6 # the width of the bars: can also be len(x) sequence p1 = plt.bar(ind, means, width, color='0.7', yerr=std) plt.ylabel('Average Performance') plt.title('Classifier Performance') plt.xticks(ind+width/2., labels) plt.yticks(np.arange(0,101,10)) plt.legend( (p1[0],), ('RBF Kernel SVM',) ) plt.axhline(y=50) # Horizontal line indicating random choice. #ax = plt.gca() #ax.set_aspect(0.05) if save is None: plt.show() else: path = os.path.join(save, '%s.eps' % name) plt.savefig(path, format='eps') plt.clf()
def build_plot(profilerResults): # Calculate each value. x = [] mean = [] std = [] for t in xrange(profilerResults.getLookBack()*-1, profilerResults.getLookForward()+1): x.append(t) values = np.asarray(profilerResults.getValues(t)) mean.append(values.mean()) std.append(values.std()) # Cleanup plt.clf() # Plot a line with the mean cumulative returns. plt.plot(x, mean, color='#0000FF') # Error bars starting on the first lookforward period. lookBack = profilerResults.getLookBack() firstLookForward = lookBack+1 plt.errorbar( x=x[firstLookForward:], y=mean[firstLookForward:], yerr=std[firstLookForward:], capsize=3, ecolor='#AAAAFF', alpha=0.5 ) # Horizontal line at the level of the first cumulative return. plt.axhline( y=mean[lookBack], xmin=-1*profilerResults.getLookBack(), xmax=profilerResults.getLookForward(), color='#000000' ) plt.xlim(profilerResults.getLookBack()*-1-0.5, profilerResults.getLookForward()+0.5) plt.xlabel('Time') plt.ylabel('Cumulative returns')
def measurement_frozen(nr_samples, LXmax, amplitude): nr_particles = [] time_ratio = [] time_ratio_lbfgs = [] for LX in xrange(3, LXmax + 1): print "---LX: ", LX t_fire = 0 t_lbfgs = 0 nrp = 0 for tmp in xrange(nr_samples): print "frozen sample", tmp conf = Config2DFrozenBoundary(LX, amplitude) print "construction" conf.optimize() print "optimization" t_fire = t_fire + conf.t_ratio / nr_samples t_lbfgs = t_lbfgs + conf.t_ratio_lbfgs / nr_samples nrp = conf.N nr_particles.append(nrp) time_ratio.append(t_fire) time_ratio_lbfgs.append(t_lbfgs) print "---done:", LX, "max: ", LXmax popt, pcov = curve_fit(ll, nr_particles, time_ratio) plt.plot(nr_particles, np.array(time_ratio)-1, "s", label = r"M-FIRE") plt.plot(nr_particles, np.array(time_ratio_lbfgs)-1, "o", label = r"LBFGS") xr = np.linspace(nr_particles[0], nr_particles[-1]) plt.plot(xr, np.array([ll(xi, popt[0], popt[1]) for xi in xr])-1, "k", label=r"Exponent: " + to_string(popt[1], 2)) plt.axhline(0, color='black') ax = plt.axes() ax.grid(True) plt.legend(loc = 2) plt.xlabel(r"Number of particles") plt.ylabel(r"Time no cell lists / time cell lists - 1") save_pdf(plt, "hs_wca_cell_lists_frozen.pdf") print "done: frozen measurement"
def display_convw2(w, s, r, c, fig, title='conv_filters'): """w: num_filters X sizeX**2 * num_colors.""" num_f, num_d = w.shape assert s**2 * 3 == num_d pvh = np.zeros((s*r, s*c, 3)) for i in range(r): for j in range(c): pvh[i*s:(i+1)*s, j*s:(j+1)*s, :] = w[i*c + j, :].reshape(3, s, s).T mx = pvh.max() mn = pvh.min() pvh = 255*(pvh - mn) / (mx-mn) pvh = pvh.astype('uint8') plt.figure(fig) plt.suptitle(title) plt.imshow(pvh, interpolation="nearest") scale = 1 xmax = s * c ymax = s * r color = 'k' for x in range(0, c): plt.axvline(x=x*s/scale, ymin=0, ymax=ymax/scale, color=color) for y in range(0, r): plt.axhline(y=y*s/scale, xmin=0, xmax=xmax/scale, color=color) plt.draw() return pvh
def main( args ): hash = get_genes_with_features(args['file']) for key, featurearray in hash.iteritems(): cluster, branch = key.split() length = int(featurearray[0][0]) import matplotlib.pyplot as P x = [e+1 for e in range(length+1)] y1 = [0] * (length+1) y2 = [0] * (length+1) for feature in featurearray: length, pos, aa, prob = feature[0:4] if prob > 0.95: y1[pos] = prob else: y2[pos] = prob P.bar(x, y1, color='#000000', edgecolor='#000000') P.bar(x, y2, color='#bbbbbb', edgecolor='#bbbbbb') P.ylim(ymin=0, ymax=1) P.xlim(xmin=0, xmax=length) P.xlabel("position in the ungapped alignment [aa]") P.ylabel(r'$P (\omega > 1)$') P.title(cluster + " (branch " + branch + ")") P.axhline(y=.95, xmin=0, xmax=length, linestyle=":", color="k") P.savefig(cluster + "." + branch + ".png", format="png") P.close()
def draw_plot(self, x_table, y_table, string_number, sound_name, string_target_frequency): fig = plt.figure() ax = fig.add_subplot(111) ax.set_xlabel('t [s]') ax.set_ylabel('f [Hz]') ax.set_title('Struna '+str((string_number+1))+' do dzwieku '+sound_name) plt.plot(x_table, y_table) ax.set_xlim(0) plt.axhline(y=string_target_frequency) plt.axhspan(string_target_frequency-0.7, string_target_frequency+0.7, xmin=0, facecolor='g', alpha=0.5) data = ('f0 = '+str(round(y_table[0],2))+' Hz\n'+ 'fk = '+str(round(y_table[len(y_table)-1],2))+' Hz\n'+ 'fz = '+str(round(string_target_frequency,2))+' Hz\n'+ 't = '+str(round(x_table[len(x_table)-1]-x_table[0],2))+' s') print data ax.text(0.8, 0.01, data, verticalalignment='bottom', horizontalalignment='left', transform=ax.transAxes, fontsize=11) plot_name = 'wykres_'+str(string_number)+'.png' plt.savefig(os.path.join('/home/pi/Dyplom/', plot_name)) plt.clf()
def plotresult(i=0, j=101, step=1): import matplotlib.pyplot as mpl from numpy import arange res = getevaluation(i, j, step) x = [k / 100.0 for k in range(i, j, step)] nbcurve = len(res[0]) nres = [[] for i in xrange(nbcurve)] mres = [] maxofmin = -1, 0.01 for kindex, kres in enumerate(res): minv = min(kres.values()) if minv > maxofmin[1]: maxofmin = kindex, minv lres = [(i, j) for i, j in kres.items()] lres.sort(lambda x, y: cmp(x[0], y[0])) for i, v in enumerate(lres): nres[i].append(v[1]) mres.append(sum([j for i, j in lres]) / nbcurve) print maxofmin for y in nres: mpl.plot(x, y) mpl.plot(x, mres, linewidth=2) mpl.ylim(0.5, 1) mpl.xlim(0, 1) mpl.axhline(0.8) mpl.axvline(0.77) mpl.xticks(arange(0, 1.1, 0.1)) mpl.yticks(arange(0.5, 1.04, 0.05)) mpl.show()
def analy_deal_type(self, stock_name, start_date, end_date, plt, color): delta_days = (end_date - start_date).days mysql = MySQL('trans') mysql.connect() for day in xrange(0, delta_days, 1): today = start_date + timedelta(days=day) next_day = start_date + timedelta(days=day+1) state_where_buy = "DEAL_DATE > '%s' and DEAL_DATE < '%s' and DEAL_TYPE = 1" % (today, next_day) buy_data = mysql.query_where(stock_name, state_where_buy) cnt_buy_data = len(buy_data) state_where_sell = "DEAL_DATE > '%s' and DEAL_DATE < '%s' and DEAL_TYPE = -1" % (today, next_day) sell_data = mysql.query_where(stock_name, state_where_sell) cnt_sell_data = len(sell_data) scale = 5 if cnt_sell_data is not 0: if cnt_buy_data is not 0: buy_total = self.sum_long(buy_data) else: buy_total = 0 sell_total = self.sum_long(sell_data) perc = (float(buy_total) / sell_total) * scale # perc = (float(cnt_buy_data) / cnt_sell_data) * scale else: perc = 0 plt.scatter(today, perc, c=color) plt.axhline(y=scale, xmin=0, xmax=1) try: plt.savefig(".\\results\\"+self.stock_name+".png", dpi=200) except Exception: print "save png file error!" mysql.close_connect()
def plot_profile(frame, index): T = sc[:,'w_xy'].keys()[frame] data = sc[T,'w_xy'][index,:] x = sc[0.0,'z_z'][-sc[T,'w_xy'].shape[0]:] plt.imshow(sc[T,'w_xy']) plt.axhline(index, color='k') plt.figure() plt.plot(x,data[:]) plt.xlabel("y") plt.ylabel("w") plt.figure() scalex = np.sqrt(np.abs(data[1]/x[1]) * p['viscosity']) / p['viscosity'] scaley = 1./np.sqrt(np.abs(data[1]/x[1]) * p['viscosity']) plt.plot(scalex*x[:50],scaley*data[:50], 'x-') plt.grid(True) plt.xlabel("y+") plt.ylabel("w+") plt.figure() scalex = np.sqrt(np.abs(data[-1]/(x[-1]-1)) * p['viscosity']) / p['viscosity'] scaley = 1./np.sqrt(np.abs(data[-1]/(x[-1]-1)) * p['viscosity']) plt.plot(scalex*(x[-50:]-1),scaley*data[-50:], 'x-') plt.grid(True) plt.xlabel("y+") plt.ylabel("w+")
def plot(sampling_points, sine_input, last_training_run, last_training_errors): """Plot the results.""" # plot results print('{:^18} | {:^18} | {:^18} | {:^18}'.format('input', 'expected', 'actual', 'error')) print('{:-^18} | {:-^18} | {:-^18} | {:-^18}'.format('', '', '', '')) for index in range(NUM_SAMPLES): next_index = (index + 1) % NUM_SAMPLES print( '{:18} | {:18} | {:< 18} | {:< 18}'.format( sine_input[index], sine_input[next_index], last_training_run[next_index], last_training_errors[next_index] ) ) plt.plot(sampling_points, sine_input, 'b', marker='.', label='input') plt.plot(sampling_points, last_training_run, 'r', label='learnt') plt.plot(sampling_points, last_training_errors, '0.5', label='error') plt.plot([2*np.pi, 4*np.pi, 6*np.pi], [0, 0, 0], 'ok') plt.axis([0, GENERATING_FACTOR*2*np.pi, -1.5, 1.5]) plt.axhline(color='k') plt.legend() plt.show()
def plot_1d(xdata, ydata, color, x_axis, y_axis, system, analysis, average = False, t0 = 0, **kwargs): """ Creates a 1D scatter/line plot: Usage: plot_1d(xdata, ydata, color, x_axis, y_axis, system, analysis, average = [False|True], t0 = 0) Arguments: xdata, ydata: self-explanatory color: color to be used to plot data x_axis, y_axis: strings to be used for the axis label system: descriptor for the system that produced the data analysis: descriptor for the analysis that produced the data average: [False|True]; Default is False; if set to True, the function will calc the average, standard dev, and standard dev of mean of the y-data # THERE IS A BUG IF average=True; must read in yunits for this function to work at the moment. t0: index to begin averaging from; Default is 0 kwargs: xunits, yunits: string with correct math text describing the units for the x/y data x_lim, y_lim: list w/ two elements, setting the limits of the x/y ranges of plot plt_title: string to be added as the plot title draw_line: int value that determines the line style to be drawn; giving myself space to add more line styles if I decide I need them """ # INITIATING THE PLOT... plt.plot(xdata, ydata, '%s' %(color)) # READING IN KWARG DICTIONARY INTO SPECIFIC VARIABLES for name, value in kwargs.items(): if name == 'xunits': x_units = value x_axis = '%s (%s)' %(x_axis, value) elif name == 'yunits': y_units = value y_axis = '%s (%s)' %(y_axis, value) elif name == 'x_lim': plt.xlim(value) elif name == 'y_lim': plt.ylim(value) elif name == 'plt_title': plt.title(r'%s' %(value), size='14') elif name == 'draw_line': draw_line = value if draw_line == 1: plt.plot([0,max(ydata)],[0,max(ydata)],'r-',linewidth=2) else: print 'draw_line = %s has not been defined in plotting_functions script' %(line_value) plt.grid(b=True, which='major', axis='both', color='#808080', linestyle='--') plt.xlabel(r'%s' %(x_axis), size=12) plt.ylabel(r'%s' %(y_axis), size=12) # CALCULATING THE AVERAGE/SD/SDOM OF THE Y-DATA if average != False: avg = np.sum(ydata[t0:])/len(ydata[t0:]) SD = stdev(ydata[t0:]) SDOM = SD/sqrt(len(ydata[t0:])) plt.axhline(avg, xmin=0.0, xmax=1.0, c='r') plt.figtext(0.680, 0.780, '%s\n%6.4f $\\pm$ %6.4f %s \nSD = %4.3f %s' %(analysis, avg, SDOM, y_units, SD, y_units), bbox=dict(boxstyle='square', ec='r', fc='w'), fontsize=12) plt.savefig('%s.%s.plot1d.png' %(system,analysis),dpi=300) plt.close()
def graphAny(df, y): yName = np.where(df['Parameter Code'] == y)[0] #finds where the param code is in MasterFile06 yNamedf = df[(yName[0]):(yName[-1]+1)] #creates separate DF from MasterFile06 w/ only param print(yNamedf['Date'][:1]) #prints first date in yNamedf print(yNamedf['Date'][-1:]) #prints last date in yNamedf print(yNamedf['Parameter Description'][:1]) #prints parameter description yNamedf.plot(x='Date', y='Values', kind='line', color='red') #plotting the new parameter DF plt.xlabel('Date') yax = input('Enter y-axis label: ') #input whatever you want for y-axis label plt.ylabel(yax) #whatever the input is is what y-axis label is #plt.ylabel(yNamedf['Parameter Description'][:1]) gTitle = input('Enter title: ') #similar to y-axis label... plt.title(gTitle) #whatever the input is is what title is referenceLine = input('Does this graph need a reference line? ') while referenceLine != 'no': #while statement loops if user doesn't enter 'no' yRefL = float(input('Enter y value for reference line: ')) #input whatever number you want ref line to be labelRefl = input('Enter reference line description: ') #input description of line color lineColor = input('Enter color for reference line: ') #input color of reference line plt.axhline(y=yRefL, xmin=0, xmax=1, linewidth=1.5, color=lineColor, label=labelRefl) plt.legend() done = input('Done? ') #are you done w/ reference lines? if done == 'yes': #if yes show plot and stop looping through statement plt.show() break plt.show()
def test_blc2(oversample=2, verbose=True, wavelength=2e-6, angle=0, kind='circular', sigma=1.0, loc = 0.3998): import scipy x = np.linspace(-5, 5, 401) sigmar = sigma*x if kind == 'circular': trans = (1- (2*scipy.special.jn(1,sigmar)/sigmar)**2)**2 else: trans = (1- (np.sin(sigmar)/sigmar)**2)**2 plt.clf() plt.plot(x, trans) plt.axhline(0.5, ls='--', color='k') plt.axvline(loc, ls='--', color='k') #plt.gca().set_xbound(loc*0.98, loc*1.02) wg = np.where(sigmar > 0.01) intfn = scipy.interpolate.interp1d(x[wg], trans[wg]) print "Value at %.4f :\t%.4f" % (loc, intfn(loc)) # figure out the FWHM # cut out the portion of the curve from the origin to the first positive maximum wp = np.where(x > 0) xp = x[wp] transp = trans[wp] wm = np.argmax(transp) wg = np.where(( x>0 )& ( x<xp[wm])) interp = scipy.interpolate.interp1d(trans[wg], x[wg]) print "For sigma = %.4f, HWHM occurs at %.4f" % (sigma, interp(0.5))
def plot(data): plt.scatter(*zip(*data)) #plt.ylim(-5,5) plt.axhline(0.1) plt.xlabel("embedding demension m") plt.ylabel("the ratio false neighbors") plt.show()
def getMonthlyReturns(self, lotSizeInUSD, commissionPerPip): prevMonth = 0 profit = 0 deals = 0 monthlyReturns = [] for p in self.getClosedPositions(): if prevMonth == 0: prevMonth = p.openTime.month profit += (p.closePrice - p.order.price) * p.order.orderType * lotSizeInUSD - commissionPerPip deals += 1 if p.openTime.month != prevMonth: monthlyReturns.append([profit, deals]) prevMonth = p.openTime.month profit = 0 deals = 0 result = np.array(monthlyReturns) import matplotlib.pyplot as plt plt.figure(1) plt.subplot(211) plt.axhline(y=0) plt.plot(result[:,0]) plt.subplot(212) plt.axhline(y=0) plt.plot(result[:,1]) plt.show() return result
def plotRegr(exp): """ Plots data for one experiment. Arguments: exp --- {"004A", "004B"} """ src = 'selected_dm_%s_WITH_drift_corr_onlyControl_False.csv' % exp dm = CsvReader(src).dataMatrix() R = RBridge() fig = plt.figure(figsize = (8,3)) plt.subplots_adjust(left=.2, bottom=.15) # Plot hline: plt.axhline(0, color = "black", linestyle = "--") legend = True for sacc in [1, 2]: colList = [green[1], orange[1], blue[1]] for contrast in dm.unique("contrast_side"): print "contrast side = ", contrast print "sacc = ", sacc _dm = dm.select("contrast_side == '%s'" % contrast) lmeRegr(R, _dm, sacc=sacc, corr=False, \ color = colList.pop(),stats=False, legend = contrast.strip("_")) if legend: plt.legend(frameon = False, loc='best') legend = False plt.savefig("Timecourse_contrast_per_cond%s.png" % exp)
def hpd_beta(y, n, h=.1, a=1, b=1, plot=False, **plot_kwds): apost = y + a bpost = n - y + b if apost > 1 and bpost > 1: mode = (apost - 1)/(apost + bpost - 2) else: raise Exception("mode at 0 or 1: HPD not implemented yet") post = stats.beta(apost, bpost) dmode = post.pdf(mode) lt = opt.bisect(lambda x: post.pdf(x) / dmode - h, 0, mode) ut = opt.bisect(lambda x: post.pdf(x) / dmode - h, mode, 1) coverage = post.cdf(ut) - post.cdf(lt) if plot: plt.figure() plotf(post.pdf) plt.axhline(h*dmode) plt.plot([ut, ut], [0, post.pdf(ut)]) plt.plot([lt, lt], [0, post.pdf(lt)]) plt.title(r'$p(%s < \theta < %s | y)$' % tuple(np.around([lt, ut], 2))) return lt, ut, coverage, h
def test_airy_1d(display=False): """ Compare analytic airy function results to the expected locations for the first three dark rings and the FWHM of the PSF.""" lam = 1.0e-6 D = 1.0 r, airyprofile = airy_1d(wavelength=lam, diameter=D, length=20480, pixelscale=0.0001) # convert to units of lambda/D r_norm = r*_ARCSECtoRAD / (lam/D) if display: plt.semilogy(r_norm,airyprofile) plt.axvline(1.028/2, color='k', ls=':') plt.axhline(0.5, color='k', ls=':') plt.ylabel('Intensity relative to peak') plt.xlabel('Separation in $\lambda/D$') for rad in airy_zeros: plt.axvline(rad, color='red', ls='--') airyfn = scipy.interpolate.interp1d(r_norm, airyprofile) # test FWHM occurs at 1.028 lam/D, i.e. HWHM is at 0.514 assert (airyfn(0.5144938) - 0.5) < 1e-5 # test first minima occur near 1.22 lam/D, 2.23, 3.24 lam/D # TODO investigate/improve numerical precision here? for rad in airy_zeros: #print(rad, airyfn(rad), airyfn(rad+0.005)) assert airyfn(rad) < airyfn(rad+0.0003) assert airyfn(rad) < airyfn(rad-0.0003)
def make_plot(self): cols = ['x', 'y'] D = self.read_data(cols) M = D[cols].values print(M) # Sort rows on first column's value M = M[M[:,0].argsort()] x = M[:,0] y = M[:,1] # widths of bars window = 101 width = (len(M) / float(self.bins)) / float(window) plt.title(self.title) plt.xlabel('score') plt.ylabel('frequency') plt.grid(True) plt.xlim(-1.0,1.0) plt.axhline(0, color='black') plt.hist(x, self.bins, weights=y, width=width, linewidth=0.5,alpha=0.5) # Draw a smooth curve # Split the region in two sections (negative, positive) to ensure the # very noisy point of 0.0 is not taken into account in smoothening. zero = np.where(x == 0)[0][0] yNeg = self.savitzky_golay(y[0:zero], window, 3) yPos = self.savitzky_golay(y[zero:-1], window, 3) plt.plot(x[0:zero], yNeg, 'r') plt.plot(x[zero:-1], yPos, 'g') self.end_plot(os.path.splitext(self.data_file)[0])
def barChart(title="Nothing"): dataY = [-8.51,0.27,0.52] #Import data from csv dataX = ['Energy For Production','GDP','Household Consumption'] n_groups = len(dataX) index = np.arange(n_groups) bar_width = 0.4 width = 0.8 fig,a = plt.subplots() plt.axhline(y = 0,xmin=0,xmax=4,linewidth = 2) #for horizontal line plotting # p1=a.bar(dataY ,dataX) p1 = plt.bar(index,dataY,bar_width,alpha=0.4,color='b') plt.title(title) plt.xticks(index + bar_width/2, dataX) print(a.get_position()) def autolabel(rects): for rect in rects: height = rect.get_height() #-ve rect height?? a.text(rect.get_x() + rect.get_width()/2., 1.05*height, '%f' % float(height), ha='center', va='bottom') autolabel(p1) plt.legend() plt.tight_layout() plt.show()
def SetAxes(legend=False): f_b = 0.164 f_star = 0.01 err_b = 0.006 err_star = 0.004 f_gas = f_b - f_star err_gas = np.sqrt(err_b**2 + err_star**2) plt.axhline(y=f_gas, ls='--', c='k', label='', zorder=-1) x = np.linspace(.0,2.,1000) plt.fill_between(x, y1=f_gas - err_gas, y2=f_gas + err_gas, color='k', alpha=0.3, zorder=-1) plt.text(.6, f_gas+0.006, r'f$_{gas}$', verticalalignment='bottom', size='large') plt.xlabel(r'r/r$_{vir}$', size='x-large') plt.ylabel(r'f$_{gas}$ ($<$ r)', size='x-large') plt.xscale('log') plt.xticks([1./1.9, 1.33/1.9, 1, 1.5, 2.],[r'r$_{500}$', r'r$_{200}$', 1, 1.5, 2], size='large') #plt.yticks([.1, .2], ['0.10', '0.20']) plt.tick_params(length=10, which='major') plt.tick_params(length=5, which='minor') plt.xlim([0.4,1.5]) plt.minorticks_on() if legend: plt.legend(loc=0, prop={'size':'small'}, markerscale=0.7, numpoints=1, ncol=2)
def plot_scatter(points, rects, level_id, fig_area=FIG_AREA, grid_area=GRID_AREA, with_axis=False, with_img=True, img_alpha=1.0): rect = rects[level_id] top_lat, top_lng, bot_lat, bot_lng = get_rect_bounds(rect) plevel = get_points_level(points, rects, level_id) ax = plevel.plot('lng', 'lat', 'scatter') plt.xlim(left=top_lng, right=bot_lng) plt.ylim(top=top_lat, bottom=bot_lat) if with_img: img = plt.imread('/data/images/level%s.png' % level_id) plt.imshow(img, zorder=0, alpha=img_alpha, extent=[top_lng, bot_lng, bot_lat, top_lat]) width, height = get_rect_width_height(rect) fig_width, fig_height = get_fig_width_height(width, height, fig_area) plt.gcf().set_size_inches(fig_width, fig_height) if grid_area: grid_horiz, grid_vertic = get_grids(rects, level_id, grid_area, fig_area) for lat in grid_horiz: plt.axhline(lat, color=COLOR_GRID, lw=GRID_LW) for lng in grid_vertic: plt.axvline(lng, color=COLOR_GRID, lw=GRID_LW) if not with_axis: ax.set_axis_off() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) return ax
def wfplot2(wflist,bounds): # improved ploting, it can plot more than one wavefunction, also it can change the plot range, # if the bounds parameter is -1 it plots the wavefunctions in their full range. It also shows the axis x line. # usage: wfplot2([ynL],[0,10]) (NOTE THE BRAKETS) to plot ynL in the interval (0,10) or wfplot2([ynL],-1) to plot ynL in its full range, or # wfplot2([ynL,ynL2],-1) to plot two functions in te full range. if bounds == -1: plt.axhline(0,color='black') for element in wflist: wfplot(element) return plt.show() elif len(bounds) == 2: plt.axhline(0,color='black') for element in wflist: xmin = min(simm(bounds[0],element[:,0])) xmax = max(simm(bounds[1],element[:,0])) xaux = element[:,0][xmin:xmax] yaux = element[:,1][xmin:xmax] plt.plot(np.array(xaux),np.array(yaux)) return plt.show() else: print 'invalid bounds'
def analyze_edge_velocities(df): from numpy import arange import article2_time_averaged_routines as tar import pandas as pd import matplotlib.pyplot as plt reload(tar) # This function was once used to evaluate the selection of # U_e based on different parameters conditions = [ 'vorticity_integration_rate_of_change', #'u_rms_rate_of_change', #'v_rms_rate_of_change', #'u_rate_of_change', ] thresholds = arange(1,100,0.1)[::-1] U_e_df = pd.DataFrame() for c in conditions: U_e = [] for t in thresholds: U_e.append(tar.get_edge_velocity(df, condition = c, threshold = t)) U_e_df[c] = U_e U_e_df.plot() plt.axhline( df.u.max() ) plt.show() return U_e_df
def plot_adsorbed_circles(adsorbed_x, adsorbed_y, radius, width, reference_indices=[]): import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Circle # Plot each run fig = plt.figure() ax = fig.add_subplot(111) for p in range(len(adsorbed_x)): if len(np.where(reference_indices == p)[0]) > 0: ax.add_patch(Circle((adsorbed_x[p], adsorbed_y[p]), radius, edgecolor='black', facecolor='black')) else: ax.add_patch(Circle((adsorbed_x[p], adsorbed_y[p]), radius, edgecolor='black', facecolor='white')) ax.set_aspect(1.0) plt.axhline(y=0, color='k') plt.axhline(y=width, color='k') plt.axvline(x=0, color='k') plt.axvline(x=width, color='k') plt.axis([-0.1*width, width*1.1, -0.1*width, width*1.1]) plt.xlabel("non-dimensional x") plt.ylabel("non-dimensional y") return ax
def adjust_axes(ax, title, header_ticks, header_list, axis_num, cols, axesfontsize): ''' A small helper function that adjusts the axes on the plots ''' frame = plt.gca() axes_font = {'fontsize':axesfontsize} title_font = {'fontsize':18} plt.text(0.1,3, title, **title_font) plt.axhline() if (axis_num <= cols) and (axis_num % 2 == 0): frame.xaxis.set_ticks_position('top') frame.xaxis.set_label_position('top') frame.xaxis.tick_top() ax.set_xticks(header_ticks) ax.set_xticklabels(header_list, **axes_font) else: frame.axes.get_xaxis().set_visible(False) if (((axis_num - 1) / cols) % 2 == 0) and axis_num % cols == 1: ax.set_yticks((-4,-2,0,2,4)) ax.set_yticklabels([-4,-2,0,2,4], **axes_font) else: frame.axes.get_yaxis().set_visible(False)
def _double_hit_check(x): # first PSD W, fr = PSD(x, dt=dt) # second PSD: look for oscillations in PSD W2, fr2 = PSD(W, dt=fr[1]) upto = int(0.01 * len(x)) max_impact = np.max(W2[:upto]) max_after_impact = np.max(W2[upto:]) if plot_figure: import matplotlib.pyplot as plt plt.subplot(121) l = int(0.002*len(x)) plt.plot(1000*dt*np.arange(l), x[:l]) plt.xlabel('t [ms]') plt.ylabel('F [N]') plt.subplot(122) plt.semilogy((W2/np.max(W2))[:5*upto]) plt.axhline(limit, color='r') plt.axvline(upto, color='g') plt.xlabel('Double freq') plt.ylabel('') plt.show() if max_after_impact / max_impact > limit: return True else: return False
def _sp(data): """ Generate plots of convergence criteria, and energy vs. optimization cycles :job: ccdata object, or file :returns: TODO """ # TODO scfenergies, scfvalues, scftargets vs. scf cycles print("\n\n") #print("Optimization Converged: ", data.optdone) criteria = [0, 0, 0] criteria[0] = [x[0] for x in data.scfvalues] criteria[1] = [x[1] for x in data.scfvalues] criteria[2] = [x[2] for x in data.scfvalues] idx = np.arange(len(criteria[0])) # Plot Geometry Optimization Criteria for Convergence over opt cycles plt.plot(idx, criteria[0], label='Criteria 1') plt.plot(idx, criteria[1], label='Criteria 2') plt.plot(idx, criteria[2], label='Criteria 3') # Plot target criteria for convergence plt.axhline(y=data.scftargets[0]) plt.yscale('log') plt.title("SCF Convergence Analysis") plt.xlabel("SCF Cycle") plt.legend() print(idx, criteria, data.scftargets) plt.show()
def display_w(w, s, r, c, fig, vmax=None, vmin=None, dataset='mnist', title='weights'): if dataset == 'norb': numvis = 4096 else: numvis = w.shape[0] numhid = w.shape[1] sc = s sr = numvis/s if isinstance(w, np.ndarray): vh = w.T[:,:sr*sc].reshape(sr*numhid, sc) else: vh = w.asarray().T[:,:sr*sc].reshape(sr*numhid, sc) pvh = np.zeros((sr*r, sc*c)) for i in range(r): for j in range(c): pvh[i*sr:(i+1)*sr , j*sc:(j+1)*sc] = vh[ (i*c+j)*sr : (i*c+j+1)*sr ,:] plt.figure(fig) plt.clf() plt.title(title) plt.imshow(pvh, cmap = plt.cm.gray, interpolation = 'nearest', vmax=vmax, vmin=vmin) scale = 1 xmax = sc*c ymax = sr*r color = 'k' if r > 1: for x in range(0,c): plt.axvline(x=x*sc/scale, ymin=0,ymax=ymax/scale, color = color) if c > 1: for y in range(0,r): plt.axhline(y=y*sr/scale, xmin=0,xmax=xmax/scale, color = color) plt.draw() return pvh
def TCP_plot(no_ind_plots, label): #no_ind_plots = 50 ## individual plots cannot be more than total patients if(no_ind_plots>n): no_ind_plots=n ## want to select the individual plots randomly from those calcualted... ind_plots = np.random.choice(len(TCPs),no_ind_plots, replace=False) ## individuals (specified number of plots chosen) for i in ind_plots: plt.plot(nom_doses,TCPs[i], color = 'grey', alpha = 0.5) ## population plt.plot(nom_doses,TCP_pop, color='black', linewidth='2', alpha=0.5) plt.plot(nom_doses,TCP_pop, marker = 'o', ls='none', label=label) ## plot formatting plt.xlim(0,max(nom_doses)) plt.ylim(0,1.0) plt.xlabel('Dose (Gy)') plt.ylabel('TCP') plt.title('TCPs') #plt.legend(loc = 'best', fontsize = 'medium', framealpha = 1) plt.axvline(d_interest, color = 'black', ls='--',) plt.axhline(TCP_pop[frac_interest-1], color='black', ls='--') ## add labels with TCP at dose of interest text_string = ('Pop. TCP = ' + str(round(TCP_cure_at_d_interest,2)) + ' % at ' + str(d_interest) + 'Gy') plt.text(5,0.4,text_string, backgroundcolor='white') plt.legend(loc = 'lower left',numpoints=1) plt.show()
def plot(self): r = self.data _, newra, newra_sigma = self.fit_gaussian(r['ra'], mean=self.ra) _, newdec, newdec_sigma = self.fit_gaussian(r['dec'], mean=self.dec) _, newdist, newdist_sigma = self.fit_gaussian(r['dist'], mean=self.dist) _, newpmra, newpmra_sigma = self.fit_gaussian(r['pmra'], mean=self.pmra) _, newpmdec, newpmdec_sigma = self.fit_gaussian(r['pmdec'], mean=self.pmdec) newdiam = np.max([newra_sigma, newdec_sigma]) * 3.0 * 60.0 newpmradius = np.max([newpmra_sigma, newpmdec_sigma]) * 3.0 * 2.0 plt.style.use('/home/jwe/workspace/SOCS/src/a4paper.mplstyle') # newpmra = np.median(r['pmra']) # newpmdec = np.median(r['pmdec']) plt.subplot(221) plt.title(self.clustername) plt.plot(r['brmag'], r['gmag'], 'k.', alpha=0.25, mec='None') plt.xlabel('b - r') plt.ylabel('G mag') plt.ylim(plt.ylim()[::-1]) plt.minorticks_on() plt.grid() ax2 = plt.subplot(222) ax2.set_aspect(1.) plt.plot(r['ra'], r['dec'], 'k.', alpha=0.5, mec='None') plt.xlabel('RA %.2f +- %.2f' % (newra, newra_sigma)) plt.ylabel('Dec %.2f +- %.2f' % (newdec, newdec_sigma)) plt.axvline(self.cluster['ra'], color='r', linestyle='--') plt.axhline(self.cluster['dec'], color='r', linestyle='--') plt.axvline(newra, color='g', linestyle='--') plt.axhline(newdec, color='g', linestyle='--') plt.grid() plt.xlim(newra + 2, newra - 2) plt.ylim(newdec - 2, newdec + 2) plt.xticks(np.round(np.arange(newra - 2, newra + 3))) plt.yticks(np.round(np.arange(newdec - 2, newdec + 3))) ax3 = plt.subplot(223) ax3.set_aspect(1.) plt.plot(r['pmra'], r['pmdec'], 'k.', alpha=0.5, mec='None') plt.axvline(self.cluster['pmra'], color='r', linestyle='--') plt.axhline(self.cluster['pmdec'], color='r', linestyle='--') plt.axvline(newpmra, color='g', linestyle='--') plt.axhline(newpmdec, color='g', linestyle='--') plt.xlim(self.pmra - self.pmradius, self.pmra + self.pmradius) plt.ylim(self.pmdec - self.pmradius, self.pmdec + self.pmradius) plt.xlabel('pm RA %.2f +- %.2f' % (newpmra, newpmra_sigma)) plt.ylabel('pm Dec %.2f +- %.2f' % (newpmdec, newpmdec_sigma)) plt.grid() plt.subplot(224) plt.title('total: %d' % len(r['source_id'])) plt.hist(r['dist'], bins=20) plt.xlim(min(r['dist']), max(r['dist'])) plt.axvline(self.cluster['d'], color='r', linestyle='-.') plt.axvline(newdist, color='g', linestyle='-.') plt.xlabel('distance %.1f +- %.1f' % (newdist, newdist_sigma)) plt.savefig('/work2/jwe/SOCS/plots/Gaia DR2 new %s.pdf' % self.clustername) plt.close() print("GaiaCluster('%s', ra=%.2f, dec=%.2f, pmra=%.2f, pmdec=%.2f, diam=%.1f, pmradius=%.1f, dist=%.1f)" % \ (self.clustername, newra, newdec, newpmra, newpmdec, newdiam, newpmradius, newdist))
import pandas as pd import matplotlib.pyplot as plt from data import games attendance = games.loc[(games['type'] == 'info') & (games['multi2'] == 'attendance'), ['year', 'multi3']] attendance.columns = ['year', 'attendance'] attendance.loc[:, 'attendance'] = pd.to_numeric(attendance.loc[:, 'attendance']) attendance.plot(x='year', y='attendance', figsize=(15, 7), kind='bar') plt.xlabel('Year') plt.ylabel('Attendance') plt.axhline(y=attendance['attendance'].mean(), label='Mean', linestyle='--', color='green') plt.show()
1) os.chdir(r'/Users/tehyuqi/Dropbox') latest[['ticker', 'PE_ratio', 'PS_ratio', 'trend']].to_csv('deleteme.csv', index=False) print('plotting data..') print('ps ratio chart') plt.figure(figsize=(19, 8.5)) plt.clf() ax1 = plt.subplot(1, 2, 1) for i in latest['industry'].unique(): subgroup = latest[(latest['industry'] == i)] plt.scatter(subgroup['trend'], subgroup['PS_ratio'], label=i, alpha=0.65) plt.legend() plt.axhline(1, color='black', alpha=0.2) plt.axvline(0, color='black', alpha=0.2) ax1.xaxis.set_major_formatter(mtick.PercentFormatter()) plt.xlabel('Trend ~ 200MA of 5 years (range -1:1)', fontweight='bold') plt.ylabel('P/S Ratio', fontweight='bold') for x, y in zip(latest['trend'], latest['PS_ratio']): company_name = list(latest[(latest['trend'] == x) & (latest['PS_ratio'] == y)]['name'])[-1] value = int( np.exp(latest[latest.name == company_name]['PS_ratio'].values[0]) - 1) if value >= 1: value = int( np.exp(latest[latest.name == company_name]['PS_ratio'].values[0]) - 1)
def compare_fwhm(d_out, efilter1, efilter2, efilter3, verbose=False): print(f'Comparing FWHM using {efilter1} and {efilter2} filters') df_1 = pd.read_hdf(f'{d_out}/{efilter1}_results.h5', key='results') df_2 = pd.read_hdf(f'{d_out}/{efilter2}_results.h5', key='results') print(df_1) print(df_2) df_1[['g', 'geds']] = df_1['ged'].str.split('0', 1, expand=True) geds = df_1['geds'] fwhm_1 = df_1['fwhm'].astype(float) fwhm_1_err = df_1['fwhmerr'].astype(float) fwhm_2 = df_2['fwhm'].astype(float) fwhm_2_err = df_2['fwhmerr'].astype(float) fwhm_diff = 100 * (fwhm_1 - fwhm_2) / fwhm_1 fwhm_diff_err = 100 * np.sqrt( np.square(fwhm_1_err) + np.square(fwhm_2_err)) / fwhm_1 plt.figure(1) plt.errorbar(geds, fwhm_1, fwhm_1_err, fmt='o', c='red', label=f'{efilter1} filter') plt.errorbar(geds, fwhm_2, fwhm_2_err, fmt='o', c='blue', label=f'{efilter2} filter') if efilter3 is not 0: df_3 = pd.read_hdf(f'{d_out}/{efilter3}_results.h5', key='results') print(df_3) fwhm_3 = df_3['fwhm'].astype(float) fwhm_3_err = df_3['fwhmerr'].astype(float) fwhm_2_diff = 100 * (fwhm_1 - fwhm_3) / fwhm_1 fwhm_2_diff_err = 100 * np.sqrt( np.square(fwhm_1_err) + np.square(fwhm_3_err)) / fwhm_1 plt.errorbar(geds, fwhm_3, fwhm_3_err, fmt='o', c='green', label=f'{efilter3} filter') plt.xlabel("detector number", ha='right', x=1) plt.ylabel("FWHM (keV)", ha='right', y=1) plt.legend() #plt.ylim((2,10)) plt.savefig(f"{d_out}/FWHM_compare_{efilter1}-{efilter2}-{efilter3}.pdf") if verbose: plt.show(block=False) plt.figure(2) plt.cla() plt.errorbar(geds, fwhm_diff, fwhm_diff_err, fmt='o', c='green', label=f'FWHM {efilter1} - {efilter2}') plt.axhline(0, c='black', linestyle=":") plt.xlabel("detector number", ha='right', x=1) plt.ylabel("FWHM difference (%)", ha='right', y=1) plt.legend() #plt.ylim((-15,15)) plt.savefig(f"{d_out}/FWHM_diff_{efilter1}-{efilter2}.pdf") if verbose: plt.show()
import numpy as np import matplotlib.pyplot as plt radar_threshold = 7.815 NIS_radar = [line.rstrip('\n') for line in open('NIS_radar_data_file.txt')] float_values_radar = [float(i) for i in NIS_radar] radar_percentage = sum(i > radar_threshold for i in float_values_radar) / sum(float_values_radar) plt.title("Radar NIS") plt.xlabel('Number of measurement') plt.ylabel('NIS vaule') plt.plot(NIS_radar, c='b', label='NIS vaule') plt.axhline(y=radar_threshold, c='b') plt.text( 5, 60, 'Percentage of values above threshold: ' + str(format(radar_percentage, '.2f')) + '%') plt.legend() plt.savefig("NIS_plot.png")
ymin = min(ys) ymax = max(ys) y_range = ymax - ymin y_start = ymin - y_range / 50. y_end = ymax + y_range / 30. #plot data plt.bar(xs, ys, width=0.4, bottom=0) #define axes with offsets plt.axis([x_start, x_end, y_start, y_end], frameon=False) #plot axes (black with line width of 4) plt.axvline(x=x_start, color="k", lw=4) plt.axhline(y=0, color="k", lw=3) for x, p in zip(xs, ps): if p < 0.01: plt.scatter([x], [ymax + 0.5], marker="*", s=6, color="k") #plot ticks plt.xticks(xs, datasets, rotation=90) plt.tick_params(direction="out", top=False, right=False, bottom=False, length=12, width=3, labelsize=9)
def viewWheeler(width=800, height=400, time=None, rangeE=None, shoreID_left=None, shoreID_right=None, height_bar=None, npts=None, color=None, rangeX=None, rangeY=None, linesize=3, title='Wheeler Diagram', xlegend='xaxis', ylegend='yaxis'): """ Plot wheeler diagram colored by deposition environment on a graph. Parameters ---------- variable: width, height Figure width and height. variable: time Stacking time of each extracted layer. variable: rangeE Depositional environments extent. variable: shoreID_left, shoreID_right Shoreline positions through time at the left and right sides. variable: dnlay, npts Layer step to plot the Wheeler diagram, number of extracted outfiles. variable: color Depositional environments color scale. variable: rangeX, rangeY Extent of the cross section plot. variable: linesize Requested size for the line. variable: title Title of the graph. variable: xlegend Legend of the x axis. variable: ylegend Legend of the y axis. """ fig = plt.figure(figsize=(width, height)) plt.rc("font", size=9) patch_handles = [] for i, d in enumerate(rangeE): patch_handles.append( plt.barh(time, d, color=color[i], align='edge', left=d, height=height_bar, edgecolor="none")) for j in range(0, npts): plt.axhline(time[j], color='k', linewidth=0.5) plt.plot(shoreID_left, time, 'ko', markersize=3) plt.plot(shoreID_right, time, 'ko', markersize=3) # plt.xlim(rangeX) plt.ylim(rangeY) plt.xlabel(xlegend) plt.ylabel(ylegend) # plt.title(title) return
def dataWithGradient(filename): # %% config = getConfig(filename) config["channel_width_m"] = 0.00019001261833616293 data = getData(filename) getVelocity(data, config) # take the mean of all values of each cell # data = data.groupby(['cell_id']).mean() correctCenter(data, config) # exit() data = data[(data.solidity > 0.96) & (data.irregularity < 1.06)] # data = data[(data.solidity > 0.98) & (data.irregularity < 1.02)] data.reset_index(drop=True, inplace=True) getStressStrain(data, config) def getVelGrad(r): p0, p1, p2 = config["vel_fit"] r = r p0 = p0 * 1e3 r0 = config["channel_width_m"] * 0.5 * 1e6 # 100e-6 return - (p1 * p0 * (np.abs(r) / r0) ** p1) / r vel = fit_func_velocity(config) import scipy.optimize def curve(x, x0, delta1, a1, delta2, a2, w): x = np.abs(x) delta1 = 2 return x0 * ((a1+a2) - a1 * (x) ** delta1 - (a2 * x) ** delta2) def derivative(x, x0, d1, a1, d2, a2): d1 = 2 return -((d1 * (a1 * np.abs(x)) ** d1 + d2 * (a2 * np.abs(x)) ** d2) * x0) / x # def curve(x, x0, delta, delta2): # return x0 * ((2) - (x)**delta - (x)**delta2) return data x = data.rp * 1e-6 y = data.velocity * 1e-3 x, y = removeNan(x, y) p, popt = scipy.optimize.curve_fit(curve, x, y, [1.00512197e-02, 1.00000000e+00, 9.15342978e+03, 4.85959006e+00, 1.15235584e+04])#[25e-3, 1, 1 / 95e-6, 2, 1 / 95e-6]) getVelGrad = lambda x: derivative(x*1e-6, *p) vel = lambda x: curve(x*1e-6, *p) * 1e3 data["grad"] = getVelGrad(data.rp)#/(2*np.pi) dx = data.short_axis/2 #data["grad"] = (vel(data.rp+dx)-vel(data.rp))*1e-3/(dx*1e-6) if 1: print("vel fit", p) plt.figure(10) plt.subplot(131) plt.plot(data.rp * 1e-6, data.velocity * 1e-3, "o") dx = 1 x = np.arange(-100, 100, dx) * 1e-6 v = vel(x * 1e6) * 1e-3 plt.plot(x, v, "r+") plt.axhline(0, color="k", lw=0.8) plt.subplot(132) grad = np.diff(v) / np.diff(x) # * 1e3 plt.plot(data.rp * 1e-6, data.velocity_gradient, "o") plt.plot(data.rp * 1e-6, getVelGrad(data.rp), "s") plt.plot(x[:-1] + 0.5 * np.diff(x), grad, "-+") plt.show() return data
def plot_decision_boundary_shaded(model, X, Y, feat_crosses=None, axis_lines=False, save=False): """ Plots shaded decision boundary Args: model: neural network layer and activations in lambda function X: Data in shape (num_of_examples x features) feat_crosses: list of tuples showing which features to cross axis_lines: Draw axis lines at x=0 and y=0(bool, default False) save: flag to save plot image """ # first plot the data to see what is the size of the plot plt.scatter(X[:, 0], X[:, 1], s=200, c=np.squeeze(Y)) # s-> size of marker # get the x and y range of the plot x_ticks = plt.xticks()[0] y_ticks = plt.yticks()[0] plt.clf() # clear figure after getting size # Generate a grid of points between min_x_point-0.5 and max_x_point+0.5 with 1000 points in between, # similarly, for y points xs = np.linspace(min(x_ticks) - 0.5, max(x_ticks) + 0.5, 1000) ys = np.linspace(max(y_ticks) + 0.5, min(y_ticks) - 0.5, 1000) xx, yy = np.meshgrid(xs, ys) # Predict the function value for the whole grid prediction_data = np.c_[xx.ravel(), yy.ravel()] # add feat_crosses if provided if feat_crosses: for feature in feat_crosses: prediction_data = np.c_[prediction_data, prediction_data[:, feature[0]] * prediction_data[:, feature[1]]] Z = model(prediction_data) Z = Z.reshape(xx.shape) # Plot the contour and training examples cmap = matplotlib.colors.ListedColormap(["red", "green"], name='from_list', N=None) plt.style.use('seaborn-whitegrid') # 'contourf'-> filled contours (red('#EABDBD'): 0 / green('#C8EDD6'): 1) plt.contourf(xx, yy, Z, cmap=matplotlib.colors.ListedColormap(['#EABDBD', '#C8EDD6'], name='from_list', N=None)) plt.title('Shaded Decision Boundary', size=18) plt.xlabel('$x_1$', size=20) plt.ylabel('$x_2$', size=20) if axis_lines: plt.axhline(0, color='black') plt.axvline(0, color='black') plt.scatter(X[:, 0], X[:, 1], s=200, c=np.squeeze(Y), marker='x', cmap=cmap) # s-> size of marker if save: plt.savefig('decision_boundary_shaded.png', bbox_inches='tight') plt.show()
b_num) with open('fs_2014', 'wb') as f: pickle.dump([ rvs, S_data, T_data, b_array, (rho, sigma, a, samples, b_frac, b_num) ], f) ############################################################################### ############################################################################### ############################################################################### #plot expected values and percentiles n = len(C) plt.plot(b_array / np.sum(Dp), np.mean(S_data, axis=0) / n) plt.plot(b_array / np.sum(Dp), np.percentile(S_data, 90, axis=0) / n) plt.plot(b_array / np.sum(Dp), np.percentile(S_data, 10, axis=0) / n) plt.axhline(y=np.mean(T_data) / n) plt.title('Expected Fraction of Defaults Prevented', fontsize=14) plt.ylabel('Estimated E|S| / |U|', fontsize=14) plt.xlabel('b / |Dp|', fontsize=14) plt.ticklabel_format(axis="x", style="sci", scilimits=(0, 0)) plt.savefig('plot.eps') plt.show() ############################################################################### #plot conditional expected defaults q = 100 S_tvar, T_tvar = interpret.tvar(S_data, T_data, q) n = len(C) plt.plot(b_array / np.sum(Dp), (np.mean(T_data) - np.mean(S_data, axis=0)) / n, label='Expected') plt.plot(b_array / np.sum(Dp), (T_tvar - S_tvar) / n, label='TVaR(q=10)')
plt.xlabel("Wins", fontsize = 20) plt.ylabel("Runs Scored", fontsize = 20) plt.axvline(99, 0, 1, color = "Black", ls = '--') plt.show() x = np.array(df.RD) y = np.array(df.W) slope, intercept = np.polyfit(x, y, 1) abline_values = [slope * i + intercept for i in x] plt.figure(figsize=(10,8)) plt.scatter(x, y) plt.plot(x, abline_values, 'r') plt.title("Slope = %s" % (slope), fontsize = 12) plt.xlabel("Run Difference", fontsize =20) plt.ylabel("Wins", fontsize = 20) plt.axhline(99, 0, 1, color = "k", ls = '--') plt.show() corrcheck = df[['RD', 'W', 'Playoffs']].copy() g = sns.pairplot(corrcheck, hue = 'Playoffs',vars=["RD", "W"]) g.fig.set_size_inches(14,10) corrcheck.corr(method='pearson') podesta = df[['OBP','SLG','BA','RS']] podesta.corr(method='pearson') moneyball = df.dropna() from sklearn.feature_selection import SelectKBest from sklearn.feature_selection import chi2 X = moneyball.iloc[:,6:9] #independent columns
def plot_decision_boundary_distances(model, X, Y, feat_crosses=None, axis_lines=False, save=False): """ Plots decision boundary Args: model: neural network layer and activations in lambda function X: Data in shape (num_of_examples x features) feat_crosses: list of tuples showing which features to cross axis_lines: Draw axis lines at x=0 and y=0(bool, default False) save: flag to save plot image """ # first plot the data to see what is the size of the plot plt.scatter(X[:, 0], X[:, 1], s=200, c=np.squeeze(Y)) # get the x and y range of the plot x_ticks = plt.xticks()[0] y_ticks = plt.yticks()[0] plt.clf() # clear figure after getting size # Generate a grid of points between min_x_point-0.5 and max_x_point+0.5 with 1000 points in between, # similarly, for y points xs = np.linspace(min(x_ticks) - 0.5, max(x_ticks) + 0.5, 1000) ys = np.linspace(max(y_ticks) + 0.5, min(y_ticks) - 0.5, 1000) xx, yy = np.meshgrid(xs, ys) # create data points # Predict the function value for the whole grid prediction_data = np.c_[xx.ravel(), yy.ravel()] # add feat_crosses if provided if feat_crosses: for feature in feat_crosses: prediction_data = np.c_[prediction_data, prediction_data[:, feature[0]] * prediction_data[:, feature[1]]] Z = model(prediction_data) Z = Z.reshape(xx.shape) # Plot the contour and training examples plt.style.use('seaborn-whitegrid') c = plt.contour(xx, yy, Z, cmap='Blues') # draw a blue colored decision boundary plt.title('Distances from Decision Boundary', size=18) plt.xlabel('$x_1$', size=20) plt.ylabel('$x_2$', size=20) if axis_lines: plt.axhline(0, color='black') plt.axvline(0, color='black') # color map 'cmap' maps 0 labeled data points to red and 1 labeled points to green cmap = matplotlib.colors.ListedColormap(["red", "green"], name='from_list', N=None) plt.scatter(X[:, 0], X[:, 1], s=200, c=np.squeeze(Y), marker='x', cmap=cmap) # s-> size of marker points = X # data points from to which perpendicular lines are drawn v = c.collections[0].get_paths( )[0].vertices # returns two points from the decision line(visible start & end point) P1 = np.expand_dims(np.asarray((v[0, 0], v[0, 1])), axis=0) # the visible start point of the line P2 = np.expand_dims(np.asarray((v[-1, 0], v[-1, 1])), axis=0) # the visible end point of the line inter_points, distances = point_on_line(P1, P2, points) # combine the intersection points so that they're in the format required by `plt.plot` so # each list item is: # [(x_1,x_2), (y_1, y_2), len_of_line] perpendicular_line_points = [ list(zip(a, b)) + [c] for a, b, c in zip(points, inter_points, distances) ] # plot and label perpendicular lines to the decision boundary one by one # labelLine function comes from https://github.com/cphyc/matplotlib-label-lines/tree/master/labellines/baseline for line in perpendicular_line_points: x_points = np.clip( line[0], a_min=-0.5, a_max=1.5) # clip lines going out of bounds of visible area y_points = np.clip(line[1], a_min=-0.5, a_max=1.5) len = line[2] plt.plot(x_points, y_points, 'm--', label='{:.2f}'.format(len)) # print label to 2 decimal places labelLine(plt.gca().get_lines()[-1], x=sum(x_points) / 2) # label of the line should be centered, so (x_1+x_2)/2 if save: plt.savefig('decision_boundary_with_distances.png', bbox_inches='tight') plt.tight_layout() plt.show()
def plotSensorData(self, sensor="TEMPERATURE", show=True, saveFile=None, minValue=0, autoNight=False): """ plots data for temperature """ print("plotting sensor data. ", sensor) cursor = self.conn.cursor() # Check the number of row available in base query = "SELECT FRAMENUMBER," + sensor + " FROM FRAME" try: cursor.execute(query) except: print("plot sensor data: can't access data for ", sensor) return rows = cursor.fetchall() cursor.close() frameNumberList = [] sensorValueList = [] for row in rows: sensorValue = row[1] if (sensorValue > minValue): if sensorValue != None and minValue != None: frameNumberList.append(row[0]) sensorValueList.append(sensorValue) fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(24, 8)) ''' set x axis ''' formatter = ticker.FuncFormatter(self.frameToTimeTicker) ax.xaxis.set_major_formatter(formatter) ax.tick_params(labelsize=20) ax.xaxis.set_major_locator(ticker.MultipleLocator(30 * 60 * 60 * 12)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(30 * 60 * 60)) ax.plot(frameNumberList, sensorValueList, color="black", linestyle='-', linewidth=1, label=sensor) ax.legend(loc=1) # plot night nightTimeLine = EventTimeLine(self.conn, "night", None, None, None, None) mean = 0 std = 0 if len(sensorValueList) > 1: mean = np.mean(sensorValueList) std = np.std(sensorValueList) for nightEvent in nightTimeLine.eventList: ax.axvspan(nightEvent.startFrame, nightEvent.endFrame, alpha=0.1, color='black', ymin=0, ymax=1) ax.text((nightEvent.startFrame + nightEvent.endFrame) / 2, mean + std, "dark phase", fontsize=20, ha='center') autoNightList = [] if autoNight: # compute mean value: plt.axhline(y=mean, color='r', linestyle='--') ax.text(0, mean, "auto night threshold", fontsize=20, ha='left', va='bottom') inNight = False start = None end = None for i in range(len(frameNumberList)): frame = frameNumberList[i] value = sensorValueList[i] if value < mean: # night if inNight: continue else: inNight = True start = frame else: if inNight: end = frame inNight = False if end - start > 300: autoNightList.append((start, end)) else: print( "Skipping very short night phase. (less than 10 seconds)" ) start = None end = None for autoNight in autoNightList: ax.axvspan(autoNight[0], autoNight[1], alpha=0.1, color='red', ymin=0.2, ymax=0.8) ax.text((autoNight[0] + autoNight[1]) / 2, mean, "auto dark phase", fontsize=20, ha='center', color='red') plt.xlabel('time') if sensor == "TEMPERATURE": plt.ylabel('Temperature (C)') plt.ylim(17, 28) if sensor == "SOUND": plt.ylabel('Sound level (indice)') if sensor == "HUMIDITY": plt.ylabel('Humidity %') plt.ylim(0, 100) if sensor == "LIGHTVISIBLE": plt.ylabel('Visible light (indice)') if sensor == "LIGHTVISIBLEANDIR": plt.ylabel('Visible and infrared light (indice)') if saveFile != None: print("Saving figure : " + saveFile) fig.savefig(saveFile, dpi=100) ''' print("TEST 1") import os os.startfile( saveFile, 'open') print( "TEST 2") ''' if (show): plt.show() plt.close() if autoNight: return autoNightList
loss_s2 = 2 loss_s1 = 1 likelihood_ratio = s2_pdf / s1_pdf loss_ratio = loss_s2 / loss_s1 # Plot distribution (pdf vs stimuli) plt.figure(facecolor='white', dpi=200) plt.subplot(1, 2, 1) plt.plot(dist, s1_pdf, color='blue', label='Stimulus 1 (s1)') plt.plot(dist, s2_pdf, color='red', label='Stimulus 2 (s2)') plt.title('Stimulus conditional distributions') plt.xlabel('Stimulus') plt.ylabel('PDF') plt.axvline(x=s1_mean, linestyle='--', color='blue') plt.axvline(x=s2_mean, linestyle='--', color='red') plt.legend() # Plot likelihood ratio - what we're solving for plt.subplot(1, 2, 2) plt.plot(dist, likelihood_ratio, color='green', label='Likelihood ratio') plt.axhline(loss_ratio, color='green', linestyle='--') plt.title('Likelihood ratio') plt.xlabel('Stimulus') plt.ylabel('Likelihood ratio') plt.ylim((0, 4)) plt.legend() # Plot plt.show() plt.close()
mean = somme/len(temperatures_C) print("la moyenne est de",mean) temperatures_C = [33,66,65,0,59,60,62,64,70,76,80,69,80,83,68,79,61,53,50,49,53,48,45,39] temperature_hot = 0 for i in temperatures_C: if i>70: temperature_hot = i+=1 print("il y a",i,"elements superieur à 70°") for i in temperatures_C: if i==0: i == sum(i-1,i+1)/2 temperatures_C.append(i) print("la nouvelle liste:",temperatures_C) # axis x, axis y y = [33,66,65,0,59,60,62,64,70,76,80,81,80,83,90,79,61,53,50,49,53,48,45,39] x = list(range(len(y))) # plot plt.plot(x, y) plt.axhline(y=70, linewidth=1, color='r') plt.xlabel('hours') plt.ylabel('Temperature ºC') plt.title('Temperatures of our server throughout the day') fahrenheit = [((i*1.8)+32), for i in temperatures_C]
vp=(v1+v2)/2 xp=xarray(vp) solutions.append(xp) hp=xp[-1] count=count+1 if(h1*hp>0): v1=vp h1=hp else: v2=vp h2=hp v=(v1+v2)/2 x=xarray(v) solutions.append(x) h=x[-1] count=count+1 print('initial velocity',v) numeric_solution=np.zeros(N) for i in range(N): numeric_solution[i]=solutions[-1][i] plt.plot(t,numeric_solution,'c',label=r'$numerical$') plt.plot(t,-g*t**2/2+g*tf*t/2,'r',label=r'$exact$') #plotting exact solution plt.axhline(0,color='k') plt.xlabel(r'$t$',fontsize=20) plt.ylabel(r'$x$',fontsize=20) plt.legend() plt.show()
def generate_splitter_plots(exp_dict, pp, hperf_ids, hybclu_structs, s, m, c): # Prepare arrays for plotting f1_ba_arr = np.array(exp_dict['f1_ba']) f1_diff_arr = (f1_ba_arr[:, 1] - f1_ba_arr[:, 0]) / (1 - f1_ba_arr[:, 0]) # Plot presets xlocs = [1, 2] xlabels = ['Before', 'After'] # PLOTS TO SHOW CHANGES IN FDR / F1 # F1 fig1 = plt.figure() for i, clu in enumerate(exp_dict['hyb_clu']): plt.plot(xlocs, exp_dict['f1_ba'][i] - exp_dict['f1_ba'][i][0], '-ok', alpha=.8) plt.title('F1 Before/After automated curation') plt.xticks(xlocs, xlabels, fontsize='x-large') plt.xlim(0, 3) plt.ylabel('F1 Score') fig1.tight_layout() plt.draw() pp.savefig(plt.gcf()) # Plot F1 score before vs after fig1 = plt.figure() plt.scatter(f1_ba_arr[:, 0], f1_ba_arr[:, 1], s=.2) for i in range(f1_ba_arr.shape[0]): plt.text(f1_ba_arr[i, 0] * 1.002, f1_ba_arr[i, 1] * 1.002, '%d' % exp_dict['hyb_clu'][i], size='xx-small') plt.plot(np.linspace(0, np.max(f1_ba_arr.flatten()), 100), np.linspace(0, np.max(f1_ba_arr.flatten()), 100), 'k--', lw=.1) plt.ylabel('F1 After') plt.xlabel('F1 Before') fig1.tight_layout() plt.draw() pp.savefig(plt.gcf()) plt.close() # Plot SNR vs dF1 fig1 = plt.figure() plt.scatter(exp_dict['snr'], f1_diff_arr, s=.2) for i in range(f1_ba_arr.shape[0]): plt.text(exp_dict['snr'][i] * 1.002, f1_diff_arr[i] * 1.002, '%d' % exp_dict['hyb_clu'][i], size='xx-small') plt.axhline(y=0, c='k', ls='--') plt.ylabel('dF1 (fraction of possible +dF1)') plt.xlabel('SNR') fig1.tight_layout() plt.draw() pp.savefig(plt.gcf()) # Plot nspikes vs dF1 fig1 = plt.figure() plt.scatter(exp_dict['nspikes'], f1_diff_arr, s=.2) for i in range(f1_ba_arr.shape[0]): plt.text(exp_dict['nspikes'][i] * 1.002, f1_diff_arr[i] * 1.002, '%d' % exp_dict['hyb_clu'][i], size='xx-small') plt.axhline(y=0, c='k', ls='--') plt.ylabel('dF1 (fraction of possible +dF1)') plt.xlabel('Number of spikes (count)') fig1.tight_layout() plt.draw() pp.savefig(plt.gcf()) # Plot per-cluster representation of splitting process. for i, clu in enumerate(exp_dict['hyb_clu']): fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8, 4), sharey=False, sharex=True) axes[0].bar(np.arange(1, nclusts + 1), (np.flip(np.sort(exp_dict['clu_precision'][i])) * 100)) axes[0].set_xlabel('Sub-cluster (idx)') axes[0].set_ylabel('Precision (%)') axes[0].set_title('Unit %d' % (clu)) f1_merged_scores = np.array(exp_dict['f1_scores'][i]) best_idx = np.argmax(f1_merged_scores) axes[1].plot(np.arange(1, nclusts + 1), (f1_merged_scores * 100)) axes[1].plot( np.arange(1, nclusts + 1)[best_idx], (f1_merged_scores * 100)[best_idx], 'ro') axes[1].set_xlabel('Sub-clusters combined (count)') axes[1].set_ylabel('F1 Score (%) of combined clust.') axes[1].set_title('Unit %d' % (clu)) fig.tight_layout() pp.savefig(plt.gcf())
d = month.copy() while d < next: if d in data: for i,b in enumerate(hdata.Bombers): monthly[month][i][0] += data[d][i][0] monthly[month][i][1] += data[d][i][1] d = d.next() month = next fig = plt.figure() ax = fig.add_subplot(1,1,1) dates = sorted(monthly.keys()) total = {m: [sum(monthly[m][i][0] for i,b in enumerate(hdata.Bombers)), sum(monthly[m][i][1] for i,b in enumerate(hdata.Bombers))] for m in monthly} top = max(v[0] for v in total.values()) bottom = min(v[1] for v in total.values()) plt.axis(ymax=max(top, -bottom), ymin=min(-top, bottom)) for bi,b in enumerate(hdata.Bombers): def ins(m): return hdata.inservice(m, b) or hdata.inservice(m.nextmonth(), b) bprod = [monthly[d][bi][0] for d in dates if ins(d)] bloss = [monthly[d][bi][1] for d in dates if ins(d)] if not any(bprod+bloss): continue bdate = [d.ordinal() for d in dates if ins(d)] gp = plt.plot_date(bdate, bprod, fmt='o-', mew=0, color=extra[b['name']]['colour'], tz=None, xdate=True, ydate=False, label=b['name'], zorder=0) gl = plt.plot_date(bdate, bloss, fmt='o-', mew=0, color=extra[b['name']]['colour'], tz=None, xdate=True, ydate=False, label=None, zorder=0) gt = plt.plot_date([d.ordinal() for d in dates], [total[d][0] for d in dates], fmt='k+-', tz=None, xdate=True, ydate=False, label='total', zorder=-2) gb = plt.plot_date([d.ordinal() for d in dates], [total[d][1] for d in dates], fmt='k+-', tz=None, xdate=True, ydate=False, label=None, zorder=-2) ax.grid(b=True, axis='y') plt.axhline(y=0, xmin=0, xmax=1, c='k', zorder=-1) if legend: plt.legend(ncol=2, loc='upper left') plt.show()
p1 = ax.bar(indices, values, width, align='center') values = [] indices = [] i = 0 while i < len(plotdata_after): indices.append(i + width / 2) values.append(plotdata_after[str(i)]) i += 1 p2 = ax.bar(indices, values, width, align='center') axes = plt.gca() axes.set_ylim([0.225, 0.265]) ax.set_xticks([0, 1, 2, 3]) ax.set_xticklabels(["0%-25%", "25%-50%", "50%-75%", "75%-100%"]) plt.axhline(y=0.25, c="r") ax.legend((p1[0], p2[0]), ("Year before the change", "Year after the change")) plt.ylabel("Distribution of contributions over all streaks with length > 30") plt.xlabel("Streaks lifetime") #plt.annotate("TEXT", xy=(3.1,0.07), xytext=(2.6,0.2), arrowprops=dict(facecolor='black', shrink=0.03)) plt.show() logging.info("Done.") log_endtime = datetime.datetime.now() log_runtime = (log_endtime - log_starttime) logging.info("Total runtime: " + str(log_runtime))
d.append(np.linalg.norm(z_b - (Z[i] + s[b] * g))) adv_x.append(get_min_wrt(d, s)) x_0[2:0x3c] = adv_x # print("====================ADVERSARIAL BYTES====================") # print(adv_x) pred = model.predict(np.asarray([x_0])) plot_arr.append(pred[0][0]) print(pred) print("Iteration n: {}".format(t)) # print(x[:int(0x3c)]) t = t + 1 # print(adv_x) print("BEFORE") print(x[:61]) print(model.predict(np.asarray([x]))) print("AFTER") print(x_0[:61]) print(model.predict(np.asarray([x_0]))) plt.xlabel("Iterations") plt.ylabel("Confidence of being a malware") plt.xticks(range(0, t)) plt.ylim(0, 1) plt.axhline(y=0.5, ls='--', color='r') plt.plot(range(0, t), plot_arr, marker='x') plt.show()
def sim_ens(n): sel = np.random.choice(all_preds.shape[0], n) return (all_preds[sel].mean(axis=0).argmax(axis=1) == targets).mean() s = np.arange(1, 30, 1) res = [] for n in tqdm(s): res.append([sim_ens(n=n) for _ in range(30)]) res = np.column_stack(res) for r in res: _ = plt.scatter(s, r, alpha=0.25, c='grey') _ = plt.plot(s, res.mean(axis=0), c='red') _ = plt.axhline(accs.max(), c='blue') show_plot() # Looks like it saturates around ~ 10 randomly chosen models sort_by_time = np.array([list(meta.model_name).index(os.path.basename(i).split('.')[0]) for i in fs]) fs, all_preds, accs = fs[sort_by_time], all_preds[sort_by_time], accs[sort_by_time] sel = accs > 0.8 fs, all_preds, accs = fs[sel], all_preds[sel], accs[sel] ens_accs = [(all_preds[:i].mean(axis=0).argmax(axis=1) == targets).mean() for i in range(1, 1 + 20)] best_acc = [accs[:i].max() for i in range(1, 1 + 20)] _ = plt.plot(ens_accs[:20]) _ = plt.plot(best_acc[:20], c='grey')
values = solve([ 0.2 * (x1 - 70)**2 + 0.8 * (x2 - 20)**2 - fx, 0.8 * (x1 - 10)**2 + 0.2 * (x2 - 70)**2 - fy ], dict=True) return [values[0][x1], values[0][x2]] # Настройки отображения plt.gcf().canvas.set_window_title("Домашняя работа #4") plt.xlabel("X1") plt.ylabel("X2") plt.axvline(0, 0, 1) plt.axhline(0, 0, 1) Q = [] Q80 = [] for w in range(1, 1024): a = lptau(w, 2) # Q.append(a) print(str(w) + " - " + str(a)) ax = a[0] * 80 ay = a[1] * 80 Q80.append([ax, ay]) # plt.scatter(a[0], a[1], color="black") tmp = a[0] a[0] = f1(a[0] * 80, a[1] * 80) a[1] = f2(tmp * 80, a[1] * 80)
def main(argv): if len(argv) < 1: usage() sys.exit(1) experiments = [] exp_by_mechanism = defaultdict(list) exp_by_start_config = defaultdict(list) exp_by_task_file = defaultdict(list) dt_re = re.compile('(.*)\.bag') # Make one, flat list of paths to log files bag_paths = [] for path_arg in argv: bag_paths.extend(glob.glob(path_arg)) for i, bag_path in enumerate(bag_paths): print("Reading {0}".format(bag_path)) bag = None try: bag = rosbag.Bag(bag_path) except: print("Couldn't open {0} for reading!".format(bag_path)) continue experiment_finished = False for topic, msg, msg_time in bag.read_messages('/experiment'): if msg.event == 'END_EXPERIMENT': experiment_finished = True if not experiment_finished: print("{0}: experiment timed out, skipping...".format(bag_path)) continue bag_filename = os.path.basename(bag_path) (map, start_config, mechanism, task_file, remainder) = bag_filename.split('__') dt_match = dt_re.search(remainder) exp = Experiment(bag, mechanism, start_config, task_file) exp_by_mechanism[mechanism].append(exp) exp_by_start_config[start_config].append(exp) exp_by_task_file[task_file].append(exp) # ['A', 'C', 'E'] for task_file in task_files: # Set plot title plt.title( "Task Allocation, Task File '{0}' (Simulation)".format(task_file)) #plt.tick_params(axis='y', which='major', labelsize=10) #plt.tick_params(axis='x', which='major', labelsize=8) # Position of the current allocation 'bar' x_pos = 0 #grid = np.random.rand(8, len(experiments), 3) grid = np.random.rand(8, 80, 3) #grid = np.random.rand(8, 20, 3) for start_config in start_configs: for mechanism in mechanisms: exps = set(exp_by_start_config[start_config]).intersection( set(exp_by_mechanism[mechanism])).intersection( set(exp_by_task_file[task_file])) #for exp in sorted(exps, key=lambda e: int(e.run_number))[0:10]: #for exp in exps: for exp in list(exps)[0:10]: print "{0}, {1}, {2}".format(start_config, mechanism, task_file) # For each robot # for role in sorted(exp.roles.keys()): # robot_name = exp.roles[role] # robot = exp.robots_by_name[robot_name] # # For each task that that robot won... # for task in robot.tasks.values(): # # Record it in our grid # print "{0} won point {1}".format(role,int(task.auction_id)+1) # grid[task.auction_id][x_pos] = stroke_colors[role] run_msgs = defaultdict(list) for topic, msg, msg_time in exp.bag.read_messages( '/tasks/award'): print "{0} won task {1}".format( msg.robot_id, msg.tasks[0].task.task_id) #grid[int(msg.tasks[0].task.task_id)-1][x_pos] = stroke_colors[msg.robot_id] stroke_color = stroke_colors[msg.robot_id] y_pos = int(msg.tasks[0].task.task_id) - 1 grid[y_pos][x_pos] = stroke_color #print "y_pos=={0}, x_pos=={1}, stroke_color=={2}".format(y_pos, # x_pos, # stroke_color) x_pos += 1 plt.imshow(grid[::-1], interpolation='none', extent=[0, 80, 0, 8]) plt.axes().set_aspect(8) # Set x, y limits (range) plt.ylim(0, 8) plt.xlim(0, 80) # Tick locations, labels, and sizes yticks = plt.getp(plt.gca(), 'yticklines') plt.setp(yticks, 'linewidth', 0) plt.yticks([y + 0.5 for y in range(0, 8)], ['Point {0}'.format(y) for y in range(1, 8)]) xticks = plt.getp(plt.gca(), 'xticklines') plt.setp(xticks, 'linewidth', 0) plt.xticks([x + 5 for x in range(0, 80, 10)], [ 'RR-C', 'OSI-C', 'SSI-C', 'PSI-C', 'RR-D', 'OSI-D', 'SSI-D', 'PSI-D' ]) # Grid lines (manually drawn) for i in range(0, 80): plt.axvline(x=i, color='k', linewidth=0.1, solid_capstyle='butt') for i in range(0, 80, 10): plt.axvline(x=i, color='k', linewidth=0.5, solid_capstyle='butt') for i in range(8): plt.axhline(y=i, color='k', linewidth=0.5, solid_capstyle='butt') # Legend: robot colors ax = plt.subplot(111) legend_keys = [] legend_labels = [] for robot_num in range(1, 4): legend_keys.append( plt.Rectangle((0, 0), 1, 1, fc=stroke_colors['robot_%d' % (robot_num)])) legend_labels.append('Robot %d' % robot_num) lgd = ax.legend(legend_keys, legend_labels, loc='upper center', bbox_to_anchor=(0.5, -0.05), fancybox=True, ncol=3, fontsize='small') plt.savefig(FILENAME_TEMPL.format(task_file), dpi=300, bbox_extra_artists=(lgd, ), bbox_inches='tight') plt.close()
usecols=(0)) # in arcmins i_zeta_vec = np.loadtxt(str(sq_degrees) + '_sq_degrees/' + str(N) + '_pts/i_zeta_theoretical_lognormal_patch_' + str(sq_degrees) + '_sq_degrees.txt', usecols=(1)) plt.figure(figsize=(10, 10)) plt.plot(theta_scale_vec, i_zeta_vec, c='r', label='theoretical $i\\zeta(\\theta)$') plt.xlim(1, 200) #plt.ylim(1e-6, 1e-1) plt.ylim(-0.001, 0.02) plt.xscale('log') #plt.yscale('log') plt.axhline(0, linestyle='dashed') plt.xlabel('Angle, $\\theta$ (arcmins)', fontsize=16) plt.ylabel('Integrated 3-pt function, $i\\zeta(\\theta)$', fontsize=16) plt.tick_params(labelsize=16) plt.title('Integrated 3-pt function of lognormal field (' + str(sq_degrees) + ' sq. degrees patch) \n # of evaluations = ' + str(N) + '; $\\delta_0$ = ' + str(log_shift), fontsize=14) plt.legend(loc='best', fontsize=14) plt.savefig( str(sq_degrees) + '_sq_degrees/' + str(N) + '_pts/i_zeta_theoretical_lognormal_patch_' + str(sq_degrees) + '_sq_degrees.pdf') #################################### # Theoretical & simulations i_zeta plot
## Calculating an average background image for i in np.arange(0, len(background_frames), 1): fname = 'frame%s.png' % (str(background_frames[i])) filein = os.path.join( 'C:\\Users\\bsukra\\repositories\\TESTING\\Image_Analysis\\analysis_notebook\\frames\\', fname) ##CHANGE## avg_back += imageio.imread(filein, as_gray=True) avg_back = avg_back / len(background_frames) ## Manually locating region of interest, this could be automated when implemented fig = plt.figure(figsize=(12, 12)) plt.imshow(avg_back) plt.axvline(400, c='r', lw=2) plt.axvline(920, c='r', lw=2) plt.axhline(260, c='r', lw=2) plt.axhline(780, c='r', lw=2) # Saving the back-image fname = "original_image_back.png" fileout = os.path.join( 'C:\\Users\\bsukra\\repositories\\TESTING\\Image_Analysis\\' + out_dir, fname) plt.savefig(fileout, dpi=300, bbox_inches="tight") ## Loading on-image ## this will be replaced with data acquisition real time using the cam ## N.B. the on-image is a terribly clipped e+ beam but the script still works fname = 'frame%s.png' % (2) filein = os.path.join( 'C:\\Users\\bsukra\\repositories\\TESTING\\Image_Analysis\\analysis_notebook\\frames\\',
a_SFflux_SODA3[i], b_SFflux_SODA3[i] = np.linalg.lstsq(A_SFflux_SODA3,SFflux_atl_SODA3_series[:,i])[0] a_OMET_converge_SODA3 = np.zeros((len(latitude_SODA3_center)),dtype = float) b_OMET_converge_SODA3 = np.zeros((len(latitude_SODA3_center)),dtype = float) A_OMET_converge_SODA3 = np.vstack([counter_SODA3,np.ones(len(counter_SODA3))]).T for i in np.arange(len(latitude_SODA3_center)): a_OMET_converge_SODA3[i], b_OMET_converge_SODA3[i] = np.linalg.lstsq(A_OMET_converge_SODA3,OMET_converge_atl_SODA3_series[:,i])[0] a_OHC_dt_SODA3 = np.zeros((len(latitude_SODA3_center)),dtype = float) b_OHC_dt_SODA3 = np.zeros((len(latitude_SODA3_center)),dtype = float) A_OHC_dt_SODA3 = np.vstack([counter_SODA3,np.ones(len(counter_SODA3))]).T for i in np.arange(len(latitude_SODA3_center)): a_OHC_dt_SODA3[i], b_OHC_dt_SODA3[i] = np.linalg.lstsq(A_OHC_dt_SODA3,OHC_dt_atl_SODA3_series[:,i])[0] fig4 = plt.figure() plt.axhline(y=0, color='k',ls='-') plt.plot(latitude_ORAS4_center,a_SFflux_ORAS4*12,'b-',linewidth=1.0,label='ORAS4 SFflux') plt.plot(latitude_GLORYS2V3_center,a_SFflux_GLORYS2V3*12,'r-',linewidth=1.0,label='GLORYS2V3 SFflux') plt.plot(latitude_SODA3_center,a_SFflux_SODA3*12,'g-',linewidth=1.0,label='SODA3 SFflux') plt.plot(latitude_ORAS4_center,a_OMET_converge_ORAS4*12,'b--',linewidth=1.0,label='ORAS4 OMET') plt.plot(latitude_GLORYS2V3_center,a_OMET_converge_GLORYS2V3*12,'r--',linewidth=1.0,label='GLORYS2V3 OMET') plt.plot(latitude_SODA3_center,a_OMET_converge_SODA3*12,'g--',linewidth=1.0,label='SODA3 OMET') plt.plot(latitude_ORAS4_center,a_OHC_dt_ORAS4*12,'b:',linewidth=2.0,label='ORAS4 OHC') plt.plot(latitude_GLORYS2V3_center,a_OHC_dt_GLORYS2V3*12,'r:',linewidth=2.0,label='GLORYS2V3 OHC') plt.plot(latitude_SODA3_center,a_OHC_dt_SODA3*12,'g:',linewidth=2.0,label='SODA3 OHC') plt.title('Trend of Turbulent Flux, OHC tendency and OMET convergence') fig4.set_size_inches(10.5, 6) plt.xlabel("Latitude",fontsize = 16) plt.xticks(fontsize = 16) plt.ylabel("Trend (PW/year)",fontsize = 16) plt.yticks(fontsize=16)
x = np.arange(-5.0, 5.1, 0.1) weight, bias = 1, 0 y1 = sigmoid(weight, x) mylabel = f'y={str(weight)}*x{str(bias)}' plt.plot(x, y1, color='g', label=mylabel) weight, bias = 5, 0 y2 = sigmoid(weight, x, bias) mylabel = f'y={str(weight)}*x{str(bias)}' plt.plot(x, y2, color='b', label=mylabel) weight, bias = 5, 3 y3 = sigmoid(weight, x, bias) mylabel = f'y={str(weight)}*x{str(bias)}' plt.plot(x, y3, color='r', label=mylabel) weight, bias = 5, 3 y3 = sigmoid(weight, x, asc=False) mylabel = f'y={str(weight)}*x{str(bias)}' plt.plot(x, y3, color='r', label=mylabel) plt.axhline(y=0, color='black', linewidth=1, linestyle='dashed') plt.axhline(y=1, color='black', linewidth=1, linestyle='dashed') plt.title('sigmoid function') plt.ylim(-0.1, 1.1) plt.legend(loc='best') # 범례 plt.savefig('sigmoid_function.png')
# Sx = (1.0 / len(X)) * Xc.T.dot(Xc) # 標本共分散行列 Sx = np.cov(X, rowvar=0, bias=1) # 標本共分散行列 a = (Xc.dot(np.linalg.pinv(Sx)) * Xc).sum(axis=1) / X.shape[1] # 1変数当たりの異常度 # 閾値を決定 # (標本が正常範囲に入るように1変数当たりのマハラノビス距離の閾値を決める) th = 1.0 state_label = df.index[a > th] # 閾値を超えた州の名前リスト state_a = a[a > th] # 閾値を超えた州の異常度リスト print(state_label) print(state_a) # プロット plt.plot(range(len(a)), a, "bo") plt.axhline(y=th, color='red') plt.title("Anomaly score") plt.xlabel("Sample number") plt.ylabel("Anomaly score") plt.show() # SN比解析 xc_prime = Xc[4, :] # 中心化行列からCalifのデータ行を取得 SN1 = 10 * np.log10(xc_prime**2 / np.diag(Sx)) plt.bar(range(len(SN1)), SN1, tick_label=["deaths", "popden", "rural", "temp", "fuel"], align="center") plt.title("SN ratio") plt.show()
########################################################################### # Nested cross-validation from sklearn.grid_search import GridSearchCV # We are going to tune the parameter 'k' of the step called 'anova' in # the pipeline. Thus we need to address it as 'anova__k'. # Note that GridSearchCV takes an n_jobs argument that can make it go # much faster grid = GridSearchCV(anova_svc, param_grid={'anova__k': k_range}, verbose=1) nested_cv_scores = cross_val_score(grid, X, y) print("Nested CV score: %.4f" % np.mean(nested_cv_scores)) ########################################################################### # Plot the prediction scores using matplotlib from matplotlib import pyplot as plt plt.figure(figsize=(6, 4)) plt.plot(cv_scores, label='Cross validation scores') plt.plot(scores_validation, label='Left-out validation data scores') plt.xticks(np.arange(len(k_range)), k_range) plt.axis('tight') plt.xlabel('k') plt.axhline(np.mean(nested_cv_scores), label='Nested cross-validation', color='r') plt.legend(loc='best', frameon=False) plt.show()
for amount in least_rank_amounts: x_axis.append(amount) coords_user = get_coordinates(user, labels, embeddings) coords_user_without_least_rank = get_coordinates( str(user) + str(amount), labels, embeddings) y_axis.append( dist.euclidean(coords_user, coords_user_without_least_rank)) bp[amount - 1].append( dist.euclidean(coords_user, coords_user_without_least_rank)) print(x_axis) print(y_axis) fig = plt.figure(figsize=(15, 10), facecolor='w') axes = plt.subplot(1, 1, 1) plt.title(''.format()) plt.ylabel('Euclidean distance') plt.xlabel('Number of removed symbols') plt.xticks(least_rank_amounts) plt.axhline(y=13, color='r', linestyle='-') box = axes.boxplot(bp) plt.text(5.2, 13.25, 'Max Distance', horizontalalignment='center', verticalalignment='top', multialignment='center') plt.show()