def build_graph(self): """ Update the plot area with loss values and cycle through to animate """ self.ax1.set_xlabel('Iterations') self.ax1.set_ylabel('Loss') self.ax1.set_ylim(0.00, 0.01) self.ax1.set_xlim(0, 1) losslbls = [lbl.replace('_', ' ').title() for lbl in self.losskeys] for idx, linecol in enumerate(['blue', 'red']): self.losslines.extend(self.ax1.plot(0, 0, color=linecol, linewidth=1, label=losslbls[idx])) for idx, linecol in enumerate(['navy', 'firebrick']): lbl = losslbls[idx] lbl = 'Trend{}'.format(lbl[lbl.rfind(' '):]) self.trndlines.extend(self.ax1.plot(0, 0, color=linecol, linewidth=2, label=lbl)) self.ax1.legend(loc='upper right') plt.subplots_adjust(left=0.075, bottom=0.075, right=0.95, top=0.95, wspace=0.2, hspace=0.2) plotcanvas = FigureCanvasTkAgg(self.fig, self.frame) plotcanvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True) ani = animation.FuncAnimation(self.fig, self.animate, interval=2000, blit=False) plotcanvas.draw()
def iris_flowers(): data_features = [] data_labels = [] for line in open('iris.data'): if line.split(): data = line.split(',') data_features.append(map(float, data[:-1])) data_labels.append(data[-1].strip()) data_points_count = len(data_features) attributes_count = len(data_features[0]) labels = list(set(data_labels)) colors = map(lambda data_label : labels.index(data_label), data_labels) transpose_data_feature = [[row[i] for row in data_features] for i in range(attributes_count)] figure, subplots = plt.subplots(attributes_count, attributes_count, sharex='row', sharey='col') figure.suptitle('Iris Data, red=setosa, green=versicolor, blue=virginica') label_names = ['Sepal length', 'Sepal width', 'Petal Length', 'Petal Width'] for x_axis in range(attributes_count): for y_axis in range(attributes_count): if x_axis != y_axis: subplot = subplots[x_axis][y_axis] subplot.scatter(transpose_data_feature[x_axis], transpose_data_feature[y_axis], c=colors) subplot.set_xlabel(label_names[x_axis]) subplot.set_ylabel(label_names[y_axis]) plt.subplots_adjust(wspace=0.25, hspace=0.25,left=0.1,right=0.9, bottom=0.1) plt.show()
def plot(self, path, num_bins=0): """ draw a histogram to represent the data :param num_bins: number of bars, default is (Number different word in the file )/ 2, if it is too large take 50 as default (see '#default of num_bins') """ # plot data mu = self.Average # mean of distribution sigma = self.StdE # standard deviation of distribution if num_bins == 0: # default of num_bins num_bins = min([round(self.NumWord / 2), 50]) # print num_bins # the histogram of the data n, bins, patches = plt.hist(self.WordCount.values(), num_bins, normed=1, facecolor='green', alpha=0.5) # add a 'best fit' line y = mlab.normpdf(bins, mu, sigma) plt.plot(bins, y, 'r--') plt.xlabel('Word Count') plt.ylabel('Probability(how many words have this word count)') plt.title(r'Histogram of word count: $\mu=' + str(self.Average) + '$, $\sigma=' + str(self.StdE) + '$') # Tweak spacing to prevent clipping of ylabel plt.subplots_adjust(left=0.15) plt.savefig(path) plt.close()
def _plot(x, Y, file_name): title = file_name.replace('_', ' ').upper() fig = plt.figure(figsize=(8,4)) ax = fig.add_subplot(111) plt.subplots_adjust(left=0.075, right=0.96, top=0.92, bottom=0.08) #ax.set_autoscaley_on(False) #ax.set_ylim([0,0.1]) ax.set_xlim(0, RANGE[1]) powerlaw = lambda x, amp, index: amp * (x**index) for y in Y: day, region = y amp, index = DATA[day][region] label = '{region} ({day})'.format(day=day, region=region).upper() ax.plot(x, powerlaw(x, amp, index), label=label, linewidth=1, color=COLORS[region], alpha=0.95, linestyle=LINESTYLE[day]) formatter = FuncFormatter(lambda v, pos: str(round(v*100, 2))+'%') plt.gca().yaxis.set_major_formatter(formatter) formatter = FuncFormatter(lambda v, pos: '' if v/1000 == 0 else str(int(v/1000))+'km') plt.gca().xaxis.set_major_formatter(formatter) ax.set_title(title, fontsize=11) ax.legend(fontsize=10) if not os.path.exists('data/' + my.DATA_FOLDER + 'disp_stat/'): os.makedirs('data/' + my.DATA_FOLDER + 'disp_stat/') plt.savefig('data/' + my.DATA_FOLDER + 'disp_stat/' + file_name + '.png') print 'Stored chart: %s' % file_name
def visualize_type(): # grab our parsed data data_file = parse.parse(MY_FILE,",") # make a new variable, 'counter', from iterating through each line # of data in the parsed data, and count how many incidents happen # by category counter = Counter(item["Category"]for item in data_file) # Set the labels which are based on the keys of our counter. # Since order doesn't matter, we can just used counter.keys() labels = tuple(counter.keys()) # Set exactly where the labels hit the x-axis # numpy.arange() creates evenly spaced numbers xlocations = np.arange(len(labels)) + 0.5 # Width of each bar that will be plotted width = 0.5 # Assign data to a bar plot (similar to plt.plot()!) plt.bar(xlocations, counter.values(),width=width) # Assign labels and tick location to x-axis plt.xticks(xlocations + width /2, labels, rotation = 90) # Give some more room so the x-axis labels aren't cut off in the # graph plt.subplots_adjust(bottom=0.5) # Make the overall graph/figure is larger plt.rcParams['figure.figsize'] = 12,12 # Save the graph! plt.savefig("Type.png") # Close plot figure plt.clf()
def _plot_validation_scores(self): validation_sources = self.db.validation_scores.distinct( key='source_id', filter={'experiment_id': self.experiment_id}) validation_sources.sort() num_cols = len(validation_sources) fig, axes = plt.subplots( nrows=3, ncols=num_cols, sharex="col", sharey=True, squeeze=False) fig.patch.set_facecolor('white') source_names = self.source_names for col, source_id in enumerate(validation_sources): for row, fold in enumerate(DATA_FOLD_NAMES): ax = axes[row, col] self._plot_validation_scores_for_source_and_fold( ax=ax, source_id=source_id, fold=fold, show_axes_labels=(row == 0), show_scales=(col == num_cols-1)) if row == 0: ax.set_title(source_names[source_id], position=(.5, 1.05)) elif row == 2: ax.set_xlabel('Iteration', labelpad=10) if col == 0: ax.set_ylabel(fold.replace("_", " ").title(), labelpad=10) ax.patch.set_facecolor((0.95, 0.95, 0.95)) plt.subplots_adjust( top=0.91, bottom=0.05, left=0.03, right=0.7, hspace=0.15, wspace=0.1) plt.show()
def plot_slice(x, y, i_slice, lx, H_range, K_range): # plot the HK plane i_slice_range = [0.006, 0.0115] f = plt.figure("HK plane", figsize=(7.5, 2.3)) plt.subplots_adjust(left=0.10, bottom=0.155555, right=1.05, top=0.95, wspace=0.2, hspace=0.45) subp = f.add_subplot(111) cnt = subp.contourf(x, y, i_slice, np.linspace(i_slice_range[0], i_slice_range[1], 50, endpoint=True), extend='both') subp.axis('scaled') subp.set_xlim(H_range) subp.set_ylim(K_range) subp.set_xlabel("H", size=10) subp.set_ylabel("K", size=10) subp.tick_params(labelsize=9) cbar = plt.colorbar(cnt, ticks=np.linspace(i_slice_range[0], i_slice_range[1], 3, endpoint=True), format='%.4f') cbar.ax.tick_params(labelsize=8)
def plot_flux(f, q_left, q_right, plot_zero=True): qvals = np.linspace(q_right, q_left, 200) fvals = f(qvals) dfdq = np.diff(fvals) / (qvals[1]-qvals[0]) # approximate df/dq qmid = 0.5*(qvals[:-1] + qvals[1:]) # midpoints for plotting dfdq #plt.figure(figsize=(12,4)) plt.subplot(131) plt.plot(qvals,fvals) plt.xlabel('q') plt.ylabel('f(q)') plt.title('flux function f(q)') plt.subplot(132) plt.plot(qmid, dfdq) plt.xlabel('q') plt.ylabel('df/dq') plt.title('characteristic speed df/dq') plt.subplot(133) plt.plot(dfdq, qmid) plt.xlabel('df/dq') plt.ylabel('q') plt.title('q vs. df/dq') if plot_zero: plt.plot([0,0],[qmid.min(), qmid.max()],'k--') plt.subplots_adjust(left=0.) plt.tight_layout()
def plot_origin_piechart(db_fname): engine = sqlalchemy.create_engine('sqlite:///{}'.format(db_fname)) matplotlib.style.use('ggplot') fig, axes = plt.subplots(2, 3) plt.subplots_adjust(hspace=0.2) fig.suptitle('Annotations of Pangenes Flagged as Non-Viral by Fluidity Threshold', fontsize=22) for ix, threshold in enumerate(THRESHOLDS): threshold_str_file = '%se-%d' % (re.search('([1-9])', str(threshold)).group(1), str(threshold).count('0')) n = len(os.listdir(os.path.join(OUTPUTDIR, dir, 'fasta_out_pangenome', threshold_str_file, 'unified'))) threshold_str_sql = '%sEXPneg%d' % (re.search('([1-9])', str(threshold)).group(1), str(threshold).count('0')) stats_table_name = 'pangenome_%s_stats' % threshold_str_sql stats_table_df = pandas.read_sql_query('SELECT pangene_id, viral_flag, origin FROM {} WHERE paralog_flag=0'.format(stats_table_name), engine, index_col='pangene_id') plot_data = stats_table_df.groupby(['viral_flag', 'origin']).size() n_pangenes = plot_data.sum() print plot_data[0] #return plot_data[0].plot(ax=axes[ix//3, ix%3], kind='pie', autopct='%.2f', fontsize=16, labels=['', '', '']) if ix//3==1 and ix%3==1: axes[ix//3, ix%3].legend(bbox_to_anchor=(0.85,0.30), fontsize=18, bbox_transform=plt.gcf().transFigure, labels=plot_data[1].index) axes[ix//3, ix%3].set_title('%s\n(n_genomes=%d, n_pangenes=%d)' % (threshold_str_file, n, n_pangenes), fontsize=18) axes[ix//3, ix%3].set_xlabel('') ''' patches, labels = axes[1,1].get_legend_handles_labels() fig.legend(patches, ('bacterial', 'unknown', 'viral'), bbox_to_anchor=(0.85,0.30), fontsize=18, bbox_transform=plt.gcf().transFigure) ''' axes[-1, -1].axis('off') plt.show() return
def make_lick_individual(targetSN, w1, w2): """ Make maps for the kinematics. """ filename = "lick_corr_sn{0}.tsv".format(targetSN) binimg = pf.getdata("voronoi_sn{0}_w{1}_{2}.fits".format(targetSN, w1, w2)) intens = "collapsed_w{0}_{1}.fits".format(w1, w2) extent = calc_extent(intens) bins = np.loadtxt(filename, usecols=(0,), dtype=str).tolist() bins = np.array([x.split("bin")[1] for x in bins]).astype(int) data = np.loadtxt(filename, usecols=np.arange(25)+1).T labels = [r'Hd$_A$', r'Hd$_F$', r'CN$_1$', r'CN$_2$', r'Ca4227', r'G4300', r'Hg$_A$', r'Hg$_F$', r'Fe4383', r'Ca4455', r'Fe4531', r'C4668', r'H$_\beta$', r'Fe5015', r'Mg$_1$', r'Mg$_2$', r'Mg$_b$', r'Fe5270', r'Fe5335', r'Fe5406', r'Fe5709', r'Fe5782', r'Na$_D$', r'TiO$_1$', r'TiO$_2$'] mag = "[mag]" ang = "[\AA]" units = [ang, ang, mag, mag, ang, ang, ang, ang, ang, ang, ang, ang, ang, ang, mag, mag, ang, ang, ang, ang, ang, ang, ang, mag, mag] lims = [[None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None], [None, None]] pdf = PdfPages("figs/lick_sn{0}.pdf".format(targetSN)) fig = plt.figure(1, figsize=(6.25,5)) plt.subplots_adjust(bottom=0.12, right=0.97, left=0.09, top=0.96) plt.minorticks_on() ax = plt.subplot(111) ax.minorticks_on() plot_indices = np.arange(12,22) for i, vector in enumerate(data): if i not in plot_indices: continue print "Making plot for {0}...".format(labels[i]) kmap = np.zeros_like(binimg) kmap[:] = np.nan for bin,v in zip(bins, vector): idx = np.where(binimg == bin) kmap[idx] = v vmin = lims[i][0] if lims[i][0] else np.median(vector) - 2 * vector.std() vmax = lims[i][1] if lims[i][1] else np.median(vector) + 2 * vector.std() m = plt.imshow(kmap, cmap="inferno", origin="bottom", vmin=vmin, vmax=vmax, extent=extent, aspect="equal") make_contours() plt.minorticks_on() plt.xlabel("X [kpc]") plt.ylabel("Y [kpc]") plt.xlim(extent[0], extent[1]) plt.ylim(extent[2], extent[3]) cbar = plt.colorbar(m) cbar.set_label("{0} {1}".format(labels[i], units[i])) pdf.savefig() plt.clf() pdf.close() return
def plot_data(l): fig, ax = plt.subplots() counts, bins, patches = ax.hist(l,30,facecolor='yellow', edgecolor='gray') # Set the ticks to be at the edges of the bins. #ax.set_xticks(bins) # Set the xaxis's tick labels to be formatted with 1 decimal place... #ax.xaxis.set_major_formatter(FormatStrFormatter('%0.1f')) # Label the raw counts and the percentages below the x-axis... bin_centers = 0.5 * np.diff(bins) + bins[:-1] for count, x in zip(counts, bin_centers): # Label the raw counts ax.annotate(str(int(count)), xy=(x, 0), xycoords=('data', 'axes fraction'), xytext=(0, -40), textcoords='offset points', va='top', ha='center') # Label the percentages percent = '%0.0f%%' % (100 * float(count) / counts.sum()) ax.annotate(percent, xy=(x, 0), xycoords=('data', 'axes fraction'), xytext=(0, -50), textcoords='offset points', va='top', ha='center') # Give ourselves some more room at the bottom of the plot plt.subplots_adjust(bottom=0.15) plt.grid(True) plt.xlabel("total reply") plt.ylabel("pages") plt.title("2014/10/21-2014/10/22 sina new pages") plt.show()
def test_twin_axes_empty_and_removed(): # Purely cosmetic font changes (avoid overlap) matplotlib.rcParams.update({"font.size": 8}) matplotlib.rcParams.update({"xtick.labelsize": 8}) matplotlib.rcParams.update({"ytick.labelsize": 8}) generators = [ "twinx", "twiny", "twin" ] modifiers = [ "", "host invisible", "twin removed", "twin invisible", "twin removed\nhost invisible" ] # Unmodified host subplot at the beginning for reference h = host_subplot(len(modifiers)+1, len(generators), 2) h.text(0.5, 0.5, "host_subplot", horizontalalignment="center", verticalalignment="center") # Host subplots with various modifications (twin*, visibility) applied for i, (mod, gen) in enumerate(product(modifiers, generators), len(generators)+1): h = host_subplot(len(modifiers)+1, len(generators), i) t = getattr(h, gen)() if "twin invisible" in mod: t.axis[:].set_visible(False) if "twin removed" in mod: t.remove() if "host invisible" in mod: h.axis[:].set_visible(False) h.text(0.5, 0.5, gen + ("\n" + mod if mod else ""), horizontalalignment="center", verticalalignment="center") plt.subplots_adjust(wspace=0.5, hspace=1)
def main(): infileName = input("Enter the name of the input .wav file: ") # Next, get all the samples as an array of short, and the parameters as a # tuple (numChannels, sampleWidth, sampleRate, numFrames, not-used, not-used) data, params = readwav(infileName) # Example: find the maximum value in the samples y = [] n = 1000 #initialize x = np.arange(0,n//2,1) for j in x: s = 0 for i in range(j,n): s += (data[i]*data[i-j]) s = s/(n-j) y +=[s] ''' graph template''' plt.plot(x, y, 'k') plt.title('Autocorrelation', fontdict=font) plt.xlabel('Lag time', fontdict=font) plt.ylabel('Correlation', fontdict=font) # Tweak spacing to prevent clipping of ylabel plt.subplots_adjust(left=0.15) plt.show()
def fit_and_plot(cand, spd): data = cand.profile n = len(data) rms = np.std(data[(n/2):]) xs = np.linspace(0.0, 1.0, n, endpoint=False) G = gauss._compute_data(cand) print " Reduced chi-squared: %f" % (G.get_chisqr(data) / G.get_dof(n)) print " Baseline rms: %f" % rms print " %s" % G.components[0] fig1 = plt.figure(figsize=(10,10)) plt.subplots_adjust(wspace=0, hspace=0) # upper ax1 = plt.subplot2grid((3,1), (0,0), rowspan=2, colspan=1) ax1.plot(xs, data/rms, color="black", label="data") ax1.plot(xs, G.components[0].make_gaussian(n), color="red", label="best fit") # lower ax2 = plt.subplot2grid((3,1), (2,0), sharex=ax1) ax2.plot(xs, data/rms - G.components[0].make_gaussian(n), color="black", label="residuals") ax2.set_xlabel("Fraction of pulse window") plt.figure() plt.pcolormesh(xs, spd.waterfall_freq_axis(), spd.data_zerodm_dedisp, cmap=Greys) plt.xlabel("Fraction of pulse window") plt.ylabel("Frequency (MHz)") plt.xlim(0, 1) plt.ylim(spd.min_freq, spd.max_freq) plt.show()
def plot_line_graph(self, complaint_dataset): self.complaint_dataset = pd.DataFrame() self.complaint_dataset = complaint_dataset # Adjust dataset for plotting category = self.complaint_dataset['MajorCategory'].unique()[0] category = category self.complaint_dataset = self.rows_violation_1(self.complaint_dataset) self.complaint_dataset = self.keep_needed_columns(self.complaint_dataset) self.complaint_dataset = self.group_by_date(self.complaint_dataset) # Create figure for plotting ax = self.complaint_dataset.plot() fig = ax.get_figure() # Set graph labels and graph size on the figure plt.legend('', title=category, fontsize='xx-small', loc='best') plt.title('Distribution Of Violations Per Date') plt.ylabel('Number Of Violations') plt.xlabel('Date') plt.subplots_adjust(top=.9, left=.09, right=.99, bottom=.19 ) # Save graph to .pdf file fig.savefig('figures/linegraph.pdf') # Close figure plt.close('all') plt.clf()
def plot_TP(): with open ('p_files/ROC_table_' + str(seg) + '_pc' + str(p) + '_k' + str(k) + '.p', 'rb') as f: ROC_table = pickle.load(f) with open ('p_files/species_stats.p', 'rb') as f: species_table = pickle.load(f) with open ('p_files/species.p', 'rb') as f: species_name = pickle.load(f) xes = []#[item['number'] for item in ROC_table.values()] yes = []#[item['fp'] for item in ROC_table.values()] label = [] low_TP = [] for specie in ROC_table: xes.append(species_table[specie]) yes.append(ROC_table[specie]['tp_rate']) label.append(specie) if float(ROC_table[specie]['tp_rate']) < 0.3 and species_table[specie] > 100: #print(ROC_table[specie]['tp_rate']) low_TP.append((specie, ROC_table[specie]['tp'])) fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.1) ax.plot([0,max(xes)],[0.3,0.3], ls="--") ax.scatter(xes, yes, marker = '.') for i, txt in enumerate(label): if txt in interesting: #if float(yes[i]) < 0.3 and xes[i] > 100 : #print(txt) #ax.annotate(parser.get_specie_name('../data/train/', str(species_name[txt][0]) + '.xml'), (xes[i],yes[i])) ax.annotate(txt, (xes[i],yes[i])) plt.suptitle('False Positive', fontsize = 14) # ax.set_xlabel('Principal Components') # ax.set_ylabel('Percentage of Variance') plt.show()
def plot_corr_diag(corr_matrix, out_basename): N_col = np.shape(corr_matrix)[1] extend = (0.5 , N_col+0.5 , N_col+0.5, 0.5 ) fig , ax = pl.subplots(figsize=(15, 12)) ax.tick_params('both', length=15, width=8, which='major') pl.subplots_adjust(left=0.10, right=0.95, top=0.95, bottom=0.12) for i in range(0,N_col): for j in range(0, N_col): if i==j: corr_matrix[i,j] = 1 #pl.imshow(corr_matrix, interpolation='nearest', extent=extend) pl.imshow(corr_matrix, interpolation='nearest', vmin=-0.45, vmax=0.45, extent=extend) ## vmin & vmax for EMPIRICAL data corr matrix : #pl.imshow(corr_matrix, interpolation='nearest', vmin=0.0, vmax=1.0, extent=extend) cbar = pl.colorbar() for t in cbar.ax.get_yticklabels(): t.set_fontsize(50) pl.xticks(fontsize = 50) pl.yticks(fontsize = 50) pl.xlabel('Nodes', fontsize = 50) pl.ylabel('Nodes', fontsize = 50) return
def plot_bold_signal(timeseries, x, y): # plots timeseries of two given nodes in a specific time interval v1 = timeseries[:, x] v2 = timeseries[:, y] T = len(v1) time = np.linspace(0, T-1, T) / float(60000) [R_pearson , p_value] = sistat.pearsonr(v1 , v2) ## if the given signal downsampled : #time_bds = np.arange(0, 530, float(530)/len(v1) )/float(60) #pl.plot(time_bds, v1, 'r',label=('node '+str(x))) #pl.plot(time_bds, v2, 'b',label=('node '+str(y))) # if no downsampling : fig , ax = pl.subplots(figsize=(25, 5.5)) pl.subplots_adjust(left=0.08, right=0.98, top=0.94, bottom=0.20) pl.plot(time, v1, 'm', label=('$u_{' + str(x+1) + '}(t)$')) pl.plot(time, v2, 'g', label=('$u_{' + str(y+1) + '}(t)$')) pl.setp(pl.gca().get_xticklabels(), fontsize = 30) pl.setp(pl.gca().get_yticklabels(), fontsize = 30) #ax.set_ylim(-v2.max()-0.05, v2.max()+0.05) ax.set_ylim(-0.6, 0.6) pl.legend(prop={'size':35}) pl.xlabel('t [min]', fontsize=30) pl.ylabel('BOLD % change' ,fontsize=40) return
def plotRPKMfor2strains(sgrfileW_WT,sgrfileC_WT,sgrfileW_MUT,sgrfileC_MUT,chromosome,strains): # Convert Watson sgr file to a watson sgrlist for a chromosome for WT sgrlistforchromW_WT = ps.getSgrList(sgrfileW_WT,chromosome) # Convert Crick sgr file to a crick sgrlist for a chromosome for WT sgrlistforchromC_WT = ps.getSgrList(sgrfileC_WT,chromosome) # Convert Watson sgr file to a watson sgrlist for a chromosome for MUT sgrlistforchromW_MUT = ps.getSgrList(sgrfileW_MUT,chromosome) # Convert Crick sgr file to a crick sgrlist for a chromosome for MUT sgrlistforchromC_MUT = ps.getSgrList(sgrfileC_MUT,chromosome) # Plot RPKM for Watson and Crick strands for the wild type species rpkm.plotRPKM(sgrlistforchromW_WT,sgrlistforchromC_WT,strains[0],chromosome,211) # Create some space between the subplots plt.subplots_adjust(hspace=0.5) # Plot RPKM for Watson and Crick strands for the knock out species rpkm.plotRPKM(sgrlistforchromW_MUT,sgrlistforchromC_MUT,strains[1],chromosome,212) # Show plot plt.show()
def internal_surf_afteraxes(cd): plt.hold(True) plt.title('') plt.ylabel('m') plt.subplots_adjust(hspace=0.05) plt.plot([multilayer_data.bathy_location,multilayer_data.bathy_location],bottom_surf_zoomed,'--k') plt.hold(False)
def gen_graph(kind, xvals, yvals, xlabel, ylabel): if len(xvals) > 10: xvals = xvals[:10] if len(yvals) > 10: yvals = yvals[:10] fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ind = np.arange(len(yvals)) if kind == 'line': ax.plot(ind, yvals[::-1]) elif kind == 'bar': ax.bar(ind, yvals[::-1]) plt.xticks(ind + .3 / 2, xvals[::-1]) plt.xlabel(xlabel, labelpad=20) plt.ylabel(ylabel) plt.margins(xmargin=0.05) plt.xticks(range(0, len(xvals))) plt.subplots_adjust(bottom=0.3, right=0.9) ax.set_xticklabels(xvals[::-1], rotation=45) io = StringIO() fig.savefig(io, format='png') graph = io.getvalue().encode('base64') return graph
def pylot_show(): sql = 'select * from douban;' cur.execute(sql) rows = cur.fetchall() # 把表中所有字段读取出来 count = [] # 每个分类的数量 category = [] # 分类 for row in rows: count.append(int(row[2])) category.append(row[1]) y_pos = np.arange(len(category)) # 定义y轴坐标数 #color = cm.jet(np.array(2)/max(count)) plt.barh(y_pos, count, color='y', align='center', alpha=0.4) # alpha图表的填充不透明度(0~1)之间 plt.yticks(y_pos, category) # 在y轴上做分类名的标记 plt.grid(axis = 'x') for count, y_pos in zip(count, y_pos): # 分类个数在图中显示的位置,就是那些数字在柱状图尾部显示的数字 plt.text(count+3, y_pos, count, horizontalalignment='center', verticalalignment='center', weight='bold') plt.ylim(+28.0, -2.0) # 可视化范围,相当于规定y轴范围 plt.title('douban_top250') # 图表的标题 fontproperties='simhei' plt.ylabel('movie category') # 图表y轴的标记 plt.subplots_adjust(bottom = 0.15) plt.xlabel('count') # 图表x轴的标记 #plt.savefig('douban.png') # 保存图片 plt.show()
def dose_plot(df,err,cols,scale='linear'): n_rows = int(np.ceil(len(cols)/3.0)) plt.figure(figsize=(20,4 * n_rows)) subs = gridspec.GridSpec(n_rows, 3) plt.subplots_adjust(hspace=0.54,wspace=0.27) for col,sub in zip(cols,subs): plt.subplot(sub) for base in df['Base'].unique(): for drug in get_drugs_with_multiple_doses(filter_rows(df,'Base',base)): data = thread_first(df, (filter_rows,'Drug',drug), (filter_rows,'Base',base), (DF.sort, 'Dose')) error = thread_first(err, (filter_rows,'Drug',drug), (filter_rows,'Base',base), (DF.sort, 'Dose')) if scale == 'linear': plt.errorbar(data['Dose'],data[col],yerr=error[col]) title = "{} vs. Dose".format(col) else: plt.errorbar(data['Dose'],data[col],yerr=error[col]) plt.xscale('log') title = "{} vs. Dose (Log Scale)".format(col) plt.xticks(data['Dose'].values,data['Dose'].values) plt.xlim(0.06,15) label('Dose ({})'.format(data.Unit.values[0]), col,title,fontsize = 15) plt.legend(df['Base'].unique(), loc = 0)
def plotTestSet(self): plt.figure(figsize=(12,6), facecolor='white') # Plot test set currents plt.subplot(3,1,1) for tr in self.testset_traces : plt.plot(tr.getTime(), tr.I, 'gray') plt.ylabel("I (nA)") plt.title('Experiment ' + self.name + " - Test Set") # Plot test set voltage plt.subplot(3,1,2) for tr in self.testset_traces : plt.plot(tr.getTime(), tr.V, 'black') plt.ylabel("Voltage (mV)") # Plot test set raster plt.subplot(3,1,3) cnt = 0 for tr in self.testset_traces : cnt += 1 if tr.spks_flag : plt.plot(tr.getSpikeTimes(), cnt*np.ones(tr.getSpikeNb()), '|', color='black', ms=5, mew=2) plt.yticks([]) plt.ylim([0, cnt+1]) plt.xlabel("Time (ms)") plt.subplots_adjust(left=0.10, bottom=0.07, right=0.95, top=0.92, wspace=0.25, hspace=0.25) plt.show()
def plot_images(data_list, data_shape="auto", fig_shape="auto"): """ plotting data on current plt object. In default,data_shape and fig_shape are auto. It means considered the data as a sqare structure. """ n_data = len(data_list) if data_shape == "auto": sqr = int(n_data ** 0.5) if sqr * sqr != n_data: data_shape = (sqr + 1, sqr + 1) else: data_shape = (sqr, sqr) plt.figure(figsize=data_shape) for i, data in enumerate(data_list): plt.subplot(data_shape[0], data_shape[1], i + 1) plt.gray() if fig_shape == "auto": fig_size = int(len(data) ** 0.5) if fig_size ** 2 != len(data): fig_shape = (fig_size + 1, fig_size + 1) else: fig_shape = (fig_size, fig_size) Z = data.reshape(fig_shape[0], fig_shape[1]) plt.imshow(Z, interpolation="nearest") plt.tick_params(labelleft="off", labelbottom="off") plt.tick_params(axis="both", which="both", left="off", bottom="off", right="off", top="off") plt.subplots_adjust(hspace=0.05) plt.subplots_adjust(wspace=0.05)
def eval_mean_error_functions(act,ang,n_vec,toy_aa,timeseries,withplot=False): """ Calculates sqrt(mean(E)) and sqrt(mean(F)) """ Err = np.zeros(6) NT = len(timeseries) size = len(ang[6:])/3 UA = ua(toy_aa.T[3:].T,np.ones(3)) fig,axis=None,None if(withplot): fig,axis=plt.subplots(3,2) plt.subplots_adjust(wspace=0.3) for K in range(3): ErrJ = np.array([(i[K]-act[K]-2.*np.sum(n_vec.T[K]*act[3:]*np.cos(np.dot(n_vec,i[3:]))))**2 for i in toy_aa]) Err[K] = np.sum(ErrJ) ErrT = np.array(((ang[K]+timeseries*ang[K+3]-UA.T[K]-2.*np.array([np.sum(ang[6+K*size:6+(K+1)*size]*np.sin(np.sum(n_vec*i,axis=1))) for i in toy_aa.T[3:].T])))**2) Err[K+3] = np.sum(ErrT) if(withplot): axis[K][0].plot(ErrJ,'.') axis[K][0].set_ylabel(r'$E$'+str(K+1)) axis[K][1].plot(ErrT,'.') axis[K][1].set_ylabel(r'$F$'+str(K+1)) if(withplot): for i in range(3): axis[i][0].set_xlabel(r'$t$') axis[i][1].set_xlabel(r'$t$') plt.show() EJ = np.sqrt(Err[:3]/NT) ET = np.sqrt(Err[3:]/NT) return np.array([EJ,ET])
def prepare_plot(data): # rearranging data into 4 arrays (just for clarity) dataX = list(data[:, 0]) dataY = list(data[:, 1]) dataZ = list(data[:, 2]) dataC = list(data[:, 3]) # creating colormap according to present data cm = plt.get_cmap('brg') cNorm = matplotlib.colors.Normalize(vmin=min(dataC), vmax=max(dataC)) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm) # plotting fig = plt.figure() fig.set_facecolor('black') ax = fig.add_subplot(111, projection='3d', axisbg='k') ax.axis('off') ax.w_xaxis.set_pane_color((0, 0, 0)) ax.w_yaxis.set_pane_color((0, 0, 0)) ax.w_zaxis.set_pane_color((0, 0, 0)) ax.grid(False) ax.scatter(dataX, dataY, dataZ, c=scalarMap.to_rgba(dataC), edgecolors=scalarMap.to_rgba(dataC), marker='.', s=5) scalarMap.set_array(dataC) plt.subplots_adjust(left=0.0, right=1.0, bottom=0.0, top=1.0) return ax
def plot_rolling_auto_home(df_attack=None,df_defence=None, window=5, nstd=1, detected_events_home=None, detected_events_away=None, sky_events=None): sns.set_context("notebook", font_scale=1.8 ,rc={"lines.linewidth": 3.5, "figure.figsize":(18,12) }) plt.subplots_adjust(bottom=0.85) mean = pd.rolling_mean(df_attack, center=True, window=window) std = pd.rolling_std(df_attack, center=True, window=window) detected_plot_extrema = df_attack.ix[argrelextrema(df_attack.values, np.greater)] df_filt_noise = df_attack[(df_attack > mean-std) & (df_attack < mean+std)] df_filt_noise = df_filt_noise.ix[detected_plot_extrema.index].dropna() df_filt_keep = df_attack[~((df_attack > mean-std) & (df_attack < mean+std))] df_filt_keep = df_filt_keep.ix[detected_plot_extrema.index].dropna() plt.plot(df_attack, color='#4CA64C', label='{} Attack'.format(all_matches[0]['home_team'].title())) plt.fill_between(df_attack.index, (mean-nstd*std), (mean+nstd*std), interpolate=False, alpha=0.4, color='#B2B2B2', label='$\mu + {} \\times \sigma$'.format(nstd)) plt.scatter(df_filt_keep.index, df_filt_keep.values, marker='*', s=120, color='#000000', zorder=10, label='Selected maxima post-filtering') plt.scatter(df_filt_noise.index, df_filt_noise.values, marker='x', s=120, color='#000000', zorder=10, label='Unselected maxima post-filtering') df_defence.apply(lambda x: -1*x).plot(color='#000000', label='{} Defence'.format(all_matches[0]['home_team'].title())) if(len(detected_events_home) > 0): classifier_events_df_home= pd.DataFrame(detected_events_home) classifier_events_df_home[classifier_events_df_home.category == 'GOAL'] if(len(detected_events_away) > 0): classifier_events_df_away= pd.DataFrame(detected_events_away) classifier_events_df_away[classifier_events_df_away.category == 'GOAL'] font0 = FontProperties(family='arial', weight='bold',style='italic', size=16) for i, row in classifier_events_df_home.iterrows(): if row.category == 'OTHER': continue plt.text(row.event, df_attack.max(), "{} {} {}".format(all_matches[0]['home_team'].upper(), row.category, row.event), rotation='vertical', color='black', bbox=dict(facecolor='green', alpha=0.2))#, transform=transform) for i, row in classifier_events_df_away.iterrows(): if row.category == 'OTHER': continue plt.text(row.event, (df_attack.max()), "{} {} {}".format(all_matches[0]['away_team'].upper(), row.category, row.event), rotation='vertical', color='black', bbox=dict(facecolor='red', alpha=0.2)) high_peak_position = 0; if(df_attack.max() > df_defence.max()): high_peak_position = -(df_defence.max() * 2.0) else: high_peak_position = -(df_defence.max() * 1.25) # Functionality to include Sky Sports text commentary updates on plot for goal events. # for i, row in pd.DataFrame(sky_events).iterrows(): # dedented_text = textwrap.dedent(row.text).strip() # plt.text(row.event, high_peak_position, "@SkySports {} AT {}:\n{}:\n{}".format(row.category, row.event.time(), row.title, textwrap.fill(dedented_text, width=40)), color='black', bbox=dict(facecolor='blue', alpha=0.2)) plt.legend(loc=4) ax = plt.gca() label = ax.set_xlabel('time') plt.ylabel('Tweet frequency') plt.title('{} vs. {} (WK {}) - rolling averages window={} mins'.format(all_matches[0]['home_team'].title(), all_matches[0]['away_team'].title(), all_matches[0]['dbname'], window)) plt.savefig('{}attack_{}_plain.pdf'.format(all_matches[0]['home_team'].upper(), all_matches[0]['away_team'].upper())) return detected_plot_extrema
def plot_tfidf_classfeats(dfs): fig = plt.figure(figsize=(12, 9), facecolor="w") for i, df in enumerate(dfs): ax = fig.add_subplot(len(dfs), 1, i+1) ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.set_frame_on(False) ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left() if i == len(dfs)-1: ax.set_xlabel("Feature name", labelpad=14, fontsize=14) ax.set_ylabel("Tf-Idf score", labelpad=16, fontsize=14) #if i == 0: ax.set_title("Mean Tf-Idf scores for label = " + str(df.label), fontsize=16) x = range(1, len(df)+1) ax.bar(x, df.tfidf, align='center', color='#3F5D7D') #ax.lines[0].set_visible(False) ax.set_xticks(x) ax.set_xlim([0,len(df)+1]) xticks = ax.set_xticklabels(df.feature) #plt.ylim(0, len(df)+2) plt.setp(xticks, rotation='vertical') #, ha='right', va='top') plt.subplots_adjust(bottom=0.24, right=1, top=0.97, hspace=0.9) plt.show()
def begin(): m = Measurement() sample_count = 0 try: fifo = open("npipe","r") add_data = AddData() f1 = plt.figure(figsize=(15,8)) [sp1, sp2, sp3, sp4, sp5] = [f1.add_subplot(231), f1.add_subplot(232), f1.add_subplot(233), f1.add_subplot(234), f1.add_subplot(235)] h1, = sp1.plot([],[]) h2, = sp2.plot([],[]) h3, = sp3.plot([],[]) h4, = sp4.plot([],[]) plt.subplots_adjust(hspace = 0.3) plt.show(block=False) while True: if sample_count == 1024*5: estimate_tf(m,[h1,h2,h3,h4],f1,[sp1, sp2, sp3, sp4, sp5]) sample_count = 0 m = Measurement() add_data = AddData() sample = get_data(fifo) add_data(m,sample) sample_count = sample_count + 1 except KeyboardInterrupt: fifo.close() exit(0)
import matplotlib.dates as mdates plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # In[6]: fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.set_title(u"C盘已使用空间的时序图") # ax.set_xlabel(u'日期') ax.set(xlabel=u'日期', ylabel=u'磁盘使用大小') # 图上时间间隔显示为10天 ax.xaxis.set_major_locator( mdates.DayLocator(bymonthday=range(1, 32), interval=10)) ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d")) plt.subplots_adjust(bottom=0.13, top=0.95) ax.plot( d['COLLECTTIME'], d['VALUE'], 'ro-', ) fig.autofmt_xdate() #自动根据标签长度进行旋转 '''for label in ax.xaxis.get_ticklabels(): #此语句完成功能同上 label.set_rotation(45) ''' plt.savefig('c.jpg') plt.show() # In[7]:
def plot_logodds_ratios_from_dict_no_bootstrap(w, mer_list, mer_dict): n_mer_length = len(mer_list) highest_weight_index = np.argsort(w)[::-1] highest_weight_index_top = highest_weight_index[0:10] lowest_weight_index = np.argsort(w) lowest_weight_index_top = lowest_weight_index[0:10] w_sort_index = np.argsort(w) #[::-1] w_sorted = w[w_sort_index] mer_sorted = np.array(mer_list)[w_sort_index] not_zero_index = np.nonzero(w_sorted != 0)[0] w_sorted = w_sorted[not_zero_index] mer_sorted = mer_sorted[not_zero_index] n_mer_length = len(mer_sorted) f, axarr = plt.subplots(1, 2, figsize=(10, 10), sharex=False) #, sharey=True axarr[0].scatter(w_sorted, np.arange(n_mer_length), color='purple', alpha=0.6, s=10) axarr[0].set_title('Logodds ratios of proximal selection') plt.sca(axarr[0]) plt.axis([np.min(w_sorted), np.max(w_sorted), 0, n_mer_length]) plt.yticks([], []) plt.xlabel('Logodds ratio') plt.ylabel('6-mers sorted on Logodds ratio') plt.tick_params(axis='x', which='major', labelsize=9) plt.tick_params(axis='x', which='minor', labelsize=7) plt.xticks(rotation=30) top_n = min(10, int(n_mer_length / 2)) w_sorted_top = np.concatenate( [w_sorted[0:top_n], w_sorted[len(w_sorted) - top_n:]]) mer_sorted_top = np.concatenate( [mer_sorted[0:top_n], mer_sorted[len(mer_sorted) - top_n:]]) axarr[1].scatter(w_sorted_top, np.arange(2 * top_n), color='purple', alpha=0.7, s=10) axarr[1].plot( [np.min(w_sorted_top), np.max(w_sorted_top)], [top_n - 0.25, top_n - 0.25], color='black', alpha=1.0) axarr[1].plot( [np.min(w_sorted_top), np.max(w_sorted_top)], [top_n - 0.75, top_n - 0.75], color='black', alpha=1.0) axarr[1].set_title('Zoom-in view') axarr[1].yaxis.tick_right() plt.sca(axarr[1]) plt.axis([np.min(w_sorted_top), np.max(w_sorted_top), -1, 2 * top_n]) plt.yticks(np.arange(2 * top_n), mer_sorted_top.tolist(), color='purple') plt.xlabel('Logodds ratio') plt.tick_params(axis='x', which='major', labelsize=9) plt.tick_params(axis='x', which='minor', labelsize=7) plt.xticks(rotation=30) f.tight_layout() plt.subplots_adjust(top=0.85, wspace=0.4) transFigure = f.transFigure.inverted() top_1_all = transFigure.transform(axarr[0].transData.transform( [w_sorted[n_mer_length - top_n], n_mer_length - top_n])) top_n_all = transFigure.transform(axarr[0].transData.transform( [w_sorted[n_mer_length - 1], n_mer_length - 1])) bottom_1_all = transFigure.transform(axarr[0].transData.transform( [w_sorted[0], 0])) bottom_n_all = transFigure.transform(axarr[0].transData.transform( [w_sorted[top_n - 1], top_n - 1])) top_1_zoom = transFigure.transform(axarr[1].transData.transform( [w_sorted_top[top_n], top_n])) top_n_zoom = transFigure.transform(axarr[1].transData.transform( [w_sorted_top[2 * top_n - 1], 2 * top_n - 1])) bottom_1_zoom = transFigure.transform(axarr[1].transData.transform( [w_sorted_top[0], 0])) bottom_n_zoom = transFigure.transform(axarr[1].transData.transform( [w_sorted_top[top_n - 1], top_n - 1])) line_top_1 = pltl.Line2D((top_1_all[0], top_1_zoom[0]), (top_1_all[1], top_1_zoom[1]), transform=f.transFigure, color='black', linestyle='dashed') line_top_n = pltl.Line2D((top_n_all[0], top_n_zoom[0]), (top_n_all[1], top_n_zoom[1]), transform=f.transFigure, color='black', linestyle='dashed') line_bottom_1 = pltl.Line2D((bottom_1_all[0], bottom_1_zoom[0]), (bottom_1_all[1], bottom_1_zoom[1]), transform=f.transFigure, color='black', linestyle='dashed') line_bottom_n = pltl.Line2D((bottom_n_all[0], bottom_n_zoom[0]), (bottom_n_all[1], bottom_n_zoom[1]), transform=f.transFigure, color='black', linestyle='dashed') f.lines = [line_top_1, line_top_n, line_bottom_1, line_bottom_n] plt.show()
cmap = fig.colorbar(image,orientation='horizontal',fraction=0.025,pad=0.01,aspect=50, ticks=vec_l)#,spacing='proportional', ticks=vec_l, boundaries=vec_l, format='%1i') cmap.ax.tick_params(labelsize=18) ax.text(-0.09, -1.38,r'$\ell_{max}$', fontsize=22) ax.text(-2.01, 0.78, 'EE', fontsize=30) #ax.text(0.35, 0.222,'Doppler', fontsize=16,color='grey') #ax.text(0.35, -0.148,'Doppler', fontsize=16,color='lightgrey') #ax.text(1.2, 0.34,'Aberration', fontsize=16,color='grey') #ax.text(1.3, 0.13,'Aberration', fontsize=16,color='lightgrey') plt.legend(handles=[legend1, legend2, legend3, legend4],loc="lower right",title="SMICA NILC", title_fontsize=15.3, ncol=2,handletextpad=1.24)._legend_box.align='left' plt.gca().set_axis_off() plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) plt.margins(0,0) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) def coord_plot(long_start,lat_start,color_var,size_var,text): hp.visufunc.projtext(long_start,lat_start,text,lonlat=True,color=color_var,size=2*size_var) coord_color='grey' coord_color2='grey' #coord_size=4 #coord_plot(0+5,1.5,coord_color,coord_size,'0') #coord_plot(30+8,1.5,coord_color,coord_size,'30') #coord_plot(60+8,1.5,coord_color,coord_size,'60') #coord_plot(90+8,1.5,coord_color,coord_size,'90') #coord_plot(120+11,1.5,coord_color,coord_size,'120')
# Plot bars and create text labels for the table cell_text = [] for row in range(n_rows): plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row]) y_offset = y_offset + data[row] cell_text.append(['%1.1f' % (x/1000.0) for x in y_offset]) # Reverse colors and text labels to display the last value at the top. colors = colors[::-1] cell_text.reverse() # Add a table at the bottom of the axes the_table = plt.table(cellText=cell_text, rowLabels=rows, rowColours=colors, colLabels=columns, loc='bottom') # Adjust layout to make room for the table: plt.subplots_adjust(left=0.2, bottom=0.2) plt.ylabel("Loss in ${0}'s".format(value_increment)) plt.yticks(values * value_increment, ['%d' % val for val in values]) plt.xticks([]) plt.title('Loss by Disaster') plt.show() from utilities import * save('plot/bartest0')
def plot_states(tf, alldd, state_profiling_table_file="", waitwins=[], conf={}, alldd_w=[], trdata=([], [], [])): #------------------------- plot_states_pdf(tf, alldd, state_profiling_table_file, waitwins, conf, alldd_w, trdata) return print conf ddtup_tr, trins, trcounts, totrcnts, cumrcnts = trdata alldd_tr = ddtup_tr if state_profiling_table_file: f = open(state_profiling_table_file, 'r') state_profiling_table = [l.strip().split(',') for l in f.readlines()] f.close() dd0, dd1, dd2, dd3 = alldd print 'dd0:', dd0 print 'dd3:', dd3 c0_ts = [ts for (st, ts) in dd0] c0_st = [st for (st, ts) in dd0] c1_ts = [ts for (st, ts) in dd1] c1_st = [st for (st, ts) in dd1] c2_ts = [ts for (st, ts) in dd2] c2_st = [st for (st, ts) in dd2] c3_ts = [ts for (st, ts) in dd3] c3_st = [st for (st, ts) in dd3] all_states_visited = sorted( [int(s) for s in set(c0_st + c1_st + c2_st + c3_st)]) visited_states_enumeration = enumerate(all_states_visited) visited_states_dict = {} for i, s in visited_states_enumeration: visited_states_dict[s] = i vsd = visited_states_dict #print all_states_visited #print visited_states_enumeration #print visited_states_dict #raw_input() fig = plt.figure(figsize=conf['figsize']) ax = fig.add_subplot(111) plt.margins(0.1) #extend axis limits by this fraction plt.subplots_adjust(left=0.1, right=0.75, bottom=0.1, top=0.85) plt.title('State transition timing diagram') dy = 0.2 c0_st = [float(s) + 0 * dy for s in c0_st] c1_st = [float(s) + 1 * dy for s in c1_st] c2_st = [float(s) + 2 * dy for s in c2_st] c3_st = [float(s) + 3 * dy for s in c3_st] plt.plot(c0_ts, c0_st, 'r.-', c1_ts, c1_st, 'g.-', c2_ts, c2_st, 'b.-', c3_ts, c3_st, 'k.-') cx_ts_st_pairs = ((c0_ts, c0_st), (c1_ts, c1_st), (c2_ts, c2_st), (c3_ts, c3_st)) xmin, xmax = ax.get_xlim() xmin = 0 xmax = int(math.ceil(xmax)) #xmax = 250000 #xmax = 500000 #add ofdm symbol markers #ax.xaxis.set_ticks(range(45000,130000,5000)) #xticks_extra = range(45000,200000,5000) ####xticks_extra = range(xmin,xmax,5000) ####labels_extra = [""] * len(xticks_extra) #plt.axvline(x=c0_ts_tail_entry, ls='--') #plt.axvline(x=c3_ts_fini_exit, ls='--') #plt.annotate('Tail sym latency: %dcy' % tail_sym_latency, xy=(c3_ts_fini_exit + 200, 10.5)) ts_max = max(c3_ts) + 1000 state_ids = [] for e in state_profiling_table: id, lbl = e id = int(id) state_ids.append(id) xy = (ts_max, id + dy) plt.annotate(lbl, xy) ymin, ymax = ax.get_ylim() #---------------------------------------- ### read and prepare configuration #---------------------------------------- ltf_proc_entry_state = conf['ltf_proc_entry_state'] ofdm_sym_dur_us = conf['ofdm_sym_dur_us'] clock_rate_mhz = conf['clock_rate_mhz'] n_ofdm_syms_in_pkt = conf['n_ofdm_syms_in_pkt'] n_ofdm_syms_in_stf = conf['n_ofdm_syms_in_stf'] slack_us = conf['slack_us'] ssi = conf['sample_consumption_starts_in_state'] sso = conf['sample_consumption_ends_in_state'] spi = conf['rx_pkt_init_state'] prepad_us = conf['prepad_dur_us'] postpad_us = conf['postpad_dur_us'] tri = conf['traceReaderInTSID'] tro = conf['traceReaderOutTSID'] WAIT_WIN_THRESHOLD = conf['WAIT_WIN_THRESHOLD'] slack_cyc = slack_us * clock_rate_mhz ofdm_sym_dur_cyc = ofdm_sym_dur_us * clock_rate_mhz prepad_cyc = prepad_us * clock_rate_mhz postpad_cyc = postpad_us * clock_rate_mhz pkt_us = n_ofdm_syms_in_pkt * ofdm_sym_dur_us plt.yticks(state_ids) plt.xlabel('Timestamp (cycle)') plt.ylabel('State number') #add ofdm symbol markers xticks, lbls = plt.xticks() xticks = list(xticks) #lbls = [l.get_text() for l in lbls] lbls = ['%d' % int(t) for t in xticks] print '999999999999999999' print xticks print lbls plt.legend(['dsp0', 'dsp1', 'dsp2', 'dsp3'], loc='upper left') ###---------------------------------------- ##### add smallest-packet region indicators ###---------------------------------------- ###find pkt start points ##st_is = [i for i, (st,ts) in enumerate(dd0) if st == ltf_proc_entry_state] ##entry_is = st_is[::2] ###plot indicators ##width = ofdm_sym_dur_cyc * (n_ofdm_syms_in_pkt - n_ofdm_syms_in_stf) + slack_cyc ##ymin, ymax = ax.get_ylim() ##height = ymax - ymin ##currentAxis = plt.gca() ##for i in entry_is: ## st, ts = dd0[i] ## #print ts ## someX, someY = ts, ymin ## currentAxis.add_patch(Rectangle((someX, someY), width, height, facecolor="grey")) #---------------------------------------- ### add sample rx cycle indicators #---------------------------------------- #ddtup_tr, trins, trcounts, totrcnts, cumrcnts = trdata #it = iter(cumrcnts) #find pkt start points #st_is = [i for i, (st,ts) in enumerate(dd0) if st == ssi] #entry_is = st_is[::2] st_is_ = [i for i, (st, ts) in enumerate(dd0) if st == spi] st_is_ = st_is_[::2] st_is = [i + 2 for i in st_is_] entry_is = st_is xmin -= ofdm_sym_dur_cyc plt.xlim(xmin, xmax) #---------------------------- #plot traceReader entry/exit #---------------------------- plot_traceReader_entry_exit(alldd_tr, tri, tro, ymax) #--------------------------- #annotate state transitions #--------------------------- annotate_state_transitions(cx_ts_st_pairs, dy) #--------------- #plot indicators #--------------- yrange = (ymin, ymax) spcs = [] spcs = plot_indicators(conf, cumrcnts, yrange, entry_is, alldd, spcs) #---------------- #plot waitwins #---------------- plot_waitwins(waitwins, dy, WAIT_WIN_THRESHOLD) spcs.append(xmax) xticks_extra = [] for x1, x2 in zip(spcs[:-1], spcs[1:]): xticks_extra += range(x1, x2, 5000) labels_extra = [""] * len(xticks_extra) #xticks += xticks_extra #lbls += labels_extra xticks = xticks_extra lbls = labels_extra print xticks print lbls plt.xticks(xticks, lbls) #plt.xticks(xticks) plt.grid(True) #plt.show() plt.savefig('%s_plot_states.png' % tf)
def plot_states_pdf(tf, alldd, state_profiling_table_file="", waitwins=[], conf={}, alldd_w=[], trdata=([], [], [])): #------------------------- fig = plt.figure(figsize=(10, 8), dpi=1200) ax = fig.add_subplot(111) axR = plt.subplot(1, 1, 1, sharex=ax, frameon=False) axR.yaxis.tick_right() axR.yaxis.set_label_position("right") #---------------------------------------- ### read and prepare configuration #---------------------------------------- ofdm_sym_dur_us = conf['ofdm_sym_dur_us'] clock_rate_mhz = conf['clock_rate_mhz'] ssi = conf['sample_consumption_starts_in_state'] sso = conf['sample_consumption_ends_in_state'] spi = conf['rx_pkt_init_state'] spo = conf['rx_data_finish_state'] ddtup_tr, trins, trcounts, totrcnts, cumrcnts = trdata alldd_tr = ddtup_tr if state_profiling_table_file: f = open(state_profiling_table_file, 'r') state_profiling_table = [l.strip().split(',') for l in f.readlines()] f.close() dd0, dd1, dd2, dd3 = alldd print 'dd0:', dd0 print 'dd3:', dd3 c0_ts = [ts for (st, ts) in dd0] c0_st = [st for (st, ts) in dd0] c1_ts = [ts for (st, ts) in dd1] c1_st = [st for (st, ts) in dd1] c2_ts = [ts for (st, ts) in dd2] c2_st = [st for (st, ts) in dd2] c3_ts = [ts for (st, ts) in dd3] c3_st = [st for (st, ts) in dd3] ls = [[ts for st, ts in ddi if st == sso] for ddi in alldd] xmax = max([l[1] for l in ls]) xmin = 0 plt.xlim(xmin, xmax) all_states_visited = sorted( [int(s) for s in set(c0_st + c1_st + c2_st + c3_st)]) visited_states_enumeration = enumerate(all_states_visited) visited_states_dict = {} for i, s in visited_states_enumeration: visited_states_dict[s] = i vsd = visited_states_dict #print all_states_visited #print visited_states_enumeration #print visited_states_dict #raw_input() dy = 0.2 c0_st = [float(vsd[int(s)]) + 0 * dy for s in c0_st] c1_st = [float(vsd[int(s)]) + 1 * dy for s in c1_st] c2_st = [float(vsd[int(s)]) + 2 * dy for s in c2_st] c3_st = [float(vsd[int(s)]) + 3 * dy for s in c3_st] plt.plot(c0_ts, c0_st, 'rx-', c1_ts, c1_st, 'gx-', c2_ts, c2_st, 'bx-', c3_ts, c3_st, 'k.-') xmin, xmax = ax.get_xlim() xmin = 0 xmax = int(math.ceil(xmax)) ts_max = xmax + 1000 state_ids = [] yy = [] lbls = [] for e in state_profiling_table: id, lbl = e id = int(id) if not id in vsd: continue id = vsd[id] state_ids.append(id) xy = (ts_max, id + dy) print xy #plt.annotate(lbl.strip(), xy) yy.append(id) lbls.append("%2d %s" % (id, lbl)) #plt.yticks(yy, lbls) axR.set_yticklabels(lbls) ymin, ymax = ax.get_ylim() #ax.axes.get_yaxis().set_visible(False) ax.set_frame_on(False) ax.get_xaxis().tick_bottom() plt.grid(True) ofdm_sym_dur_cyc = ofdm_sym_dur_us * clock_rate_mhz plt.yticks(state_ids) #ax.set_yticklabels(state_ids) plt.xlabel('Timestamp (cycle)') #plt.ylabel('State number') #add ofdm symbol markers xticks, lbls = plt.xticks() xticks = list(xticks) #lbls = [l.get_text() for l in lbls] lbls = ['%d' % int(t) for t in xticks] print xticks print lbls plt.legend(['dsp0', 'dsp1', 'dsp2', 'dsp3'], loc='upper left') xmin -= ofdm_sym_dur_cyc plt.xlim(xmin, xmax) #plt.margins(0.5) #extend axis limits by this fraction plt.tight_layout() plt.subplots_adjust(left=0.07, right=0.55, bottom=0.18, top=0.98, wspace=0, hspace=0) #rmargin = 100000 #bmargin = 10000 #x0, x1, y0, y1 = plt.axis() #plt.axis((x0, x1 + rmargin, y0, y1)) locs, labels = plt.xticks() plt.setp(labels, rotation=30) plt.savefig('%s_plot_states.png.pdf' % tf)
def plot_joint_states(tf, tfst): f = open(tf, 'rb') d = pickle.load(f) f.close() print d f = open(tfst, 'rb') dst = pickle.load(f) f.close() print dst colors = { 'test1-st': 'c', 'test2-st': 'r', 'test3-st': 'y', 'test5-st': 'm', 'test6-st': 'g', 'test8-st': 'b' } hatches = { 'test1-st': '/', 'test2-st': '\\', 'test3-st': '.', 'test5-st': 'x', 'test6-st': '+', 'test8-st': '-' } cases = [1, 2, 3, 5, 6, 8] tags_ord = ['test%d-st' % v for v in cases] #labels = ['Scheme%d' % v for v in cases] labels = ['1) L1P all cache, L1D all cache',\ '2) L1P all cache, L1D disabled',\ '3) L1P disabled , L1D disabled',\ '5) L1P all cache, L1D all SRAM',\ '6) L1P all cache, L1D 28K SRAM',\ '8) L1P all cache, L1D all SRAM/stack in L1D'] bar_array = [] #------------------------------ # tail sym histogram #------------------------------ #width=0.2 width = 0.15 offset = 0 plt.figure(figsize=(15, 10)) for tag in tags_ord: [x, y, errx, erry, ys] = d[tag] x = [v + offset for v in x] #errx = [v + offset for v in errx] errx = [v + (width / 2) for v in x] offset += width if tag in colors: c = colors[tag] else: c = 'k' h = hatches[tag] b = plt.bar(x, y, width=width, color=c, yerr=erry, hatch=h) bar_array.append(b) #(_, caps, _) = plt.errorbar(errx, ymean, ydiff, capsize=20, elinewidth=3, linestyle='.') (_, caps, _) = plt.errorbar(errx, y, erry, capsize=5, elinewidth=4, linestyle='.', ecolor='black') for cap in caps: cap.set_color('black') cap.set_markeredgewidth(2) plt.legend(bar_array, labels) plt.xlabel('Core ID (-1 is for the total symbol latency)') #plt.xticks(['Net','Core1','Core2','Core3','Core4']) plt.xticks([-1, 0, 1, 2, 3]) plt.ylabel('Cycles') plt.title('Mean tail symbol latency (err: [min, max])') outfname = '%s_plot_hist.png' % tf print 'saving to %s' % outfname plt.savefig(outfname) #------------------------------ # state histogram #------------------------------ width = 0.8 offset = 0 plt.figure(figsize=(10, 10)) plt.subplots_adjust(left=0.15, right=0.9, bottom=0.05, top=0.95) xs = range(len(cases)) for tag, x in zip(tags_ord, xs): [y, erry, ys] = dst[tag] x = [x] x = [v + offset for v in x] #errx = [v + offset for v in errx] errx = [v + (width / 2) for v in x] offset += width if tag in colors: c = colors[tag] else: c = 'k' h = hatches[tag] b = plt.bar(x, y, width=width, color=c, yerr=erry, hatch=h) bar_array.append(b) #(_, caps, _) = plt.errorbar(errx, ymean, ydiff, capsize=20, elinewidth=3, linestyle='.') (_, caps, _) = plt.errorbar(errx, y, erry, capsize=5, elinewidth=4, linestyle='.', ecolor='black') plt.annotate('%0.2f%%' % (erry * 100 / y), xy=(x[0], y + 3 * erry)) for cap in caps: cap.set_color('black') cap.set_markeredgewidth(2) plt.xlabel('Various memory configurations') #plt.xticks(['Net','Core1','Core2','Core3','Core4']) plt.xticks([]) plt.ylabel('Cycles') plt.title('Mean packet compute time (err: [min, max])') #plt.legend(bar_array, labels) #plt.legend(bar_array, labels, loc='upper left') #------ ## Shink current axis by 25% in width #ax = plt.subplot(111) #box = ax.get_position() #ax.set_position([box.x0, box.y0, box.width * 0.75, box.height]) ## Put a legend to the right of the current axis ##plt.legend(bar_array, labels) #ax.legend(bar_array, labels, loc='center left', bbox_to_anchor=(1, 0.5)) #------ # Shink current axis by 25% in height ax = plt.subplot(111) box = ax.get_position() #ax.set_position([box.x0, box.y0, box.width, box.height * 0.65]) ax.set_position( [box.x0, box.y0 + box.height * 0.30, box.width, box.height * 0.70]) # Put a legend to the bottom of the current axis #plt.legend(bar_array, labels) #ax.legend(bar_array, labels, loc='lower center', bbox_to_anchor=(0.5, 0)) ax.legend(bar_array, labels, loc='upper center', bbox_to_anchor=(0.5, -0.05)) outfname = '%s_plot_st_hist.png' % tfst print 'saving to %s' % outfname plt.savefig(outfname)
ordered_CluStream_16 = [2.057, 1.236, 5.669] unordered_Clustream_16 = [2.717, 0.801, -4.290] ordered_CluStream_32 = [9.625, 5.149, 21.505] unordered_Clustream_32 = [10.406, 5.910, 33.113] # ordered_CluStream_16 = [11.682, 6.485, 27.174] # unordered_Clustream_16 = [13.123, 6.711, 28.818] pos = list(range(len(ordered_CluStream_16))) width = 0.2 plt.rcParams['xtick.direction'] = 'in' plt.rcParams['ytick.direction'] = 'in' fig, ax = plt.subplots(figsize=(4.3, 2.5)) plt.subplots_adjust(left=0.15, bottom=0.13, right=0.95, top=0.95, wspace=0.00, hspace=0.00) rects1 = plt.bar(pos, ordered_CluStream_32, width, color='lightpink', edgecolor='black') rects2 = plt.bar(pos, ordered_CluStream_16, width, bottom=ordered_CluStream_32, color='lightpink', label="ordered-CluStream", edgecolor='black')
def plot_results(self, filename=None): """ Plot the Tearsheet """ rc = { 'lines.linewidth': 1.0, 'axes.facecolor': '0.995', 'figure.facecolor': '0.97', 'font.family': 'serif', 'font.serif': 'Ubuntu', 'font.monospace': 'Ubuntu Mono', 'font.size': 10, 'axes.labelsize': 10, 'axes.labelweight': 'bold', 'axes.titlesize': 10, 'xtick.labelsize': 8, 'ytick.labelsize': 8, 'legend.fontsize': 10, 'figure.titlesize': 12 } sns.set_context(rc) sns.set_style("whitegrid") sns.set_palette("deep", desat=.6) if self.rolling_sharpe: offset_index = 1 else: offset_index = 0 vertical_sections = 6 + offset_index fig = plt.figure(figsize=(10, vertical_sections * 5.5)) fig.suptitle(self.title, y=0.94, weight='bold') gs = gridspec.GridSpec(vertical_sections, 3, wspace=0.25, hspace=1) stats = self.get_results() ax_equity = plt.subplot(gs[:2, :]) if self.rolling_sharpe: ax_sharpe = plt.subplot(gs[2, :]) ax_drawdown = plt.subplot(gs[2 + offset_index, :]) ax_monthly_returns = plt.subplot(gs[3 + offset_index, :2]) ax_yearly_returns = plt.subplot(gs[3 + offset_index, 2]) ax_txt_curve = plt.subplot(gs[4 + offset_index:, 0]) ax_txt_trade = plt.subplot(gs[4 + offset_index:, 1]) ax_txt_time = plt.subplot(gs[4 + offset_index:, 2]) self._plot_equity(stats, ax=ax_equity) if self.rolling_sharpe: self._plot_rolling_sharpe(stats, ax=ax_sharpe) self._plot_drawdown(stats, ax=ax_drawdown) self._plot_monthly_returns(stats, ax=ax_monthly_returns) self._plot_yearly_returns(stats, ax=ax_yearly_returns) self._plot_txt_curve(stats, ax=ax_txt_curve) self._plot_txt_trade(stats, ax=ax_txt_trade) self._plot_txt_time(stats, ax=ax_txt_time) # Plot the figure # plt.show(block=False) plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1) plt.show() if filename is not None: fig.savefig(filename, dpi=150, bbox_inches='tight')
def plot_logodds_ratios(w, bootstrap, unique4mer=False): #Construct 4/6-mer lookup dictionaries bases = "ACGT" mer4 = [] mer6 = [] mer6_dict = {} mer6_i = 0 for base1 in bases: for base2 in bases: for base3 in bases: for base4 in bases: mer4.append(base1 + base2 + base3 + base4) for base5 in bases: for base6 in bases: mer6.append(base1 + base2 + base3 + base4 + base5 + base6) mer6_dict[base1 + base2 + base3 + base4 + base5 + base6] = mer6_i mer6_i += 1 n_mer_length = len(mer6) highest_weight_index = np.argsort(w)[::-1] highest_weight_index_top = highest_weight_index[0:10] lowest_weight_index = np.argsort(w) lowest_weight_index_top = lowest_weight_index[0:10] w_sort_index = np.argsort(w) #[::-1] w_sorted = w[w_sort_index] mer_sorted = np.array(mer6)[w_sort_index] bootstrap_sorted = bootstrap[:, w_sort_index] not_zero_index = np.nonzero(w_sorted != 0)[0] w_sorted = w_sorted[not_zero_index] mer_sorted = mer_sorted[not_zero_index] bootstrap_sorted = bootstrap_sorted[:, not_zero_index] n_mer_length = len(mer_sorted) f, axarr = plt.subplots(1, 2, figsize=(10, 10), sharex=False) #, sharey=True for i in range(0, n_mer_length): axarr[0].plot([ 2 * w_sorted[i] - bootstrap_sorted[0, i], 2 * w_sorted[i] - bootstrap_sorted[1, i] ], [i, i], color='black', alpha=1.0) axarr[0].scatter(w_sorted, np.arange(n_mer_length), color='purple', alpha=0.6, s=10) axarr[0].set_title('Logodds ratios of proximal selection') plt.sca(axarr[0]) plt.axis([np.min(w_sorted), np.max(w_sorted), 0, n_mer_length]) plt.yticks([], []) plt.xlabel('Logodds ratio') plt.ylabel('6-mers sorted on Logodds ratio') plt.tick_params(axis='x', which='major', labelsize=9) plt.tick_params(axis='x', which='minor', labelsize=7) plt.xticks(rotation=30) top_n = int(min(10, len(w_sorted) / 2)) w_sorted_top = np.concatenate( [w_sorted[0:top_n], w_sorted[len(w_sorted) - top_n:]]) mer_sorted_top = np.concatenate( [mer_sorted[0:top_n], mer_sorted[len(mer_sorted) - top_n:]]) bootstrap_sorted_top = np.concatenate([ bootstrap_sorted[:, 0:top_n], bootstrap_sorted[:, bootstrap_sorted.shape[1] - top_n:] ], axis=1) if unique4mer == True: unique_4mer_top_dict = {} unique_4mer_bottom_dict = {} w_sorted_top = [] mer_sorted_top = [] bootstrap_sorted_top = [] w_sorted_bottom = [] mer_sorted_bottom = [] bootstrap_sorted_bottom = [] k = 0 while len(mer_sorted_top) < top_n: l_motif = mer_sorted[k] unique_s_motif = True for p in range(0, len(l_motif) - 3): s_motif = l_motif[p:p + 4] if s_motif in unique_4mer_top_dict: unique_s_motif = False if unique_s_motif == True: for p in range(0, len(l_motif) - 3): s_motif = l_motif[p:p + 4] unique_4mer_top_dict[s_motif] = True mer_sorted_top.append(mer_sorted[k]) w_sorted_top.append(w_sorted[k]) bootstrap_sorted_top.append(bootstrap_sorted[:, k]) k += 1 k = 0 mer_sorted_t = mer_sorted[::-1] w_sorted_t = w_sorted[::-1] bootstrap_sorted_t = bootstrap_sorted[:, ::-1] while len(mer_sorted_bottom) < top_n: l_motif = mer_sorted_t[k] unique_s_motif = True for p in range(0, len(l_motif) - 3): s_motif = l_motif[p:p + 4] if s_motif in unique_4mer_bottom_dict: unique_s_motif = False if unique_s_motif == True: for p in range(0, len(l_motif) - 3): s_motif = l_motif[p:p + 4] unique_4mer_bottom_dict[s_motif] = True mer_sorted_bottom.append(mer_sorted_t[k]) w_sorted_bottom.append(w_sorted_t[k]) bootstrap_sorted_bottom.append(bootstrap_sorted_t[:, k]) k += 1 bootstrap_sorted_top = np.array(bootstrap_sorted_top) bootstrap_sorted_top = bootstrap_sorted_top.T bootstrap_sorted_bottom = np.array(bootstrap_sorted_bottom) bootstrap_sorted_bottom = bootstrap_sorted_bottom.T[:, ::-1] w_sorted_top = np.concatenate( [np.array(w_sorted_top), np.array(w_sorted_bottom)[::-1]]) mer_sorted_top = np.concatenate( [np.array(mer_sorted_top), np.array(mer_sorted_bottom)[::-1]]) bootstrap_sorted_top = np.concatenate( [bootstrap_sorted_top, bootstrap_sorted_bottom], axis=1) for i in range(0, 2 * top_n): axarr[1].plot([ 2 * w_sorted_top[i] - bootstrap_sorted_top[0, i], 2 * w_sorted_top[i] - bootstrap_sorted_top[1, i] ], [i, i], color='black', alpha=1.0) axarr[1].scatter(w_sorted_top, np.arange(2 * top_n), color='purple', alpha=0.7, s=10) axarr[1].plot( [np.min(w_sorted_top), np.max(w_sorted_top)], [top_n - 0.25, top_n - 0.25], color='black', alpha=1.0) axarr[1].plot( [np.min(w_sorted_top), np.max(w_sorted_top)], [top_n - 0.75, top_n - 0.75], color='black', alpha=1.0) axarr[1].set_title('Zoom-in view') axarr[1].yaxis.tick_right() plt.sca(axarr[1]) plt.axis([np.min(w_sorted_top), np.max(w_sorted_top), -1, 2 * top_n]) plt.yticks(np.arange(2 * top_n), mer_sorted_top.tolist(), color='purple') plt.xlabel('Logodds ratio') plt.tick_params(axis='x', which='major', labelsize=9) plt.tick_params(axis='x', which='minor', labelsize=7) plt.xticks(rotation=30) f.tight_layout() plt.subplots_adjust(top=0.85, wspace=0.4) transFigure = f.transFigure.inverted() top_1_all = transFigure.transform(axarr[0].transData.transform( [w_sorted[n_mer_length - top_n], n_mer_length - top_n])) top_n_all = transFigure.transform(axarr[0].transData.transform( [w_sorted[n_mer_length - 1], n_mer_length - 1])) bottom_1_all = transFigure.transform(axarr[0].transData.transform( [w_sorted[0], 0])) bottom_n_all = transFigure.transform(axarr[0].transData.transform( [w_sorted[top_n - 1], top_n - 1])) top_1_zoom = transFigure.transform(axarr[1].transData.transform( [w_sorted_top[top_n], top_n])) top_n_zoom = transFigure.transform(axarr[1].transData.transform( [w_sorted_top[2 * top_n - 1], 2 * top_n - 1])) bottom_1_zoom = transFigure.transform(axarr[1].transData.transform( [w_sorted_top[0], 0])) bottom_n_zoom = transFigure.transform(axarr[1].transData.transform( [w_sorted_top[top_n - 1], top_n - 1])) line_top_1 = pltl.Line2D((top_1_all[0], top_1_zoom[0]), (top_1_all[1], top_1_zoom[1]), transform=f.transFigure, color='black', linestyle='dashed') line_top_n = pltl.Line2D((top_n_all[0], top_n_zoom[0]), (top_n_all[1], top_n_zoom[1]), transform=f.transFigure, color='black', linestyle='dashed') line_bottom_1 = pltl.Line2D((bottom_1_all[0], bottom_1_zoom[0]), (bottom_1_all[1], bottom_1_zoom[1]), transform=f.transFigure, color='black', linestyle='dashed') line_bottom_n = pltl.Line2D((bottom_n_all[0], bottom_n_zoom[0]), (bottom_n_all[1], bottom_n_zoom[1]), transform=f.transFigure, color='black', linestyle='dashed') f.lines = [line_top_1, line_top_n, line_bottom_1, line_bottom_n] plt.show()
ax[1].vlines( 25.0, 1.20 - 0.27, 1.20 + 0.27) ax[1].vlines( 10.5, 1.07 - 0.33, 1.07 + 0.33) ax[1].vlines(-11.5, 0.56 - 0.26, 0.55 + 0.26) ax[1].vlines(-32.0, 0.34 - 0.18, 0.34 + 0.18) ax[1].vlines( 26.5, 1.24 - 0.33, 1.24 + 0.33) ax[2].text( 47.5, 0.04,"o",fontsize=17,horizontalalignment='center',verticalalignment='center') ax[2].text( 23.0, 0.64,"o",fontsize=17,horizontalalignment='center',verticalalignment='center') ax[2].text( 10.5, 0.51,"o",fontsize=17,horizontalalignment='center',verticalalignment='center') ax[2].text(-18.5,-1.15,"o",fontsize=17,horizontalalignment='center',verticalalignment='center') ax[2].text(-29.5,-0.91,"o",fontsize=17,horizontalalignment='center',verticalalignment='center') ax[2].vlines( 47.5, 0.04 - 0.16, 0.04 + 0.16) ax[2].vlines( 23.0, 0.64 - 0.29, 0.64 + 0.29) ax[2].vlines( 10.5, 0.51 - 1.22, 0.51 + 1.22) ax[2].vlines(-18.5,-1.15 - 0.61,-1.15 + 0.61) ax[2].vlines(-29.5,-0.91 - 0.36,-0.91 + 0.36) plt.subplots_adjust(left=0.12,right=0.98,top=0.92,bottom=0.08) outpdf = outfile+'.pdf' outpng = outfile+'.png' plt.savefig(outpng, bbox_inches='tight', pad_inches=0.0) plt.savefig(outpdf, bbox_inches='tight', pad_inches=0.0) if (len(sys.argv) == 2 and sys.argv[1] == 'show') : plt.show() print("figure is saved to " + outpng + " and " + outpdf)
if bdens_slice[xpoint_loc_tmp] > bdens_tolerence: #print('bdens criterion triggered') #print(xpoint_loc) xpoint_dellist.append(index) #just for plotting, doesn't make an actual difference for spectra xpoint_list_ind = np.array(xpoint_list_ind) xpoint_list_ind = np.delete(xpoint_list_ind, xpoint_dellist) print('number of secondary x-points : ', np.size(xpoint_list_ind)) #plotting for sanity check fig, (ax1, ax2) = plt.subplots(2,1,sharex=True) plt.subplots_adjust(hspace=0) dens = myfld['dens'][0,bottomcut:topcut,leftcut:rightcut] dens_shape = np.shape(dens) xlen = dens_shape[1] xext = xlen * grid_to_plot_coord yext = dens_shape[0] * grid_to_plot_coord ax1.imshow(np.rot90(dens), origin='lower',vmin=0,vmax=20,extent=[-yext/2,yext/2,-xext/2,xext/2]) xpoint_list2 = xpoint_list_ind * istep / c_omp / 1000 - yext/2. #just for plotting local min xpoints which won't account for inclined current layer ''' for j in range(len(xpoint_list2)): myx = xpoint_list2[j] if myx < 0: myy=.02
def summary_plot( shap_values, features=None, feature_names=None, max_display=None, plot_type=None, color=None, axis_color="#333333", title=None, alpha=1, show=True, get_png=False, sort=True, color_bar=True, plot_size="auto", layered_violin_max_num_bins=20, class_names=None, class_inds=None, color_bar_label=labels["FEATURE_VALUE"], # depreciated auto_size_plot=None, ): """Create a SHAP summary plot, colored by feature values when they are provided. Parameters ---------- shap_values : numpy.array For single output explanations this is a matrix of SHAP values (# samples x # features). For multi-output explanations this is a list of such matrices of SHAP values. features : numpy.array or pandas.DataFrame or list Matrix of feature values (# samples x # features) or a feature_names list as shorthand feature_names : list Names of the features (length # features) max_display : int How many top features to include in the plot (default is 20, or 7 for interaction plots) plot_type : "dot" (default for single output), "bar" (default for multi-output), "violin", or "compact_dot". What type of summary plot to produce. Note that "compact_dot" is only used for SHAP interaction values. plot_size : "auto" (default), float, (float, float), or None What size to make the plot. By default the size is auto-scaled based on the number of features that are being displayed. Passing a single float will cause each row to be that many inches high. Passing a pair of floats will scale the plot by that number of inches. If None is passed then the size of the current figure will be left unchanged. """ # deprecation warnings if auto_size_plot is not None: warnings.warn( "auto_size_plot=False is deprecated and is now ignored! Use plot_size=None instead." ) multi_class = False if isinstance(shap_values, list): multi_class = True if plot_type is None: plot_type = "bar" # default for multi-output explanations assert plot_type == "bar", "Only plot_type = 'bar' is supported for multi-output explanations!" else: if plot_type is None: plot_type = "dot" # default for single output explanations assert len( shap_values.shape ) != 1, "Summary plots need a matrix of shap_values, not a vector." # default color: if color is None: if plot_type == 'layered_violin': color = "coolwarm" elif multi_class: color = lambda i: colors.red_blue_circle(i / len(shap_values)) else: color = colors.blue_rgb # convert from a DataFrame or other types if str(type(features)) == "<class 'pandas.core.frame.DataFrame'>": if feature_names is None: feature_names = features.columns features = features.values elif isinstance(features, list): if feature_names is None: feature_names = features features = None elif (features is not None) and len( features.shape) == 1 and feature_names is None: feature_names = features features = None num_features = (shap_values[0].shape[1] if multi_class else shap_values.shape[1]) if features is not None: shape_msg = "The shape of the shap_values matrix does not match the shape of the " \ "provided data matrix." if num_features - 1 == features.shape[1]: assert False, shape_msg + " Perhaps the extra column in the shap_values matrix is the " \ "constant offset? Of so just pass shap_values[:,:-1]." else: assert num_features == features.shape[1], shape_msg if feature_names is None: feature_names = np.array( [labels['FEATURE'] % str(i) for i in range(num_features)]) # plotting SHAP interaction values if not multi_class and len(shap_values.shape) == 3: if plot_type == "compact_dot": new_shap_values = shap_values.reshape(shap_values.shape[0], -1) new_features = np.tile(features, (1, 1, features.shape[1])).reshape( features.shape[0], -1) new_feature_names = [] for c1 in feature_names: for c2 in feature_names: if c1 == c2: new_feature_names.append(c1) else: new_feature_names.append(c1 + "* - " + c2) return summary_plot(new_shap_values, new_features, new_feature_names, max_display=max_display, plot_type="dot", color=color, axis_color=axis_color, title=title, alpha=alpha, show=show, sort=sort, color_bar=color_bar, plot_size=plot_size, class_names=class_names, color_bar_label="*" + color_bar_label) if max_display is None: max_display = 7 else: max_display = min(len(feature_names), max_display) sort_inds = np.argsort(-np.abs(shap_values.sum(1)).sum(0)) # get plotting limits delta = 1.0 / (shap_values.shape[1]**2) slow = np.nanpercentile(shap_values, delta) shigh = np.nanpercentile(shap_values, 100 - delta) v = max(abs(slow), abs(shigh)) slow = -v shigh = v pl.figure(figsize=(1.5 * max_display + 1, 0.8 * max_display + 1)) pl.subplot(1, max_display, 1) proj_shap_values = shap_values[:, sort_inds[0], sort_inds] proj_shap_values[:, 1:] *= 2 # because off diag effects are split in half summary_plot(proj_shap_values, features[:, sort_inds] if features is not None else None, feature_names=feature_names[sort_inds], sort=False, show=False, get_png=get_png, color_bar=False, plot_size=None, max_display=max_display) pl.xlim((slow, shigh)) pl.xlabel("") title_length_limit = 11 pl.title(shorten_text(feature_names[sort_inds[0]], title_length_limit)) for i in range(1, min(len(sort_inds), max_display)): ind = sort_inds[i] pl.subplot(1, max_display, i + 1) proj_shap_values = shap_values[:, ind, sort_inds] proj_shap_values *= 2 proj_shap_values[:, i] /= 2 # because only off diag effects are split in half summary_plot(proj_shap_values, features[:, sort_inds] if features is not None else None, sort=False, get_png=get_png, feature_names=["" for i in range(len(feature_names))], show=False, color_bar=False, plot_size=None, max_display=max_display) pl.xlim((slow, shigh)) pl.xlabel("") if i == min(len(sort_inds), max_display) // 2: pl.xlabel(labels['INTERACTION_VALUE']) pl.title(shorten_text(feature_names[ind], title_length_limit)) pl.tight_layout(pad=0, w_pad=0, h_pad=0.0) pl.subplots_adjust(hspace=0, wspace=0.1) if show: pl.show() return if max_display is None: max_display = 20 if sort: # order features by the sum of their effect magnitudes if multi_class: feature_order = np.argsort( np.sum(np.mean(np.abs(shap_values), axis=1), axis=0)) else: feature_order = np.argsort(np.sum(np.abs(shap_values), axis=0)) feature_order = feature_order[-min(max_display, len(feature_order)):] else: feature_order = np.flip(np.arange(min(max_display, num_features)), 0) row_height = 0.4 if plot_size == "auto": pl.gcf().set_size_inches(8, len(feature_order) * row_height + 1.5) elif type(plot_size) in (list, tuple): pl.gcf().set_size_inches(plot_size[0], plot_size[1]) elif plot_size is not None: pl.gcf().set_size_inches(8, len(feature_order) * plot_size + 1.5) pl.axvline(x=0, color="#999999", zorder=-1) if plot_type == "dot": for pos, i in enumerate(feature_order): pl.axhline(y=pos, color="#cccccc", lw=0.5, dashes=(1, 5), zorder=-1) shaps = shap_values[:, i] values = None if features is None else features[:, i] inds = np.arange(len(shaps)) np.random.shuffle(inds) if values is not None: values = values[inds] shaps = shaps[inds] colored_feature = True try: values = np.array( values, dtype=np.float64) # make sure this can be numeric except: colored_feature = False N = len(shaps) # hspacing = (np.max(shaps) - np.min(shaps)) / 200 # curr_bin = [] nbins = 100 quant = np.round(nbins * (shaps - np.min(shaps)) / (np.max(shaps) - np.min(shaps) + 1e-8)) inds = np.argsort(quant + np.random.randn(N) * 1e-6) layer = 0 last_bin = -1 ys = np.zeros(N) for ind in inds: if quant[ind] != last_bin: layer = 0 ys[ind] = np.ceil(layer / 2) * ((layer % 2) * 2 - 1) layer += 1 last_bin = quant[ind] ys *= 0.9 * (row_height / np.max(ys + 1)) if features is not None and colored_feature: # trim the color range, but prevent the color range from collapsing vmin = np.nanpercentile(values, 5) vmax = np.nanpercentile(values, 95) if vmin == vmax: vmin = np.nanpercentile(values, 1) vmax = np.nanpercentile(values, 99) if vmin == vmax: vmin = np.min(values) vmax = np.max(values) if vmin > vmax: # fixes rare numerical precision issues vmin = vmax assert features.shape[0] == len( shaps ), "Feature and SHAP matrices must have the same number of rows!" # plot the nan values in the interaction feature as grey nan_mask = np.isnan(values) pl.scatter(shaps[nan_mask], pos + ys[nan_mask], color="#777777", vmin=vmin, vmax=vmax, s=16, alpha=alpha, linewidth=0, zorder=3, rasterized=len(shaps) > 500) # plot the non-nan values colored by the trimmed feature value cvals = values[np.invert(nan_mask)].astype(np.float64) cvals_imp = cvals.copy() cvals_imp[np.isnan(cvals)] = (vmin + vmax) / 2.0 cvals[cvals_imp > vmax] = vmax cvals[cvals_imp < vmin] = vmin pl.scatter(shaps[np.invert(nan_mask)], pos + ys[np.invert(nan_mask)], cmap=colors.red_blue, vmin=vmin, vmax=vmax, s=16, c=cvals, alpha=alpha, linewidth=0, zorder=3, rasterized=len(shaps) > 500) else: pl.scatter(shaps, pos + ys, s=16, alpha=alpha, linewidth=0, zorder=3, color=color if colored_feature else "#777777", rasterized=len(shaps) > 500) elif plot_type == "violin": for pos, i in enumerate(feature_order): pl.axhline(y=pos, color="#cccccc", lw=0.5, dashes=(1, 5), zorder=-1) if features is not None: global_low = np.nanpercentile( shap_values[:, :len(feature_names)].flatten(), 1) global_high = np.nanpercentile( shap_values[:, :len(feature_names)].flatten(), 99) for pos, i in enumerate(feature_order): shaps = shap_values[:, i] shap_min, shap_max = np.min(shaps), np.max(shaps) rng = shap_max - shap_min xs = np.linspace( np.min(shaps) - rng * 0.2, np.max(shaps) + rng * 0.2, 100) if np.std(shaps) < (global_high - global_low) / 100: ds = gaussian_kde(shaps + np.random.randn(len(shaps)) * (global_high - global_low) / 100)(xs) else: ds = gaussian_kde(shaps)(xs) ds /= np.max(ds) * 3 values = features[:, i] window_size = max(10, len(values) // 20) smooth_values = np.zeros(len(xs) - 1) sort_inds = np.argsort(shaps) trailing_pos = 0 leading_pos = 0 running_sum = 0 back_fill = 0 for j in range(len(xs) - 1): while leading_pos < len(shaps) and xs[j] >= shaps[ sort_inds[leading_pos]]: running_sum += values[sort_inds[leading_pos]] leading_pos += 1 if leading_pos - trailing_pos > 20: running_sum -= values[sort_inds[trailing_pos]] trailing_pos += 1 if leading_pos - trailing_pos > 0: smooth_values[j] = running_sum / (leading_pos - trailing_pos) for k in range(back_fill): smooth_values[j - k - 1] = smooth_values[j] else: back_fill += 1 vmin = np.nanpercentile(values, 5) vmax = np.nanpercentile(values, 95) if vmin == vmax: vmin = np.nanpercentile(values, 1) vmax = np.nanpercentile(values, 99) if vmin == vmax: vmin = np.min(values) vmax = np.max(values) # plot the nan values in the interaction feature as grey nan_mask = np.isnan(values) pl.scatter(shaps[nan_mask], np.ones(shap_values[nan_mask].shape[0]) * pos, color="#777777", vmin=vmin, vmax=vmax, s=9, alpha=alpha, linewidth=0, zorder=1) # plot the non-nan values colored by the trimmed feature value cvals = values[np.invert(nan_mask)].astype(np.float64) cvals_imp = cvals.copy() cvals_imp[np.isnan(cvals)] = (vmin + vmax) / 2.0 cvals[cvals_imp > vmax] = vmax cvals[cvals_imp < vmin] = vmin pl.scatter(shaps[np.invert(nan_mask)], np.ones(shap_values[np.invert(nan_mask)].shape[0]) * pos, cmap=colors.red_blue, vmin=vmin, vmax=vmax, s=9, c=cvals, alpha=alpha, linewidth=0, zorder=1) # smooth_values -= nxp.nanpercentile(smooth_values, 5) # smooth_values /= np.nanpercentile(smooth_values, 95) smooth_values -= vmin if vmax - vmin > 0: smooth_values /= vmax - vmin for i in range(len(xs) - 1): if ds[i] > 0.05 or ds[i + 1] > 0.05: pl.fill_between( [xs[i], xs[i + 1]], [pos + ds[i], pos + ds[i + 1]], [pos - ds[i], pos - ds[i + 1]], color=colors.red_blue_no_bounds(smooth_values[i]), zorder=2) else: parts = pl.violinplot(shap_values[:, feature_order], range(len(feature_order)), points=200, vert=False, widths=0.7, showmeans=False, showextrema=False, showmedians=False) for pc in parts['bodies']: pc.set_facecolor(color) pc.set_edgecolor('none') pc.set_alpha(alpha) elif plot_type == "layered_violin": # courtesy of @kodonnell num_x_points = 200 bins = np.linspace( 0, features.shape[0], layered_violin_max_num_bins + 1 ).round(0).astype( 'int') # the indices of the feature data corresponding to each bin shap_min, shap_max = np.min(shap_values), np.max(shap_values) x_points = np.linspace(shap_min, shap_max, num_x_points) # loop through each feature and plot: for pos, ind in enumerate(feature_order): # decide how to handle: if #unique < layered_violin_max_num_bins then split by unique value, otherwise use bins/percentiles. # to keep simpler code, in the case of uniques, we just adjust the bins to align with the unique counts. feature = features[:, ind] unique, counts = np.unique(feature, return_counts=True) if unique.shape[0] <= layered_violin_max_num_bins: order = np.argsort(unique) thesebins = np.cumsum(counts[order]) thesebins = np.insert(thesebins, 0, 0) else: thesebins = bins nbins = thesebins.shape[0] - 1 # order the feature data so we can apply percentiling order = np.argsort(feature) # x axis is located at y0 = pos, with pos being there for offset y0 = np.ones(num_x_points) * pos # calculate kdes: ys = np.zeros((nbins, num_x_points)) for i in range(nbins): # get shap values in this bin: shaps = shap_values[order[thesebins[i]:thesebins[i + 1]], ind] # if there's only one element, then we can't if shaps.shape[0] == 1: warnings.warn( "not enough data in bin #%d for feature %s, so it'll be ignored. Try increasing the number of records to plot." % (i, feature_names[ind])) # to ignore it, just set it to the previous y-values (so the area between them will be zero). Not ys is already 0, so there's # nothing to do if i == 0 if i > 0: ys[i, :] = ys[i - 1, :] continue # save kde of them: note that we add a tiny bit of gaussian noise to avoid singular matrix errors ys[i, :] = gaussian_kde(shaps + np.random.normal( loc=0, scale=0.001, size=shaps.shape[0]))(x_points) # scale it up so that the 'size' of each y represents the size of the bin. For continuous data this will # do nothing, but when we've gone with the unqique option, this will matter - e.g. if 99% are male and 1% # female, we want the 1% to appear a lot smaller. size = thesebins[i + 1] - thesebins[i] bin_size_if_even = features.shape[0] / nbins relative_bin_size = size / bin_size_if_even ys[i, :] *= relative_bin_size # now plot 'em. We don't plot the individual strips, as this can leave whitespace between them. # instead, we plot the full kde, then remove outer strip and plot over it, etc., to ensure no # whitespace ys = np.cumsum(ys, axis=0) width = 0.8 scale = ys.max( ) * 2 / width # 2 is here as we plot both sides of x axis for i in range(nbins - 1, -1, -1): y = ys[i, :] / scale c = pl.get_cmap(color)( i / (nbins - 1) ) if color in pl.cm.datad else color # if color is a cmap, use it, otherwise use a color pl.fill_between(x_points, pos - y, pos + y, facecolor=c) pl.xlim(shap_min, shap_max) elif not multi_class and plot_type == "bar": feature_inds = feature_order[:max_display] y_pos = np.arange(len(feature_inds)) global_shap_values = np.abs(shap_values).mean(0) pl.barh(y_pos, global_shap_values[feature_inds], 0.7, align='center', color=color) pl.yticks(y_pos, fontsize=13) pl.gca().set_yticklabels([feature_names[i] for i in feature_inds]) elif multi_class and plot_type == "bar": if class_names is None: class_names = ["Class " + str(i) for i in range(len(shap_values))] feature_inds = feature_order[:max_display] y_pos = np.arange(len(feature_inds)) left_pos = np.zeros(len(feature_inds)) if class_inds is None: class_inds = np.argsort([ -np.abs(shap_values[i]).mean() for i in range(len(shap_values)) ]) elif class_inds == "original": class_inds = range(len(shap_values)) for i, ind in enumerate(class_inds): global_shap_values = np.abs(shap_values[ind]).mean(0) pl.barh(y_pos, global_shap_values[feature_inds], 0.7, left=left_pos, align='center', color=color(i), label=class_names[ind]) left_pos += global_shap_values[feature_inds] pl.yticks(y_pos, fontsize=13) pl.gca().set_yticklabels([feature_names[i] for i in feature_inds]) pl.legend(frameon=False, fontsize=12) # draw the color bar if color_bar and features is not None and plot_type != "bar" and \ (plot_type != "layered_violin" or color in pl.cm.datad): import matplotlib.cm as cm m = cm.ScalarMappable( cmap=colors.red_blue if plot_type != "layered_violin" else pl. get_cmap(color)) m.set_array([0, 1]) cb = pl.colorbar(m, ticks=[0, 1], aspect=1000) cb.set_ticklabels( [labels['FEATURE_VALUE_LOW'], labels['FEATURE_VALUE_HIGH']]) cb.set_label(color_bar_label, size=12, labelpad=0) cb.ax.tick_params(labelsize=11, length=0) cb.set_alpha(1) cb.outline.set_visible(False) bbox = cb.ax.get_window_extent().transformed( pl.gcf().dpi_scale_trans.inverted()) cb.ax.set_aspect((bbox.height - 0.9) * 20) # cb.draw_all() pl.gca().xaxis.set_ticks_position('bottom') pl.gca().yaxis.set_ticks_position('none') pl.gca().spines['right'].set_visible(False) pl.gca().spines['top'].set_visible(False) pl.gca().spines['left'].set_visible(False) pl.gca().tick_params(color=axis_color, labelcolor=axis_color) pl.yticks(range(len(feature_order)), [feature_names[i] for i in feature_order], fontsize=13) if plot_type != "bar": pl.gca().tick_params('y', length=20, width=0.5, which='major') pl.gca().tick_params('x', labelsize=11) pl.ylim(-1, len(feature_order)) if plot_type == "bar": pl.xlabel(labels['GLOBAL_VALUE'], fontsize=13) else: pl.xlabel(labels['VALUE'], fontsize=13) if show: pl.show() if get_png: file = BytesIO() pl.savefig(file, format='png', bbox_inches="tight") return file
def hide(): # input of the cover image temp1 = input("Please input the name of the cover image:") cover_image = str(temp1) # input of the text file temp2 = input("Please input the name of the text file which you want to hide:") text_file = str(temp2) img = np.array(Image.open(cover_image)) file = open(text_file, "r") rows, columns, channels = img.shape counter = 0 x = 1 y = 0 z = 0 while True: # read a byte of the file temp = file.read(1) # If it is the end of the file, stop the loop if temp == '': break counter = counter + 1 temp_int = ord(temp) temp_4 = temp_int // 4 temp_16 = temp_int // 16 # get the last 2 bits of the char r = temp_int - (temp_4 * 4) # get the middle 2 bits of the char g = temp_4 - (temp_16 * 4) # get the highest 3 bits of the char b = temp_16 # add the last 2 bits of the char on the red channel img[x, y, z] = img[x, y, z] // 4 * 4 + r z = z + 1 # add the middle 2 bits of the char on the green channel img[x, y, z] = img[x, y, z] // 4 * 4 + g z = z + 1 # add the highest 3 bits of the char on the blue channel img[x, y, z] = img[x, y, z] // 8 * 8 + b z = 0 # move to next pixel if (y + 1) < columns: y = y + 1 else: if (x + 1) < rows: x = x + 1 y = 0 else: print('You need a larger image!') return # calculate the number of bits that are changed and stores in the image counter1 = counter x1 = 0 y1 = 0 z1 = 0 while y1 < columns: for z1 in range(0, 3): img[x1, y1, z1] = img[x1, y1, z1] // 4 * 4 + (counter1 % 4) counter1 = counter1 // 4 y1 = y1 + 1 file.close() # show the image plt.figure("hide_information") plt.imshow(img) plt.axis('off') fig = plt.gcf() fig.set_size_inches(2.2 / 0.72, 2.2 / 0.72) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.margins(0, 0) # save the image plt.savefig("stego_image.png", transparent=True, dpi=72, pad_inches=0) plt.show()
def build_plot(self, xlabel="", ylabel="Overhead w.r.t. native", legend_loc="", figsize=(12, 2), text_points=(), vline_position=6, title="", ncol=1, **kwargs): logging.info("Building plot") # prepare a plot canvas fig, plot = plt.subplots(1, 4, figsize=figsize, sharey=True) plt.subplots_adjust(wspace=0.04, hspace=0.04) # set common y label plot[0].set_ylabel(ylabel, fontsize=10) # draw the subplots for i, (name, group) in enumerate(self.df.groupby("name")): # restructure subplot data group = group.dropna() group = group.pivot_table(index="compilertype", columns="input", values="overhead", margins=False) group.rename(columns=prepare.INPUT_NAMES["long"], inplace=True) group.rename(index=self.conf.build_names["short"], inplace=True) style = self.StyleClass(need_hatching=False) subplot = group.plot(kind="bar", linewidth=0.5, edgecolor="black", color=style.colors, ax=plot[i], legend=False, title=name, **kwargs) if kwargs.get("logy", False): plot.set_yscale('log', basey=2, nonposy='clip') plot.yaxis.set_major_formatter(plticker.ScalarFormatter()) subplot = style.apply(subplot) subplot.set_xlabel("", fontsize=0) # remove x label # get handles and labels for the shared legend h, l = subplot.get_legend_handles_labels() # common legend fig.legend(h, l, title="", bbox_to_anchor=(0.13, 0.9), frameon=False, ncol=ncol, labelspacing=0.5, columnspacing=0, borderpad=0, handlelength=0.7, fontsize=10) # additional text for point in text_points: plt.text(point[0], point[1], point[2], fontsize=8) # common title plt.suptitle(title, fontsize=10, fontweight='bold') self.plot = fig
def figure_4_2(POISSON_UPPER_BOUND, constant_returned_cars=True): value = np.zeros((MAX_CARS + 1, MAX_CARS + 1)) policy = np.zeros(value.shape, dtype=np.int) iterations = 0 supfig, axes = plt.subplots(2, 3, figsize=(40, 20)) supfig.suptitle( f'policy iteration[POISSON_UPPER_BOUND={POISSON_UPPER_BOUND}]', fontsize=30) plt.subplots_adjust(wspace=0.1, hspace=0.2) axes = axes.flatten() while True: fig = sns.heatmap(np.flipud(policy), cmap="YlGnBu", ax=axes[iterations]) fig.set_ylabel('# cars at first location', fontsize=30) fig.set_yticks(list(reversed(range(MAX_CARS + 1)))) fig.set_xlabel('# cars at second location', fontsize=30) fig.set_title('$\pi_{}$'.format(iterations), fontsize=30) # policy evaluation (in-place) while True: old_value = value.copy() for i in range(MAX_CARS + 1): for j in range(MAX_CARS + 1): new_state_value = expected_return([i, j], policy[i, j], value, constant_returned_cars, POISSON_UPPER_BOUND) value[i, j] = new_state_value max_value_change = abs(old_value - value).max() print('max value change {}'.format(max_value_change)) if max_value_change < 1e-4: break # policy improvement print(f'policy improvement on policy:{policy}') policy_stable = True for i in range(MAX_CARS + 1): for j in range(MAX_CARS + 1): old_action = policy[i, j] action_returns = [] for action in actions: if (0 <= action <= i) or (-j <= action <= 0): action_returns.append( expected_return([i, j], action, value, constant_returned_cars, POISSON_UPPER_BOUND)) else: action_returns.append(-np.inf) # 这里是关键所在:寻找使得value function最大化的action new_action = actions[np.argmax(action_returns)] policy[i, j] = new_action if policy_stable and old_action != new_action: policy_stable = False print('policy stable {}'.format(policy_stable)) if policy_stable: fig = sns.heatmap(np.flipud(value), cmap="YlGnBu", ax=axes[-1]) fig.set_ylabel('# cars at first location', fontsize=30) fig.set_yticks(list(reversed(range(MAX_CARS + 1)))) fig.set_xlabel('# cars at second location', fontsize=30) fig.set_title('optimal value', fontsize=30) plt.savefig( f'../images/rl/dp/figure_4_2_{POISSON_UPPER_BOUND}.png') # 也画出三维曲面图 plot_3d_value(value) break iterations += 1 plt.close()
def test_grid_labels(): plt.figure(figsize=(8, 10)) crs_pc = ccrs.PlateCarree() crs_merc = ccrs.Mercator() crs_osgb = ccrs.OSGB() ax = plt.subplot(3, 2, 1, projection=crs_pc) ax.coastlines() ax.gridlines(draw_labels=True) # Check that adding labels to Mercator gridlines gives an error. # (Currently can only label PlateCarree gridlines.) ax = plt.subplot(3, 2, 2, projection=ccrs.PlateCarree(central_longitude=180)) ax.coastlines() with assert_raises(TypeError): ax.gridlines(crs=crs_merc, draw_labels=True) ax.set_title('Known bug') gl = ax.gridlines(crs=crs_pc, draw_labels=True) gl.xlabels_top = False gl.ylabels_left = False gl.xlines = False ax = plt.subplot(3, 2, 3, projection=crs_merc) ax.coastlines() ax.gridlines(draw_labels=True) # Check that labelling the gridlines on an OSGB plot gives an error. # (Currently can only draw these on PlateCarree or Mercator plots.) ax = plt.subplot(3, 2, 4, projection=crs_osgb) ax.coastlines() with assert_raises(TypeError): ax.gridlines(draw_labels=True) ax = plt.subplot(3, 2, 4, projection=crs_pc) ax.coastlines() gl = ax.gridlines( crs=crs_pc, linewidth=2, color='gray', alpha=0.5, linestyle='--') gl.xlabels_bottom = True gl.ylabels_right = True gl.xlines = False gl.xlocator = mticker.FixedLocator([-180, -45, 45, 180]) gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER gl.xlabel_style = {'size': 15, 'color': 'gray'} gl.xlabel_style = {'color': 'red'} # trigger a draw at this point and check the appropriate artists are # populated on the gridliner instance plt.draw() assert len(gl.xlabel_artists) == 4 assert len(gl.ylabel_artists) == 5 assert len(gl.ylabel_artists) == 5 assert len(gl.xline_artists) == 0 ax = plt.subplot(3, 2, 5, projection=crs_pc) ax.set_extent([-20, 10.0, 45.0, 70.0]) ax.coastlines() ax.gridlines(draw_labels=True) ax = plt.subplot(3, 2, 6, projection=crs_merc) ax.set_extent([-20, 10.0, 45.0, 70.0], crs=crs_pc) ax.coastlines() ax.gridlines(draw_labels=True) # Increase margins between plots to stop them bumping into one another. plt.subplots_adjust(wspace=0.25, hspace=0.25)
def build_plot(self, xlabels=(), ylabel="[ylabel not specified]", legend_loc="", figsize=(12, 2.35), text_points=(), vline_position=0, title="", df_callback=None, df_callback_args=(), **kwargs): logging.info("Building plot") # prepare a plot canvas plot = plt.subplot(111) style = self.StyleClass(need_hatching=False) # get build types and rename xlabels = [self.conf.build_names["long"].get(l, l) for l in xlabels] # builds clusters df_bars = [g for _, g in self.df.groupby('compilertype')] for index, df_bar in enumerate(df_bars): # restructure and better-name table df_bar.reindex(index=["name"], columns=self.columns) if df_callback: df_callback(df_bar, *df_callback_args) df_bar.plot(kind="bar", linewidth=1, stacked=True, ax=plot, legend=False, figsize=figsize, edgecolor=style.bar_edge_color, color=style.colors, title=title, **kwargs) df_bars[index] = df_bar plot = style.apply(plot) # bar labels self.shorten_bar_names(plot, xlabels, self.columns, df_bars, style.hatches) # axis labels n_ind = len(df_bars[0].index) plot.set_xticks( (np.arange(0, 2 * n_ind, 2) + 1 / float(len(df_bars) + 1)) / 2.) plot.set_xticklabels(df_bars[0]["name"], rotation=0) plot.xaxis.set_tick_params(pad=7) plot.set_ylabel(ylabel, fontsize=10) plot.set_xlabel("", fontsize=0) # remove x label # legend self.add_stacked_legend(plot, xlabels, self.columns, df_bars) # align subplots plt.subplots_adjust(left=0.07, right=0.86, top=0.95, bottom=0.14) # save the resulting plot as an object property self.plot = plot
plt.figure(figsize=(10, 8), facecolor='w') for i, clf in enumerate(clfs): clf.fit(x, y) y_hat = clf.predict(x) # show_accuracy(y_hat, y) # 正确率 # show_recall(y, y_hat) # 召回率 print(i + 1, '次:') print('accuracy:\t', accuracy_score(y, y_hat)) print('precision:\t', precision_score(y, y_hat, pos_label=1)) print('recall:\t', recall_score(y, y_hat, pos_label=1)) print('F1-score:\t', f1_score(y, y_hat, pos_label=1)) print() # 画图 plt.subplot(2, 2, i + 1) grid_hat = clf.predict(grid_test) # 预测分类值 grid_hat = grid_hat.reshape(x1.shape) # 使之与输入的形状相同 plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light, alpha=0.8) plt.scatter(x[:, 0], x[:, 1], c=y, edgecolors='k', s=s, cmap=cm_dark) # 样本的显示 plt.xlim(x1_min, x1_max) plt.ylim(x2_min, x2_max) plt.title(titles[i]) plt.grid(b=True, ls=':') plt.suptitle('不平衡数据的处理', fontsize=18) plt.tight_layout(1.5) plt.subplots_adjust(top=0.92) plt.savefig('2.png') plt.show()
Textbox ======= Allowing text input with the Textbox widget. You can use the Textbox widget to let users provide any text that needs to be displayed, including formulas. You can use a submit button to create plots with the given input. """ import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import TextBox fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.2) t = np.arange(-2.0, 2.0, 0.001) s = t**2 initial_text = "t ** 2" l, = plt.plot(t, s, lw=2) def submit(text): ydata = eval(text) l.set_ydata(ydata) ax.set_ylim(np.min(ydata), np.max(ydata)) plt.draw() axbox = plt.axes([0.1, 0.05, 0.8, 0.075]) text_box = TextBox(axbox, 'Evaluate', initial=initial_text)
def train(self, data, model, log_dir): # Build GAN model & optimizer model.build_model() d_min, g_min = model.get_optimizers() # Prepare dataset # Create placeholders for efficient batch feed with tf.dataset dataset = tf.data.Dataset.from_generator(data.get_data, (tf.uint8, tf.float32), ([IMAGE_HEIGHT, IMAGE_WIDTH, 3], AUX_DIM)) dataset = dataset.prefetch(BATCH_SIZE*10) dataset = dataset.shuffle(buffer_size=10000) dataset = dataset.batch(BATCH_SIZE, drop_remainder=True) dataset_iter = dataset.make_initializable_iterator() next_batch = dataset_iter.get_next() # Make a summary writer train_writer = tf.summary.FileWriter(log_dir) iter_counter = 0 # Make a train saver (for checkpoint) train_saver = tf.train.Saver(max_to_keep=10, keep_checkpoint_every_n_hours=1) with tf.Session() as sess: # initialize all variables to be trained sess.run(tf.global_variables_initializer()) # training body for epoch in range(EPOCHS): # (re)initialize dataset iterator sess.run(dataset_iter.initializer) d_loss, g_loss = 0, 0 n_iter = data.get_size()//(BATCH_SIZE*N_CRITICS) with tqdm(total=n_iter) as pbar: while True: try: # optimize D --> G # make merged summary when training G for _ in range(N_CRITICS): # make a batch image_batch, label_batch = sess.run(next_batch) feed_dict = {model.images: image_batch, model.z: np.random.randn(BATCH_SIZE, Z_DIM).astype(np.float32)} _, _d_loss = sess.run([d_min, model.d_loss], feed_dict=feed_dict) # Reuse the last batch (since G doesn't care) # Make sure that z is newly sampled feed_dict = {model.images: image_batch, model.z: np.random.randn(BATCH_SIZE, Z_DIM).astype(np.float32)} _, _g_loss, summary = sess.run([g_min, model.g_loss, model.summary], feed_dict=feed_dict) d_loss += _d_loss g_loss += _g_loss iter_counter += 1 pbar.update(1) except tf.errors.OutOfRangeError: break d_loss /= n_iter g_loss /= n_iter # Write summary & save the model. train_writer.add_summary(summary, iter_counter) train_saver.save(sess, os.path.join(log_dir, 'model'), global_step=iter_counter) print('epoch={:03d}/{:03d}, d_loss={:.4f}, g_loss={:.4f}'.format( (epoch+1), EPOCHS, d_loss, g_loss)) # Save a snapshot of generated images per each epoch. img_tensor = tf.get_default_graph().get_tensor_by_name(os.path.join(G_NAME, 'gen_out:0')) imgs = sess.run(img_tensor, feed_dict={model.z: np.random.randn(100, Z_DIM).astype(np.float32)}) imgs = 127.5*(imgs+1) imgs = np.clip(imgs, 0, 255).astype(np.uint8) # Make and save snapshot. fig, axs = plt.subplots(10, 10) plt.subplots_adjust(hspace=0, wspace=0) for ax, i in zip(axs.flat, range(imgs.shape[0])): ax.imshow(imgs[i]) ax.axis('off') fpath = os.path.join(log_dir, '{0:03d}.png'.format(epoch+1)) plt.savefig(fpath) plt.close()
def plot_trade_list(lq_time=60, nm_trades=60, tr_risk=1e-6, show_trl=False): # Create simulation environment env = sca.MarketEnvironment() # Reset the environment with the given parameters env.reset(liquid_time=lq_time, num_trades=nm_trades, lamb=tr_risk) # Get the trading list from the environment trade_list = env.get_trade_list() # Add a zero at the beginning of the trade list to indicate that at time 0 we don't sell any stocks new_trl = np.insert(trade_list, 0, 0) # We create a dataframe with the trading list and trading trajectory df = pd.DataFrame(data=list(range(nm_trades + 1)), columns=['Trade Number'], dtype='float64') df['Stocks Sold'] = new_trl df['Stocks Remaining'] = (np.ones(nm_trades + 1) * env.total_shares) - np.cumsum(new_trl) # Create a figure with 2 plots in 1 row fig, axes = plt.subplots(nrows=1, ncols=2) # Make a scatter plot of the trade list df.iloc[1:].plot.scatter(x='Trade Number', y='Stocks Sold', c='Stocks Sold', colormap='gist_rainbow', alpha=1, sharex=False, s=50, colorbar=False, ax=axes[0]) # Plot a line through the points of the scatter plot of the trade list axes[0].plot(df['Trade Number'].iloc[1:], df['Stocks Sold'].iloc[1:], linewidth=2.0, alpha=0.5) axes[0].set_facecolor(color='k') yNumFmt = mticker.StrMethodFormatter('{x:,.0f}') axes[0].yaxis.set_major_formatter(yNumFmt) axes[0].set_title('Trading List') # Make a scatter plot of the number of stocks remaining after each trade df.plot.scatter(x='Trade Number', y='Stocks Remaining', c='Stocks Remaining', colormap='gist_rainbow', alpha=1, sharex=False, s=50, colorbar=False, ax=axes[1]) # Plot a line through the points of the scatter plot of the number of stocks remaining after each trade axes[1].plot(df['Trade Number'], df['Stocks Remaining'], linewidth=2.0, alpha=0.5) axes[1].set_facecolor(color='k') yNumFmt = mticker.StrMethodFormatter('{x:,.0f}') axes[1].yaxis.set_major_formatter(yNumFmt) axes[1].set_title('Trading Trajectory') # Set the spacing between plots plt.subplots_adjust(wspace=0.4) plt.show() print('\nNumber of Shares Sold: {:,.0f}\n'.format(new_trl.sum())) if show_trl: # Since we are not selling fractional shares we round up the shares in the trading list rd_trl = round_trade_list(new_trl) # rd_trl = new_trl # We create a dataframe with the modified trading list and trading trajectory df2 = pd.DataFrame(data=list(range(nm_trades + 1)), columns=['Trade Number'], dtype='float64') df2['Stocks Sold'] = rd_trl df2['Stocks Remaining'] = (np.ones(nm_trades + 1) * env.total_shares) - np.cumsum(rd_trl) return df2.style.hide_index().format({ 'Trade Number': '{:.0f}', 'Stocks Sold': '{:,.0f}', 'Stocks Remaining': '{:,.0f}' })
# plot false positive rate (false alarm rate) plt.figure('False alarm rate for multiclass') barWidth = 0.2 r1 = np.arange(len(recall_rf)) r2 = [x + barWidth for x in r1] r3 = [x + barWidth for x in r2] r4 = [x + barWidth for x in r3] r5 = [x + barWidth for x in r4] plt.bar(r1, fpr_dt, color='red', width=barWidth, label='Decision tree') plt.bar(r2, fpr_rf, color='magenta', width=barWidth, label='Random forest') plt.bar(r3, fpr_knn, color='orange', width=barWidth, label='KNN') plt.bar(r4, fpr_svc, color='green', width=barWidth, label='SVM') plt.bar(r5, fpr_nn, color='blue', width=barWidth, label='NN') plt.xlabel('Normality', fontweight='bold') plt.ylabel('False alarm rate') plt.subplots_adjust(bottom=0.1, top=0.84) plt.xticks([r + barWidth for r in range(len(fpr_rf))], ['0', '1', '2', '3', '4', '5', '6', '7']) plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left', ncol=2, mode="expand", borderaxespad=0.) # plot detection rate (a.k.a. recall score) plt.figure('Detection rate for multi-class') barWidth = 0.2 r1 = np.arange(len(recall_rf)) r2 = [x + barWidth for x in r1] r3 = [x + barWidth for x in r2] r4 = [x + barWidth for x in r3]
p = pre() response = json.loads(p) preds = response.get('prediction') image = response.get('image') image = np.reshape(image, (28, 28)) st.image(image, width=150) for layer, p in enumerate(preds): numbers = np.squeeze(np.array(p)) plt.figure(figsize=(32, 4)) if layer == 2: row = 1 col = 10 else: row = 2 col = 16 for i, number in enumerate(numbers): plt.subplot(row, col, i + 1) plt.imshow(number * np.ones((8, 8, 3)).astype('float32')) plt.xticks([]) plt.yticks([]) if layer == 2: plt.xlabel(str(i), fontsize=40) plt.subplots_adjust(wspace=0.05, hspace=0.05) plt.tight_layout() st.text(f"Layer {layer + 1}") st.pyplot()
plt.plot(netflix_stocks['Date'], netflix_stocks['Price']) ax1.set_title("Netflix") ax1.set_xlabel("Date") ax1.set_ylabel("Stock Price") ax1.set_xticks(netflix_stocks['Date']) ax1.set_xticklabels(netflix_stocks['Date'], rotation=50) # Right plot Dow Jones ax2 = plt.subplot(1, 2, 2) plt.plot(dowjones_stocks['Date'], dowjones_stocks['Price']) ax2.set_title("Dow Jones") ax2.set_xlabel("Date") ax2.set_ylabel("Stock Price") ax2.set_xticks(dowjones_stocks['Date']) ax2.set_xticklabels(dowjones_stocks['Date'], rotation=50) plt.subplots_adjust(wspace=.5) plt.savefig('netflixline.png') plt.show() # - How did Netflix perform relative to Dow Jones Industrial Average in 2017? # - Which was more volatile? # - How do the prices of the stocks compare? # # # Step 9 # # It's time to make your presentation! Save each of your visualizations as a png file with `plt.savefig("filename.png")`. # # As you prepare your slides, think about the answers to the graph literacy questions. Embed your observations in the narrative of your slideshow! #
# chose a color! c=plot_colors[bin_set] # plot the data plt.plot(data[bin_set], lw=2.5, color=c) # add plot label x_pos = len(data[bin_set])-1 y_pos = data[bin_set][-1] plt.text(x_pos, y_pos, bin_set, fontsize=18, color=c) # add plot and axis titles and adjust the edges plt.title("Bin contamination ranking", fontsize=26) plt.xlabel("Acending contamination rank", fontsize=16) plt.ylabel("Estimated bin contamination (log scale)", fontsize=16) plt.gcf().subplots_adjust(right=0.9) # save figure print "Saving figures binning_results.eps and binning_results.png ..." plt.tight_layout(w_pad=10) plt.subplots_adjust(top=0.92, right=0.90, left=0.08) plt.savefig("binning_results.eps",format='eps', dpi=600) plt.savefig("binning_results.png",format='png', dpi=600) #plt.show()
bbox_to_anchor=legend_bbox, loc=legend_loc, prop={'size':6}) leg.get_frame().set_alpha(0.5) for j in ax.get_xticklabels() + ax.get_yticklabels(): j.set_fontsize(6) if not xaxis: xaxis = ax #if n >= 2 and not yset and l != -1: # ax.set_ylabel('(% of execution time)') # yset = True if n != numplots: max_legend = max(len(non_null), max_legend) #ax.margins(0, 0) n += 1 if len(timestamps) == 1: plt.gca().axes.get_xaxis().set_visible(False) plt.subplots_adjust(hspace=1.5 if max_legend > 6 else 0.9, bottom=0.20, top=0.95) if args.title: #plt.subplot(numplots, 1, 1) plt.title(args.title) if args.output: plt.savefig(args.output) else: plt.show()
def run(dlc, dlc_noipc, SAVE=False): C = ['ipc04', 'ipc07'] keys = ['RBMf', 'MBt', 'MBy'] titles = ['Blade (flapwise)', 'Main bearing (tilt)', 'Main bearing (yaw)'] labels = ['$C_{f1p}$', '$C_{2}$'] #WSP = np.array([4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26]) WSP = np.arange(4, 27, 4) fig, axes = plt.subplots(1, 3, sharex=True, sharey=True, figsize=[10,3]) plt.subplots_adjust(wspace=0.05) # for eack key and title, make a new bar graph. for key, title, ax in zip(keys, titles, axes.ravel()): Req_ol, Req_cl = [0]*len(WSP), np.zeros([len(C), len(WSP)]) for i, wsp in enumerate(WSP): sim_ref = dlc_noipc(wsp=wsp, yaw=0)[0] Req_ol[i] = float(sim_ref.data[key]) for j, c in enumerate(C): sim = dlc(wsp=wsp, yaw=0, controller=c)[0] Req_cl[j, i] = float(sim.data[key]) # bar graph width = 3/(len(C) + 1) ax.set_xticks(WSP + 1) ax.set_xticklabels(WSP) ax.grid(True, axis='y') ax.set_axisbelow(True) ax.bar(WSP, Req_ol, width, label = 'No IPC', hatch='\\\\', fc='0.8', ec='0') for j, c in enumerate(C): ax.bar(WSP + width*(j+1), Req_cl[j], width, label=labels[j], ec='0') ax.annotate(title , xy=(0.5, 0.97), xycoords='axes fraction', size=10, ha='center', va='top', bbox=dict(ec='w', fc='w', alpha=0.7)) # labels fig.text(0.06, 0.5, 'Equivalent Bending Moment [kNm]', va='center', rotation='vertical') fig.text(0.5, 0.01, 'Wind Speed [m/s]', ha='center', rotation='horizontal') axes.ravel()[1].legend(ncol=4,bbox_to_anchor=(1.3, 1.2)) if SAVE: plt.savefig(SAVE, dpi=200, bbox_inches='tight') plt.show(); print() # lifetime equivalent load table tableRows = ['Blade (flap)', 'Main Bearing (tilt)', 'Main Bearing (yaw)'] f = open('../Figures/Tables/Ch2_inverse_Reqlt.txt', 'w') for i, row in enumerate(keys): leqref = lifetimeReq(dlc_noipc.Sims, row) leq1 = lifetimeReq(dlc(controller='ipc04'), row) leq2 = lifetimeReq(dlc(controller='ipc07'), row) line = tableRows[i] line += '& {:2.0f} & {:+2.2f}'.format( leq1, (leq1/leqref - 1)*100) line += '& {:2.0f} & {:+2.2f} \\\\\n'.format( leq2, (leq2/leqref - 1)*100) f.write(line) print(line) f.close()
def run(self): #episode_final = False plt.rcParams['font.family'] = 'Times New Roman' plt.rcParams["mathtext.fontset"] = 'cm' plt.rcParams['mathtext.default'] = 'it' params = {'legend.fontsize': 12, 'legend.handlelength': 3} plt.rcParams.update(params) fig, axes = plt.subplots(nrows=5, ncols=1, figsize=(9, 10)) plt.subplots_adjust(hspace=0.8) #reward list sum_reward_list = [] #======================Hyper Parameter===================== weight = np.array([[1 / 4, 1 / 4, 1 / 4, 1 / 4]]) learn_alpha = 5e-5 gamma = 0.99 MAX_STEP = 1000 #========================================================== max_reward = -10000.0 a_param = self.args.a_param b_param = self.args.b_param weight_1_list = [] weight_2_list = [] weight_3_list = [] weight_4_list = [] time_list = [] a_list = [] x_1_list = [] x_2_list = [] td_error_list = [] Discrete_time = 0 state = np.array([[np.pi, 0.0]]) for test_step in range(MAX_STEP): weight_1_list.append(weight[0, 0]) #store the initial parameter weight_2_list.append(weight[0, 1]) weight_3_list.append(weight[0, 2]) weight_4_list.append(weight[0, 3]) time_list.append(test_step) x_1_list.append(state[0, 0]) x_2_list.append(state[0, 1]) current_obs = torch.Tensor([[state[0, 0], state[0, 1]]]) action = self.agent.get_action(current_obs, weight) #Not input ounoise action = action.detach().numpy()[ 0] #action: torch.Tensor(1,1) -> numpy(scaler) #exploration noise############################################### noise = max( (400 - Discrete_time), 0.0) / 400 * 0.1 * np.random.normal() action = action + noise a_list.append(action) action = torch.Tensor([action]) Q_vec = self.agent.get_Q_value( current_obs, action) # Q(x[k],a[k]) as characteristic functions action = action.detach().numpy()[ 0] #action: torch.Tensor(1,1) -> numpy(1,) next_state, reward, done = dynamics.Dynamics( state, action, a_param, b_param) #next_state: numpy(1,2) next_obs = torch.Tensor([[next_state[0, 0], next_state[0, 1]]]) #update of the parameters max_Q_next_vec = self.agent.get_next_value(next_obs, weight) param = np.array( [[weight[0, 0], weight[0, 1], weight[0, 2], weight[0, 3]]]) #w=[w_{1},...,w_{N}] td_error = param @ Q_vec.T - (reward + gamma * (param @ max_Q_next_vec.T)) td_error_list.append(abs(td_error[0, 0])) chara_vec = np.array( [[Q_vec[0, 0], Q_vec[0, 1], Q_vec[0, 2], Q_vec[0, 3]]]) update_vec = td_error * chara_vec #Barrier eta = 1e-7 epsilon_w = 1e-9 barrier_vec = eta*np.array([[-1/(weight[0,0]+epsilon_w),\ -1/(weight[0,1]+epsilon_w),\ -1/(weight[0,2]+epsilon_w),\ -1/(weight[0,3]+epsilon_w)]]) update_vec = update_vec + barrier_vec pre_weight = weight #memorize pre_weight weight = weight - learn_alpha * (update_vec ) #weight is next weight if (weight[0, 0] < 0.0) or (weight[0, 1] < 0.0) or (weight[0, 2] < 0.0) or ( weight[0, 3] < 0.0): #If some weights are negative update_error_count = 1 while (True): weight = pre_weight weight = weight - (2**( -update_error_count)) * learn_alpha * (update_vec) update_error_count += 1 if (weight[0, 0] >= 0.0) and (weight[0, 1] >= 0.0) and ( weight[0, 2] >= 0.0) and (weight[0, 3] >= 0.0): break weight_sum = weight[0, 0] + weight[0, 1] + weight[0, 2] + weight[0, 3] weight = weight / weight_sum state = next_state Discrete_time += 1 axes[0].plot([0, MAX_STEP], [0, 0], "red", linestyle='dashed') axes[0].plot(time_list, a_list, linewidth=2) axes[0].set_xlim(0.0, MAX_STEP) axes[0].set_ylim(-1, 1) axes[0].set_xlabel('$k$', fontsize=16) axes[0].set_ylabel('$a[k]$', fontsize=16) axes[0].grid(True) axes[1].plot([0, MAX_STEP], [0, 0], "red", linestyle='dashed') axes[1].plot(time_list, x_1_list, linewidth=2) axes[1].set_xlim(0.0, MAX_STEP) axes[1].set_ylim(-np.pi, np.pi) axes[1].set_xlabel('$k$', fontsize=16) axes[1].set_ylabel('$x_1[k]$', fontsize=16) axes[1].grid(True) axes[2].plot([0, MAX_STEP], [0, 0], "red", linestyle='dashed') axes[2].plot(time_list, x_2_list, linewidth=2) axes[2].set_xlim(0.0, MAX_STEP) axes[2].set_ylim(-7, 7) axes[2].set_xlabel('$k$', fontsize=16) axes[2].set_ylabel('$x_2[k]$', fontsize=16) axes[2].grid(True) axes[3].plot(time_list, weight_1_list, linewidth=2, label="$w_5$") axes[3].plot(time_list, weight_2_list, linewidth=2, label="$w_6$") axes[3].plot(time_list, weight_3_list, linewidth=2, label="$w_7$") axes[3].plot(time_list, weight_4_list, linewidth=2, label="$w_8$") axes[3].set_xlim(0.0, MAX_STEP) axes[3].set_ylim(0, 1) axes[3].set_xlabel('$k$', fontsize=16) axes[3].set_ylabel('$w[k]$', fontsize=16) axes[3].grid(True) axes[3].legend(loc='upper left', ncol=4) axes[4].plot(time_list, td_error_list, linewidth=2) axes[4].set_xlim(0.0, MAX_STEP) # axes[4].set_ylim(0,0.5) axes[4].set_xlabel('$k$', fontsize=16) axes[4].set_ylabel(r'$|\delta[k]|$', fontsize=16) axes[4].grid(True) fig.savefig('TR_N4_case2.eps', bbox_inches="tight", pad_inches=0.05) fig.savefig('TR_N4_case2.png', bbox_inches="tight", pad_inches=0.05)
def show_gauss_func(w): xb=np.linspace(X_min,X_max,100) y=gauss_func(w,xb) plt.plot(xb,y,c=[.5,.5,.5],lw=4) #split test data and training data X_test=X[:int(X_n / 4 + 1)] T_test=T[:int(X_n / 4 + 1)] X_train = X[int(X_n / 4 + 1):] T_train = T[int(X_n / 4 + 1):] #main plt.figure(figsize=(10,2.5)) plt.subplots_adjust(wspace=0.3) M=[2,4,7,9] for i in range(len(M)): plt.subplot(1,len(M),i+1) W=fit_gauss_func(X_train,T_train,M[i]) show_gauss_func(W) plt.plot(X_train,T_train, marker='o', linestyle='None', color='white', markeredgecolor='black', label='training') plt.plot(X_test, T_test, marker='o', linestyle='None', color='cornflowerblue', markeredgecolor='black', label='test') plt.legend(loc='lower right', fontsize=10, numpoints=1) plt.xlim(X_min,X_max) plt.ylim(130,180) plt.grid(True) mse=mse_gauss_func(X_test,T_test,W) plt.title("M={0:d}, SD={1:.1f}".format(M[i],np.sqrt(mse))) plt.show()