我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用pylab.ion()。
def display_wav(filename): input_data = read(filename) audio_in = input_data[1] samples = len(audio_in) fig = pylab.figure(); print samples/44100.0," seconds" k = 0 plot_data_out = [] for i in xrange(samples): plot_data_out.append(audio_in[k]/32768.0) k = k+1 pdata = numpy.array(plot_data_out, dtype=numpy.float) pylab.plot(pdata) pylab.grid(True) pylab.ion() pylab.show()
def plot_profiles(self): """ Plot TOPP profiles, e.g. for debugging. """ import pylab pylab.ion() self.topp.WriteProfilesList() self.topp.WriteSwitchPointsList() profileslist = TOPP.TOPPpy.ProfilesFromString( self.topp.resprofilesliststring) switchpointslist = TOPP.TOPPpy.SwitchPointsFromString( self.topp.switchpointsliststring) TOPP.TOPPpy.PlotProfiles(profileslist, switchpointslist) TOPP.TOPPpy.PlotAlphaBeta(self.topp) pylab.title("%s phase profile" % type(self).__name__) pylab.axis([0, 1, 0, 10])
def draw_loop(): """ Draw the graph in a loop """ global G plt.ion() # mng = plt.get_current_fig_manager() # mng.resize(*mng.window.maxsize()) plt.draw() for line in fileinput.input(): if output(line): plt.clf() nx.draw(G) plt.draw()
def _plot(self): # Called from the main thread pylab.ion() if not getattr(self, 'data_available', False): return if self.peaks is not None: for key in self.sign_peaks: for channel in self.peaks[key].keys(): self.rates[key][int(channel)] += len(self.peaks[key][channel]) pylab.scatter(self.positions[0, :], self.positions[1, :], c=self.rates[key]) pylab.gca().set_title('Buffer %d' %self.counter) pylab.draw() return
def visualiseNormObject(self): shape = (2*self.extent, 2*self.extent) pylab.ion() pylab.clf() #pylab.set_cmap("bone") pylab.hot() pylab.title("image: %s" % self.fitsFile) pylab.imshow(np.reshape(self.signPreserveNorm(), shape, order="F"), interpolation="nearest") pylab.plot(np.arange(0,2*self.extent), self.extent*np.ones((2*self.extent,)), "r--") pylab.plot(self.extent*np.ones((2*self.extent,)), np.arange(0,2*self.extent), "r--") pylab.colorbar() pylab.ylim(-1, 2*self.extent) pylab.xlim(-1, 2*self.extent) pylab.xlabel("Pixels") pylab.ylabel("Pixels") pylab.show()
def data_loop(self): import pylab fig = pylab.figure() pylab.ion() while True: fig.clear() #pylab.plot(self.t[np.where(self.on==0)]) hz = 1000000 / self.delta pylab.hist(hz, 50, range=(800, 1200)) pylab.xlim(500, 1500) pylab.ylim(0, 100) self.delta = self.delta[:0] fig.canvas.draw() fig.canvas.flush_events()
def __init__(self): pylab.ion() self.com_real = [] self.com_ref = [] self.support_areas = [] self.xlabel = "$y$ (m)" self.ylabel = "$x$ (m)" self.xlim = (-0.6, 0.1) self.ylim = (0. - 0.05, 1.4 + 0.05) self.zmp_real = [] self.zmp_ref = []
def test_discretization(nmpc, nb_steps): dT = nmpc.preview.dT pylab.ion() pylab.clf() ax = pylab.subplot(311) ax.set_color_cycle(['r', 'g', 'b']) pylab.plot( [sum(dT[:i]) for i in xrange(len(dT))], nmpc.preview.P, marker='o') pylab.plot( pylab.linspace(0., sum(dT), nb_steps + 1), [x[0:3] for x in nmpc.preview.discretize(nb_steps)], marker='s', linestyle='--') ax = pylab.subplot(312) ax.set_color_cycle(['r', 'g', 'b']) pylab.plot( [sum(dT[:i]) for i in xrange(len(dT))], nmpc.preview.V, marker='o') pylab.plot( pylab.linspace(0., sum(dT), nb_steps + 1), [x[3:6] for x in nmpc.preview.discretize(nb_steps)], marker='s', linestyle='--') ax = pylab.subplot(313) ax.set_color_cycle(['r', 'g', 'b']) pylab.plot( [sum(dT[:i]) for i in xrange(len(dT))], nmpc.preview.Z, marker='o') pylab.plot( pylab.linspace(0., sum(dT), nb_steps + 1), [x[6:9] for x in nmpc.preview.discretize(nb_steps)], marker='s', linestyle='--')
def test_dT_impact(xvals, f, nmpc, sim, start=0.1, end=0.8, step=0.02, ymax=200, sample_size=100, label=None): """Used to generate Figure XX of the paper.""" c = raw_input("Did you remove iter/time caps in IPOPT settings? [y/N] ") if c.lower() not in ['y', 'yes']: print "Then go ahead and do it." return stats = [Statistics() for _ in xrange(len(xvals))] fails = [0. for _ in xrange(len(xvals))] pylab.ion() pylab.clf() for (i, dT) in enumerate(xvals): f(dT) for _ in xrange(sample_size): nmpc.on_tick(sim) if 'Solve' in nmpc.nlp.return_status: stats[i].add(nmpc.nlp.solve_time) else: # max CPU time exceeded, infeasible problem detected, ... fails[i] += 1. yvals = [1000 * ts.avg if ts.avg is not None else 0. for ts in stats] yerr = [1000 * ts.std if ts.std is not None else 0. for ts in stats] pylab.bar( xvals, yvals, width=step, yerr=yerr, color='y', capsize=5, align='center', error_kw={'capsize': 5, 'elinewidth': 5}) pylab.xlim(start - step / 2, end + step / 2) pylab.ylim(0, ymax) pylab.grid(True) if label is not None: pylab.xlabel(label, fontsize=24) pylab.ylabel('Comp. time (ms)', fontsize=20) pylab.tick_params(labelsize=16) pylab.twinx() yfails = [100. * fails[i] / sample_size for i in xrange(len(xvals))] pylab.plot(xvals, yfails, 'ro', markersize=12) pylab.plot(xvals, yfails, 'r--', linewidth=3) pylab.xlim(start - step / 2, end + step / 2) pylab.ylabel("Failure rate [%]", fontsize=20) pylab.tight_layout()
def wave_plotting(sonic, block=False): if isinstance(sonic.wave_bin_data, list): bin_buffer = bytearray() for data in sonic.wave_bin_data: bin_buffer.extend(data) sonic.wave_bin_data = bytes(bin_buffer) elif not isinstance(sonic.wave_bin_data, bytes): raise Exception("Type of bin_data need bytes!") #????????????????????????????????????? wave_data = numpy.fromstring(sonic.wave_bin_data, dtype=number_type.get(sonic.sample_width)) #???????wave_data??????short??????????????????????? # ??????????????????LRLRLRLR....LR?L??????????R????????????wave_data?sharp wave_data.shape = (sonic.sample_length, sonic.channels) wave_data = wave_data.T time = numpy.arange(0, sonic.sample_length) * (1.0 / sonic.sample_frequency) # ???? colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] pylab.figure() for index in range(0, sonic.channels): pylab.subplot(sonic.channels, 1, index + 1) pylab.plot(time, wave_data[index], colors[index % len(colors)]) pylab.ylabel("quantization") pylab.xlabel("time (seconds)") pylab.ion() if block: pylab.ioff() pylab.show()
def hinton(W, bg='grey', facecolors=('w', 'k')): """Draw a hinton diagram of the matrix W on the current pylab axis Hinton diagrams are a way of visualizing numerical values in a matrix/vector, popular in the neural networks and machine learning literature. The area occupied by a square is proportional to a value's magnitude, and the colour indicates its sign (positive/negative). Example usage: R = np.random.normal(0, 1, (2,1000)) h, ex, ey = np.histogram2d(R[0], R[1], bins=15) hh = h - h.T hinton.hinton(hh) """ M, N = W.shape square_x = np.array([-.5, .5, .5, -.5]) square_y = np.array([-.5, -.5, .5, .5]) ioff = False if plt.isinteractive(): plt.ioff() ioff = True plt.fill([-.5, N - .5, N - .5, - .5], [-.5, -.5, M - .5, M - .5], bg) Wmax = np.abs(W).max() for m, Wrow in enumerate(W): for n, w in enumerate(Wrow): c = plt.signbit(w) and facecolors[1] or facecolors[0] plt.fill(square_x * w / Wmax + n, square_y * w / Wmax + m, c, edgecolor=c) plt.ylim(-0.5, M - 0.5) plt.xlim(-0.5, M - 0.5) if ioff is True: plt.ion() plt.draw_if_interactive()
def __init__(self, **kwargs): super(self.__class__, self).__init__(**kwargs) self.update(**plt.rcParamsDefault) plt.ion()
def __init__(self, fontSize=16., lineWidth=1., labelSize=None, tickmajorsize=10, tickminorsize=5, figsize=(8, 6)): if labelSize is None: labelSize = fontSize + 2 rcParams = {} rcParams['figure.figsize'] = figsize rcParams['lines.linewidth'] = lineWidth rcParams['grid.linewidth'] = lineWidth rcParams['font.sans-serif'] = ['Helvetica'] rcParams['font.serif'] = ['Helvetica'] rcParams['font.family'] = ['Times New Roman'] rcParams['font.size'] = fontSize rcParams['font.family'] = 'serif' rcParams['font.weight'] = 'bold' rcParams['axes.linewidth'] = lineWidth rcParams['axes.labelsize'] = labelSize rcParams['legend.borderpad'] = 0.1 rcParams['legend.markerscale'] = 1. rcParams['legend.fancybox'] = False rcParams['text.usetex'] = True rcParams['image.aspect'] = 'auto' rcParams['ps.useafm'] = True rcParams['ps.fonttype'] = 3 rcParams['xtick.major.size'] = tickmajorsize rcParams['xtick.minor.size'] = tickminorsize rcParams['ytick.major.size'] = tickmajorsize rcParams['ytick.minor.size'] = tickminorsize rcParams['text.latex.preamble'] = ["\\usepackage{amsmath}"] super(self.__class__, self).__init__(**rcParams) plt.ion()
def __init__(self, fontSize=None, labelSize=None): rcParams = {} if fontSize is not None: if labelSize is None: labelSize = fontSize rcParams['font.sans-serif'] = ['Helvetica'] rcParams['font.serif'] = ['Helvetica'] rcParams['font.family'] = ['Times New Roman'] rcParams['font.size'] = fontSize rcParams["axes.labelsize"] = labelSize rcParams["axes.titlesize"] = labelSize rcParams["xtick.labelsize"] = labelSize rcParams["ytick.labelsize"] = labelSize rcParams["legend.fontsize"] = fontSize rcParams['font.family'] = 'serif' rcParams['font.weight'] = 'bold' rcParams['axes.labelsize'] = labelSize rcParams['text.usetex'] = True rcParams['ps.useafm'] = True rcParams['ps.fonttype'] = 3 rcParams['text.latex.preamble'] = ["\\usepackage{amsmath}"] super(self.__class__, self).__init__(**rcParams) plt.ion()
def plot_marginals(state_space,p,name,t,labels = False,interactive = False): import matplotlib import matplotlib.pyplot as pl if interactive == True: pl.ion() pl.clf() pl.suptitle("time: "+ str(t)+" units") #print("time : "+ str(t)) D = state_space.shape[1] for i in range(D): marg_X = np.unique(state_space[:,i]) A = np.where(marg_X[:,np.newaxis] == state_space[:,i].T[np.newaxis,:],1,0) marg_p = np.dot(A,p) pl.subplot(int(D/2)+1,2,i+1) pl.plot(marg_X,marg_p) pl.yticks(np.linspace(np.amin(marg_p), np.amax(marg_p), num=3)) pl.axvline(np.sum(marg_X*marg_p),color= 'r') pl.axvline(marg_X[np.argmax(marg_p)],color='g') if labels == False: pl.xlabel("Specie: " + str(i+1)) else: pl.xlabel(labels[i]) if interactive == True: pl.draw() else: pl.tight_layout() pl.show()
def visualiseObject(self, cmap="hot"): pylab.ion() #pylab.set_cmap("gray") pylab.gray() pylab.title("image: %s" % self.fitsFile) pylab.imshow(self.getObject(), interpolation="nearest", cmap=cmap) pylab.colorbar() pylab.ylim(-1, 2*self.extent) pylab.xlim(-1, 2*self.extent) pylab.xlabel("Pixels") pylab.ylabel("Pixels") pylab.show()
def main(args): # seed PRNG np.random.seed(args.seed) pl.ion() if args.mode.startswith("test_"): test_models(args) else: idim = None # if args.mode.startswith("type03_1"): # idim = 3 # print "args.goal_sample_interval", args.goal_sample_interval # initialize experiment inf = ActiveInferenceExperiment( args.mode, args.model, args.numsteps, idim = idim, environment_str = args.environment, goal_sample_interval = args.goal_sample_interval, e2pmodel = args.e2pmodel, saveplots = SAVEPLOTS) # run experiment inf.run() # wait for plots to be closed pl.ioff() pl.show()
def __init__(self, modules, vis): pylab.style.use('ggplot') self.node_size_add = 1.5 self.init_node_size = 0.1 self.edge_weight_add = 0.1 self.init_edge_weight = 0.0 self.fixed_path = [[None] * 3] * 3 self.fixed_color = None self.fixed_weight = 6.4 pylab.ion() self.graph = nx.Graph() self.node_ids = {} node_num = 0 self.vis = vis if not self.vis: print("visualizing graph disabled!!") for layer_num, one_layer in enumerate(modules): for module_num in range(one_layer): self.graph.add_node(node_num, Position=(10 * layer_num, 10 * module_num), size = self.init_node_size) self.node_ids[(layer_num, module_num)] = node_num node_num += 1 pylab.show()
def load(self): self.output_attribute = self.config.get('output_attribute', 'prom') self.feats = clist(self.config.get('features', ["F0", "Gain","dur"])) self.prom_weights = clist(self.config.get('prom_weights', [0.4, 0.4, 0.2])) self.param_dir = self.config.get('param_dir', 'acoustic') self.frame_len = self.config.get('frame_length', 5) self.level = self.config.get('level','//token[@token_class=\"word\"]') self.scale_distance = float(self.config.get('scale_distance',0.5)) self.num_octaves = int(self.config.get('num_octaves', 12)) self.wscale = 10 ## this is set in training self.variances = {} ## this is set in training if CWT_DEBUG: pylab.ion() self.fzero_feat = '' for f in self.feats: if 'f0' in f.lower(): self.fzero_feat = f assert self.fzero_feat != '', 'ProminenceLabeller needs a feature containing f0!' self.dynamic_size_wavelet = str2bool(self.config.get('dynamic_size_wavelet', 'no')) self.use_stress_track = str2bool(self.config.get('use_stress_track', 'no')) self.stress_xpath = self.config.get('stress_xpath', '//syllable[@stress="stress_1"]') ## for plotting/debugging:- self.text_attribute = self.config.get('text_attribute', 'text')
def main(): from argparse import ArgumentParser p = ArgumentParser() p.add_argument('--grammar', choices=('both', 'medium', 'big')) p.add_argument('--rollout', choices=('CP', 'DP')) args = p.parse_args() CP = ('evalb_avg', 'pops') DP = ('expected_recall_avg', 'mask') GRAMMARS = ['medium', 'big'] if args.grammar == 'both' else [args.grammar] ACC, RUN = DP if args.rollout == 'DP' else CP pl.ion() fig1, ax1 = pl.subplots(nrows=3, #sharex=True, ncols=2, figsize=(10,10)) for i in range(3): for j in range(2): ax1[i,j].grid(False) fig2, ax2 = pl.subplots(nrows=1, #sharex=True, ncols=2, figsize=(10,5)) for i, GRAMMAR in enumerate(GRAMMARS): plot(GRAMMAR, ACC, RUN, ax=ax1[:,i], col=i) plot2(GRAMMAR, ACC, RUN, ax=ax2[i], col=i) fig1.tight_layout() fig2.tight_layout() pl.ioff() pl.show()
def check_matplotlib_backends(): # from http://stackoverflow.com/questions/5091993/list-of-all-available-matplotlib-backends # get the directory where the backends live backends_dir = os.path.dirname(matplotlib.backends.__file__) # filter all files in that directory to identify all files which provide a backend backend_fnames = filter(is_backend_module, os.listdir(backends_dir)) backends = [backend_fname_formatter(fname) for fname in backend_fnames] print("supported backends: \t" + str(backends)) # validate backends backends_valid = [] for b in backends: try: plt.switch_backend(b) backends_valid += [b] except: continue print("valid backends: \t" + str(backends_valid)) # try backends performance for b in backends_valid: pylab.ion() try: plt.switch_backend(b) pylab.clf() tstart = time.time() # for profiling x = range(0,2*pylab.pi,0.01) # x-array line, = pylab.plot(x,pylab.sin(x)) for i in range(1,200): line.set_ydata(pylab.sin(x+i/10.0)) # update the data pylab.draw() # redraw the canvas print(b + ' FPS: \t' , 200/(time.time()-tstart)) pylab.ioff() except: print(b + " error :(")
def image_loop(self, decay, display_mode): import pylab fig = pylab.figure() pylab.ion() img = pylab.imshow(self.image, vmax=1, vmin=-1, interpolation='none', cmap='binary') pylab.xlim(0, 127) pylab.ylim(127, 0) regions = {} if self.count_spike_regions is not None: for k, v in self.count_spike_regions.items(): minx, miny, maxx, maxy = v rect = pylab.Rectangle((minx - 0.5, miny - 0.5), maxx - minx, maxy - miny, facecolor='yellow', alpha=0.2) pylab.gca().add_patch(rect) regions[k] = rect if self.track_periods is not None: colors = ([(0,0,1), (0,1,0), (1,0,0), (1,1,0), (1,0,1)] * 10)[:len(self.p_y)] scatter = pylab.scatter(self.p_x, self.p_y, s=50, c=colors) else: scatter = None while True: img.set_data(self.image) for k, rect in regions.items(): alpha = self.get_spike_rate(k) * 0.5 alpha = min(alpha, 0.5) rect.set_alpha(0.05 + alpha) if scatter is not None: scatter.set_offsets(np.array([self.p_x, self.p_y]).T) c = [(r,g,b,min(self.track_certainty[i],1)) for i,(r,g,b) in enumerate(colors)] scatter.set_color(c) if display_mode == 'quick': # this is much faster, but doesn't work on all systems fig.canvas.draw() fig.canvas.flush_events() else: # this works on all systems, but is kinda slow pylab.pause(0.001) self.image *= decay
def images_loop(self, decays, display_mode): import pylab fig = pylab.figure() num_images = len(decays) pylab.ion() imgs = [] for i in range(len(decays)): fig.add_subplot(1, num_images, i+1) imgs.append( pylab.imshow(self.images[i], vmax=1, vmin=-1, interpolation='none', cmap='binary') ) pylab.xlim(0, 127) pylab.ylim(127, 0) regions = {} if self.count_spike_regions is not None: for k, v in self.count_spike_regions.items(): minx, miny, maxx, maxy = v rect = pylab.Rectangle((minx - 0.5, miny - 0.5), maxx - minx, maxy - miny, facecolor='yellow', alpha=0.2) pylab.gca().add_patch(rect) regions[k] = rect if self.track_periods is not None: colors = ([(0,0,1), (0,1,0), (1,0,0), (1,1,0), (1,0,1)] * 10)[:len(self.p_y)] scatter = pylab.scatter(self.p_x, self.p_y, s=50, c=colors) else: scatter = None while True: for i,d in enumerate(decays): imgs[i].set_data(self.images[i]) for k, rect in regions.items(): alpha = self.get_spike_rate(k) * 0.5 alpha = min(alpha, 0.5) rect.set_alpha(0.05 + alpha) if scatter is not None: scatter.set_offsets(np.array([self.p_x, self.p_y]).T) c = [(r,g,b,min(self.track_certainty[i],1)) for i,(r,g,b) in enumerate(colors)] scatter.set_color(c) if display_mode == 'quick': # this is much faster, but doesn't work on all systems fig.canvas.draw() fig.canvas.flush_events() else: # this works on all systems, but is kinda slow pylab.pause(0.001) for i,d in enumerate(decays): self.images[i] *= d
def basic_sgd_demo(train_X, train_Y, val_X, val_Y, test_X, test_Y, config): # This is to demonstrate the process of learning a simple one hidden-layer NN # Input kernel: linear # Num hidden layer: 1 # Learning method: SGD # Parse param from config lr = config['lr'] num_epoch = config['num_epoch'] num_train_per_class = config['num_train_per_class'] num_hidden_node = config['num_hidden_node'] display_rate = config['display_rate'] activation_function_type = config['activation_function'] num_train_sample = train_X.shape[0] num_feature = train_X.shape[1] num_class = train_Y.shape[1] # Create a weight matrix of shape (2, num_hidden_node) W1 = rng.randn(num_feature, num_hidden_node) b1 = rng.randn(1, num_hidden_node) # Create output weight W2 = rng.randn(num_hidden_node, num_class) b2 = rng.randn(1, num_class) num_train_sample = 1 pylab.ion() pylab.show() all_cost = [] for i in range(0, num_epoch): # Calculate the loss a1 = np.dot(train_X, W1) + b1 z1 = activation_function(a1, activation_function_type) a2 = np.dot(z1, W2) + b2 J = softmax_log_loss(a2, train_Y) # Doing backprop print('[Epoch %d] Train loss: %f' % (i, J)) dJ_dW1, dJ_db1, dJ_dW2, dJ_db2 = get_grad(train_X, train_Y, W1, b1, W2, b2, config) # NumericalGradientCheck(train_X, train_Y, W1, b1, W2, b2, dJ_db1) W1 = W1 - dJ_dW1 * lr b1 = b1 - dJ_db1 * lr W2 = W2 - dJ_dW2 * lr b2 = b2 - dJ_db2 * lr all_cost.append(J) if (i % display_rate == 0): config['train_method'] = 'sgd' train_draw(train_X, train_Y, W1, b1, W2, b2, config, all_cost, i, J) bp = 1