def show(self, rescale=True, ax=None): """Visualization of a design matrix Parameters ---------- rescale: bool, optional rescale columns magnitude for visualization or not ax: axis handle, optional Handle to axis onto which we will draw design matrix Returns ------- ax: axis handle """ import matplotlib.pyplot as plt # normalize the values per column for better visualization x = self.matrix.copy() if rescale: x = x / np.sqrt(np.sum(x ** 2, 0)) if ax is None: plt.figure() ax = plt.subplot(1, 1, 1) ax.imshow(x, interpolation='Nearest', aspect='auto') ax.set_label('conditions') ax.set_ylabel('scan number') if self.names is not None: ax.set_xticks(range(len(self.names))) ax.set_xticklabels(self.names, rotation=60, ha='right') return ax
def draw_ranges_for_parameters(data, title='', save_path='./pictures/'): parameters = data.columns.values.tolist() # remove flight name parameter for idx, parameter in enumerate(parameters): if parameter == 'flight_name': del parameters[idx] flight_names = np.unique(data['flight_name']) print len(flight_names) for parameter in parameters: plt.figure() axis = plt.gca() # ax.set_xticks(numpy.arange(0,1,0.1)) axis.set_yticks(flight_names) axis.tick_params(labelright=True) axis.set_ylim([94., 130.]) plt.grid() plt.title(title) plt.xlabel(parameter) plt.ylabel('flight name') colors = iter(cm.rainbow(np.linspace(0, 1,len(flight_names)))) for flight in flight_names: temp = data[data.flight_name == flight][parameter] plt.plot([np.min(temp), np.max(temp)], [flight, flight], c=next(colors), linewidth=2.0) plt.savefig(save_path+title+'_'+parameter+'.jpg') plt.close()
def plot_predict_is(self,h=5,**kwargs): """ Plots forecasts with the estimated model against data (Simulated prediction with data) Parameters ---------- h : int (default : 5) How many steps to forecast Returns ---------- - Plot of the forecast against data """ figsize = kwargs.get('figsize',(10,7)) plt.figure(figsize=figsize) date_index = self.index[-h:] predictions = self.predict_is(h) data = self.data[-h:] t_params = self.transform_z() plt.plot(date_index,np.abs(data-t_params[-1]),label='Data') plt.plot(date_index,predictions,label='Predictions',c='black') plt.title(self.data_name) plt.legend(loc=2) plt.show()
def plot_scenario(strategies, names, scenario_id=1): probabilities = get_scenario(scenario_id) plt.figure(figsize=(6, 4.5)) ax = plt.subplot(111) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left() plt.yticks(fontsize=14) plt.xticks(fontsize=14) plt.xlim((0, 1300)) # Remove the tick marks; they are unnecessary with the tick lines we just plotted. plt.tick_params(axis="both", which="both", bottom="on", top="off", labelbottom="on", left="off", right="off", labelleft="on") for rank, (strategy, name) in enumerate(zip(strategies, names)): plot_strategy(probabilities, strategy, name, rank) plt.title("Bandits: " + str(probabilities), fontweight='bold') plt.xlabel('Number of Trials', fontsize=14) plt.ylabel('Cumulative Regret', fontsize=14) plt.legend(names) plt.show()
def plotResults(datasetName, sampleSizes, foldsSet, cvScalings, sampleMethods, fileNameSuffix): """ Plots the errors for a particular dataset on a bar graph. """ for k in range(len(sampleMethods)): outfileName = outputDir + datasetName + sampleMethods[k] + fileNameSuffix + ".npz" data = numpy.load(outfileName) errors = data["arr_0"] meanMeasures = numpy.mean(errors, 0) for i in range(sampleSizes.shape[0]): plt.figure(k*len(sampleMethods) + i) plt.title("n="+str(sampleSizes[i]) + " " + sampleMethods[k]) for j in range(errors.shape[3]): plt.plot(foldsSet, meanMeasures[i, :, j]) plt.xlabel("Folds") plt.ylabel('Error') labels = ["VFCV", "PenVF+"] labels.extend(["VFP s=" + str(x) for x in cvScalings]) plt.legend(tuple(labels)) plt.show()
def test_get_obs(self): plt.figure() ant_sigs = antennas.antennas_signal(self.ants, self.ant_models, self.sources, self.rad.timebase) rad_sig_full = self.rad.sampled_signal(ant_sigs[0, :], 0) obs_full = self.rad.get_full_obs(ant_sigs, self.utc_date, self.config) ant_sigs_simp = antennas.antennas_simplified_signal(self.ants, self.ant_models, self.sources, self.rad.baseband_timebase, self.rad.int_freq) obs_simp = self.rad.get_simplified_obs(ant_sigs_simp, self.utc_date, self.config) freqs, spec_full_before_obs = spectrum.plotSpectrum(rad_sig_full, self.rad.ref_freq, label='full_before_obs_obj', c='blue') freqs, spec_full = spectrum.plotSpectrum(obs_full.get_antenna(1), self.rad.ref_freq, label='full', c='cyan') freqs, spec_simp = spectrum.plotSpectrum(obs_simp.get_antenna(1), self.rad.ref_freq, label='simp', c='red') plt.legend() self.assertTrue((spec_full_before_obs == spec_full).all(), True) plt.figure() plt.plot(freqs, (spec_simp-spec_full)/spec_full) plt.show() print len(obs_full.get_antenna(1)), obs_full.get_antenna(1).mean() print len(obs_simp.get_antenna(1)), obs_simp.get_antenna(1).mean()
def plotTestData(tree): plt.figure() plt.axis([0,1,0,1]) plt.xlabel("X axis") plt.ylabel("Y axis") plt.title("Green: Class1, Red: Class2, Blue: Class3, Yellow: Class4") for value in class1: plt.plot(value[0],value[1],'go') plt.hold(True) for value in class2: plt.plot(value[0],value[1],'ro') plt.hold(True) for value in class3: plt.plot(value[0],value[1],'bo') plt.hold(True) for value in class4: plt.plot(value[0],value[1],'yo') plotRegion(tree) for value in classPlot1: plt.plot(value[0],value[1],'g.',ms=3.0) plt.hold(True) for value in classPlot2: plt.plot(value[0],value[1],'r.', ms=3.0) plt.hold(True) for value in classPlot3: plt.plot(value[0],value[1],'b.', ms=3.0) plt.hold(True) for value in classPlot4: plt.plot(value[0],value[1],'y.', ms=3.0) plt.grid(True) plt.show()
def regress_show4( yEv, yEv_calc, disp = True, graph = True, plt_title = None, ms_sz = None): # if the output is a vector and the original is a metrix, # the output is translated to a matrix. r_sqr, RMSE, MAE, DAE = estimate_accuracy4( yEv, yEv_calc, disp = disp) if graph: #plt.scatter( yEv.tolist(), yEv_calc.tolist()) plt.figure() if ms_sz is None: ms_sz = max(min( 6000 / yEv.shape[0], 8), 3) # plt.plot( yEv.tolist(), yEv_calc.tolist(), '.', ms = ms_sz) # Change ms plt.scatter( yEv.tolist(), yEv_calc.tolist(), s = ms_sz) ax = plt.gca() lims = [ np.min([ax.get_xlim(), ax.get_ylim()]), # min of both axes np.max([ax.get_xlim(), ax.get_ylim()]), # max of both axes ] # now plot both limits against eachother #ax.plot(lims, lims, 'k-', alpha=0.75, zorder=0) ax.plot(lims, lims, '-', color = 'pink') plt.xlabel('Experiment') plt.ylabel('Prediction') if plt_title is None: plt.title( '$r^2$={0:.1e}, RMSE={1:.1e}, MAE={2:.1e}, MedAE={3:.1e}'.format( r_sqr, RMSE, MAE, DAE)) elif plt_title != "": plt.title( plt_title) # plt.show() return r_sqr, RMSE, MAE, DAE
def cv_show( yEv, yEv_calc, disp = True, graph = True, grid_std = None): # if the output is a vector and the original is a metrix, # the output is translated to a matrix. if len( np.shape(yEv_calc)) == 1: yEv_calc = np.mat( yEv_calc).T if len( np.shape(yEv)) == 1: yEv = np.mat( yEv).T r_sqr, RMSE = jchem.estimate_accuracy( yEv, yEv_calc, disp = disp) if graph: #plt.scatter( yEv.tolist(), yEv_calc.tolist()) plt.figure() ms_sz = max(min( 4000 / yEv.shape[0], 8), 1) plt.plot( yEv.tolist(), yEv_calc.tolist(), '.', ms = ms_sz) # Change ms ax = plt.gca() lims = [ np.min([ax.get_xlim(), ax.get_ylim()]), # min of both axes np.max([ax.get_xlim(), ax.get_ylim()]), # max of both axes ] # now plot both limits against eachother #ax.plot(lims, lims, 'k-', alpha=0.75, zorder=0) ax.plot(lims, lims, '-', color = 'pink') plt.xlabel('Experiment') plt.ylabel('Prediction') if grid_std: plt.title( '($r^2$, std) = ({0:.2e}, {1:.2e}), RMSE = {2:.2e}'.format( r_sqr, grid_std, RMSE)) else: plt.title( '$r^2$ = {0:.2e}, RMSE = {1:.2e}'.format( r_sqr, RMSE)) plt.show() return r_sqr, RMSE
def make_fish(zoom=False): plt.close(1) plt.figure(1, figsize=(6, 4)) plt.plot(plot_limits['pitch'], plot_limits['rolldev'], '-g', lw=3) plt.plot(plot_limits['pitch'], -plot_limits['rolldev'], '-g', lw=3) plt.plot(pitch.midvals, roll.midvals, '.b', ms=1, alpha=0.7) p, r = make_ellipse() # pitch, off nominal roll plt.plot(p, r, '-c', lw=2) gf = -0.08 # Fudge on pitch value for illustrative purposes plt.plot(greta['pitch'] + gf, -greta['roll'], '.r', ms=1, alpha=0.7) plt.plot(greta['pitch'][-1] + gf, -greta['roll'][-1], 'xr', ms=10, mew=2) if zoom: plt.xlim(46.3, 56.1) plt.ylim(4.1, 7.3) else: plt.ylim(-22, 22) plt.xlim(40, 180) plt.xlabel('Sun pitch angle (deg)') plt.ylabel('Sun off-nominal roll angle (deg)') plt.title('Mission off-nominal roll vs. pitch (5 minute samples)') plt.grid() plt.tight_layout() plt.savefig('fish{}.png'.format('_zoom' if zoom else ''))
def make_entity_plot(filename, title, fixed_noip, fixed_ip, dynamic_noip, dynamic_ip): plt.figure(figsize=(12,5)) plt.title("Settings comparison - " + title) plt.xlabel('Time (ms)', fontsize=12) plt.xlim([0,62000]) x = 0 barwidth = 0.5 bargroupspacing = 1.5 fixed_noip_mean,fixed_noip_conf = conf_stats(fixed_noip) fixed_ip_mean,fixed_ip_conf = conf_stats(fixed_ip) dynamic_noip_mean,dynamic_noip_conf = conf_stats(dynamic_noip) dynamic_ip_mean,dynamic_ip_conf = conf_stats(dynamic_ip) values = [fixed_noip_mean,fixed_ip_mean,dynamic_noip_mean, dynamic_ip_mean] errs = [fixed_noip_conf,fixed_ip_conf,dynamic_noip_conf, dynamic_ip_conf] y_pos = numpy.arange(len(values)) plt.barh(y_pos, values, xerr=errs, align='center', color=['r', 'b', 'r', 'b'], ecolor='black', alpha=0.7) plt.yticks(y_pos, ["Fixed | no I.P.", "Fixed | I.P.", "Dynamic | no I.P.", "Dynamic | I.P."]) plt.savefig(output_file(filename)) plt.clf()
def delta(): beta = 0.99 N = 1000 u = lambda c: np.sqrt(c) W = np.linspace(0,1,N) X, Y = np.meshgrid(W,W) Wdiff = (X-Y).T index = Wdiff <0 Wdiff[index] = 0 util_grid = u(Wdiff) util_grid[index] = -10**10 Vprime = np.zeros((N,1)) delta = np.ones(1) tol = 10**-9 it = 0 max_iter = 500 while (delta[-1] >= tol) and (it < max_iter): V = Vprime it += 1; val = util_grid + beta*V.T Vprime = np.amax(val, axis = 1) Vprime = Vprime.reshape((N,1)) delta = np.append(delta,np.dot((Vprime-V).T,Vprime-V)) plt.figure() plt.plot(delta[1:]) plt.ylabel(r'$\delta_k$') plt.xlabel('iteration') plt.savefig('convergence.pdf') plt.clf()
def blue_sideband_thermal_tester(): #BSB sideband = +1 import matplotlib.pyplot as plt dataobj = ReadData('2014Jun19',experiment = 'RabiFlopping' ) data = dataobj.get_data('1212_15') sideband = 1 trap_freq = 2.57 nbar_init= .3 #default 20. rabi = RabiFlop(data) rabi.setData(data) f_rabi = thermal_tester() #f_rabi is the same on same set of data print("f_rabi from thermal tester is :"+str(f_rabi)) initial_guess = {'nbar':nbar_init, 'f_rabi':f_rabi, #same as that of thermal tester 'delta':0.0, 'delta_fluctuations':0., 'trap_freq':trap_freq, 'sideband':sideband, 'nmax':1000, 'angle': 50./360.*2*np.pi, 'rabi_type':'thermal','eta': 0.04 #rabi.guess_eta()#0.05 } fit_params = {} #Put it in the fit params format for key in initial_guess.keys(): fit_params[key] = (False, False, initial_guess[key]) # fixed most of the parameters fit_params['nbar'] = (False, False, initial_guess['nbar']) fit_params['angle'] = (True, False, initial_guess['angle']) #fit_params['delta'] = (True, False, initial_guess['delta']) #we decided to fix delta rabi.setUserParameters(fit_params) x,y = rabi.fit() plt.figure() plt.plot(data[:,0], data[:,1],'o') # plotting raw data : excitation probability versus time plt.plot(x,y) #plotting the fit nbar = rabi.get_parameter_value('nbar') # rabi frequency in Hz angle= rabi.get_parameter_value('angle') print ('nbar: {}'.format(nbar)) print ('{} radians = {} degrees'.format(str(angle), str(angle/(2*np.pi)*360)))
def thermal_tester(): import matplotlib.pyplot as plt dataobj = ReadData('2014Jun19',experiment = 'RabiFlopping' ) car_data = dataobj.get_data('1219_57') sideband = 0 trap_freq = 2.57 nbar_init= 0.1 #default 20. carrier_rabi = RabiFlop(car_data) initial_guess = {'nbar':nbar_init, 'f_rabi':carrier_rabi.guess_f_rabi(), #changing inital guess for rabi frequency to 1/2 max 'delta':0., 'delta_fluctuations':0., 'trap_freq':trap_freq, 'sideband':sideband, 'nmax':1000, 'angle':10./360.*2*np.pi , 'rabi_type':'thermal' ,'eta': 0.05 } fit_params = {} #Put it in the fit params format for key in initial_guess.keys(): fit_params[key] = (False, False, initial_guess[key]) # fix most of the parameters fit_params['f_rabi'] = (True, False, initial_guess['f_rabi']) # fit for the rabi frequency carrier_rabi.setUserParameters(fit_params) x,y = carrier_rabi.fit() plt.figure() plt.plot(car_data[:,0], car_data[:,1],'o') # plotting raw data : excitation probability versus time plt.plot(x,y) #plotting the fit # Note: Only get_parameter_value returns the user_guess. Use get_parameter_info for autofit result. f_rabi = carrier_rabi.get_parameter_info()['f_rabi'][2][2] #autofit result (rabi frequency in Hz) return f_rabi
def plot_data(kx,omega,F,F_R,F_L,K,O): #plt.figure(4) #plt.imshow(K,extent=[omega[0],omega[-1],kx[0],kx[-1]],\ # interpolation = "nearest", aspect = "auto") #plt.xlabel('KX') #plt.colorbar() #plt.figure(5) #plt.imshow(O,extent =[omega[0],omega[-1],kx[0],kx[-1]],interpolation="nearest", aspect="auto") #plt.xlabel('omega') #plt.colorbar() plt.figure(6) pylab.subplot(1,2,1) plt.imshow(abs(F_R), extent= [omega[0],omega[-1],kx[0],kx[-1]], interpolation= "nearest", aspect = "auto") plt.xlabel('abs FFT_R') plt.colorbar() plt.subplot(1,2,2) plt.imshow(abs(F_L), extent= [omega[0],omega[-1],kx[0],kx[-1]], interpolation= "nearest", aspect = "auto") plt.xlabel('abs FFT_L') plt.colorbar() plt.figure(7) plt.subplot(2,1,1) plt.imshow(abs(F_L+F_R),extent=[omega[0],omega[-1],kx[0],kx[-1]],interpolation= "nearest", aspect = "auto") plt.xlabel('abs(F_L+F_R) reconstructed') plt.colorbar() pylab.subplot(2,1,2) plt.imshow(abs(F),extent=[omega[0],omega[-1],kx[0],kx[-1]],interpolation ="nearest",aspect = "auto") plt.xlabel('FFT of the original data') plt.colorbar() #plt.show() return
def mlr_show4( clf, RMv, yEv, disp = True, graph = True): yEv_calc = clf.predict( RMv) if len( np.shape(yEv)) == 2 and len( np.shape(yEv_calc)) == 1: yEv_calc = np.mat( yEv_calc).T r_sqr, RMSE, MAE, DAE = estimate_accuracy4( yEv, yEv_calc, disp = disp) if graph: plt.figure() ms_sz = max(min( 4000 / yEv.shape[0], 8), 1) plt.plot( yEv.tolist(), yEv_calc.tolist(), '.', ms = ms_sz) ax = plt.gca() lims = [ np.min([ax.get_xlim(), ax.get_ylim()]), # min of both axes np.max([ax.get_xlim(), ax.get_ylim()]), # max of both axes ] # now plot both limits against eachother #ax.plot(lims, lims, 'k-', alpha=0.75, zorder=0) ax.plot(lims, lims, '-', color = 'pink') plt.xlabel('Experiment') plt.ylabel('Prediction') #plt.title( '$r^2$={0:.2e}, RMSE={1:.2e}, AAE={2:.2e}'.format( r_sqr, RMSE, aae)) plt.title( '$r^2$={0:.1e},$\sigma$={1:.1e},MAE={2:.1e},DAE={3:.1e}'.format( r_sqr, RMSE, MAE, DAE)) plt.show() return r_sqr, RMSE, MAE, DAE
def heatmap(vals, size=6, aspect=1): """ Plot a heatmap from matrix data """ plt.figure(figsize=(size, size)) plt.imshow(vals, cmap="gray", aspect=aspect, interpolation="none", vmin=0, vmax=1) plt.axis("off")
def plot_jacobian(A, name, cmap= plt.cm.coolwarm, normalize=True, precision=1e-6): """ Customized visualization of jacobian matrices for observing sparsity patterns """ plt.figure() fig, ax = plt.subplots() if normalize is True: plt.imshow(A, interpolation='none', cmap=cmap, norm = mpl.colors.Normalize(vmin=-1.,vmax=1.)) else: plt.imshow(A, interpolation='none', cmap=cmap) plt.colorbar(format=ticker.FuncFormatter(fmt)) ax.spy(A, marker='.', markersize=0, precision=precision) ax.spines['right'].set_visible(True) ax.spines['bottom'].set_visible(True) ax.xaxis.set_ticks_position('top') ax.yaxis.set_ticks_position('left') xlabels = np.linspace(0, A.shape[0], 5, True, dtype=int) ylabels = np.linspace(0, A.shape[1], 5, True, dtype=int) plt.xticks(xlabels) plt.yticks(ylabels) plt.savefig(name, bbox_inches='tight', pad_inches=0.05) plt.close() return
def plotISVar(): plt.figure() plt.title('Variance minimization problem (call).\nVertical lines mark the minima.') for K in [0.6, 0.8, 1.0, 1.2]: theta = np.linspace(-0.6, 2) var = [BS.exactCallVar(K*s0, theta) for theta in theta] minth = theta[np.argmin(var)] line, = plt.plot(theta, var, label=str(K)) plt.axvline(minth, color=line.get_color()) plt.xlabel(r'$\theta$') plt.ylabel('call variance') plt.legend(title=r'$K/s_0$', loc='upper left') plt.autoscale(tight=True) plt.figure() plt.title('Variance minimization problem (put).\nVertical lines mark the minima.') for K in [0.8, 1.0, 1.2, 1.4]: theta = np.linspace(-2, 0.5) var = [BS.exactPutVar(K*s0, theta) for theta in theta] minth = theta[np.argmin(var)] line, = plt.plot(theta, var, label=str(K)) plt.axvline(minth, color=line.get_color()) plt.xlabel(r'$\theta$') plt.ylabel('put variance') plt.legend(title=r'$K/s_0$', loc='upper left') plt.autoscale(tight=True)
def entries_histogram(turnstile_weather): ''' Before we perform any analysis, it might be useful to take a look at the data we're hoping to analyze. More specifically, lets examine the hourly entries in our NYC subway data and determine what distribution the data follows. This data is stored in a dataframe called turnstile_weather under the ['ENTRIESn_hourly'] column. Why don't you plot two histograms on the same axes, showing hourly entries when raining vs. when not raining. Here's an example on how to plot histograms with pandas and matplotlib: turnstile_weather['column_to_graph'].hist() Your histograph may look similar to the following graph: http://i.imgur.com/9TrkKal.png You can read a bit about using matplotlib and pandas to plot histograms: http://pandas.pydata.org/pandas-docs/stable/visualization.html#histograms You can look at the information contained within the turnstile weather data at the link below: https://www.dropbox.com/s/meyki2wl9xfa7yk/turnstile_data_master_with_weather.csv ''' plt.figure() (turnstile_weather[turnstile_weather.rain==0].ENTRIESn_hourly).hist(bins=175) # your code here to plot a historgram for hourly entries when it is not raining (turnstile_weather[turnstile_weather.rain==1].ENTRIESn_hourly).hist(bins=175) # your code here to plot a historgram for hourly entries when it is raining plt.ylim(ymax = 45000, ymin = 0) plt.xlim(xmax = 6000, xmin = 0) return plt
def statistics_charts(self): if plt is None: return for chart in self.stats_charts: if chart["type"] == "plot": fig = plt.figure(figsize=(8, 2)) for xdata, ydata, label in chart["data"]: plt.plot(xdata, ydata, "-", label=label) plt.legend(loc="center left", bbox_to_anchor=(1, 0.5)) elif chart["type"] == "timeline": fig = plt.figure(figsize=(16, 2)) for i, (starts, stops, label) in enumerate(chart["data"]): plt.hlines([i] * len(starts), starts, stops, label=label) plt.ylim(-1, len(chart["data"])) elif chart["type"] == "bars": fig = plt.figure(figsize=(16, 4)) plt.bar(range(len(chart["data"])), chart["data"]) elif chart["type"] == "boxplot": fig = plt.figure(figsize=(16, 4)) plt.boxplot(chart["data"]) else: raise Exception("Unknown chart") png = serialize_fig(fig) yield chart["name"], html_embed_img(png)
def visualize(u1, t1, u2, t2, U, omega): plt.figure(1) plt.plot(t1, u1, 'r--o') t_fine = np.linspace(0, t1[-1], 1001) # мелкая сетка для точного решения u_e = u_exact(t_fine, U, omega) plt.hold('on') plt.plot(t_fine, u_e, 'b-') plt.legend([u'приближенное', u'точное'], loc='upper left') plt.xlabel('$t$') plt.ylabel('$u$') tau = t1[1] - t1[0] plt.title('$\\tau = $ %g' % tau) umin = 1.2*u1.min(); umax = -umin plt.axis([t1[0], t1[-1], umin, umax]) plt.savefig('tmp1.png'); plt.savefig('tmp1.pdf') plt.figure(2) plt.plot(t2, u2, 'r--o') t_fine = np.linspace(0, t2[-1], 1001) # мелкая сетка для точного решения u_e = u_exact(t_fine, U, omega) plt.hold('on') plt.plot(t_fine, u_e, 'b-') plt.legend([u'приближенное', u'точное'], loc='upper left') plt.xlabel('$t$') plt.ylabel('$u$') tau = t2[1] - t2[0] plt.title('$\\tau = $ %g' % tau) umin = 1.2 * u2.min(); umax = -umin plt.axis([t2[0], t2[-1], umin, umax]) plt.savefig('tmp2.png'); plt.savefig('tmp2.pdf')
def scatter_time_vs_s(time, norm, point_labels, title): plt.figure() size = 100 for i, l in enumerate(sorted(norm.keys())): if l is not "fbpca": plt.scatter(time[l], norm[l], label=l, marker='o', c='b', s=size) for label, x, y in zip(point_labels, list(time[l]), list(norm[l])): plt.annotate(label, xy=(x, y), xytext=(0, -80), textcoords='offset points', ha='right', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"), va='bottom', size=11, rotation=90) else: plt.scatter(time[l], norm[l], label=l, marker='^', c='red', s=size) for label, x, y in zip(point_labels, list(time[l]), list(norm[l])): plt.annotate(label, xy=(x, y), xytext=(0, 30), textcoords='offset points', ha='right', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"), va='bottom', size=11, rotation=90) plt.legend(loc="best") plt.suptitle(title) plt.ylabel("norm discrepancy") plt.xlabel("running time [s]")
def plot_wav_fft(wav_filename, desc=None): plt.clf() plt.figure(num=None, figsize=(6, 4)) sample_rate, X = scipy.io.wavfile.read(wav_filename) spectrum = np.fft.fft(X) freq = np.fft.fftfreq(len(X), 1.0 / sample_rate) plt.subplot(211) num_samples = 200.0 plt.xlim(0, num_samples / sample_rate) plt.xlabel("time [s]") plt.title(desc or wav_filename) plt.plot(np.arange(num_samples) / sample_rate, X[:num_samples]) plt.grid(True) plt.subplot(212) plt.xlim(0, 5000) plt.xlabel("frequency [Hz]") plt.xticks(np.arange(5) * 1000) if desc: desc = desc.strip() fft_desc = desc[0].lower() + desc[1:] else: fft_desc = wav_filename plt.title("FFT of %s" % fft_desc) plt.plot(freq, abs(spectrum), linewidth=5) plt.grid(True) plt.tight_layout() rel_filename = os.path.split(wav_filename)[1] plt.savefig("%s_wav_fft.png" % os.path.splitext(rel_filename)[0], bbox_inches='tight')
def scree_plot(pca_obj, fname=None): ''' Scree plot for variance & cumulative variance by component from PCA. Arguments: - pca_obj: a fitted sklearn PCA instance - fname: path to write plot to file Output: - scree plot ''' components = pca_obj.n_components_ variance = pca.explained_variance_ratio_ plt.figure() plt.plot(np.arange(1, components + 1), np.cumsum(variance), label='Cumulative Variance') plt.plot(np.arange(1, components + 1), variance, label='Variance') plt.xlim([0.8, components]); plt.ylim([0.0, 1.01]) plt.xlabel('No. Components', labelpad=11); plt.ylabel('Variance Explained', labelpad=11) plt.legend(loc='best') plt.tight_layout() if fname is not None: plt.savefig(fname) plt.close() else: plt.show() return
def bandpass_filter(data, lowcut, highcut, fs, order,filter_type='butter_worth'): """ the function performs bandpass filtering Parameters ------------- data : numpy-array input signal lowcut,highcut : integer lower and higher cutoff freuencies in Hz Fs : Integer Samping freuency in Hz order : Integer Order of butterworth filter Returns -------- out : numpy-array Filtered signal """ global once if filter_type=='butter_worth': b, a = butter_bandpass(lowcut, highcut, fs, order=order) if once==0: plt.figure(2) plotFrequency(b,a) once=1 y = filtfilt(b, a, data) return y
def show_plot(X, y, n_neighbors=10, h=0.2): # Create color maps cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF','#FFAAAA', '#AAFFAA', '#AAAAFF','#FFAAAA', '#AAFFAA', '#AAAAFF','#AAAAFF']) cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF','#FF0000','#FF0000','#FF0000','#FF0000','#FF0000','#FF0000','#FF0000',]) for weights in ['uniform', 'distance']: # we create an instance of Neighbours Classifier and fit the data. clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights) clf.fit(X, y) clf.n_neighbors = n_neighbors # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, x_max]x[y_min, y_max]. x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) plt.figure() plt.pcolormesh(xx, yy, Z, cmap=cmap_light) # Plot also the training points plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold) plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.title("3-Class classification (k = %i, weights = '%s')" % (n_neighbors, weights)) plt.show()
def _plot_histogram(self, data, number_of_devices=1, preamp_timeout=1253): if number_of_devices == 0: return data = np.array(data) plt.figure(3) plt.ioff() plt.get_current_fig_manager().window.wm_geometry("800x550+700+25") plt.clf() if number_of_devices == 1: plt.hist(data[0,:], bins=preamp_timeout, range=(1, preamp_timeout-1), color='b') elif number_of_devices == 2: plt.hist(data[0,:], bins=preamp_timeout, range=(1, preamp_timeout-1), color='r', label='JPM A') plt.hist(data[1,:], bins=preamp_timeout, range=(1, preamp_timeout-1), color='b', label='JPM B') plt.legend() elif number_of_devices > 2: raise Exception('Histogram plotting for more than two ' + 'devices is not implemented.') plt.xlabel('Timing Information [Preamp Time Counts]') plt.ylabel('Counts') plt.xlim(0, preamp_timeout) plt.draw() plt.pause(0.05)
def test_plot_acf_kwargs(): # Just test that it runs. fig = plt.figure() ax = fig.add_subplot(111) ar = np.r_[1., -0.9] ma = np.r_[1., 0.9] armaprocess = tsp.ArmaProcess(ar, ma) rs = np.random.RandomState(1234) acf = armaprocess.generate_sample(100, distrvs=rs.standard_normal) buff = BytesIO() plot_acf(acf, ax=ax) fig.savefig(buff, format='rgba') plt.close(fig) buff_with_vlines = BytesIO() fig_with_vlines = plt.figure() ax = fig_with_vlines.add_subplot(111) vlines_kwargs = {'linestyles': 'dashdot'} plot_acf(acf, ax=ax, vlines_kwargs=vlines_kwargs) fig_with_vlines.savefig(buff_with_vlines, format='rgba') plt.close(fig_with_vlines) buff.seek(0) buff_with_vlines.seek(0) plain = buff.read() with_vlines = buff_with_vlines.read() assert_(with_vlines != plain)
def show(self): stopresults = self.get_stopresults() print 'show', stopresults # print ' dir(vehicleman)',dir(vehicleman) print ' len(stopresults)', len(stopresults) if len(stopresults) > 0: i_fig = 0 plt.close("all") #i_fig +=1;fig = plt.figure(i_fig) # self.plot_waiting_person(fig) i_fig += 1 fig = plt.figure(i_fig) self.plot_waiting_person_number(fig) #i_fig +=1;fig = plt.figure(i_fig) # self.plot_waiting_person_time(fig) i_fig += 1 fig = plt.figure(i_fig) self.plot_waiting_person_number_stop(fig) i_fig += 1 fig = plt.figure(i_fig) self.plot_flow_stop(fig) #i_fig +=1;fig = plt.figure(i_fig) # self.plot_flows_compare(fig) #i_fig +=1;fig = plt.figure(i_fig) # self.plot_flows_compare_stop(fig) plt.show()