def drawPRC(result, sp, methodlabel): ''' Accept a result dataframe whose 'cond' coulmn is binary series representing the ture condition and 'pred' column is the normalized score of predicton. ''' print("drawing prc curve...") sp.set_xlabel('Recall') sp.set_ylabel('Precision') sp.set_ylim([0.0, 1.0]) sp.set_xlim([0.0, 1.0]) sp.set_title('2-class Precision-Recall curve') precision, recall, threshold = precision_recall_curve( result['cond'], result['pred']) average_precision = average_precision_score(result['cond'], result['pred']) myauc = auc(recall, precision) step_kwargs = ({ 'step': 'post' } if 'step' in signature(sp.fill_between).parameters else {}) sp.step(recall, precision, alpha=0.2, where='post') sp.fill_between(recall, precision, alpha=0.2, label=methodlabel + " (AUC = %.3f)" % myauc, **step_kwargs) return myauc
def set_limits(l, plt): try: plt.xlim((-l ,l)) plt.ylim((-l ,l)) except: plt.set_xlim(-l ,l) plt.set_ylim(-l ,l)
def draw_twitter_dupicate(plt): f = open("../compare/avg_degree/dup_Twitter_Mhrw.txt", "r") try: _x = [] _y = [] for line in f: x, b = line.split(" ") y = b.split("\n")[0] _x.append(x) _y.append(y) finally: f.close() f = open("../compare/avg_degree/dup_Twitter_UD.txt", "r") try: _x_ = [] _y_ = [] for line in f: x, b = line.split(" ") y = b.split("\n")[0] _x_.append(x) _y_.append(y) finally: f.close() plt.plot(_x, _y, "b-.", label="MHRW", linewidth=3.0) plt.plot(_x_, _y_, "r-", label="UD", linewidth=3.0) plt.set_ylim(0, 1) plt.set_ylabel("update rate", size=20) plt.set_xlabel("number of nodes", size=20) plt.text(100, 0.85, "Twitter", fontsize=20) plt.legend(loc="upper right")
def plotSolnAndGrad(self, plt, solVec, title=""): plt.set_title(title) # plt.set_xlabel('x',size=14,weight='bold') # plt.set_ylabel('y',size=14,weight='bold') plt.set_aspect('equal') plt.set_xlim(-0.1, 5.1) plt.set_ylim(-0.1, 1.2) xy = np.asarray(self.mesh.vertices) tci = tri.CubicTriInterpolator(self.mesh.dtri, solVec) (Ex, Ey) = tci.gradient(self.mesh.dtri.x, self.mesh.dtri.y) E_norm = np.sqrt(Ex**2 + Ey**2) vals = plt.tricontourf(self.mesh.dtri, solVec, cmap="jet") plt.quiver(self.mesh.dtri.x, self.mesh.dtri.y, -Ex / E_norm, -Ey / E_norm, units='xy', scale=20., zorder=3, color='blue', width=0.002, headwidth=2., headlength=2.) return vals
def plotPop(C, T=None, lbda = lbda, alpha=alpha, beta=beta, save = 'n', n=None): #plots the distribution within each stage given the C if T == None: #if the user does not specify the T range T = np.linspace(-50,5,250) scpDist = [] for i in range(len(alpha)): scpDist.append(SCP(alpha[i],beta[i], T)) scpDist = np.array(scpDist) popFac = popProp(C, lbda) props = [] for i in range(len(popFac)): plt.fill(T, popFac[i]*scpDist[i], label ='State %s'%(i+1), alpha = 0.5) props.append(popFac[i]*scpDist[i]) max_p = np.max(np.array(props)) lt50 = medianSCP(popFac, alpha) ax = plt.subplot(111) plt.vlines(lt50, 0, max_p, color = 'red', linestyle = '--', label = 'Median SCP') plt.xlabel('Supercooling Point (SCP)') plt.ylabel('Simulated Normalize Frequency') plt.set_ylim([0,.18]) plt.title('Day:%s; Supercooling state C: %0.2f; Median SCP: %0.2f$^oC$'%(n, C, lt50)) plt.grid() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) if save =='y': plt.savefig('dist_plot%s.png'%(n)) plt.show() return()
def plotHistogram(graph_number, weight_range, optimizer, weight_decay): weights_histogram_small = small_batch_weights_by_graph_number[graph_number] weights_histogram_big = big_batch_weights_by_graph_number[graph_number] #plot a histogram of the weights fig, plt = subplots() plt.hist(weights_histogram_small, bins=number_bins, range=weight_range, facecolor='blue', alpha=0.5, label=str(small_batch_size)) plt.hist(weights_histogram_big, bins=number_bins, range=weight_range, facecolor='orange', alpha=0.5, label=str(big_batch_size)) #set axis labels and titles plt.set_xlabel('Weight Bins') plt.set_ylabel('Number of Weights') plt.set_title('Histogram of Weights for Configuration' + str(optimizer) + ', WD = ' + str(weight_decay)) #obtain min and max values of tuple mi, mx = weight_range #set axis limits plt.set_xlim([mi, mx]) plt.set_ylim([0, 5000000]) plt.legend(loc='upper right')
def plotPolys( plt, fullDataset, fittingData, echemRegion, polyQ=False): # Generates polynomials and plots them to a given graph polyArray = np.zeros(len(fullDataset[:, 0])) for i in range(3, 11): coefficients = np.polyfit(x=fittingData[:, 0], y=fittingData[:, 1], deg=i) poly = np.poly1d(coefficients) baseline_y = poly(fullDataset[:, 0]) plt.plot(fullDataset[:, 0], baseline_y, alpha=0.5, label=i, linestyle="--") polyArray = np.c_[polyArray, baseline_y] plt.plot(echemRegion[:, 0], echemRegion[:, 1], 'r-') plt.legend() plt.set_xlim((0.7 * min(echemRegion[:, 0])), (1.3 * max(echemRegion[:, 0]))) plt.set_ylim((0.7 * min(echemRegion[:, 1])), (1.3 * max(echemRegion[:, 1]))) if (polyQ == True): polyArray = np.delete(polyArray, 0, 1) np.savetxt(".out/polys.out", polyArray)
def plotPop(C, T=None, lbda=lbda, alpha=alpha, beta=beta, save='n', n=None): #plots the distribution within each stage given the C if T == None: #if the user does not specify the T range T = np.linspace(-50, 5, 250) scpDist = [] for i in range(len(alpha)): scpDist.append(SCP(alpha[i], beta[i], T)) scpDist = np.array(scpDist) popFac = popProp(C, lbda) props = [] for i in range(len(popFac)): plt.fill(T, popFac[i] * scpDist[i], label='State %s' % (i + 1), alpha=0.5) props.append(popFac[i] * scpDist[i]) max_p = np.max(np.array(props)) lt50 = medianSCP(popFac, alpha) ax = plt.subplot(111) plt.vlines(lt50, 0, max_p, color='red', linestyle='--', label='Median SCP') plt.xlabel('Supercooling Point (SCP)') plt.ylabel('Simulated Normalize Frequency') plt.set_ylim([0, .18]) plt.title('Day:%s; Supercooling state C: %0.2f; Median SCP: %0.2f$^oC$' % (n, C, lt50)) plt.grid() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) if save == 'y': plt.savefig('dist_plot%s.png' % (n)) plt.show() return ()
def pivot_plot(index,columns,values): fig = plt.figure(figsize=(14,6)) table1 = pd.pivot_table(df,index=index,columns=columns,values=values) table1.plot(kind='bar',ax=ax1) # ax1.set_ylabel(); plt.set_ylim((0,9.3))
def label(plt, xlabel): rng = np.arange(122.7, 123.5, 0.1) plt.set_ylim(rng[0], rng[-1]) plt.set_yticks(rng, ["%.1f" for i in rng]) plt.set_xlabel(xlabel) plt.set_ylabel("Convertible Bond Price") plt.legend(["Binomial", "FDE - Implicit", "FDE - Crank-Nicolson", "FDE - Penalty"], prop={"size": "small"})
def plotMesh(self, plt, title=""): plt.set_title("Mesh") # plt.set_xlabel('x',size=14,weight='bold') # plt.set_ylabel('y',size=14,weight='bold') plt.set_aspect('equal'); plt.set_xlim(-0.1,5.1); plt.set_ylim(-0.1,1.2); xy = np.asarray(self.mesh.vertices); vals=plt.triplot(xy[:,0],xy[:,1],self.mesh.elements,'b-',linewidth=0.5); return vals
def penta_plot(self): matplotlib.rcParams['axes.formatter.useoffset'] = False f, ax = plt.subplots(3, 2,sharey='all', sharex='all', figsize=(4, 6)) f.set_size_inches(11, 12) #(11,10.5) #modify graph 1 to cover 2 columns in first row gs = ax[0, 0].get_gridspec() # remove the underlying axes ax[0, 1].remove() ax[0, 0].remove() axbig = f.add_subplot(gs[0, 0:],sharex=ax[1,0],sharey=ax[1,0]) y_min = 1 y_max = 10 i = 0 for plot_id in self.plot_order: subset = self.data.iloc[self.data.index.get_level_values(self.index) == plot_id] plot_name = self.get_plot_name(plot_id) subset = subset.reset_index(level=self.index, drop=True) legend = False top = 1.8 if i == 0: top = 1.05 axis = axbig legend =True elif i == 1: axis = ax[1,0] elif i == 2: axis = ax[1,1] elif i == 3: top = 1.5 axis = ax[2,0] elif i == 4: top = 1.5 axis = ax[2,1] i += 1 if subset.empty == False: self.set_plot(axis,plot_name,subset,legend=legend, top_factor=top) axis.set_xlim(xmin=self.start_year,xmax=self.end_year) min_val, max_val = axis.get_ylim() if float(min_val) < y_min: y_min = min_val if float(max_val) < y_max y_max = max_val else: axis.remove() f.suptitle(self.title,fontsize=16) plt.set_ylim(ymin=y_min,y_max) plt.rc('xtick',labelsize=14) plt.rc('ytick',labelsize=14) plt.tight_layout() plt.savefig(os.path.join(self.path, self.file_name),bbox_inches='tight',dpi=1200) plt.close('all')
def sub_training_plot(plt, df, cols, index_col= None, main='',xlabel='xname', ylabel='yname', ylims=None, xlims=None, iflegend= 1 ): x= df[index_col] if index_col else df.index for col in cols: plt.plot(x, df[col],'-', label=col) #plt.bar(x, df[col],width=2/4, label='fps' ) if ylims and len(ylims)== 2: # a tuple plt.set_ylim(ylims) if xlims and len(xlims)== 2: # a tuple plt.set_xlim(xlims) if iflegend: plt.legend(loc = 'best') plt.set_title(main, fontsize=11)
def size_residual(m, dT, min_mused, legend=None, color=None): import numpy as np import matplotlib.pyplot as plt min_mag = 13.5 max_mag = 21 #min_mused = 15 # Bin by mag mag_bins = np.linspace(min_mag, max_mag, 71) print('mag_bins = ', mag_bins) index = np.digitize(m, mag_bins) print('len(index) = ', len(index)) bin_dT = [dT[index == i].mean() for i in range(1, len(mag_bins))] print('bin_dT = ', bin_dT) bin_dT_err = [ np.sqrt(dT[index == i].var() / len(dT[index == i])) for i in range(1, len(mag_bins)) ] print('bin_dT_err = ', bin_dT_err) # Fix up nans for i in range(1, len(mag_bins)): if i not in index: bin_dT[i - 1] = 0. bin_dT_err[i - 1] = 0. print('fixed nans') print('index = ', index) print('bin_dT = ', bin_dT) if legend is None: legend = [r'$\delta T$'] plt.plot([min_mag, max_mag], [0, 0], color='black') plt.plot([min_mused, min_mused], [-1, 1], color='Grey') plt.fill([min_mag, min_mag, min_mused, min_mused], [-1, 1, 1, -1], fill=False, hatch='/', color='Grey') t_line = plt.errorbar(mag_bins[:-1], bin_dT, yerr=bin_dT_err, color=color, fmt='o') plt.legend([t_line], [legend]) plt.set_ylabel(r'$T_{\rm PSF} - T_{\rm model} ({\rm arcsec}^2)$') plt.set_ylim(-0.002, 0.012) plt.set_xlim(min_mag, max_mag) plt.set_xlabel('Magnitude')
def plot_lc(estimator, title, X, y, axes=None, ylim=None, cv=None, n_jobs=None, train_sizes=np.linspace(.1, 1.0, 5), path=None): plt.title(title) if ylim is not None: plt.set_ylim(*ylim) plt.xlabel("Training examples") plt.ylabel("Score") train_sizes, train_scores, test_scores, fit_times, _ = \ learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes, return_times=True) train_scores_mean = np.mean(train_scores, axis=1) train_scores_std = np.std(train_scores, axis=1) test_scores_mean = np.mean(test_scores, axis=1) test_scores_std = np.std(test_scores, axis=1) fit_times_mean = np.mean(fit_times, axis=1) fit_times_std = np.std(fit_times, axis=1) # plot learning curve plt.grid() plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.1, color="r") plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.1, color="g") plt.plot(train_sizes, train_scores_mean, 'o-', color="r", label="Training score") plt.plot(train_sizes, test_scores_mean, 'o-', color="g", label="Cross-validation score") plt.legend(loc="best") plt.savefig(path)
def make_gif(frame): plt.pcolormesh(np.array(frame), norm=colors.Normalize(vmin=np.min(frames),vmax=np.max(frames)),cmap=pickcol) plt.set_ylim(0,30) plt.set_xlim(0,20) plt.axis('square') plt.axis('off') buf = io.BytesIO() plt.savefig(buf, format='png', bbox_inches='tight') plt.clf() buf.seek(0) im = Image.open(buf) images.append(im)
def make_plot(): with open('i2.dump', 'rb') as dump_file: polies = pickle.load(dump_file) plt = GeoPlot() plt.add_collection( PatchCollection(polies, alpha=0.5, edgecolor='black', linewidth=0.001, match_original=True)) plt.set_xlim(-120, -40) plt.set_ylim(-15, 80) plt.save_fig('../figures/i2.pdf')
def shape1_residual(m, de1, min_mused, legend=None, color=None): import numpy as np import matplotlib.pyplot as plt min_mag = 13.5 max_mag = 21 #min_mused = 15 # Bin by mag mag_bins = np.linspace(min_mag, max_mag, 71) print('mag_bins = ', mag_bins) index = np.digitize(m, mag_bins) print('len(index) = ', len(index)) bin_de1 = [de1[index == i].mean() for i in range(1, len(mag_bins))] print('bin_de1 = ', bin_de1) bin_de1_err = [ np.sqrt(de1[index == i].var() / len(de1[index == i])) for i in range(1, len(mag_bins)) ] print('bin_de1_err = ', bin_de1_err) # Fix up nans for i in range(1, len(mag_bins)): if i not in index: bin_de1[i - 1] = 0. bin_de1_err[i - 1] = 0. print('fixed nans') print('index = ', index) print('bin_de1 = ', bin_de1) print('bin_de2 = ', bin_de2) print('bin_dT = ', bin_dT) if legend is None: legend = [r'$\delta e_1$'] plt.plot([min_mag, max_mag], [0, 0], color='black') plt.plot([min_mused, min_mused], [-1, 1], color='Grey') plt.fill([min_mag, min_mag, min_mused, min_mused], [-1, 1, 1, -1], fill=False, hatch='/', color='Grey') e1_line = plt.errorbar(mag_bins[:-1], bin_de1, yerr=bin_de1_err, color=color, fmt='o') plt.set_ylim(-3.e-4, 6.5e-4) plt.set_xlim(min_mag, max_mag) plt.set_xlabel('Magnitude')
def mesh_plot(plt, X, Y, Z, title): plt.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.7, cmap=cm.jet) # DEBUG plt.plot_wireframe(X, Y, Z, rstride=1, cstride=1) plt.contourf(X, Y, Z, zdir='z', cmap=cm.jet, offset=-400) # These used to use coolwarm plt.contourf(X, Y, Z, zdir='x', cmap=cm.jet, offset=41.6) plt.contourf(X, Y, Z, zdir='y', cmap=cm.jet, offset=-87.4) plt.set_xlabel('Lat') plt.set_xlim(41.6, 42.2) plt.set_ylabel('Long') plt.set_ylim(-88.0, -87.4) plt.set_zlabel('Number of Crimes') plt.set_zlim(-100, 1000) plt.set_title(f'{title}')
def plot_cnot_prob(cnot): probs = cnot[0] x = [1, 2, 3, 4] rects = plt.bar(x, probs) for rect in rects: height = rect.get_height() plt.text(rect.get_x() + rect.get_width() / 2., 1.05 * height, f'{height:.3f}', ha='center', va='bottom') plt.set_xticklabels(['|00>', '|01>', '|10>', '|11>']) plt.set_ylim(0., 1.4) plt.show()
def scatterhw12(F=0.4, dt=0.001, o=0): '''scatter method ''' oTitle = str(o * 100) odes = solve_odes(0, 0, F, T=1000 * np.pi) for i in range(1000): plt.scatter(odes["x"][(int)(o + (i / dt) * 2 * np.pi)], odes["y"][(int)(o + (i / dt) * 2 * np.pi)], marker='.', color='k') plt.set_xlim(-1.5, 1.5) plt.set_ylim(-1.1, 1.1) plt.set_xlabel("x") plt.set_ylabel("x'") plt.savefig('frame' + oTitle + '.png')
def plot_hist_line(plt, data, bins, label, color=None, alpha=0.75): global right, top, font avg = data.mean() std = data.std() # label = f'{label}\nmean={avg:.0f},std={std:.0f}' label = f'{label}' # n, bins, patches = plt.hist(data, bins=bins, density=True, color=color, # alpha=alpha, label=label, histtype='step', kde=True # ) sns.distplot(data, hist=False, kde=True, color=color, ax=plt, label=label) # color='tab:red' plt.set_xlim(right=right) plt.set_ylim(top=top) plt.legend(fontsize=font['size']-1) plt.grid(axis='y', alpha=0.4)
def plot_convergence(p_1,p_2,p_3, zoom=3): plt.plot( p_1[:,0], p_1[:,1], '-', label="planet 1", c=c1) plt.plot( p_2[:,0], p_2[:,1], 's', label="planet 2", c=c2) plt.plot( p_3[:,0], p_3[:,1], '^', label="planet 3", c=c3) plt.set_xlabel("x$_{position}$ [AU]", fontsize=20) plt.set_ylabel("y$_{position}$ [AU]", fontsize=20) plt.set_xlim(-zoom, zoom) plt.set_ylim(-zoom, zoom) plt.legend(prop={'size': 15},markerscale=1,fancybox=True,loc=2) plt.tight_layout() axes.axis('equal') plt.show()
def plot_hist(plt, data, bins, label, color=None, alpha=0.75): global right, top avg = data.mean() std = data.std() label = f'{label}\nmean={avg:.0f},std={std:.0f}' n, bins, patches = plt.hist(data, bins=bins, density=True, color=color, alpha=alpha, label=label) # color='tab:red' plt.axvline(avg, color=color, linestyle='-', linewidth=1, alpha=alpha+0.2) plt.axvline(avg+std, color=color, linestyle='--', linewidth=1, alpha=alpha) plt.axvline(avg-std, color=color, linestyle='--', linewidth=1, alpha=alpha) plt.set_xlim(right=right) plt.set_ylim(top=top) plt.legend(fontsize=font['size']-1) plt.grid(axis='y', alpha=0.4) return n, bins, patches
def plotstft(plt, samples, samplerate, binsize=2**10, plotpath=None, colormap="jet"): s = stft(samples, binsize) sshow, freq = logscale_spec(s, factor=1.0, sr=samplerate) ims = 20. * np.log10(np.abs(sshow) / 10e-6) # amplitude to decibel timebins, freqbins = np.shape(ims) print("timebins: ", timebins) print("freqbins: ", freqbins) # plt.figure() plt.imshow(np.transpose(ims), origin="lower", aspect="auto", cmap=colormap, interpolation="none") # plt.figure.colorbar() plt.set_xlabel("time (s)") plt.set_ylabel("frequency (Hz)") plt.set_xlim([0, timebins - 1]) plt.set_ylim([0, freqbins]) xlocs = np.float32(np.linspace(0, timebins - 1, 5)) plt.set_xticks(xlocs, [ "%.02f" % l for l in ((xlocs * len(samples) / timebins) + (0.5 * binsize)) / samplerate ]) ylocs = np.int16(np.round(np.linspace(0, freqbins - 1, 10))) plt.set_yticks(ylocs, ["%.02f" % freq[i] for i in ylocs]) # if plotpath: # plt.savefig(plotpath, bbox_inches="tight") # else: # plt.show() # plt.clf() return ims
def plotSoln(self, plt, solVec, title=""): plt.set_title(title) # plt.set_xlabel('x',size=14,weight='bold') # plt.set_ylabel('y',size=14,weight='bold') plt.set_aspect('equal') plt.set_xlim(-0.1, 5.1) plt.set_ylim(-0.1, 1.2) xy = np.asarray(self.mesh.vertices) if xy.size < 10000: plt.triplot(xy[:, 0], xy[:, 1], self.mesh.elements, 'b-', linewidth=0.5) vals = plt.tricontourf(self.mesh.dtri, solVec, cmap="jet") return vals
def drawROC(result, sp, methodlabel): print("drawing roc curve...") sp.set_xlabel('False positive rate (FPR)') sp.set_ylabel('Ture positive rate (TPR)') sp.set_ylim([0.0, 1.0]) sp.set_xlim([0.0, 1.0]) #sp.set_title('2-class ROC curve') fpr, tpr, thresholds = roc_curve(result['cond'], result['pred']) myauc = auc(fpr, tpr) step_kwargs = ({ 'step': 'post' } if 'step' in signature(sp.fill_between).parameters else {}) sp.step(fpr, tpr, color="green", alpha=1, where='post') print("AUC: %.3f" % myauc) #sp.fill_between(fpr, tpr, color="green", alpha=1, label=methodlabel+" (AUC = %.3f)"%myauc, **step_kwargs) return myauc
def plot1DFourierCorr(dataFile, currFig=None, ylim=None, xshift=0., rerrs=None): """ @brief bin and plot the 1D fourier space correlation from a data file @param dataFile: name of data file (str) @param currFig: the figure to plot to @param ylim: set the y limits to ylim=(y_min, y_max) @param xshift: shift the x axis by xshift (float) @param rerrs: errors from random correlations to use (list) @return: current figure, ell, cell, cell_err """ # get the data spectrum l, cl, clerr = np.loadtxt(dataFile, unpack=True) # check that rerrs is correct length, if provided if rerrs is not None: assert(len(cl) == len(rerrs)) clerr = rerrs # get the current axes if currFig is not None: ax = currFig.get_axes()[0] else: ax = plt.gca() # make the x axis log ax.set_xscale('log') # plot the data ax.errorbar(l+xshift, cl, clerr, ls='', marker='.', markersize=2) ax.axhline(y=0, c='k', ls='--') ax.set_xlabel(r"$\mathrm{\ell}$", fontsize=16) ax.set_ylabel(r"$\mathrm{C_{\ell}}$", fontsize=16) # se the limits ax.set_xlim(left=10.0) if ylim is not None: plt.set_ylim(*ylim) return plt.gcf(), l, cl, clerr
def corr_map(df): ''' Generates correlation mask to hide the unwanted cells from a correlation matrix. required input is the df (DataFrame)''' # Set a new fig and its size fig, ax= plt.subplots(figsize = (12,12)) # Create a corrrelation matrix for each df columns and round it to 3 sig-figs. corr = np.abs(df.corr().round(3)) # Create a mask to hide the duplicate half of the matrix for easy comparison mask = np.zeros_like(corr, dtype=np.bool) idx = np.triu_indices_from(mask) mask[idx] = True # create a heat map with the help of the correlation values sns.heatmap(corr,annot=True,square=True,mask=mask,cmap='Blues', center=0,ax=ax,linewidths=.5, cbar_kws={"shrink": .5}, cbar=True) plt.set_ylim(len(corr),-0.5,0.5)
def ihist(iord_df, wt, subplot=False, bins=100, **kwargs): if subplot: plt = subplot trange = [iord_df['time'].min(), iord_df['time'].max()] binnorm = 1e-9 * bins / (trange[1] - trange[0]) weight = iord_df[wt] * binnorm hist, thebins, patches = plt.hist(iord_df['time'], weights=weight, bins=bins, histtype='step', **kwargs) if not subplot: plt.xlabel('Time [Gyr]', fontsize='large') plt.ylabel('whstfffffff', fontsize='large') else: plt.set_ylim(1.2 * np.min(hist), 1.2 * np.max(hist)) return pynbody.array.SimArray(hist), pynbody.array.SimArray(thebins, "Gyr")
def addToPlot(self, plt, name, groupname, *args, **kwargs): dc = dataconfigs[name] group = self.f[groupname] if name == 'totalconc': arr = np.asarray(group['conc'][:, 0, 0]) if 'obstacle' in self.datanames: arr += np.asarray(group['obstacle'][:, 0, 0]) if 'conc_necro' in self.datanames: arr += np.asarray(group['conc_necro'][:, 0, 0]) else: arr = np.asarray(group[dc.name][:, 0, 0]) * dc.get('scale', 1.) p, = plt.plot( self.xcoords if dc.get('coords', 'x') == 'x' else self.xfacecoords, arr, *args, **kwargs) if dc.rng: if dc.rng == 'zero-centered': a, b = self.findBoundsOverTime(name) v = max(abs(a), abs(b)) #v = np.max(np.abs(arr)) plt.set_ylim(-v, v) elif dc.rng == 'auto': a, b = self.findBoundsOverTime(name) a -= 0.1 * (b - a) b += 0.1 * (b - a) plt.set_ylim((a - 0.1 * (b - a)), b + 0.1 * (b - a)) else: plt.set_ylim(*dc.rng) return p
def plot_decision_regions(X, y, classifier, plt, resolution=0.02): #setup marker generator and color map markers = ('s','x','o','^','v') colors = ('red','blue','lightgreen','gray','cyan') cmap = ListedColormap(colors[:len(np.unique(y))]) x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx1, xx2 = np.meshgrid( np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution)) Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T) Z = Z.reshape(xx1.shape) plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap) plt.set_xlim(xx1.min(), xx1.max()) plt.set_ylim(xx2.min(), xx2.max()) for idx, cl in enumerate(np.unique(y)): plt.scatter(x=X[y==cl, 0], y=X[y==cl, 1], alpha=0.8, c=cmap(idx), marker=markers[idx], label=cl)
def plot1DRealCorr(pickleFile, currFig=None, ylim=None, data=None, xshift=0., rerrs=None): """ @brief bin and plot the 1D realspace correlation from a 2D array @param pickleFile: name of pickle file containing tuple (theta, corr) @param currFig: the figure to plot to @param ylim: set the y limits to ylim=(y_min, y_max) @param data: data tuple of (theta, corr) @param xshift: shift the x axis by xshift (float) @param rerrs: errors from random correlations to use (list) @return: current figure, thetas, correlation, error """ # annular averaging bins = [[0.,10.],[10.,20],[20.,30.],[30.,40.],[40.,50.],[50.,60.],[60.,80.]] #arcmin # read in the data if data is not None: theta = data[0] corr = data[1] else: d = pickle.load(open(pickleFile)) theta = d[0] corr = d[1] r = [] mean = [] std = [] # bin the 2D correlation array for bin in bins: inds = np.where((theta < bin[1]) & (theta > bin[0])) mean +=[ np.mean(corr[inds])] std += [np.std(corr[inds])/np.sqrt(len(inds[0])*1.0)] r += [theta[inds].mean()] mean = np.array(mean) std = np.array(std) n = mean.max() # check that rerrs is correct length, if provided if rerrs is not None: assert(len(mean) == len(rerrs)) std = rerrs # set up the matplotlib axes plt.rcParams['figure.subplot.left'] = 0.17 if currFig is not None: ax = currFig.get_axes()[0] else: ax = plt.gca() # plot the correlation with std dev within the bin as the error ax.errorbar(r+xshift, mean, std) ax.axhline(y=0, c='k') # label the axes ax.set_xlabel(r"$\mathrm{\theta \ (arcminutes)}$", fontsize=16) ax.set_ylabel(r"$\mathrm{\xi(\theta)}$", fontsize=16) if ylim is not None: plt.set_ylim(*ylim) return plt.gcf(), r, mean, std
def sfh(sim,filename=None,massform=True,initstarmass=False,makeplot=True, subplot=False, trange=False, bins=100, binsize=False, zmin=False,overplot=False,linestyle='-',color='k',linewidth=2,label=None,dored=True,**kwargs): ''' star formation history **Optional keyword arguments:** *trange*: list, array, or tuple size(t_range) must be 2. Specifies the time range. *bins*: int number of bins to use for the SFH *label*: string label line for legend *zmin*: float set min z to plot on second axes *overplot*: bool set to True if you are plotting a line on an already existing plot *massform*: bool decides whether to use original star mass (massform) or final star mass *subplot*: subplot object where to plot SFH *legend*: boolean whether to draw a legend or not By default, sfh will use the formation mass of the star. In tipsy, this will be taken from the starlog file. Set massform=False if you want the final (observed) star formation history **Usage:** >>> import pynbody.plot as pp >>> pp.sfh(s,linestyle='dashed',color='k') ''' if subplot: plt = subplot else: import matplotlib.pyplot as plt if 'nbins' in kwargs: bins=kwargs['nbins'] del kwargs['nbins'] if trange: assert len(trange) == 2 else: trange = [0,sim.star['tform'].in_units("Gyr").max()] if binsize: bins = int((trange[1] - trange[0])/binsize) binnorm = 1./(1e9*binsize) else: binnorm = 1e-9*(bins / (trange[1] - trange[0])) trangefilt = filt.And(filt.HighPass('tform',str(trange[0])+' Gyr'), filt.LowPass('tform',str(trange[1])+' Gyr')) tforms = sim.star[trangefilt]['tform'].in_units('Gyr') if massform and not initstarmass: try: weight = sim.star[trangefilt]['massform'].in_units('Msol') * binnorm except (KeyError, units.UnitsException) : warnings.warn("Could not load massform array -- falling back to current stellar masses", RuntimeWarning) weight = sim.star[trangefilt]['mass'].in_units('Msol') * binnorm if initstarmass: weight = np.zeros(np.size(tforms)) weight[:] = initstarmass*binnorm if not initstarmass and not massform: weight = sim.star[trangefilt]['mass'].in_units('Msol') * binnorm if not makeplot: sfhist, thebins = np.histogram(tforms, weights=weight, range=trange,bins=bins) if makeplot: sfhist, thebins, patches = plt.hist(tforms, weights=weight, range=trange,bins=bins,histtype='step',linestyle=linestyle,color=color,linewidth=linewidth,label=label) plt.legend(loc='upper left',fontsize=20) if not overplot: if not subplot: plt.ylim(0.0,1.2*np.max(sfhist)) plt.xlim(trange) plt.xlabel('Time [Gyr]',fontsize=30) plt.ylabel('SFR [M$_\odot$ yr$^{-1}$]',fontsize=30) else: plt.set_ylim(0.0,1.2*np.max(sfhist)) # Make both axes have the same start and end point. if subplot: x0,x1 = plt.get_xlim() else: x0,x1 = plt.gca().get_xlim() from pynbody.analysis import pkdgrav_cosmo as cosmo c = cosmo.Cosmology(sim=sim) if dored: pz = plt.twiny() if not zmin: labelzs = np.arange(10,int(sim.properties['z'])-1,-1) else: labelzs = np.arange(10,int(sim.properties['z'])-1,-1) times = [13.7*c.Exp2Time(1.0 / (1+z))/c.Exp2Time(1) for z in labelzs] pz.set_xticks(times) pz.set_xticklabels([str(x) for x in labelzs]) pz.set_xlim(x0, x1) pz.set_xlabel('Redshift',fontsize=30) if (filename): if config['verbose']: print "Saving "+filename plt.savefig(filename) return array.SimArray(sfhist, "Msol yr**-1"), array.SimArray(thebins, "Gyr")
def sfh(sim, filename=None, massform=True, clear=False, legend=False, subplot=False, trange=False, bins=100, **kwargs): ''' star formation history **Optional keyword arguments:** *trange*: list, array, or tuple size(t_range) must be 2. Specifies the time range. *bins*: int number of bins to use for the SFH *massform*: bool decides whether to use original star mass (massform) or final star mass *subplot*: subplot object where to plot SFH *legend*: boolean whether to draw a legend or not *clear*: boolean if False (default), plot on the current axes. Otherwise, clear the figure first. By default, sfh will use the formation mass of the star. In tipsy, this will be taken from the starlog file. Set massform=False if you want the final (observed) star formation history **Usage:** >>> import pynbody.plot as pp >>> pp.sfh(s,linestyle='dashed',color='k') ''' if subplot: plt = subplot else: import matplotlib.pyplot as plt if "nbins" in kwargs: bins = kwargs['nbins'] if 'nbins' in kwargs: bins = kwargs['nbins'] del kwargs['nbins'] if ((len(sim.g)>0) | (len(sim.d)>0)): simstars = sim.star else: simstars = sim if trange: assert len(trange) == 2 else: trange = [simstars['tform'].in_units( "Gyr").min(), simstars['tform'].in_units("Gyr").max()] binnorm = 1e-9 * bins / (trange[1] - trange[0]) trangefilt = filt.And(filt.HighPass('tform', str(trange[0]) + ' Gyr'), filt.LowPass('tform', str(trange[1]) + ' Gyr')) tforms = simstars[trangefilt]['tform'].in_units('Gyr') if massform: try: weight = simstars[trangefilt][ 'massform'].in_units('Msol') * binnorm except (KeyError, units.UnitsException): warnings.warn( "Could not load massform array -- falling back to current stellar masses", RuntimeWarning) weight = simstars[trangefilt]['mass'].in_units('Msol') * binnorm else: weight = simstars[trangefilt]['mass'].in_units('Msol') * binnorm if clear: plt.clf() sfhist, thebins, patches = plt.hist(tforms, weights=weight, bins=bins, histtype='step', **kwargs) if not subplot: # don't set the limits #plt.ylim(0.0, 1.2 * np.max(sfhist)) plt.xlabel('Time [Gyr]', fontsize='large') plt.ylabel('SFR [M$_\odot$ yr$^{-1}$]', fontsize='large') else: plt.set_ylim(0.0, 1.2 * np.max(sfhist)) # Make both axes have the same start and end point. if subplot: x0, x1 = plt.get_xlim() else: x0, x1 = plt.gca().get_xlim() # add a z axis on top if it has not been already done by an earlier plot: from pynbody.analysis import pkdgrav_cosmo as cosmo c = cosmo.Cosmology(sim=sim) old_axis = plt.gca() if len(plt.gcf().axes)<2: pz = plt.twiny() labelzs = np.arange(5, int(sim.properties['z']) - 1, -1) times = [13.7 * c.Exp2Time(1.0 / (1 + z)) / c.Exp2Time(1) for z in labelzs] pz.set_xticks(times) pz.set_xticklabels([str(x) for x in labelzs]) pz.set_xlim(x0, x1) pz.set_xlabel('$z$') plt.sca(old_axis) if legend: plt.legend(loc=1) if filename: logger.info("Saving %s", filename) plt.savefig(filename) return array.SimArray(sfhist, "Msol yr**-1"), array.SimArray(thebins, "Gyr")
def sbprofile(sim, band='v', diskheight='3 kpc', rmax='20 kpc', binning='equaln', center=True, clear=True, filename=None, axes=False, fit_exp=False, print_ylabel=True, fit_sersic=False, **kwargs): ''' surface brightness profile **Usage:** >>> import pynbody.plot as pp >>> h = s.halos() >>> pp.sbprofile(h[1],exp_fit=3,linestyle='dashed',color='k') **Options:** *band* ('v'): which Johnson band to use. available filters: U, B, V, R, I, J, H, K *fit_exp*(False): Fits straight exponential line outside radius specified. *fit_sersic*(False): Fits Sersic profile outside radius specified. *diskheight('3 kpc')* *rmax('20 kpc')*: Size of disk to be profiled *binning('equaln')*: How show bin sizes be determined? based on pynbody.analysis.profile *center(True)*: Automatically align face on and center? *axes(False)*: In which axes (subplot) should it be plotted? *filename* (None): name of file to which to save output **needs a description of all keywords** By default, sbprof will use the formation mass of the star. In tipsy, this will be taken from the starlog file. ''' if center: logger.info("Centering...") angmom.faceon(sim) logger.info("Selecting disk stars") diskstars = sim.star[filt.Disc(rmax, diskheight)] logger.info("Creating profile") ps = profile.Profile(diskstars, type=binning) logger.info("Plotting") r = ps['rbins'].in_units('kpc') if axes: plt = axes else: import matplotlib.pyplot as plt if clear: plt.clf() plt.plot(r, ps['sb,' + band], linewidth=2, **kwargs) if axes: plt.set_ylim(max(ps['sb,' + band]), min(ps['sb,' + band])) else: plt.ylim(max(ps['sb,' + band]), min(ps['sb,' + band])) if fit_exp: exp_inds = np.where(r.in_units('kpc') > fit_exp) expfit = np.polyfit(np.array(r[exp_inds]), np.array(ps['sb,' + band][exp_inds]), 1) # 1.0857 is how many magnitudes a 1/e decrease is print "h: ", 1.0857 / expfit[0], " u_0:", expfit[1] fit = np.poly1d(expfit) if 'label' in kwargs: del kwargs['label'] if 'linestyle' in kwargs: del kwargs['linestyle'] plt.plot(r, fit(r), linestyle='dashed', **kwargs) if fit_sersic: sersic_inds = np.where(r.in_units('kpc') < fit_sersic) sersicfit = np.polyfit(np.log10(np.array(r[sersic_inds])), np.array(ps['sb,' + band][sersic_inds]), 1) fit = np.poly1d(sersicfit) print "n: ", sersicfit[0], " other: ", sersicfit[1] if 'label' in kwargs: del kwargs['label'] if 'linestyle' in kwargs: del kwargs['linestyle'] plt.plot(r, fit(r), linestyle='dashed', **kwargs) #import pdb; pdb.set_trace() if axes: if print_ylabel: plt.set_ylabel(band + '-band Surface brightness [mag as$^{-2}$]') else: plt.xlabel('R [kpc]') plt.ylabel(band + '-band Surface brightness [mag as$^{-2}$]') if filename: logger.info("Saving %s", filename) plt.savefig(filename)
def sfdens(sim,Volume=50.**3, filename=None,massform=True,clear=True,legend=False, subplot=False, bins=100, label=False, zrange=False,overplot=False,logbins=True,histogram=False,pltloglog=True,**kwargs): ''' star formation history **Optional keyword arguments:** *trange*: list, array, or tuple size(t_range) must be 2. Specifies the time range. *nbins*: int number of bins to use for the SFH *massform*: bool decides whether to use original star mass (massform) or final star mass *subplot*: subplot object where to plot SFH *legend*: boolean whether to draw a legend or not By default, sfh will use the formation mass of the star. In tipsy, this will be taken from the starlog file. Set massform=False if you want the final (observed) star formation history **Usage:** >>> import pynbody.plot as pp >>> pp.sfh(s,linestyle='dashed',color='k') ''' if subplot: plt = subplot else : import matplotlib.pyplot as plt if 'nbins' in kwargs: bins=kwargs['nbins'] del kwargs['nbins'] if zrange: assert len(zrange) == 2 else: zrange = [0,20] if logbins==True: logbinsize = (np.log10(zrange[1]+1)-np.log10(zrange[0]+1))/bins logzplusonebins = np.log10(zrange[0]+1)+np.arange(bins+1)*logbinsize zplusonebins = 10**logzplusonebins zbins = zplusonebins-1 if not logbins: zbinsize = (zrange[1]-zrange[0])/bins zbins = zrange[0] +np.arange(bins+1)*zbinsize from pynbody.analysis import pkdgrav_cosmo as cosmo c = cosmo.Cosmology(sim=sim) timebins = np.zeros(bins+1) for ii in range(bins+1): timebins[ii] = 13.7-13.7*c.Exp2Time(1.0 / (1+zbins[ii]))/c.Exp2Time(1) print timebins binnorm = np.zeros(bins) for i in range(bins): binnorm[i] = 1e-9 / (timebins[i+1] - timebins[i]) trangefilt = filt.And(filt.HighPass('tform',str(13.7-timebins[bins])+' Gyr'), filt.LowPass('tform',str(13.7-timebins[0])+' Gyr')) tforms = sim.star[trangefilt]['tform'].in_units('Gyr') tformslookback = 13.7-tforms if massform : try: weight = sim.star[trangefilt]['massform'].in_units('Msol')# * binnorm / Volume except (KeyError, units.UnitsException) : warnings.warn("Could not load massform array -- falling back to current stellar masses", RuntimeWarning) weight = sim.star[trangefilt]['mass'].in_units('Msol')# * binnorm / Volume else: weight = sim.star[trangefilt]['mass'].in_units('Msol')# * binnorm / Volume if clear : plt.clf() sfdens, thebins = np.histogram(tformslookback, weights=weight, bins=timebins) sfdens = sfdens*binnorm/Volume if not histogram: zbincenter = np.zeros(bins) for i in range(bins): zbincenter[i] = 0.5*(zbins[i]+zbins[i+1]) print zbincenter print sfdens print binnorm print Volume if pltloglog: plt.loglog(zbincenter+1,sfdens,label=label,**kwargs) else: plt.plot(zbincenter,sfdens,label=label,**kwargs) plt.yscale('log') else: sfdens_hist = np.zeros((bins)*2) bins_hist = np.zeros((bins+1)*2) for i in range(bins*2): sfdens_hist[i] = sfdens[i/2] for i in range((bins+1)*2): bins_hist[i] = zbins[i/2] bins_hist = bins_hist[np.arange((bins+1)*2-2)+1] if pltloglog: plt.loglog(bins_hist,sfdens_hist,label=label,**kwargs) else: plt.plot(zbincenter,sfdens,label=label,**kwargs) plt.yscale('log') if not overplot: if not subplot: plt.ylim(0.0,1.2*np.max(sfdens)) plt.xlim(zrange) if pltloglog: plt.xlabel('log(1+z)',fontsize='large') else: plt.xlabel('z',fontsize='large') plt.ylabel(' Specific SFR [M$_\odot$ yr$^{-1} Mpc^{-3}$]',fontsize='large') else: plt.set_ylim(0.0,1.2*np.max(sfdens))