def plot(self,datastr): data=json.loads(unicode(datastr)) if "data" in data and "graphs" in data["data"]: graphdata= data["data"]["graphs"] if len(graphdata)<len(self.canvases): for i in reversed(range(self.layout.count())): self.layout.itemAt(i).widget().deleteLater() self.canvases=[] self.figures=[] for maskindex,set in enumerate(graphdata): if len(self.canvases)<=maskindex: self.figures.append(plt.figure( )) canvas=FigureCanvas(self.figures[maskindex]) canvas.setParent(self) self.canvases.append(canvas) self.layout.addWidget(self.canvases[maskindex]) figure=self.figures[maskindex] figure.clf() figure.set_frameon(False) ax=figure.add_subplot(111) ax.set_yscale('symlog') ax.set_xlabel(set["columnLabels"][0],fontsize=16) ax.set_ylabel(set["columnLabels"][1],fontsize=16) ax.set_title( set["kind"]+" "+data["data"]['filename']) ax.patch.set_alpha(0) x=np.array(set["array"][0])[:] y=np.array(set["array"][1])[:] e=np.array(set["array"][2])[:] ppl.plot(ax,x,y,lw=1.0) ppl.fill_between(ax,x,y-e,y+e) plt.subplots_adjust(bottom=0.2) self.canvases[maskindex].draw()
def generate_chart(self, how_many=-1): try: assert(os.path.exists(os.path.join(self.log_dir, self.log_filename))) except AssertionError: self.logger.log(self.logger.red(os.path.join(self.log_dir, self.log_filename) + ' not found!')) return data = np.loadtxt(os.path.join(self.log_dir, self.log_filename)) if (how_many != -1): data = data[-1 * how_many:] x_labels = range(data.shape[0]) fig, ax = plt.subplots(1) plt.title('GPU memory usage') plt.xlabel('Time') plt.ylabel('Memory') ppl.plot(ax, x_labels, data, '-r', label='memory') ppl.legend(ax, loc='lower right') chart_name = os.path.join(self.log_dir, self.log_filename[:-4] + '.png') fig.savefig(chart_name, bbox_inches='tight') plt.close(fig) self.logger.log('chart saved at', chart_name)
def findmaxstd(std, window, precentage): #window must be an odd number if window % 2 == 0: window += 1 #print 'Interval for finding max for moving standard deviation should be an odd number. window += 1' findmaxstdzeros = np.zeros(window - 1) findmaxstddiff = np.diff(std[np.nonzero(std)[0]]) findmaxstddiffwhole = np.hstack((findmaxstdzeros, findmaxstddiff, findmaxstdzeros)) fig, ax = plt.subplots(1) ppl.plot(np.arange(len(findmaxstddiffwhole)), findmaxstddiffwhole) ppl.plot(np.arange(len(findmaxstddiffwhole)), [0] * len(findmaxstddiffwhole)) fig.savefig(pathfilename + '_window_' + str(interval) + '_diff_' + '.png',dpi=500) plt.close() #One index number is lost due to differentiation #Clean the zeros and add them again to prevent the huge change between #the last zero and the first standard deviation peak = np.array([]) stdamp = np.array([]) for check in range(window - 1, len(findmaxstddiffwhole) - (window - 1)): firsthalf = np.all(findmaxstddiffwhole[check - (window-1)*precentage: check] > 0) secondhalf = np.all(findmaxstddiffwhole[check: check + (window - 1)*precentage +1] <= 0) #Compensate for the loss of one number due to the differentiation if (firsthalf & secondhalf): if std[check] != 0: peak = np.append(peak, check) stdamp = np.append(stdamp, std[check]) return peak, stdamp
def plotXVG(ax, filename): infile = open(filename, 'r') datax = [] datay = [] legends = [] first = True for line in infile: if line.find('@') != -1 or line.find('#') != -1: sl = line.split() if len(sl) < 3: continue if sl[2] == 'legend': legends.append(sl[3]) continue datax.append(float(line.split()[0])) if first: for i in range(1, len(line.split())): datay.append([]) first = False for i in range(1, len(line.split())): datay[i - 1].append(float(line.split()[i])) infile.close() for i in range(0, len(datay)): if len(legends) == len(datay): ppl.plot(ax, datax, datay[i], linewidth=2.0, label=legends[i]) # ax.plot(datax,datay[i],linewidth=2.0,label=legends[i]) else: # ax.plot(datax,datay[i],linewidth=2.0) ppl.plot(ax, datax, datay[i], linewidth=2.0)
def main(): path_review = '/Users/Constance/Desktop/data_incubator/reviews_retoken.csv' path_feature = '/Users/Constance/Desktop/data_incubator/mutual_features.csv' IDF = Calculation_IDF(path_review,path_feature) TF_IDF_review = TF_IDF(path_review,IDF) path_product = '/Users/Constance/Desktop/data_incubator/product_info_retoken.csv' TF_IDF_product = TF_IDF_product_info(path_product,IDF) cosine_score = [] for i in range(len(TF_IDF_review)): numerator = 0 for token in TF_IDF_product: if token in TF_IDF_review[i]: numerator += TF_IDF_review[i][token]*TF_IDF_product[token] if numerator>0: denominator =np.sqrt(sum(np.asarray(TF_IDF_review[i].values())**2)) score = numerator/denominator else: score = 0.0 cosine_score.append(score) pplot.plot(sorted(cosine_score,reverse=True)) mplot.axvline(1000) mplot.xlabel("Reviews") mplot.ylabel("Cosine Score") mplot.title('Cosine Similarity Curve (without consider norm of product)') mplot.savefig('plot_cosine_score.png') with open('cosine_score.csv','w') as output: writer = csv.writer(output) for item in cosine_score: writer.writerow([item])
def new_tables_cdf(): fig, ax = plt.subplots(1) data = read_csv(['query_number', 'num_new_tables'], True) c = data['num_new_tables'].astype(float) c /= sum(c) q = data['query_number'].astype(float) q /= q[-1] ppl.plot(ax, q, np.cumsum(c), label="SDSS", color=cs[0], linewidth=2, ls='-.', drawstyle='steps-post') ppl.scatter(ax, q, np.cumsum(c), color=cs[0], marker="o", s=100) data = read_csv(['table_coverage'], False) c = data['tables'].astype(float) c /= c[-1] q = data['query_id'].astype(float) q /= q[-1] ppl.plot(ax, q, c, label="SQLShare", color=cs[1], linewidth=2, ls='-.', drawstyle='steps-post') ppl.scatter(ax, q, c, color=cs[1], marker="o", s=100) ppl.legend(ax, loc='lower right') plt.gca().yaxis.set_major_formatter(formatter) ax.set_xlabel('Query number') ax.set_ylabel('% of newly used table') ax.set_ylim(0, 1.01) ax.set_xlim(0, 1) ax.yaxis.grid() plt.show() fig.savefig('num_new_tables.pdf', format='pdf', transparent=True) fig.savefig('num_new_tables.png', format='png', transparent=True)
def plot_clusters(clusters, candidates, bounds, vloc, hulls, shrink=0.9): """Plot all `clusters` among `candidates` with the `bounds` of the city (or at least `shrink` of them). Also plot convex `hulls` of gold areas if provided.""" xbounds, ybounds = bounds unique_labels = len(clusters) clustered = set().union(*map(list, clusters)) noise = list(candidates.difference(clustered)) if unique_labels > 5: colors = mpl.cm.Spectral(np.linspace(0, 1, unique_labels+1)) else: colors = [gray, red, green, blue, orange] plt.figure(figsize=(20, 15)) for k, indices, col in zip(range(unique_labels+1), [noise]+clusters, colors): k -= 1 if k == -1: col = 'gray' ppl.scatter(vloc[indices, 0], vloc[indices, 1], s=35 if k != -1 else 16, color=col, alpha=0.8 if k != -1 else 0.6, label='noise' if k == -1 else 'cluster {}'.format(k+1)) hulls = hulls or [] for idx, hull in enumerate(hulls): first_again = range(len(hull))+[0] ppl.plot(hull[first_again, 0], hull[first_again, 1], '--', c=ppl.colors.almost_black, lw=1.0, alpha=0.9, label='gold region' if idx == 0 else None) plt.xlim(shrink*xbounds) plt.ylim(shrink*ybounds) ppl.legend()
def plot(self, datastr): data = json.loads(unicode(datastr)) if "data" in data and "graphs" in data["data"]: graphdata = data["data"]["graphs"] if len(graphdata) < len(self.canvases): for i in reversed(range(self.layout.count())): self.layout.itemAt(i).widget().deleteLater() self.canvases = [] self.figures = [] for maskindex, set in enumerate(graphdata): if len(self.canvases) <= maskindex: self.figures.append(plt.figure()) canvas = FigureCanvas(self.figures[maskindex]) canvas.setParent(self) self.canvases.append(canvas) self.layout.addWidget(self.canvases[maskindex]) figure = self.figures[maskindex] figure.clf() figure.set_frameon(False) ax = figure.add_subplot(111) ax.set_yscale('symlog') ax.set_xlabel(set["columnLabels"][0], fontsize=16) ax.set_ylabel(set["columnLabels"][1], fontsize=16) ax.set_title(set["kind"] + " " + data["data"]['filename']) ax.patch.set_alpha(0) x = np.array(set["array"][0])[:] y = np.array(set["array"][1])[:] e = np.array(set["array"][2])[:] ppl.plot(ax, x, y, lw=1.0) ppl.fill_between(ax, x, y - e, y + e) plt.subplots_adjust(bottom=0.2) self.canvases[maskindex].draw()
def plot_lambda_effect(): x_training = np.linspace(0, 1, 40) y_training = exercise_11.sample_gaussian(exercise_11.f, 0, 0.3, x_training) # Loop through lambda's. weights_vector = [] for l in range(-5, 15): # Zip x and y pairs together, fit a curve. weights_vector.append(PolCurFit(zip(x_training, y_training), 9, labda=10**-l)) x_test = np.linspace(0, 1, 100) y_test = exercise_11.sample_gaussian(exercise_11.f, 0, 0.3, x_test) rmse_training = np.zeros((len(weights_vector))) rmse_test = np.zeros((len(weights_vector))) i = 0 weights_vector = weights_vector[::-1] for weights in weights_vector: poly_output_100 = [eval_polynomial(weights, x) for x in np.linspace(0, 1, 100)] poly_output_40 = [eval_polynomial(weights, x) for x in np.linspace(0, 1, 40)] rmse_training[i] = np.sqrt(np.mean((poly_output_40 - y_training)**2)) rmse_test[i] = np.sqrt(np.mean((poly_output_100 - y_test)**2)) i = i + 1 fig, ax = plt.subplots(1) ppl.plot(ax, np.arange(-14, 6), rmse_training, linewidth=0.75, label='RMSE on training set') ppl.plot(ax, np.arange(-14, 6), rmse_test, linewidth=0.75, label='RMSE on test set') ppl.legend(ax, loc='upper right', ncol=2) ax.set_xlabel('$log_{10} \lambda$') ax.set_ylabel('RMSE') ax.set_title('RMSE for the polynomial approximation of sine function') fig.savefig('exercise_lambda_rmse_plot40.pdf')
def add_content(subcluster, content, suffix): fig, ax = plt.subplots(1, figsize=(6.5,2.5)) for childax in ax.get_children(): if isinstance(childax, mpl.spines.Spine): childax.set_color('#aaaaaa') for i in ax.get_xticklabels(): i.set_color('#aaaaaa') for i in ax.get_yticklabels(): i.set_color('#aaaaaa') subcluster = sorted(subcluster, key=lambda t: max(t[1:].astype(float)), reverse=True)[:10] subcluster = np.array(subcluster) words = subcluster[:,0] ys = subcluster[:,1:].astype(float) mean = [np.mean(ys[:,i]) for i in xrange(ys.shape[1])] ys = ys.transpose() ax.set_ylim(top=max(10, max(map(max, ys)))) ppl.plot(ax, xs, ys, alpha=0.3, color="#7777ee") ppl.plot(ax, xs, mean, alpha=1, color="black") fname = './plots/%s_%s.png' % (conf, suffix) fig.savefig(fname, format='png') maxes = map(max, ys) idx = maxes.index(max(maxes)) content.append(('', words, fname, idx))
def plot_profile(self, fig, ax, x, y, xlabel='', ylabel='', axis_font={}, tick_font={}, scatter=False, **kwargs): if not axis_font: axis_font = axis_font_default if scatter: ppl.scatter(ax, x, y, **kwargs) else: ppl.plot(ax, x, y, **kwargs) if xlabel: ax.set_xlabel(xlabel.replace("_", " "), labelpad=5, **axis_font) if ylabel: ax.set_ylabel(ylabel.replace("_", " "), labelpad=11, **axis_font) if tick_font: ax.tick_params(**tick_font) ax.xaxis.set_label_position('top') # this moves the label to the top ax.xaxis.set_ticks_position('top') ax.xaxis.get_major_locator()._nbins = 5 ax.grid(True) plt.tight_layout()
def plot(): # Use 10 sample points for the noisy signal. x_training = np.linspace(0, 1, 10) y_training = sample_gaussian(f, 0, 0.3, x_training) # Use 1000 sample points for the real signal. Needs to be higher for higher resolution images. x_real = np.linspace(0, 1, 1000) y_real = [f(x_sample) for x_sample in x_real] fig, ax = plt.subplots(1) ppl.plot(ax, x_training, y_training, '-o', linewidth=0.75, label='Observations') ppl.plot(ax, x_real, y_real, linewidth=0.75, label='Function') ppl.legend(ax, loc='upper right', ncol=2) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_title('Noisy observations versus real function') fig.savefig('exercise_11.pdf')
def make_image(kic): output_name = os.path.join('.', 'data', os.extsep.join(['{:d}'.format(kic), 'png'])) output_name = os.path.abspath(output_name) if os.path.exists(output_name): return output_name time, flux = get_light_curves(kic) plt.figure() for t, f in zip(time, flux): idx = np.isfinite(f) & np.isfinite(t) ppl.plot(t[idx], f[idx]) plt.title('kic=%d' % kic) plt.xlabel('Time', color='w') plt.ylabel('Flux', color='w') plt.axis('tight') plt.tight_layout() try: os.makedirs(os.path.dirname(output_name)) except: pass plt.savefig(output_name, transparent=True) return output_name
def draw_ots_sens_scale(): all_results_scale = np.loadtxt('expdata/fig_ots_scale_auc02_new.txt') exam3 = plt.imread('expdata/face3.png') plt.imshow(exam3) t = plt.axis('off') plt.subplots(1) rs = np.arange(0.8, 1.22, 0.04) s_ids = np.argsort(-all_results_scale[:, 5]) colormap = plt.cm.spectral #rainbow plt.gca().set_color_cycle([colormap(i) for i in np.linspace(0, 0.99, 11)]) labels = [ 'TREES', 'SDM', 'RCPR', 'CCNF', 'CFAN', 'GNDPM', 'IFA', 'LBF', 'TCDCN', 'DRMF', 'CFSS' ] for s in s_ids: ppl.plot(rs, all_results_scale[s], lw=2, marker='o', label=labels[s], alpha=0.8) ppl.plot([1, 1], [0.15, 0.078], c=[0.5, 0.5, 0.5], alpha=0.5, lw=3) plt.xlabel('Scales') plt.ylabel('AUC$_{0.2}$') plt.legend(ncol=2, loc=8) plt.xlim((0.8, 1.2)) plt.grid()
def autocorr_lorenz(x, variable): z = autocorr(x) plt.subplot(122) ppl.plot(t, z / float(z.max()), '-', label=variable) plt.xlabel('time(s)') plt.legend() plt.title('Autocorelation')
def plotpopendiff(filename, result, peak, popendiffstdamp): fig, ax = plt.subplots(1) plotpeak = np.hstack((peak.astype(np.int64), len(result)))[::-1] popen = popenpeak(result, peak)[::-1] popenoriginal = np.empty(len(result)) for index in range(len(plotpeak)): popenoriginal[:plotpeak[index]] = popen[index] plotpeak = plotpeak[::-1] popenfit = copy.copy(popenoriginal) for index in range(len(popendiffstdamp)-1): popenfit[plotpeak[index]-1] = (popenoriginal[plotpeak[index]-1] + popenoriginal[plotpeak[index]])/2 - popendiffstdamp[index]/2 popenfit[plotpeak[index]] = (popenoriginal[plotpeak[index]-1] + popenoriginal[plotpeak[index]])/2 + popendiffstdamp[index]/2 ppl.plot(ax, np.arange(len(popenfit)), popenfit, label='calculated', linewidth=1) ppl.plot(ax, np.arange(len(popenoriginal)), popenoriginal, label='original', linewidth=2) if np.all(popenoriginal[len(popenoriginal)/2:] > (max(popenoriginal)/2)): ppl.legend(ax, loc='lower right') elif np.all(popenoriginal[len(popenoriginal)/2:] < (max(popenoriginal)/2)): ppl.legend(ax, loc='upper right') elif np.all(popenoriginal[:len(popenoriginal)/2] < (max(popenoriginal)/2)): ppl.legend(ax, loc='upper left') elif np.all(popenoriginal[:len(popenoriginal)/2] > (max(popenoriginal)/2)): ppl.legend(ax, loc='lower left') ax.set_title(savefilename + ' window ' + str(interval) + ' original Popen and fit') fig.savefig(pathfilename + '_window_' + str(interval) + '_original_Popen_fit' + '.png',dpi=500) plt.close() print savefilename, 'Popen and calculated Popen plot saved.'
def all(): for i in range(len(names)): name = names[i] label = labels[i] type = types[i] data = np.loadtxt('output/'+target+'/data/fit_%s.txt' % name) time = data[:, 0] counts = data[:, 1] fit = data[:, 2] # just plotting stuff now plt.close() ppl.plot(time, counts, linewidth=4, alpha=0.3) ppl.plot(time, fit, label=type, linewidth=2) plt.text(5, 0.1, label) plt.xlim([-15, 15]) plt.ylim(ymin=0) plt.legend(fontsize=8) xlabel = plt.xlabel("$\\tau (ns)$") plt.ylabel("$g^{(2)}(\\tau)$") plt.savefig('output/'+target+'/plots/%s.png' % name, bbox_extra_artists=[xlabel], bbox_inches='tight')
def originalplot(pathfilename, savefilename, amp, dwell): originaldata = convertoriginal(amp, dwell) print 'length of the original data:', len(originaldata)/9, 'ms' ppl.plot(originaldata, color='k') plt.title(savefilename + '_original') plt.savefig(pathfilename + '_original.png', dpi = 300) print 'saved original figure', savefilename plt.close()
def plot_time_series(self, fig, is_timeseries, ax, x, y, fill=False, title='', xlabel='', ylabel='', title_font={}, axis_font={}, tick_font={}, scatter=False, qaqc=[], events={}, **kwargs): if not title_font: title_font = title_font_default if not axis_font: axis_font = axis_font_default if scatter: ppl.scatter(ax, x, y, **kwargs) else: h = ppl.plot(ax, x, y, **kwargs) if is_timeseries: self.get_time_label(ax, x) fig.autofmt_xdate() else: ax.set_xlabel(xlabel.replace("_", " "), **axis_font) if ylabel: ax.set_ylabel(ylabel.replace("_", " "), **axis_font) if title: ax.set_title(title.replace("_", " "), **title_font) ax.grid(True) if fill: miny = min(ax.get_ylim()) if not scatter: ax.fill_between(x, y, miny+1e-7, facecolor = h[0].get_color(), alpha=0.15) else: ax.fill_between(x, y, miny+1e-7, facecolor = axis_font_default['color'], alpha=0.15) if events: ylim = ax.get_ylim() for event in events['events']: time = datestr2num(event['start_date']) x = np.array([time, time]) h = ax.plot(x, ylim, '--', label=event['class']) legend = ax.legend() if legend: for label in legend.get_texts(): label.set_fontsize(10) if len(qaqc) > 0: bad_data = np.where(qaqc > 0) h = ppl.plot(ax, x[bad_data], y[bad_data], marker='o', mfc='none', linestyle='None', markersize=6, markeredgewidth=2, mec='r') # plt.tick_params(axis='both', which='major', labelsize=10) if tick_font: ax.tick_params(**tick_font) plt.tight_layout()
def draw_classes(centroid, offset, chunk=3): """Plot each time patterns in `centroid`.""" size = centroid.shape[0] for i, marker in zip(range(size), LEGEND[:size]): ppl.plot(centroid[i, :], marker+'-', ms=9, c=ppl.colors.set1[i]) if centroid.shape[1] == 24/chunk: plt.xticks(range(24/chunk), named_ticks('day', offset, chunk)) else: plt.xticks(range(7*3), named_ticks('mix'))
def plotc(f, i, j, r, m, mi=False): """Plot f(i) against f(j) as column""" plt.figure() ppl.plot(f[:, i], f[:, j], 'r+') plt.xlabel(FEATURES[i]) plt.ylabel(FEATURES[j]) m = 'm_i = {:.3f}'.format(m) r = 'r = {:.3f}'.format(r) order = [m, r] if mi else [r, m] plt.title('{} ({})'.format(*order))
def plotc(f, i, j, r, m, mi=False): """Plot f(i) against f(j) as column""" plt.figure() ppl.plot(f[:, i], f[:, j], "r+") plt.xlabel(FEATURES[i]) plt.ylabel(FEATURES[j]) m = "m_i = {:.3f}".format(m) r = "r = {:.3f}".format(r) order = [m, r] if mi else [r, m] plt.title("{} ({})".format(*order))
def plot_trajectories(): # setup parameters plt.rcParams.update(plot_params) # parameters runs = ['random_1', 'random_2'] algos = ['ml', 'mcl'] algo_params = {'ml': 72, 'mcl': '400k'} # create plot fig, axs = plt.subplots(2, 3, sharex='row', sharey='row', squeeze=False, figsize=(3.3, 2.3)) #fig, axs = plt.subplots(2, 3, squeeze=False, figsize=(3, 2)) # process all runs for i, run in enumerate(runs): # data for j, algo in enumerate(algos): param = algo_params[algo] result_file = '{}_{}_{}'.format(run, algo, param) data = np.loadtxt(os.path.join(result_base_dir, result_file)) xy = data[:, 5:7] ax = axs[i, j] ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) #if i == 0: #if algo == 'ml': #ax.set_title('Markov Loc.') #else: #ax.set_title('Monte Carlo Loc.') # draw x = xy[20:, 0] y = xy[20:, 1] ppl.plot(ax, x, y) # ground truth gt = np.loadtxt(os.path.join(data_base_dir, run, 'gt.txt')) ax = axs[i, 2] ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) #if i == 0: #ax.set_title('Ground truth') ppl.plot(ax, gt[:, 0] * 100., gt[:, 1] * 100.) # save name = 'trajectories.svg'.format(algo, param, run) fig.tight_layout(pad=0.02, rect=(0, 0, 1, 1)) fig.savefig(os.path.join(dest_base_dir, name), pad_inches=0.02)
def update(fig, ax, new_data, max_batt): ###centres is a list of centre coordinates ###new data is a list of corresponding update, {'battery': , 'status':} print(new_data) one = [(i[0], i[1]['battery']) for i in new_data if i[1]['_id'] == 0] two = [(i[0], i[1]['battery']) for i in new_data if i[1]['_id'] == 1] ppl.plot(ax, [i[1] for i in one], label='Sensor 1', linewidth=1.0) ppl.plot(ax, [i[1] for i in two], label='Sensor 2', linewidth=1.0) ax.set_ylim([0, 5]) ax.set_xlim([0, 100]) ax.set_ylabel('Battery')
def ppm_efficiency_by_depth_helper2(fnames, priors, depths, test_name, opts): test_files = list(itertools.chain(*config.PPM_EFFICIENCY_BY_DEPTH_FILESETS.values())) original_sizes = {f : benchmark.tasks.corpus_size(f) for f in fnames} work = [] for opt, (prior, depth) in zip(opts, itertools.product(priors, depths)): x, fun, status = opt a, b = x work += [benchmark.tasks.my_compressor.s(test_file, paranoia, prior, ['ppm:d={0}:a={1}:b={2}'.format(depth, a, b)]) for test_file in test_files] raw_res = celery.group(work)().get() res = {} for effectiveness, (prior, depth, test_file) in zip(raw_res, itertools.product(priors, depths, test_files)): by_prior = res.get(prior, {}) by_depth = by_prior.get(depth, {}) by_depth[test_file] = effectiveness by_prior[depth] = by_depth res[prior] = by_prior fig = plot.new_figure() colors = ppl.brewer2mpl.get_map('Set2', 'qualitative', len(config.PPM_EFFICIENCY_BY_DEPTH_FILESETS)).mpl_colors for (name, fileset), color in zip(config.PPM_EFFICIENCY_BY_DEPTH_FILESETS.items(), colors): for prior in priors: y = [] for d in depths: by_file = res[prior][d] mean = np.mean([by_file[f] / original_sizes[f] * 8 for f in fileset]) y.append(mean) linestyle = config.PPM_EFFICIENCY_BY_DEPTH_PRIOR_LINESTYLES[prior] marker = config.PPM_EFFICIENCY_BY_DEPTH_PRIOR_MARKERS[prior] min_i = np.argmin(y) markevery = list(range(0, min_i)) + list(range(min_i + 1, len(depths))) ppl.plot(depths, y, label='{1} on {0}'.format(name, short_name(config.SHORT_PRIOR_NAME, prior)), color=color, linestyle=linestyle, marker=marker, markevery=markevery) min_depth = depths[min_i] min_y = y[min_i] ppl.plot([min_depth], [min_y], color=color, linestyle='None', marker='D') plt.xlabel(r'Maximal context depth $d$') plt.ylabel(r'Compression effectiveness (bits/byte)') # stretch x-axis slightly so markers are visible plt.xlim(min(depths) - 0.1, max(depths) + 0.1) ppl.legend(handlelength=4, # increase length of line segments so that linestyles can be seen numpoints=1 # but only show marker once ) return plot.save_figure(fig, test_name, ["dummy"])
def ts_chart(title, unixtime_dates, data, file_name): fig = plt.figure() plt.title(title) dates = [] for unixdate in unixtime_dates: dates.append(datetime.fromtimestamp(float(unixdate))) ppl.plot(dates, data) fig.autofmt_xdate() fig.savefig(file_name + ".eps")
def test_plot(): # Set the random seed for consistency np.random.seed(12) # Show the whole color range for i in range(8): y = np.random.normal(size=1000).cumsum() x = np.arange(1000) # For now, you need to specify both x and y :( # Still figuring out how to specify just one ppl.plot(x, y, label=str(i))
def chem160_plotting(x, y, title='LABEL ME', legend_label=None, xlabel='LABEL ME', ylabel='LABEL ME'): ''' It's not really important to understand the innerworkings of this function. Just know that this will be the general function that we'll use to plot during this semester. It has nice colours, as well as other defaults set. INPUT: x: An array or arrays to be plotted. These are the x axes to be plotted y: An array or arrays to be plotted. These are the y axes to be plotted title: String that defines the plot title. The default title is LABEL ME to remind you to always label your plots legend_label: A string or array of strings that define the legend entries to be used xlabel: A string that defines the xlabel. This can accept latex ylabel: A string that defines the ylabel. This can accept latex OUTPUT: None. A plot is displayed ''' import prettyplotlib as ppl fig, ax = plt.subplots(1) fig.set_size_inches(10, 8) for ind in range(len(y)): if legend_label != None: ppl.plot(ax, x[ind], y[ind], label=legend_label[ind], linewidth=3) else: ppl.plot(ax, x[ind], y[ind], linewidth=3) ppl.legend(ax, fontsize=18) ax.set_title(title, fontsize=24) ax.set_xlabel(xlabel, fontsize=20) ax.set_ylabel(ylabel, fontsize=20) for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(20) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(20) ax.xaxis.set_ticks_position('bottom') ax.xaxis.set_tick_params(width=3) ax.yaxis.set_ticks_position('left') ax.yaxis.set_tick_params(width=3) plt.grid(b=True, which='major', color='0.65', linestyle='-')
def plot_day(day, subplot): xs = range(24) latlons = set([tuple(d[1]) for d in all_diffs]) diffs = [d for d in all_diffs if d[2].date() == day] by_loc = partition(diffs, lambda d: d[0])#, value=lambda d: [-1]) for loc, pts in by_loc.iteritems(): hr2diff = { pt[2].hour: pt[-1] for pt in pts } ys = map(lambda hr: hr2diff.get(hr, 0), xs) ppl.plot(subplot, xs, ys, c='grey', alpha=0.2) subplot.set_xlim(xmin=0, xmax=24) subplot.set_title(str(day)) yield list(latlons)[0], 0
def plot_day(day, subplot): xs = range(24) latlons = set([tuple(d[1]) for d in all_diffs]) diffs = [d for d in all_diffs if d[2].date() == day] by_loc = partition(diffs, lambda d: d[0]) #, value=lambda d: [-1]) for loc, pts in by_loc.iteritems(): hr2diff = {pt[2].hour: pt[-1] for pt in pts} ys = map(lambda hr: hr2diff.get(hr, 0), xs) ppl.plot(subplot, xs, ys, c='grey', alpha=0.2) subplot.set_xlim(xmin=0, xmax=24) subplot.set_title(str(day)) yield list(latlons)[0], 0
def fireSampPlt(tile, veg_ty = 'savanna', fire = None, out = ''): subfire, subEVI, subNBR, s = fireSamp(tile, veg_ty, fire) ### x axis of vegetation time series x_v = [] for dp in daterange16(startY, endY): x_v = np.append(x_v, dp.toordinal()) x_v = x_v[s:] ### ticks and labels on x axis xt = [] xl = [] k = 0 for y in range(startY + 1, endY): xt = np.append(xt, datetime.date(y, 1, 1).toordinal()) if k%((endY - startY) / 3) == 0: xl = np.append(xl, str(y)) else: xl = np.append(xl, '') k += 1 ### Plotting print 'Plotting samples now...' mpl.use('PDF') import matplotlib.pyplot as plt, prettyplotlib as ppl fig = plt.figure() f = [] E = [] N = [] for p in range(np.size(subEVI,0)): fireSeri = subfire[p, :] plt.subplot(4, 2, p) k = 0 for dp in daterange16(startY,endY): if fireSeri[k]&(k>=s): f = plt.axvline((dp-datetime.timedelta(days=8)).toordinal(), color='yellow', linewidth=1.5, zorder=0) k += 1 E, = ppl.plot(x_v, subEVI[p, s:], color='blue', linewidth=0.9) N, = ppl.plot(x_v, subNBR[p, s:], color='black', linewidth=0.3) plt.xticks(xt, xl, size='small') plt.locator_params(axis='y', nbins=5) plt.xlim(x_v[0],x_v[-1]) fig.suptitle('Samples from ' + veg_ty + ' @' + tileCord(tile), fontsize=14) leg = fig.legend((f, E, N), ('fire', 'EVI', 'NBR'), loc='lower center', ncol=3, shadow=True) leg.draw_frame(False) plt.savefig(out + tile + veg_ty + 'EVISamp' + '.pdf', dpi=300) plt.close()
def plot_corr(xi,y,xnames,yname,title="",ax=None): if ax == None: fig,ax = subplots(1,len(xi),figsize=(5*len(xi),4)) if len(xi)==1: ax=[ax] for i,x in enumerate(xi): ppl.plot(ax[i],x,y,linewidth=0,marker="o") coef = np.polyfit(x,y,1) ppl.plot(ax[i],x,[coef[0]*f +coef[1] for f in x],linewidth=2) ax[i].set_xlabel(xnames[i]) ax[i].set_ylabel(yname) txt = "%s = %2.2e * %s + %2.2f"%(yname, coef[0], xnames[i],coef[1]) at = get_label(txt) ax[i].add_artist(at) suptitle(title,size=14)
def _render_axes(self, ax_array, lang): comp_series = {} for comp in self.plot.specs['comparators']: y_comp = self.plot.data[self.plot.data.country == comp].value x_comp = self.plot.data[self.plot.data.country == comp].year comp_series[comp] = (x_comp, y_comp) for facet in range(self.plot.specs['facets']): cols = self.plot.specs['cols'] r = facet // cols c = facet % cols ax = ax_array[r][c] country = self.plot.specs['countries'][facet] country = self.plot.index.get_countries(names=[country]) y = self.plot.data[self.plot.data.country == country['en'][0]].value x = self.plot.data[self.plot.data.country == country['en'][0]].year ax_title = country[lang][0] ax.text(0.5, 0.95, ax_title, verticalalignment='bottom', horizontalalignment='center', transform=ax.transAxes, color=self.plot.specs.get('color', None), fontsize=10, fontweight='bold') ax.set_ylim(self.plot.specs['ylim']) if self.plot.specs.get('ystep', None): ax.yaxis.set_ticks(np.arange(self.plot.specs['ylim'][0], self.plot.specs['ylim'][1], self.plot.specs['ystep'])) start, end = self.plot.specs['xlim'] if self.plot.specs.get('xstep', None): ax.xaxis.set_ticks(np.arange(self.plot.specs['xlim'][0], self.plot.specs['xlim'][1], self.plot.specs['xstep'])) for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(10) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(10) # Draw in-focus series: strong colour and opaque ppl.plot(ax, x, y, alpha=1.0, linewidth=1, color=self.plot.specs.get('color', None)) ppl.scatter(ax, x, y, s=12.0, alpha=1.0, color=self.plot.specs.get('color', None)) # Draw comparators: light, translucent, overlapping main series for x_comp, y_comp in comp_series.values(): ppl.plot(ax, x_comp, y_comp, alpha=0.35, linewidth=5)
def plot(x_steps, elastic, plastic, friction, plastic_total, x, y, name, dest): global counter counter += 1 fig = plt.figure(num=counter, figsize=(12, 8)) cols = 2 # for coloring figure, axes = plt.subplots() axes.color_cycle = axes._get_lines.prop_cycler color = 'royalblue' plt.subplot(3, cols, 1) ppl.plot(x_steps, elastic, marker='o', markersize=4, color=color, ) plt.grid() plt.title('Elastic') plt.subplot(3, cols, 2) ppl.plot(x_steps, plastic, marker='o', markersize=4, color=color, ) plt.grid() plt.title('Plastic') plt.subplot(3, cols, 3) ppl.plot(x_steps, friction, marker='o', markersize=4, color=color, ) plt.grid() plt.title('Friction') plt.subplot(3, cols, 4) ppl.plot(x_steps, plastic_total, marker='o', markersize=4, color=color, ) plt.grid() plt.title('Plastic total') plt.subplot(3, 1, 3) ppl.plot(x, y, linewidth=0.75, color=color, ) plt.grid() fig.set_tight_layout(True) # Save graph if not os.path.exists(os.path.join(dest, '')): os.makedirs(os.path.join(dest, '')) plt.savefig(os.path.join(dest, '') + '{}.pdf'.format(name)) plt.close()
def make_bif_diagram(num_oscillators, t_max, average_k_range): """ theta = list of time series of each oscillator average_k = list of average coupling strengths I've defined some parameters inside the function instead of making them arguements. """ #Define Parameters mean_omega, var_omega, var_k = 20, 1, 5 omega = make_positive_cauchy_rv(num_oscillators, mean_omega, var_omega) IC = [np.random.uniform(0,2*np.pi) for i in range(num_oscillators)] #initial conditions #ODE solver parameters num_time_steps = 100.0 dt = t_max / num_time_steps t = [i*dt for i in range(int(num_time_steps) + 1)] #Solve ODE's bif_data = [] #bifurcation data for mean_k in average_k_range: K = make_positive_cauchy_rv(num_oscillators, mean_k, var_k) sols = odeint(lambda theta, t: g(theta,t,omega,K), IC, t) print 'Started <k> = ' + str(mean_k) #Take last 20% of R, and average to find R_inf temp = [abs(sum(exp(sols[-i,:]*1j)) / num_oscillators) for i in range((len(sols))/20)] R_inf = np.mean(temp) bif_data.append([mean_k, R_inf]) #Plot Solution temp1 = np.array(bif_data) pp.plot(temp1[:,0], temp1[:,1], '-o') pp.plt.title('For N = ' + str(num_oscillators) + ' oscillators') pp.plt.ylim([0,1]) pp.plt.xlabel('<k>') pp.plt.ylabel('$ R_{\infty} $') return 0
def make_bif_diagram(num_oscillators, t_max, average_k_range): """ theta = list of time series of each oscillator average_k = list of average coupling strengths I've defined some parameters inside the function instead of making them arguements. """ #Define Parameters mean_omega, var_omega, var_k = 20, 1, 5 omega = make_positive_cauchy_rv(num_oscillators, mean_omega, var_omega) IC = [np.random.uniform(0, 2 * np.pi) for i in range(num_oscillators)] #initial conditions #ODE solver parameters num_time_steps = 100.0 dt = t_max / num_time_steps t = [i * dt for i in range(int(num_time_steps) + 1)] #Solve ODE's bif_data = [] #bifurcation data for mean_k in average_k_range: K = make_positive_cauchy_rv(num_oscillators, mean_k, var_k) sols = odeint(lambda theta, t: g(theta, t, omega, K), IC, t) print 'Started <k> = ' + str(mean_k) #Take last 20% of R, and average to find R_inf temp = [ abs(sum(exp(sols[-i, :] * 1j)) / num_oscillators) for i in range((len(sols)) / 20) ] R_inf = np.mean(temp) bif_data.append([mean_k, R_inf]) #Plot Solution temp1 = np.array(bif_data) pp.plot(temp1[:, 0], temp1[:, 1], '-o') pp.plt.title('For N = ' + str(num_oscillators) + ' oscillators') pp.plt.ylim([0, 1]) pp.plt.xlabel('<k>') pp.plt.ylabel('$ R_{\infty} $') return 0
def sample_and_plot(X0, A, H, dt, N, NSamples, ax): Xs = numpy.zeros((NSamples,N,2)) Xs[:,0] = X0 for i in range(1,N): for j in range(NSamples): Xs[j,i] = Xs[j,i-1] +dt*numpy.dot(A,Xs[j,i-1])\ + numpy.sqrt(dt)*numpy.dot(H, numpy.random.normal(size=(2,))) ts = numpy.arange(0.0,N*dt,dt) [ppl.plot(ts,Xs[i,:,0],ax=ax) for i in range(NSamples)]
def new_make_plot(out_fn): fig, ax = plt.subplots(1) for k in ping_results.keys(): x_data = [] y_data = [] for i, resp_time in enumerate(ping_results[k]): x_data.append(i) y_data.append(resp_time) ppl.plot(ax, x_data, y_data, label=k, linewidth=0.75) #ppl.legend(ax) ppl.legend(ax, loc='lower left', ncol=4) #ax.set_title('test') fig.savefig(out_fn) print "Saved to file: %s" % out_fn
def draw_ots_sens_scale(): all_results_scale = np.loadtxt('expdata/fig_ots_scale_auc02_new.txt') exam3 = plt.imread('expdata/face3.png') plt.imshow(exam3) t = plt.axis('off') plt.subplots(1) rs = np.arange(0.8,1.22,0.04) s_ids = np.argsort(-all_results_scale[:,5]) colormap = plt.cm.spectral#rainbow plt.gca().set_color_cycle([colormap(i) for i in np.linspace(0, 0.99, 11)]) labels = ['TREES','SDM','RCPR','CCNF','CFAN','GNDPM','IFA','LBF','TCDCN','DRMF','CFSS'] for s in s_ids: ppl.plot(rs,all_results_scale[s],lw=2, marker='o',label = labels[s],alpha=0.8) ppl.plot([1,1],[0.15,0.078],c=[0.5,0.5,0.5],alpha=0.5,lw=3) plt.xlabel('Scales') plt.ylabel('AUC$_{0.2}$') plt.legend(ncol=2,loc=8) plt.xlim((0.8,1.2)) plt.grid()
def plot_xy_path(self, is_xy_same_scale=False): """ Plots the profile of the path in a x,y coordinate system. """ fig, ax = plt.subplots(1) if (is_xy_same_scale): ppl.plot(ax, self._x_coordinate, self._y_coordinate) ax.axis('equal') ax.set_xlabel("X coordinate (m)") else: ppl.plot(ax, self._x_coordinate * 1.0e-3, self._y_coordinate) ax.set_xlabel("X coordinate (km)") ax.set_ylabel("Y coordinate (m)") ax.grid(axis='y') if (is_xy_same_scale): ax.axis('equal') fig.show() return (fig, ax)
def experiment_threshold(x, y): #x = [i for i in xrange(300)] #y = [random.randint(1, 100)*i if i > 150 else random.randint(1,100)*2*i if i < 200 else random.randint(1,50)*i/2 for i in xrange(300)] #y = [100 if i < 100 else i if i < 120 else (240 - i) if i < 140 else 100 for i in xrange(300)] fig = pyplot.figure() ax = fig.add_subplot(111) pplot.plot(ax, x, y, "#BA5EDD", linewidth=4,alpha=1,label=r"$T$") #pplot.plot(ax, x, averages, "green", linewidth=4, alpha=1,label=r"$\mu$") pplot.plot(ax, x, y, "^", markevery=SKIP) #pplot.plot(ax, x, averages, "o", markevery=SKIP) for colour, symbol, threshold in zip(("b", "y", "r"), ('s', 'p', 'D'), (1, 3, 5)): sd = SpikeDetector(x, y, threshold=threshold) spikes, averages = sd.get_spikes() spikevalues = [y[i] for i in spikes] pplot.plot(spikes, spikevalues, colour + 'o', label=("%s" % threshold ) + r"$\sigma$ peaks") # Plot the threshold pplot.plot(ax, x,[a + sd.stddev * threshold for a in averages], colour, linewidth=2, alpha=1,label="$\mu$ + " + str(threshold) + r"$\sigma$") # Plot the markers on top, but only for every month? pplot.plot(ax, x,[a + sd.stddev * threshold for a in averages], symbol, markevery=SKIP, linewidth=2, alpha=1) for i, date in enumerate(map(lambda time: sd.get_date(time), spikes)): ax.annotate(date.date(), (spikes[i], spikevalues[i])) print "STDDEV: %s \n\n SPIKES: %s" % (threshold, spikes) ax.set_title("Time series for Wikipedia article 'Julian_Assange'") pyplot.ylabel("Page views") pyplot.xlabel("Days since 2008-01-01") font = {'family' : 'normal', 'size' : 14} matplotlib.rc('font', **font) pplot.legend() pyplot.show()
def table_touch_cdf(): fig, [ax2, ax1] = plt.subplots(1, 2, sharey=True, figsize=(8, 4)) data = read_csv(['touch'], False) data.sort(order='touch') c = data['count'].astype(float) c /= sum(c) ppl.plot(ax1, data['touch'], np.cumsum(c), label="SQLShare", color=cs[0], linewidth=2, linestyle='-.') data = read_csv(['touch', 'counts'], True) c = data['counts'].astype(float) c /= sum(c) ppl.plot(ax2, data['touch'], np.cumsum(c), label="SDSS", color=cs[1], linewidth=2, linestyle='--') ppl.legend(ax1, loc='lower right') ppl.legend(ax2, loc='lower right') ax1.yaxis.set_major_formatter(formatter) ax2.yaxis.set_major_formatter(formatter) ax1.set_xlim(0, 500) ax1.set_xlim(0, 25) ax1.yaxis.grid() ax2.yaxis.grid() #ax1.set_xlabel('Table touch') fig.text(0.5, 0.02, "Table touch", ha='center') ax1.set_ylabel('% of queries') fig.subplots_adjust(wspace=0.1) ax1.set_ylim(0, 1.01) ax2.set_ylim(0, 1.01) fig.tight_layout(rect=[0, .03, 1, 1]) plt.show() fig.savefig('plot_touch_cdf.pdf', format='pdf', transparent=True) fig.savefig('plot_touch_cdf.png', format='png', transparent=True)
def runtime_cdf(): fig, [ax1, ax2] = plt.subplots(1, 2, sharey=True, figsize=(8, 4)) data = read_csv(['actual', 'counts'], True) c = data['counts'].astype(float) c /= sum(c) ppl.plot(ax1, data['actual'], np.cumsum(c), label="SDSS", color=cs[0], linewidth=2, ls='-.') data = read_csv(['time_taken'], False) data.sort(order='time_taken') c = data['count'].astype(float) c /= 1000 # ms to seconds c /= sum(c) ppl.plot(ax2, data['time_taken'], np.cumsum(c), label="SQLShare", color=cs[1], linewidth=2, ls='--') ppl.legend(ax1, loc='lower right') ppl.legend(ax2, loc='lower right') plt.gca().yaxis.set_major_formatter(formatter) #ax.set_xlabel('Runtime in seconds') ax1.set_ylabel('% of queries') fig.text(0.5, 0.02, "Runtime in seconds", ha='center') ax1.yaxis.grid() ax2.yaxis.grid() fig.subplots_adjust(wspace=0.1) ax1.set_xlim(0, 6) ax2.set_xlim(0, 500) ax1.set_ylim(0, 1.01) ax2.set_ylim(0, 1.01) fig.tight_layout(rect=[0, .03, 1, 1]) plt.show() fig.savefig('plot_runtimes_cdf.pdf', format='pdf', transparent=True) fig.savefig('plot_runtimes_cdf.png', format='png', transparent=True)
def plot(weights_vector): # Use a temporal resolution of 1000. x_real = np.linspace(0, 1, 1000) y_real = np.array([exercise_11.f(x_sample) for x_sample in x_real]) fig, ax = plt.subplots(1) ppl.plot(ax, x_real, y_real, linewidth=0.75, label='Function') i = 0 which = [0, 1, 3, 9] for weights in weights_vector: poly_output = [eval_polynomial(weights, x) for x in x_real] ppl.plot(ax, x_real, poly_output, linewidth=0.75, label='M = ' + str(which[i])) i = i + 1 ppl.legend(ax, loc='upper right', ncol=2) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_title('Polynomial approximation of sine function') fig.savefig('exercise_polynomial_plot40.pdf')
def plot_clusters(clusters, candidates, bounds, vloc, hulls, shrink=0.9): """Plot all `clusters` among `candidates` with the `bounds` of the city (or at least `shrink` of them). Also plot convex `hulls` of gold areas if provided.""" xbounds, ybounds = bounds unique_labels = len(clusters) clustered = set().union(*map(list, clusters)) noise = list(candidates.difference(clustered)) if unique_labels > 5: colors = mpl.cm.Spectral(np.linspace(0, 1, unique_labels + 1)) else: colors = [gray, red, green, blue, orange] plt.figure(figsize=(20, 15)) for k, indices, col in zip(range(unique_labels + 1), [noise] + clusters, colors): k -= 1 if k == -1: col = 'gray' ppl.scatter(vloc[indices, 0], vloc[indices, 1], s=35 if k != -1 else 16, color=col, alpha=0.8 if k != -1 else 0.6, label='noise' if k == -1 else 'cluster {}'.format(k + 1)) hulls = hulls or [] for idx, hull in enumerate(hulls): first_again = range(len(hull)) + [0] ppl.plot(hull[first_again, 0], hull[first_again, 1], '--', c=ppl.colors.almost_black, lw=1.0, alpha=0.9, label='gold region' if idx == 0 else None) plt.xlim(shrink * xbounds) plt.ylim(shrink * ybounds) ppl.legend()
def plot_distributions(G,H): G_degrees = sorted(nx.degree(G).values(),reverse=True) H_degrees = sorted(nx.degree(H).values(),reverse=True) M = nx.number_of_edges(G) # get degree frequencies and add attributes G_k = [] G_p_k = [] for n in set(G_degrees): G_k.append(n) G_p_k.append(G_degrees.count(n)/float(N)) H_k = [] H_p_k = [] for n in set(H_degrees): H_k.append(n) H_p_k.append(H_degrees.count(n)/float(N)) # Create a plot fig, ax = plt.subplots(1) # Axis needs to be set before plotting the bars # ax.set_yscale('log') # ax.set_xscale('log') ppl.plot(ax, G_k, G_p_k, 'o-', color = "#9e9ac8", label = "Network with power-law distribution") ppl.plot(ax, H_k, H_p_k, 'o-', color = "#6baed6", label = "Network with binomial degree distribution") //ax.set_title("Degree distribution of networks with "+str(N)+" nodes and "+str(M)+" edges.\n") ax.xaxis.set_label_text("degree (k)") ax.yaxis.set_label_text("Degree frequency P(k)") ppl.legend(ax, loc='upper right', ncol=1) ax.set_xlim([0,20]) #Otherwise we can'st see the curves they are so close ot 0 fig.savefig('test_distribution.pdf')
def plot_degdist(fig, ax, fname, *args, **kwargs): """ Plot a degree distribution for a graph. Parameters: fig: Matplotlib Figure Figure for the plot ax: Matplotlib Axis Axis for the plot fname: String Name of gml file Optional arguments: args: additional unnamed parameters Standard plot arguments Optional keyword arguments: kwargs: dictionary of named arguments Standard plot keyword arguments """ G = nx.read_gml(fname) y, x = np.histogram(nx.degree(G).values(), bins=range(0, G.number_of_nodes()), density=True) ppl.plot(ax, x[:-1], y, *args, alpha=1, **kwargs) plt.xscale('log') plt.yscale('log') ax.set_xlabel('degree') ax.set_ylabel('degree\ndistribution') plt.tight_layout()
def plot_rmse(y_training, weights_vector): x_test = np.linspace(0, 1, 100) y_test = exercise_11.sample_gaussian(exercise_11.f, 0, 0.3, x_test) rmse_training = np.zeros((len(weights_vector))) rmse_test = np.zeros((len(weights_vector))) i = 0 for weights in weights_vector: poly_output_100 = [eval_polynomial(weights, x) for x in np.linspace(0, 1, 100)] poly_output_40 = [eval_polynomial(weights, x) for x in np.linspace(0, 1, 40)] rmse_training[i] = np.sqrt(np.mean((poly_output_40 - y_training)**2)) rmse_test[i] = np.sqrt(np.mean((poly_output_100 - y_test)**2)) i = i + 1 fig, ax = plt.subplots(1) ppl.plot(ax, np.arange(10), rmse_training, linewidth=0.75, label='RMSE on training set') ppl.plot(ax, np.arange(10), rmse_test, linewidth=0.75, label='RMSE on test set') ppl.legend(ax, loc='upper right', ncol=2) ax.set_xlabel('Polynomial order') ax.set_ylabel('RMSE') ax.set_title('RMSE for the polynomial approximation of sine function') fig.savefig('exercise_rmse_plot40.pdf')
def plotIntegParam(self): framelimit = self.integmaxframewdgt.value() self.get_timeboundaries() df = self.tempdata[self.mindate:self.maxdate] df.to_pickle('./plotinteg_df.pickle') length = len(df) plotrange_min = max(length - int(framelimit) - 1, 0) plotrange_max = length - 1 self.IP0figure.clf() ax = self.IP0figure.add_subplot(111) self.IP0figure.set_frameon(False) ax.patch.set_alpha(0) ppl.plot(ax, df.index[plotrange_min:plotrange_max], df['I0'][plotrange_min:plotrange_max]) ax.set_ylabel('integ.I(q)') ax.xaxis.set_minor_locator(plt.MaxNLocator(6)) ax.xaxis.set_minor_formatter( dates.DateFormatter('%H:%M:%S')) # hours and minutes ax.xaxis.set_major_locator(plt.MaxNLocator(2)) ax.xaxis.set_major_formatter(dates.DateFormatter('\n%d-%m-%Y')) self.IP0figure.tight_layout() self.IP0canvas.draw() self.IP1figure.clf() ax = self.IP1figure.add_subplot(111) self.IP1figure.set_frameon(False) ax.patch.set_alpha(0) #df['I2'][max(length-int(framelimit)-1, 0):length-1].plot(ax=ax) ppl.plot(ax, df.index[plotrange_min:plotrange_max], df['I2'][plotrange_min:plotrange_max]) ax.set_ylabel("Invariant") ax.xaxis.set_minor_locator(plt.MaxNLocator(6)) ax.xaxis.set_minor_formatter( dates.DateFormatter('%H:%M:%S')) # hours and minutes ax.xaxis.set_major_locator(plt.MaxNLocator(2)) ax.xaxis.set_major_formatter(dates.DateFormatter('\n%d-%m-%Y')) self.IP1figure.tight_layout() self.IP1canvas.draw() self.IP2figure.clf() ax = self.IP2figure.add_subplot(111) self.IP2figure.set_frameon(False) ax.patch.set_alpha(0) #df['corrlength'][max(length-int(framelimit)-1, 0):length-1].plot(ax=ax, color='green') ppl.plot(ax, df.index[plotrange_min:plotrange_max], df['corrlength'][plotrange_min:plotrange_max]) ax.set_ylabel("corr.length") ax.xaxis.set_minor_locator(plt.MaxNLocator(6)) ax.xaxis.set_minor_formatter( dates.DateFormatter('%H:%M:%S')) # hours and minutes ax.xaxis.set_major_locator(plt.MaxNLocator(2)) ax.xaxis.set_major_formatter(dates.DateFormatter('\n%d-%m-%Y')) self.IP2figure.tight_layout() self.IP2canvas.draw()
def rolling_expanding_mean(dataset, col_name='Adj_Close', resample_option='M', short_window=20, long_window=150): '''Rolling Windows - Short and Long Term''' if resample_option == "none": dataset["short_window"] = dataset[col_name].rolling( window=short_window, center=True).mean() dataset["long_window"] = dataset[col_name].rolling(window=long_window, center=True).mean() dataset[[col_name, "short_window", "long_window"]].plot(figsize=(19, 7)) plt.show() else: roll_mean = dataset[[col_name]].resample(resample_option).rolling( window=short_window, center=False).mean() expanding_mean = dataset[[ col_name ]].resample(resample_option).expanding().mean() plt.figure(figsize=(19, 7)) ticks_date = dataset.resample(resample_option).index.to_pydatetime() ppl.plot(ticks_date, roll_mean, alpha=1, lw=2, label='rolling mean') ppl.plot(ticks_date, expanding_mean, alpha=1, lw=2, label='expanding mean') text = col_name.title() + " Resampled " + resample_option + \ " window " + str(short_window) plt.title(text, fontsize=20) plt.legend(loc='upper right') plt.tick_params(labelsize=14)