def plot_freq_histogram(freqs_array, x_label, bins='FD', savefig=False, save_path_and_name='./histo_fig.pdf'): """ Plots a histogram of frequencies. :param freqs_array: array-like. list of frequencies. :param x_label: string. Sets the label of x-axis :param bins: float. select the number of bins. Exception: string 'FD' for Freedman-Diaconis rule (default) :param savefig: boolean. True if you want to save the figure. :param save_path_and_name: string. path + fig-name where you want to save the file. Default is './histo_fig.pdf'. Don't forget the '.pdf' at the end of the save_path_and_name string!! """ q75, q25 = np.percentile(abs(freqs_array), [75, 25]) inch_factor = 2.54 sns.set_context("poster") sns.axes_style('white') sns.set_style("ticks") fig, ax = plt.subplots(figsize=(20./inch_factor, 15./inch_factor)) if bins == 'FD': ax.hist(freqs_array, bins=int(2 * (q75 - q25) * len(freqs_array) ** (-1. / 3.)), facecolor='cornflowerblue', alpha=0.8, lw=1.5) # Freedman-Diaconis rule for binwidth else: ax.hist(freqs_array, bins=bins, color='cornflowerblue', alpha=0.8) # Plot Cosmetics ax.set_ylabel('Counts', fontsize=16) ax.set_xlabel(x_label + ' [Hz]', fontsize=14) ax.tick_params(axis='both', which='major', labelsize=12) ax.set_title('Distribution of %s' % x_label, fontsize=16) sns.despine(fig=fig, ax=ax, offset=10) fig.tight_layout() if savefig: fig.savefig(save_path_and_name) plt.close()
def plot_dfs_histogram(dfs_array, binwidth='FD'): """ Plots a histogram of the difference frequencies :param binwidth: select the size of the binwidth. use 'FD' for Freedman-Diaconis rule :param dfs_array: array-like. list of difference frequencies. """ q75, q25 = np.percentile(abs(dfs_array), [75, 25]) inch_factor = 2.54 sns.set_context("poster") sns.axes_style('white') sns.set_style("ticks") fig, ax = plt.subplots(figsize=(15./inch_factor, 10./inch_factor)) if binwidth == 'FD': ax.hist(dfs_array, bins=int(2*(q75-q25) * len(dfs_array)**(-1./3.)), facecolor='cornflowerblue', alpha=0.8) # Freedman-Diaconis rule for binwidth else: ax.hist(dfs_array, bins=binwidth, color='cornflowerblue', alpha=0.8) # Plot Cosmetics ax.set_ylabel('Counts', fontsize=16) ax.set_xlabel('Possible Beat-Frequencies [Hz]', fontsize=14) ax.tick_params(axis='both', which='major', labelsize=12) ax.set_title('Distribution of Beat-Frequencies', fontsize=16) sns.despine(fig=fig, ax=ax, offset=10) fig.tight_layout() fig.savefig('figures/histo_of_dfs.pdf') plt.close()
def fishtype_barplot(data): """ This function creates a bar plot showing the distribution of wave-fishes vs. pulse-fishes. :param data: dictionary with fish-type as keys and array of EODfs as values. """ # Read the keys of the dictionary and use them to get the count of pulse- and wave-type fishes. keys = np.array(data.keys()) bool_wave = np.array(['wave' in e for e in keys], dtype=bool) bool_pulse = np.array(['puls' in e for e in keys], dtype=bool) count_wave = len(data[keys[bool_wave][0]]) count_pulse = len(data[keys[bool_pulse][0]]) inch_factor = 2.54 sns.set_context("poster") sns.axes_style('white') sns.set_style("ticks") fig, ax = plt.subplots(figsize=(10./inch_factor, 10./inch_factor)) width = 0.5 ax.bar(1-width/2., count_wave, width=width, facecolor='cornflowerblue', alpha=0.8) ax.bar(2-width/2., count_pulse, width=width, facecolor='salmon', alpha=0.8) ax.set_xticks([1, 2]) ax.set_xticklabels(['Wave-type', 'Pulse-type']) ax.tick_params(axis='both', which='major', labelsize=14) ax.set_ylabel('Number of Fishes', fontsize=14) ax.set_title('Distribution of Fish-types', fontsize=16) sns.despine(fig=fig, ax=ax, offset=10) fig.tight_layout() fig.savefig('figures/fishtype_barplot.pdf') plt.close()
def create_histo(data): """ gets a list of data and creates an histogram of this data. :param data: dictionary with fishtype as keys and np.array with EOD-Frequencies as values. """ print 'creating histogramm ...' inch_factor = 2.54 sns.set_context("poster") sns.axes_style('white') sns.set_style("ticks") fig, ax = plt.subplots(figsize=(15./inch_factor, 10./inch_factor)) colors = ['salmon', 'cornflowerblue'] for enu, curr_fishtype in enumerate(data.keys()): if len(data[curr_fishtype]) >= 4: hist, bins = np.histogram(data[curr_fishtype], bins=len(data[curr_fishtype])//4) width = 0.7 * (bins[1] - bins[0]) center = (bins[:-1] + bins[1:]) / 2 ax.bar(center, hist, align='center', width=width, alpha=0.8, facecolor=colors[enu], label=curr_fishtype) ax.set_ylabel('Counts', fontsize=14) ax.set_xlabel('Frequency [Hz]', fontsize=14) ax.set_xticks(np.arange(0, max(np.hstack(data.values()))+100, 250)) ax.tick_params(axis='both', which='major', labelsize=12) ax.set_title('Distribution of EOD-Frequencies', fontsize=16) ax.legend(frameon=False, loc='best', fontsize=12) sns.despine(fig=fig, ax=ax, offset=10) fig.tight_layout() fig.savefig('figures/histo_of_eod_freqs.pdf') plt.close()
def plotPosInRangeMultifly(ax, frameRange, xPos, yPos, angle, flyID, fly, currCmap, imagePx): # Plot trajectories of multi-fly tracking data (obtained with Ctrax) cNorm = colors.Normalize(vmin=-0.5*len(frameRange), vmax=1*len(frameRange)) scalarMap = plt.cm.ScalarMappable(norm=cNorm, cmap=currCmap) for ind, frame in enumerate(frameRange): currCol = scalarMap.to_rgba(len(frameRange) - ind) if fly == -1: # iterate over all available flies ax.plot(xPos[frame], yPos[frame], marker='.', markersize=10, linestyle='none', alpha=0.5, color=currCol) for fly in flyID[frame]: plotBodyAngle(ax, xPos[frame][flyID[frame] == fly], yPos[frame][flyID[frame] == fly], angle[frame][flyID[frame] == fly], currCol, 0.5, 20) else: # just plot trace of specified fly ax.plot(xPos[frame][flyID[frame] == fly], yPos[frame][flyID[frame] == fly], marker='.', markersize=6, linestyle='none', alpha=0.5, color=currCol) plotBodyAngle(ax, xPos[frame][flyID[frame] == fly], yPos[frame][flyID[frame] == fly], angle[frame][flyID[frame] == fly], currCol, 0.5, 20) ax.set_aspect('equal') sns.despine(right=True, offset=0) # trim=True) sns.axes_style({'axes.linewidth': 1, 'axes.edgecolor': '.8', }) plt.xlim([imagePx[0] / 2.0 - 500, imagePx[0] / 2.0 + 500]) plt.ylim([imagePx[1] / 2.0 - 500, imagePx[1] / 2.0 + 500])
def plot_teh(damage,title,subplot): plt.subplot(subplot) h5 = read_csv(5,damage) h4 = read_csv(4,damage) h3 = read_csv(3,damage) h2 = read_csv(2,damage) h1 = read_csv(1,damage) xlist = [z for z in range(len(h5))] ph5 = plt.plot(xlist,h5,'-o',label='h5',marker = 'v',markersize=8,color='blue') ph4 = plt.plot(xlist,h4,'-o',label='h4',marker = 'o',markersize=8,color='green') ph3 = plt.plot(xlist,h3,'-o',label='h3',marker = 'D',markersize=8,color='orange') ph2 = plt.plot(xlist,h2,'-o',label='h2',marker = '*',markersize=8,color='red') ph1 = plt.plot(xlist,h1,'-o',label='h1',marker = 's',markersize=8,color='black') #plt.legend(loc='upper left', handlelength=5, borderpad=1.2, labelspacing=1.2) #plt.legend() plt.legend(loc='upper right', fontsize = 'xx-large') plt.tick_params(axis='both',which='major', labelsize='large') plt.title(title,size = 'xx-large',weight='bold') plt.xlabel('Rank',size='xx-large',weight='bold') plt.ylabel('Transfer Entropy',size='xx-large',weight='bold') sns.axes_style("darkgrid", {"axes.facecolor": ".9"}) plt.ylim(ymin=0.0,ymax = 1.0) plt.xlim(xmin=0.0,xmax = len(h5)) plt.margins(0.2) plt.tight_layout(pad=2.5) return
def plot_chart(dictionary): x = np.array(list(dictionary.keys())) y = np.array(list(dictionary.values())) sns.axes_style('white') sns.set_style('white') COLORS = 'Greens_d' (f, ax) = plt.subplots(1) b = sns.barplot( x, y, ci = None, palette = COLORS, hline = 0, ax = ax, x_order = x) #labels _TITLE = "Registrants by MSB Activities" ax.set_title(_TITLE) _XLABEL = "MSB Activities" ax.set_xlabel(_XLABEL) _YLABEL = "No of Registrants" ax.set_ylabel(_YLABEL) b.set_xticklabels(x, rotation = 90) #create margin at bottom for better layout plt.subplots_adjust(bottom=0.35) plt.show()
def plot_hist_algo(wave_hist_algor, pulse_hist_algor, multi_wave_hist_algor): inch_factor = 2.54 sns.set_context("poster") sns.axes_style('white') # sns.set_style("ticks") fig4= plt.figure(figsize=(35./ inch_factor, 20./ inch_factor)) ax1 = fig4.add_subplot(2, 3, (1, 4)) dafr = pd.DataFrame([wave_hist_algor, multi_wave_hist_algor, pulse_hist_algor]) #turn dafr = dafr.transpose() dafr.columns = ['wave', 'multi-wave', 'pulse'] sns.violinplot(data=dafr, ax=ax1, col=("blue", "green", "red")) ax1.set_ylabel('psd_proportion') ax1.set_xlabel('EOD-type') ax1.set_title('Fishsorting based on PSD') wave_psd_data = np.load('wave_psd_data.npy') wave_hist_data = wave_psd_data[1][:len(wave_psd_data[0][wave_psd_data[0]<1500])] ax3 = fig4.add_subplot(2, 3, (2, 5)) n, bin, patch = ax3.hist(wave_hist_data, 50, color='blue', alpha=0.7, normed=True) # ax3.set_ylim([0, max(n)+10]) ax3.set_ylabel('counts in histogram bin') ax3.set_xlabel('amplitude of PSD') ax3.set_title('Histogram of pulsefish PSD') pulse_psd_data = np.load('pulse_psd_data.npy') pulse_hist_data = pulse_psd_data[1][:len(pulse_psd_data[0][pulse_psd_data[0]<1500])] ax2 = fig4.add_subplot(2, 3, (3, 6)) ax2.hist(pulse_hist_data, 50, color='red', alpha=0.7, normed=True) # ax2.set_ylim([0, max(n)+10]) ax2.set_ylabel('counts in histogram bin') ax2.set_xlabel('amplitude of PSD') ax2.set_title('Histogram of pulsefish PSD') fig4.tight_layout()
def draw_bwin_analysis_plot(filename, t_trace, eod_trace, no_of_peaks, cvs, mean_ampl): fs = 20 inch_factor = 2.54 sns.set_context("poster") sns.axes_style('white') sns.set_style("ticks") fig = plt.figure(figsize=(40. / inch_factor, 35. / inch_factor), num='Fish No. '+filename[-10:-8]) ax1 = fig.add_subplot(5, 1, 1) ax2 = fig.add_subplot(5, 1, 2) ax3 = fig.add_subplot(5, 1, 3) ax4 = fig.add_subplot(5, 1, 4) ax5 = fig.add_subplot(5, 1, 5) # ax2 plots the Number of detected EOD-cycles window_nr = np.arange(len(no_of_peaks)) * 0.2 ax2.plot(window_nr, no_of_peaks, 'o', ms=10, color='grey', mew=2., mec='black', alpha=0.6) ax2.set_ylabel('No. of detected\nEOD-cycles', fontsize=fs) # ax3 plots the Amplitude Coefficient of Variation ax3.plot(window_nr, cvs, 'o', ms=10, color='grey', mew=2., mec='black', alpha=0.6) ax3.set_ylabel('Soundtrace Amplitude\nVariation Coefficient', fontsize=fs) # ax4 plots the Mean Amplitude of each Window ax4.plot(window_nr, mean_ampl, 'o', ms=10, color='grey', mew=2., mec='black', alpha=0.6) ax4.set_ylabel('Mean Window\nAmplitude [a.u]', fontsize=fs) ax = np.array([ax1, ax2, ax3, ax4, ax5]) return ax
def analyseCCFs(ccf_G2): amp=np.empty(len(ccf_G2)) rv=np.empty(len(ccf_G2)) fwhm=np.empty(len(ccf_G2)) mjd=np.empty(len(ccf_G2)) combined_ccfs=[] for i in range(0,len(ccf_G2)): h=fits.open(ccf_G2[i]) crval1=h[0].header['CRVAL1'] cdelt1=h[0].header['CDELT1'] mjd[i]=h[0].header['MJD-OBS'] data=h[0].data # prepare the plotting array fig = pl.figure(i+1,figsize=(15,15)) fig.clf() fig.suptitle('%s CCFs' % (ccf_G2[i])) seaborn.axes_style("darkgrid") montage_dim=int(ceil(sqrt(len(data)))) c=0 for j in range(0,montage_dim): for k in range(0,montage_dim): ax = fig.add_subplot(montage_dim, montage_dim, c+1, xticks=[], yticks=[]) if c < len(data): # try fitting a Gaussian to the ccf # tweak y values to help the fit # only fit those that are not 0s x=np.arange(0,len(data[c])) xn=(x+cdelt1)+crval1 t_data=data[c]-max(data[c]) ax.plot(xn,t_data,'b.') ax.set_title('order %d' % (c+1)) if max(data[c]) > 0 and np.std(data[c]) > 0: p0=estimateGaussianParams(t_data,xn) try: coeff,cov=curve_fit(Gaussian,xn,t_data,p0) yfit = Gaussian(xn, *coeff) print ('[%d/%d] x0=%.4f ' % (c+1,len(data),coeff[1])) ax.plot(xn,yfit,'r--') if c == len(data)-1: amp[i]=coeff[0] rv[i]=coeff[1] fwhm[i]=coeff[2] combined_ccfs.append(data[c]) except RuntimeError: print ('[%d/%d] fit did not converge ' % (c+1,len(data))) if c == len(data)-1: print ('[%d/%d] FINAL FIT DID NOT CONVERGE! ' % (c+1,len(data))) combined_ccfs.append(data[c]) else: print('[%d/%d] Cannot fit zeros' % (c+1,len(data))) c+=1 #print("[%d/%d]" % (c,len(data))) # plot the montage of CCFs fig.savefig('%s.png' % (ccf_G2[i]),dpi=300) return mjd,rv_corr,amp,fwhm,combined_ccfs
def plot_colormaps_each(maps, Working_Directory, name_for_saving_figures, pp, matched_pixels, unique_clrs): Trial_Directories = [f for f in os.listdir(os.path.join(Working_Directory)) if os.path.isdir(os.path.join(Working_Directory, f)) and f.find('Figures')<0 and f.find('DataFrames')<0] #Get only directories ## To find num z planes in each trial directory num_z_planes = np.zeros((np.size(Trial_Directories)), dtype=np.int) for jj in xrange(0, np.size(Trial_Directories, axis = 0)): Image_Directory = os.path.join(Working_Directory, Trial_Directories[jj],'C=1')+filesep tif = TIFF.open(Image_Directory +'T=1.tif', mode='r') #Open multitiff count = 1 for image in tif.iter_images(): num_z_planes[jj] = count count = count+1 count = 0 count_trial1 = 0 for ii in xrange(0, np.size(Trial_Directories, axis = 0)): count_subplot = 1 for jj in xrange(0, num_z_planes[ii]): name_for_saving_figures1 = name_for_saving_figures + ' ' + Trial_Directories[ii] + ' Z=' + str(jj+1) with sns.axes_style("darkgrid"): fig2 = plt.subplot(2,2,count_subplot) plt.imshow(maps[:,:,count,:]) plt.title(name_for_saving_figures1) plt.axis('off') count = count+1 count_subplot = count_subplot + 1 # If there are more than 6 panel, save and start new figure if count_subplot == 5: fig2 = plt.gcf() pp.savefig(fig2) plt.close() count_subplot = 1 #Plot boxplots for each trial if count_subplot <= 4: with sns.axes_style("darkgrid"): fig2 = plt.subplot(2,2,count_subplot) fig2 = plot_boxplot(fig2, matched_pixels[:,count_trial1:count_trial1+num_z_planes[ii]], unique_clrs) # plt.tight_layout() fig2 = plt.gcf() pp.savefig(fig2) plt.close() count_trial1 = count_trial1 + num_z_planes[ii] else: with sns.axes_style("darkgrid"): fig3 = plt.figure() fig3 = plot_boxplot(fig3, matched_pixels[:,count_trial1:count_trial1+num_z_planes[ii]], unique_clrs) # plt.tight_layout() fig3 = plt.gcf() pp.savefig(fig3) plt.close() count_trial1 = count_trial1 + num_z_planes[ii]
def plot_BIC(all_results, size=4.6, dpi=300, ext='png', plot_dir=None): """ Plots BIC and SABIC curves Args: all_results: a dimensional structure all_results object dpi: the final dpi for the image ext: the extension for the saved figure plot_dir: the directory to save the figure. If none, do not save """ all_colors = [sns.color_palette('Blues_d',3)[0:3], sns.color_palette('Reds_d',3)[0:3], sns.color_palette('Greens_d',3)[0:3], sns.color_palette('Oranges_d',3)[0:3]] height= size*.75/len(all_results) with sns.axes_style('white'): fig, axes = plt.subplots(1, len(all_results), figsize=(size, height)) for i, results in enumerate([all_results[key] for key in ['task','survey']]): ax1 = axes[i] name = results.ID.split('_')[0].title() EFA = results.EFA # Plot BIC and SABIC curves colors = all_colors[i] with sns.axes_style('white'): x = list(EFA.results['cscores_metric-BIC'].keys()) # score keys keys = [k for k in EFA.results.keys() if 'cscores' in k] for key in keys: metric = key.split('-')[-1] BIC_scores = [EFA.results[key][i] for i in x] BIC_c = EFA.results['c_metric-%s' % metric] ax1.plot(x, BIC_scores, 'o-', c=colors[0], lw=size/6, label=metric, markersize=height*2) ax1.plot(BIC_c, BIC_scores[BIC_c-1], '.', color='white', markeredgecolor=colors[0], markeredgewidth=height/2, markersize=height*4) if i==0: if len(keys)>1: ax1.set_ylabel('Score', fontsize=height*3) leg = ax1.legend(loc='center right', fontsize=height*3, markerscale=0) beautify_legend(leg, colors=colors) else: ax1.set_ylabel(metric, fontsize=height*4) ax1.set_xlabel('# Factors', fontsize=height*4) ax1.set_xticks(x) ax1.set_xticklabels(x) ax1.tick_params(labelsize=height*2, pad=size/4, length=0) ax1.set_title(name, fontsize=height*4, y=1.01) ax1.grid(linewidth=size/8) [i.set_linewidth(size*.1) for i in ax1.spines.values()] if plot_dir is not None: save_figure(fig, path.join(plot_dir, 'BIC_curves.%s' % ext), {'bbox_inches': 'tight', 'dpi': dpi}) plt.close()
def plotpopulations(): import seaborn as sns import matplotlib as mpl sns.axes_style("darkgrid") sns.set_context("notebook", font_scale=1, rc={"lines.linewidth": 2.5}) mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern'], 'size': 46}); mpl.rc('text', usetex=True) dspath = dir + 'ALL/' individual_names_file = 'integrated_call_samples_v3.20130502.ALL.panel' All = pd.read_table(dspath + individual_names_file, sep='\t'); plt.figure(figsize=(10, 4.5), dpi=200); All['pop'].value_counts().plot(kind='bar')
def plot_NMF_maps(Working_Directory, name_for_saving_figures, name_for_saving_files, \ NMF_components, maps, colors_NMF, matched_pixels, stimulus_on_time, stimulus_off_time, flag, unique_clrs): # To save as pdf create file Figure_PDFDirectory = Working_Directory+filesep+'Figures'+filesep if not os.path.exists(Figure_PDFDirectory): os.makedirs(Figure_PDFDirectory) pp = PdfPages(Figure_PDFDirectory+name_for_saving_files+'_NMF.pdf') sns.set_context("poster") ########### Plot components ################## fig2 = plt.figure() sns.set_context("talk", font_scale=1.25) with sns.axes_style("darkgrid"): ax1 = plt.subplot(221) plot_NMF_components(NMF_components,ax1,stimulus_on_time, stimulus_off_time, colors_NMF) #Plot mean projection with sns.axes_style("white"): fig2 = plt.subplot(223) if len(maps.shape)==3: plt.imshow(maps) else: image(np.amax(maps,2)) plt.axis('off') plt.title('Max projection') ########### Plot Boxplot of number of pixels ################## with sns.axes_style("white"): fig2 = plt.subplot(222) fig2 = plot_boxplot(fig2, matched_pixels, colors_NMF[0:np.size(NMF_components,1)]) plt.tight_layout() fig2 = plt.gcf() pp.savefig(fig2) plt.close() ################ Plot color maps individually ####################### if flag == 0: plot_colormaps_ind(maps, Working_Directory, name_for_saving_figures, pp) elif flag == 1: plot_colormaps_each(maps, Working_Directory, name_for_saving_figures, pp,matched_pixels,colors_NMF[0:np.size(NMF_components,1)]) elif flag == 2: plot_colormaps_all( maps, Working_Directory, pp, matched_pixels, colors_NMF[0:np.size(NMF_components,1)]) plot_colormaps_all_z_plane_wise(maps, Working_Directory, pp,matched_pixels, colors_NMF[0:np.size(NMF_components,1)]) pp.close()
def plot_ica_maps(Working_Directory, name_for_saving_figures, name_for_saving_files, \ ica_components_plot, maps, colors_ica, matched_pixels, stimulus_on_time, stimulus_off_time, flag): # To save as pdf create file Figure_PDFDirectory = Working_Directory+filesep+'Figures'+filesep if not os.path.exists(Figure_PDFDirectory): os.makedirs(Figure_PDFDirectory) pp = PdfPages(Figure_PDFDirectory+name_for_saving_files+'_ICA.pdf') sns.set_context("poster") ########### Plot components ################## fig2 = plt.figure() sns.set_context("talk", font_scale=1.25) with sns.axes_style("darkgrid"): ax1 = plt.subplot(221) plot_ica_components(ica_components_plot,colors_ica, ax1,stimulus_on_time, stimulus_off_time) ########### Plot Boxplot of number of pixels ################## with sns.axes_style("white"): fig2 = plt.subplot(222) fig2 = plot_boxplot(fig2, matched_pixels, colors_ica) #Plot mean projection with sns.axes_style("white"): if len(maps.shape)==3: temp = maps else: temp = (np.mean(maps, axis=2)) fig2 = plt.subplot(223) plt.imshow(temp.astype(np.float16)) plt.axis('off') plt.title('Mean projection') plt.tight_layout() fig2 = plt.gcf() pp.savefig(fig2) plt.close() # ## Plot color maps individually ####################### if flag == 0: plot_colormaps_ind(maps, Working_Directory, name_for_saving_figures, pp) elif flag == 1: plot_colormaps_each(maps, Working_Directory, name_for_saving_figures, pp,matched_pixels, colors_ica) elif flag == 2: plot_colormaps_all(maps, Working_Directory, pp, matched_pixels, colors_ica) plot_colormaps_all_z_plane_wise(maps, Working_Directory, pp, matched_pixels, colors_ica) pp.close()
def plotSparseMatrix(figsize,aspectRatio,matrixToPlot,titleString): fig = plt.figure(figsize=figsize) fig.set_canvas(plt.gcf().canvas) sns.set_style('ticks') ax = fig.add_subplot(111) ax.spy(matrixToPlot) ax.set_aspect(aspectRatio) ax.set_title(titleString) sns.axes_style({'axes.linewidth': 1, 'axes.edgecolor': '.8'}) myAxisTheme(ax) return fig
def plotMoonDataFit(moon_ang,median_counts,xfit,yfit,moon_phase): action=os.getcwd().split('/')[-1] fig,ax=pl.subplots(1,figsize=(10,10)) seaborn.axes_style("darkgrid") ax.semilogy(moon_ang,median_counts,'r.') ax.semilogy(xfit,yfit,'k--') ax.set_xlabel('Moon Distance (deg)') ax.set_ylabel('Median ADU (image)') #ax.set_yticks(np.arange(0,max(yfit),2000)) ax.set_title('Moon Avoidance Test (%s - %d%% illuminated)' % (action, int(np.average(moon_phase)*100))) ax.set_xlim(0,180) ax.set_ylim(100,10000) ax.grid(True,which='both') fig.savefig("%s/MoonAvoidance_%s.png" % (outdir,action), dpi=300) return action
def plot_ais(filename,damage): x = [] s40 = [] s35 = [] s30 = [] s25 = [] s20 = [] s15 = [] s10 = [] with open(filename,'r') as csvfile: plots = csv.reader(csvfile, delimiter =',') for row in plots: x.append(row[0]) s40.append(row[1]) s35.append(row[2]) s30.append(row[3]) s25.append(row[4]) s20.append(row[5]) s15.append(row[6]) s10.append(row[7]) xlist = [z for z in range(len(x))] plt.figure(figsize=(15,15)) ps40 = plt.plot(xlist,s40,'-o',label='step40') ps35 = plt.plot(xlist,s35,'-o',label='step35') ps30 = plt.plot(xlist,s30,'-o',label='step30') ps25 = plt.plot(xlist,s25,'-o',label='step25') ps20 = plt.plot(xlist,s20,'-o',label='step20') ps15 = plt.plot(xlist,s15,'-o',label='step15') ps10 = plt.plot(xlist,s10,'-o',label='step10') plt.xticks(xlist,x,rotation='vertical') #plt.legend(loc='upper left', handlelength=5, borderpad=1.2, labelspacing=1.2) #plt.legend() plt.legend(loc='upper right',fontsize='xx-large') d_title = 'AI for %s_Normal with varying max step size and history length of 5.'%(damage) plt.title(d_title) plt.xlabel('Node') plt.ylabel('Active Information') sns.axes_style("darkgrid", {"axes.facecolor": ".9"}) plt.ylim(ymin=0.0) plt.margins(0.2) #plt.subplots_adjust(bottom=0.25) plt.tight_layout(pad=2.5) plt.savefig("ai-all-stepd-h5-%s-normal.pdf"%(damage)) plt.show() return
def decode_uniform_samples_from_latent_space(_): fig, ax = plt.subplots() nx = ny = 20 extent_x = extent_y = [-3, 3] extent = numpy.array(extent_x + extent_y) x_values = numpy.linspace(*(extent_x + [nx])) y_values = numpy.linspace(*(extent_y + [nx])) full_extent = extent * (nx + 1) / float(nx) canvas = numpy.empty((28 * ny, 28 * nx)) for ii, yi in enumerate(x_values): for j, xi in enumerate(y_values): n = ii * nx + j + 1 sys.stdout.write("\rsampling p(X|z), sample %d/%d" % (n, nx*ny)) sys.stdout.flush() np_z = numpy.array([[xi, yi]]) x_mean = sess.run(prior_model(latent=numpy.reshape(np_z, newshape=(1, LATENT_DIM)))[0]) canvas[(nx - ii - 1) * 28:(nx - ii) * 28, j * 28:(j + 1) * 28] = x_mean[0].reshape(28, 28) with seaborn.axes_style('ticks'): seaborn.set_context(context='notebook', font_scale=1.75) fig, ax = plt.subplots(figsize=(12, 9)) ax.imshow(canvas, extent=full_extent) ax.xaxis.set_ticks(numpy.linspace(*(extent_x + [nx]))) ax.yaxis.set_ticks(numpy.linspace(*(extent_y + [ny]))) ax.set_xlabel('z_1') ax.set_ylabel('z_2') ax.set_title('P(X|z); decoding latent space; (CONV, BNAE, IND_ERROR) = (%d,%d,%d)' % (CONV, BNAE, IND_ERROR)) plt.show() plt.savefig(os.path.join(FLAGS.viz_dir, 'P(X|z).png')) return fig, ax
def scale_plot(input_data, data_colors=None, cluster_colors=None, cluster_sizes=None, dissimilarity='euclidean', filey=None): """ Plot MDS of data and clusters """ if data_colors is None: data_colors = 'r' if cluster_colors is None: cluster_colors='b' if cluster_sizes is None: cluster_sizes = 2200 # scale mds = MDS(dissimilarity=dissimilarity) mds_out = mds.fit_transform(input_data) with sns.axes_style('white'): f=plt.figure(figsize=(14,14)) plt.scatter(mds_out[n_clusters:,0], mds_out[n_clusters:,1], s=75, color=data_colors) plt.scatter(mds_out[:n_clusters,0], mds_out[:n_clusters,1], marker='*', s=cluster_sizes, color=cluster_colors, edgecolor='black', linewidth=2) # plot cluster number offset = .011 font_dict = {'fontsize': 17, 'color':'white'} for i,(x,y) in enumerate(mds_out[:n_clusters]): if i<9: plt.text(x-offset,y-offset,i+1, font_dict) else: plt.text(x-offset*2,y-offset,i+1, font_dict) if filey is not None: plt.title(path.basename(filey)[:-4], fontsize=20) save_figure(f, filey) plt.close()
def plot_retest_data(retest_data, size=4.6, save_dir=None): colors = [sns.color_palette('Reds_d',3)[0], sns.color_palette('Blues_d',3)[0]] f = plt.figure(figsize=(size,size*.75)) # plot boxes with sns.axes_style('white'): box_ax = f.add_axes([.15,.1,.8,.5]) sns.boxplot(x='icc3.k', y='Measure Category', ax=box_ax, data=retest_data, palette={'Survey': colors[0], 'Task': colors[1]}, saturation=1, width=.5, linewidth=size/4) box_ax.text(0, 1, '%s Task measures' % Task_N, color=colors[1], fontsize=size*2) box_ax.text(0, 1.2, '%s Survey measures' % Survey_N, color=colors[0], fontsize=size*2) box_ax.set_ylabel('Measure category', fontsize=size*2, labelpad=size) box_ax.set_xlabel('Intraclass correlation coefficient', fontsize=size*2, labelpad=size) box_ax.tick_params(labelsize=size*1.5, pad=size, length=2) [i.set_linewidth(size/5) for i in box_ax.spines.values()] # plot distributions dist_ax = f.add_axes([.15,.6,.8,.4]) dist_ax.set_xlim(*box_ax.get_xlim()) dist_ax.set_xticklabels('') dist_ax.tick_params(length=0) for i, (name, g) in enumerate(retest_data.groupby('Measure Category')): sns.kdeplot(g['icc3.k'], color=colors[i], ax=dist_ax, linewidth=size/3, shade=True, legend=False) dist_ax.set_ylim((0, dist_ax.get_ylim()[1])) dist_ax.axis('off') if save_dir: plt.savefig(save_dir, dpi=dpi, bbox_inches='tight')
def make_scatter_plot(frame, name, **kwargs): """ Makes a scatter plot of column name in frame. """ column_x = frame[name] if name == 'deltam31': column_x*=100.0 params = [] exclude = set(['hypo','llh','mctrue']) params = list(set(frame.columns).difference(exclude)) figs = [] # Plot correlation scatter plot for all other systematics for p in params: if p == name: continue column_y = frame[p] if p == 'deltam31': column_y*=100.0 if 'theta' in p: column_y = np.rad2deg(column_y) with sns.axes_style("whitegrid"): sns.jointplot(column_x, column_y, size=8, color='b', **kwargs) plt.tight_layout() figs.append(plt.gcf()) return figs
def plot(tx=True, descriptions=False, edges=True, subset=None, figsize=(10,12), height=None, title=None, edge_color=None, tx_markersize=None): """Draw a plot of the basic map. Arguments are booleans, indicating whether tx points should be drawn, and whether edges should be drawn. tx point are drawn in zorder 2 edges are drawn in zorder 1 """ if height is not None: # Preserve 10,12 aspect ratio figsize = ((5/6)*height, height) with sns.axes_style("white"): fig, ax = sns.plt.subplots(figsize=figsize) if edges: _edges(ax, color=edge_color) if tx: _tx_points(ax, descriptions=descriptions, markersize=tx_markersize) adjust_lims(ax, subset) clear_ticks(ax) if title is not None: ax.set_title(title) return ax
def plot_boxes(self, peaks): """Draw a boxplot to show the distribution of copes at peaks.""" cope_data = nib.load(self.inputs.cope_file).get_data() peak_spheres = self._peaks_to_spheres(peaks).get_data() peak_dists = np.zeros((cope_data.shape[-1], len(peaks))) for i, peak in enumerate(peaks, 1): sphere_mean = cope_data[peak_spheres == i].mean(axis=(0)) peak_dists[:, i - 1] = sphere_mean with sns.axes_style("whitegrid"): f, ax = plt.subplots(figsize=(9, float(len(peaks)) / 3 + 0.33)) try: # seaborn >= 0.6 sns.boxplot(data=peak_dists, palette="husl", orient="h", ax=ax) labels = np.arange(len(peaks)) + 1 except TypeError: # seaborn < 0.6 pal = sns.husl_palette(peak_dists.shape[1])[::-1] sns.boxplot(peak_dists[:, ::-1], color=pal, ax=ax, vert=False) labels = np.arange(len(peaks))[::-1] + 1 sns.despine(left=True, bottom=True) ax.axvline(0, c=".3", ls="--") ax.set(yticklabels=labels, ylabel="Local Maximum", xlabel="COPE Value") out_fname = op.realpath("peak_boxplot.png") self.out_files.append(out_fname) f.savefig(out_fname, bbox_inches="tight") plt.close(f)
def plot_facet_grid(self, my_jira_df, my_filename_without_path, my_chart_title, my_output_path, my_relative_output_path, my_png_list): with sns.axes_style("white"): '''g = sns.FacetGrid( my_jira_df, row="IssueType", col="From", margin_titles=True, size=2.5, hue = 'To')''' g = sns.FacetGrid( my_jira_df, row="To", col="From", margin_titles=True, size=2.5, hue='IssueType') g.map(plt.scatter, "WeekNum", "DayDiff", color="#334488", edgecolor="white", lw=.5) g.set_axis_labels("Week Number", "Days in State") g.set(xticks=list(range(0, 101, 20)), yticks=list(range(0, 1001, 100))) g.fig.subplots_adjust(wspace=.02, hspace=.02) self.save_file(my_filename_without_path, my_chart_title, my_output_path, my_relative_output_path, my_png_list, g)
def find_LowHighTide_Amplitudes(time_array, wl_array, tau=12.42/24., prc=1./24., order=1, plot=False, log=False, datetime_fmt='%d.%m.%Y %H:%M', plot_title="", axeslabel_fontsize=18., title_fontsize=20., axesvalues_fontsize=18., annotation_fontsize=18., legend_fontsize=18.): """ This script should be used with data which has no missing regions. Although it will work with all data, but may produce inaccuraces. Missing values should be represented by np.nan in wl_array. time_array - numpy array with datetime objects wl_array - numpy array with measured values of waterlevel. Must have same lenght as time_array tau - float, signal period in days prc - indicates presicion value +- for comparing time diffrerence between found extremums with tidal_cycle order - integer for scipy.signal.argrelextrema() plot - boolean flag to show plot log - boolean flag to show log # tidal cycle is aproximately 12h25min. Each timestep is 10 min => tidal cycle is 74.5 timesteps # therefore, maxima and minima will be filtered in a range of 73 to 76 timesteps from each other # for safety reasons lets take 720min """ if len(time_array) != len(wl_array): raise ValueError('time and waterlevel arays should have equal lenght.\nGot: len(time)={0}, len(wl)={1}'.format( len(time_array), len(wl_array))) local_maximums = scipy.signal.argrelextrema(wl_array, np.greater_equal, order=order, mode='clip')[0] local_minimums = scipy.signal.argrelextrema(wl_array, np.less_equal, order=order, mode='clip')[0] local_maximums = remove_regions_from_extremums(local_maximums, log=log) local_minimums = remove_regions_from_extremums(local_minimums, log=log) errors_high = check_extremums_dt(local_maximums, time_array, tau=tau, prc=prc, log=log) errors_low = check_extremums_dt(local_minimums, time_array, tau=tau, prc=prc , log=log) if plot: with sns.axes_style("whitegrid"): plot_extremums(time_array, wl_array, local_minimums, local_maximums, time_errors_high=errors_high, time_errors_low=errors_low, date_xaxis=True, dt=[tau, prc], plot_title=plot_title, axeslabel_fontsize=axeslabel_fontsize, title_fontsize=title_fontsize, axesvalues_fontsize=axesvalues_fontsize, annotation_fontsize=annotation_fontsize, legend_fontsize=legend_fontsize) ##################### # now create list for return.... LOW_TIDE = list() for v in local_minimums: t = time_array[v] w = wl_array[v] DateTime = datetime.strftime(num2date(t), datetime_fmt) LOW_TIDE.append([DateTime, w]) HIGH_TIDE = list() for v in local_maximums: t = time_array[v] w = wl_array[v] DateTime = datetime.strftime(num2date(t), datetime_fmt) HIGH_TIDE.append([DateTime, w]) return LOW_TIDE, HIGH_TIDE
def seaborn_join(): data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000) data = pd.DataFrame(data, columns=['x', 'y']) with sns.axes_style('white'): sns.jointplot("x", "y", data, kind='hex') plt.show()
def get_heatmap(data_mat, name_for_saving_files, pp,stimulus_on_time, stimulus_off_time,delta_ff, f0_start, f0_end): #Plot heatmap for validation A1 = np.reshape(data_mat, (np.size(data_mat,0)*np.size(data_mat,1), np.size(data_mat,2))) if delta_ff == 1: delta_ff_A1 = np.zeros(np.shape(A1)) for ii in xrange(0,np.size(A1,0)): delta_ff_A1[ii,:] = (A1[ii,:]-np.mean(A1[ii,f0_start:f0_end]))/(np.std(A1[ii,f0_start:f0_end])+0.1) B = np.argsort(np.mean(delta_ff_A1, axis=1)) print np.max(delta_ff_A1) else: B = np.argsort(np.mean(A1, axis=1)) print np.max(A1) with sns.axes_style("white"): C = A1[B,:][-2000:,:] fig2 = plt.imshow(C,aspect='auto', cmap='jet', vmin = np.min(C), vmax = np.max(C)) plot_vertical_lines_onset(stimulus_on_time) plot_vertical_lines_offset(stimulus_off_time) plt.title(name_for_saving_files) plt.colorbar() fig2 = plt.gcf() pp.savefig(fig2) plt.close()
def plot_kmeans_components(self, fig1, gs, kmeans_clusters, clrs, plot_title='Hb', num_subplots=1, flag_separate=1, gridspecs=[0, 0]): with sns.axes_style('darkgrid'): if flag_separate: ax1 = fig1.add_subplot(2, 1, num_subplots) else: ax1 = eval('fig1.add_subplot(gs' + gridspecs + ')') plt.gca().set_color_cycle(clrs) for ii in xrange(0, size(kmeans_clusters, 1)): plt.plot(kmeans_clusters[:, ii], lw=4, label=str(ii)) plt.locator_params(axis='y', nbins=4) sns.axlabel("Time (seconds)", "a.u") ax1.legend(prop={'size': 14}, loc='center left', bbox_to_anchor=(1, 0.5), ncol=1, fancybox=True, shadow=True) plt.title(plot_title, fontsize=14) plt.ylim((min(kmeans_clusters) - 0.0001, max(kmeans_clusters) + 0.0001)) plt.xlim((0, size(kmeans_clusters, 0))) plt.axhline(y=0, linestyle='-', color='k', linewidth=1) self.plot_vertical_lines_onset() self.plot_vertical_lines_offset()
def plot_seaborn( self ): # https://stanford.edu/~mwaskom/software/seaborn/tutorial/distributions.html data = pd.read_csv( 'movement.csv' ).as_matrix() # 1/2 3/4 5/6 7/8 x_column = 3 y_column = 4 limit = 100 data = data[ ( data[:,0] == 0) & ( data[:,x_column] > -limit ) & ( data[:,x_column] < limit ) & ( data[:,y_column] > -limit ) & ( data[:,y_column] < limit ) ] x = data[:,x_column] y = data[:,y_column] with sns.axes_style( 'white' ): sns.jointplot( x=x, y=y, kind='kde' ) # scatter, reg, resid, hex, kde sns.plt.show()
def visualize_cluster_MRFs(cluster_MRFs, directory, args): print('> Save sensor network result figures ... ') bc_path = os.path.join( # betweenness centrality csv directory, 'bc_matrix(thres=' + str(args.threshold) + ').csv') hm_path = os.path.join( # betweenness centrality heatmap directory, 'bc_heatmap(thres=' + str(args.threshold) + ').png') ncol = len(COLUMNS_FOR_MODELING_SHORT) gp_labels = COLUMNS_FOR_MODELING_SHORT BC_array = np.zeros((args.nc, ncol)) for k, MRF in cluster_MRFs.items(): # average over diagonal blocks MRF_intra_time_list = [] for i in range(args.ws): MRF_intra_time_list.append(MRF[ncol * i:ncol * (i + 1), ncol * i:ncol * (i + 1)]) MRF_intra_time = np.mean(MRF_intra_time_list, axis=0) # save betweenness centrality scores and graphs gp_path = os.path.join( # networkx graph directory, 'graph_k={}(thres={}).png'.format(k, args.threshold)) bc_list = _calculate_bc_and_draw_graph( MRF_intra_time, labels=gp_labels, threshold=args.threshold, weighted=True, title='Graph between Sensors (k={})'.format(k), save_to=gp_path) BC_array[k, :] = bc_list # SAVE betweenness centrality values BC_df = pd.DataFrame(BC_array, columns=gp_labels) BC_df.to_csv(bc_path) # SAVE betweenness centrality heatmap with sns.axes_style("white"): plt.subplots(figsize=(6, 6)) sns.heatmap(BC_df, square=True, annot=BC_df) plt.savefig(hm_path) plt.close('all') if args.ws > 1: # for each cluster, # we will draw cross time graph with timedelta d for k, MRF in cluster_MRFs.items(): for d in range(1, args.ws): # d: timedelta # average over diagonal blocks according to the timedelta MRF_cross_time_list = [] for i in range(args.ws - d): MRF_cross_time_list.append( MRF[ncol * i:ncol * (i + 1), ncol * (i + d):ncol * (i + d + 1)]) MRF_cross_time = np.mean(MRF_cross_time_list, axis=0) gp_crosstime_path = os.path.join( # networkx graph directory, 'graph_corss_timedelta={}_k={}(thres={}).png'.format( d, k, args.threshold)) gp_crosstime_labels = \ ['{}\n({})'.format(c, t) for t, c in product(['t', 't+{}'.format(d)], COLUMNS_FOR_MODELING_SHORT)] _draw_cross_time_graph( MRF_cross_time, labels=gp_crosstime_labels, threshold=args.threshold, weighted=True, title='Cross-time Graph (timedelta={}, k={})'.format(d, k), save_to=gp_crosstime_path)
def bcm_obj(s_rt_wt, w_min, w_max, reso, para, obj_select=None, nonlinear_select=None, ori_w=0): """ Parameter: s_rt_wt: input data, num of samples * dimension w_min: mininum range of objective function landscape w_max: maximun range of objective function landscape reso: resolution of weights grid para: parameters for training local learnin rule ori_w: for laplace data, plot the original weights obj_select: str, type of objective function if specified, sweep across all combination if none Nolinear_select: str, type of nonlinearity if specified, sweep across all combination if none """ w = np.linspace(w_min, w_max, reso) wx, wy = np.meshgrid(w, w) w = np.vstack((wx.ravel(), wy.ravel())) obj_choice = ['QBCM', 'kurtosis'] nonlinear_choice = ['Relu', 'Sigmoid', 'None'] # parameter passed through para p = para[0] ny = para[1] tau = para[2] batch = para[3] n_epoch = para[4] decay = para[5] eta = para[6] # Plot a gallery of images # if nolinearty and objective defined, train for a specific case, otherwise, train all the combinations if obj_select == None: n_row = len(obj_choice) else: n_row = 1 obj_index = obj_choice.index(obj_select) if nonlinear_select == None: n_col = len(nonlinear_choice) else: n_col = 1 nonlinear_index = nonlinear_choice.index(nonlinear_select) fig, ax = plt.subplots(n_row, n_col, figsize=(12, 6), sharex=True, sharey=True) ori_w = ori_w * (w_max ** 0.5) for i in range(n_row): for j in range(n_col): if (n_row + n_col) > 2: obj_type = obj_choice[i] nonlinear = nonlinear_choice[j] para_index = i * n_col + j else: obj_type = obj_select nonlinear = nonlinear_select para_index = obj_index * len(nonlinear_choice) + nonlinear_index obj_landscape = obj(s_rt_wt, w, obj_type=obj_type, nonlinear=nonlinear) title_set = [obj_type, nonlinear] nbins = 20 levels = np.percentile(obj_landscape, np.linspace(0, 100, nbins)) with sns.axes_style('white'): if (n_row + n_col) > 2: g = ax[i, j] else: g = ax c = g.contour(wx, wy, obj_landscape.reshape(wx.shape), levels=levels, zorder=-10, cmap=plt.cm.get_cmap('viridis')) g.plot(s_rt_wt[:, 0], s_rt_wt[:, 1], '.k', ms=4) g.set_aspect(1) plt.grid('on', rasterized= True) plt.colorbar(c, ax=g) plt_title = ['Uniform', str((i+1)*(j+1)),'.pdf'] plt.savefig(''.join(plt_title)) # Training with BCM local learning rule bcm_data = bcm(eta=eta[para_index], n_epoch=n_epoch[para_index], batch=batch, ny=ny, tau=tau, thres=0, p=p, random_state=None, nonlinear=nonlinear, obj_type=obj_type, decay=decay[para_index]) bcm_data.fit(s_rt_wt) bcm_data_w = np.vstack(bcm_data.w_track) if len(ori_w) > 1: g.plot([0, ori_w[0][0]], [0, ori_w[0][1]], rasterized= True, color = '0.3') g.plot([0, ori_w[1][0]], [0, ori_w[1][1]], rasterized= True, color = '0.3') g.plot(bcm_data_w[:, 0], bcm_data_w[:, 1], 'm', rasterized= True) g.plot(bcm_data_w[-1, 0], bcm_data_w[-1, 1], 'y*', ms=15, rasterized= True) g.plot(bcm_data_w[:, 2], bcm_data_w[:, 3], 'b', rasterized= True) g.set_title(title_set) g.plot(bcm_data_w[-1, 2], bcm_data_w[-1, 3], 'y*', ms=15, rasterized= True)
from kalman3D import estimate_pose import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import sys from pathlib import Path import seaborn as sns sns.set() sns.axes_style("white") sns.set_style("whitegrid", { "xtick.major.size": 0, "ytick.major.size": 0, 'grid.linestyle': '--' }) sns.set_context("paper", font_scale=1, rc={"grid.linewidth": .5}) sns.set_palette("colorblind") directory = '~/Research-Files/Videos/Discharges and Modes/A1+A2/' dev_run = True def batch_kalman(directory, dev_run): print('What is dev_run? It is: {}'.format(dev_run)) if dev_run == True: dpi = 150 print('dpi is 150') else: dpi = 300
def plot_phrase_stats(self, DF2=None): ind_var_name = "session" for data_label in [ 'error_unc', 'error_unc', 'error_cor', 'clicks_char', 'chars_min' ]: var_name_dict = { 'sel_min': "Selections/Min", 'words_min': "Words/Min", 'chars_min': "Characters/Min", 'clicks_char': "Clicks/Character", 'clicks_word': "Clicks/Word", 'clicks_sel': "Clicks/Selection", 'error_unc': "Uncorrected Error Rate (%)", 'error_cor': "Corrections/Selections (%)" } dep_var_name = var_name_dict[data_label] DF = self.DF pd.set_option('display.max_columns', 500) fig, ax = plt.subplots() fig.set_size_inches(10, 8) sns.set(font_scale=1.4, rc={"lines.linewidth": 3}) sns.set_style({'font.serif': 'Helvetica'}) if DF2 is not None: sns.lineplot(x=ind_var_name, y=data_label, color="rosybrown", data=DF2, ci="sd", ax=ax) sns.lineplot(x=ind_var_name, y=data_label, color="brown", data=DF2, ax=ax) sns.lineplot(x=ind_var_name, y=data_label, color="cadetblue", data=DF, ci="sd", ax=ax) sns.lineplot(x=ind_var_name, y=data_label, color="darkslategrey", data=DF, ax=ax) ax.set(xlabel=ind_var_name, ylabel=dep_var_name) plt.legend(title='Key:', labels=[ 'Row Col (SD)', ' (95% CI)', 'Nomon (SD)', ' (95% CI)' ]) plt.title("Webcam Double Switch: " + dep_var_name + " vs. " + ind_var_name) sns.axes_style("darkgrid") plt.show()
def plot_preprocess_data(Working_Directory, name_for_saving_files, data, stimulus_on_time, stimulus_off_time, time_baseline): #### Plot subset of data to view ######## # To save as pdf create file start_time = time.time() print 'Plotting centered data...in ' + Working_Directory text_file = open(Working_Directory + "log.txt", "a") text_file.write("Plotting centered data in %s \n" % Working_Directory) #Save some data wide statistics to text file print 'Data Statistics :' print 'Series Mean :' + str(data.seriesMean().first()) text_file = open(Working_Directory + "log.txt", "a") text_file.write("Series Mean : %s \n" % str(data.seriesMean().first())) print 'Series Std :' + str(data.seriesStdev().first()) text_file = open(Working_Directory + "log.txt", "a") text_file.write("Series Std : %s \n" % str(data.seriesStdev().first())) from numpy import random signal = random.randn(data.index.shape[0]) print 'Series Corrrelation :' + str(data.correlate(signal).first()) text_file = open(Working_Directory + "log.txt", "a") text_file.write("Series Corrrelation : %s \n" % str(data.correlate(signal).first())) ## Plot some data related figures Figure_PDFDirectory = Working_Directory + filesep + 'Figures' + filesep if not os.path.exists(Figure_PDFDirectory): os.makedirs(Figure_PDFDirectory) pp = PdfPages(Figure_PDFDirectory + name_for_saving_files + '_PreprocessedData.pdf') with sns.axes_style("darkgrid"): fig2 = plt.figure() examples = data.center().subset(nsamples=100, thresh=1) if np.size(examples) != 0: plt.plot(examples.T[:, :]) plot_vertical_lines_onset(stimulus_on_time) plot_vertical_lines_offset(stimulus_off_time) plt.tight_layout() fig2 = plt.gcf() pp.savefig(fig2) plt.close() with sns.axes_style("darkgrid"): fig3 = plt.figure() examples = data.zscore(time_baseline).subset(nsamples=100, thresh=2) if np.size(examples) != 0: plt.plot(examples.T[:, :]) plot_vertical_lines_onset(stimulus_on_time) plot_vertical_lines_offset(stimulus_off_time) plt.tight_layout() fig2 = plt.gcf() pp.savefig(fig3) plt.close() fig4 = plt.figure() plt.plot(data.center().max()) plt.plot(data.center().mean()) plt.plot(data.center().min()) plot_vertical_lines_onset(stimulus_on_time) plot_vertical_lines_offset(stimulus_off_time) plt.tight_layout() fig2 = plt.gcf() pp.savefig(fig4) plt.close() pp.close() print 'Plotting centered data took ' + str( int(time.time() - start_time)) + ' seconds' text_file.write("Plotting centered data took %s seconds \n" % str(int(time.time() - start_time)))
print mean_squared_error(Y_predicted, Y_test) coeffs = zip(X_test.columns.values, clf.coef_) import matplotlib import seaborn predvsact = pd.DataFrame(zip(Y_predicted, Y_test)) predvsact.columns = ['predicted', 'actual'] clean_data['predicted_log(ROI)'] = clf.predict(clean_data[features]) clean_data['log(ROI)'] = [np.log(value) for value in clean_data['ROI-total']] clean_data['resid'] = clean_data['log(ROI)'] - clean_data['predicted_log(ROI)'] import matplotlib.pyplot as plt with seaborn.axes_style('white'): plot = clean_data.plot(kind='scatter', x='predicted_log(ROI)', y='log(ROI)', alpha=0.5, figsize=(10, 6)) seaborn.despine() plot.set_title("How well does our model predict ROI?") plot.set_xlim([-1, 5]) plot.set_ylim([-1, 5]) plot.set_xlabel('predicted_log(ROI)') plot.set_ylabel('log(ROI)') plt.plot(clean_data['log(ROI)'], clean_data['log(ROI)'], 'r') with seaborn.axes_style('white'): plot = clean_data.plot(kind='scatter',
print(electronics_data.info()) #Five point summary print(electronics_data.describe()['Rating'].T) #Find the minimum and maximum ratings print('Minimum rating is: %d' % (electronics_data.Rating.min())) print('Maximum rating is: %d' % (electronics_data.Rating.max())) #Check for missing values print('Number of missing values across columns: \n', electronics_data.isnull().sum()) # Check the distribution of the rating with sns.axes_style('white'): g = sns.factorplot("Rating", data=electronics_data, aspect=2.0, kind='count') g.set_ylabels("Total number of ratings") print("Total data ") print("-" * 50) print("\nTotal no of ratings :", electronics_data.shape[0]) print("Total No of Users :", len(np.unique(electronics_data.userId))) print("Total No of products :", len(np.unique(electronics_data.productId))) #Dropping the Timestamp column electronics_data.drop(['timestamp'], axis=1, inplace=True)
# if i>5: # plt.xlabel('t (Gyr)') # if np.mod(i,3)==0: # plt.ylabel('SFR (M$\odot$/yr)') # plt.legend(loc=4, fontsize=10, frameon=True, facecolor='w') # plt.xlim(1e-4,1e3) #plt.tight_layout() #cbar_ax = fig.add_axes([0.2, 0.25, 0.6, 0.03]) #colorbar = fig.colorbar(s, orientation='horizontal',cax=cbar_ax) #colorbar.set_label('Phase') #plt.show() #============================================================================== # Plotting quantities in Aldo table #============================================================================== with sns.axes_style("ticks"): fig, axes = plt.subplots(figsize=(9, 8), nrows=2, ncols=2) for (m, c) in zip(M_today.astype('str'), ['m', 'b', 'g', 'orange', 'firebrick']): for i, (ax, col, ylab) in enumerate( zip(axes.ravel(), ['lgMs', 'lgMh', 'SFR', 'SFR'], [ 'log M$_*$(M$_\odot$)', 'log M$_{halo}$(M$_\odot$)', 'log SFR(M$_\odot$/yr)', 'log sSFR(yr$^{-1}$)' ])): if i == 2: ax.plot(Data[m].t, np.log10(Data[m][col]), c=c, label='log M$_{today}$ = %s' % m, alpha=0.7) elif i == 3:
if __name__ == "__main__": if load_run_data_flag: args, grid_results_dict = load_run_data(result_dir_to_load) else: grid_results_dict = run_simulations(args, local_mode) l2_grid = grid_results_dict['l2_grid'] gam_grid = grid_results_dict['gam_grid'] loss_avg = grid_results_dict['loss_avg'] loss_std = grid_results_dict['loss_std'] ci_factor = 1.96 / np.sqrt(args.n_reps) # 95% confidence interval factor max_deviate = 100. * np.max(loss_std * ci_factor / loss_avg) print('Max 95% CI relative to mean: ', max_deviate, '%') # fig, ax = plt.subplots(figsize=(7, 7)) with sns.axes_style("white"): yticklabels = np.around(l2_grid * 1e5, decimals=3) yticklabels = np.round(yticklabels).astype(int) xticklabels = np.around(gam_grid, decimals=3) ax = sns.heatmap(loss_avg, cmap="YlGnBu", xticklabels=xticklabels, yticklabels=yticklabels, annot=True, annot_kws={"size": 8}) ax.set_yticklabels(ax.get_yticklabels(), rotation=0) plt.xlabel(r'Guidance Discount Factor $\gamma$') plt.ylabel(r'$L_2$ Regularization Factor [1e-5') if save_PDF: save_fig(args.run_name)
from PyQt5 import QtCore from PyQt5.QtCore import pyqtSlot from PyQt5.Qt import Qt # Matplotlib stuff import matplotlib as mpl import numpy as np from matplotlib.figure import Figure import seaborn as sns import h5py import ast import copy sns.set_style('ticks') sns.set_context('paper') sns.axes_style({ 'font.family': ['monospace'], 'font.sans-serif': ['monospace'] }) sns.set(font='sans-serif', style='ticks') #sns.set_palette('husl') sns.set_palette('deep') import yaml # and here http://www.boxcontrol.net/embedding-matplotlib-plot-on-pyqt5-gui.html from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar from matplotlib.backend_bases import key_press_handler fig_dict = '''{ 'type': 'shade',
PMTdiff=np.append(PMTdiff,pmt_diffs) LAPPDt=np.append(LAPPDt,lappd_ts) PMTt=np.append(PMTt,pmt_ts) mut = np.append(mut,muon_time) mutimebinedges = np.arange(-5.0, 20.0, 25.0/NBINS) ltimebinedges = np.arange(-40.0, 30.0, 70.0/NBINS) ptimebinedges = np.arange(-40.0, 30.0, 70.0/NBINS) lbinedges = np.arange(-40.0, 40.0, 80.0/NBINS) pbinedges = np.arange(-40.0, 40.0, 80.0/NBINS) #Make this into a numpy histogram and we do a fit to the histogram itself pmt_timehist, pmt_binedges = np.histogram(PMTdiff,bins=pbinedges) lappd_timehist, lappd_binedges = np.histogram(LAPPDdiff,bins=lbinedges) sns.set_style("whitegrid") sns.axes_style("darkgrid") xkcd_colors = ['light eggplant', 'black', 'slate blue', 'warm pink', 'green', 'grass'] sns.set_palette(sns.xkcd_palette(xkcd_colors)) fig = plt.figure() ax = fig.add_subplot(1,1,1) plt.hist(LAPPDt,bins=ltimebinedges) plt.ylabel("Entries") plt.xlabel("LAPPD hit times (ns)") plt.title("LAPPD hit times after being loaded with DigitBuilder and \n" +\ "saved to PhaseIITreeMaker",fontsize=30) plt.show() xkcd_colors = ['light eggplant', 'black', 'slate blue', 'warm pink', 'green', 'grass'] sns.set_palette(sns.xkcd_palette(xkcd_colors)) fig = plt.figure()
def plot_theil(methods, res_path, save_plot, obj_func): theil, poly, save_plot = path_4_plot(res_path, save_plot, 'valid', methods, obj_func, poly_type=18, proc_sim=False, theil=True) s_scl_msk = _soil_canal(poly) # tl = {m: {} for m in methods} # for m in methods: # for ts, d_u in theil[m].items(): # tl[m][ts] = [] # for p in range(len(poly)): # tl[m][ts].append([i[p] for i in list(d_u.values())]) for name, l_poly in s_scl_msk.items(): N = len(l_poly) name = name[0]+name[name.index(',')+2] for ts in theil[methods[0]]: xticks = [f'Poly{p}' for p in l_poly] plt.ioff() with sns.axes_style("white"): sns.set_style("ticks") sns.set_context("talk") # plot details bar_width = 0.15 epsilon = .005 line_width = 1 opacity = 0.7 def _plt_bar(bar_pos, m, ind): if ind == 0: slabel = f'{m} Us' clabel = f'{m} Uc' else: slabel = None clabel = None poly_ind = [np.where(poly == p)[0][0] for p in l_poly] u_data = {u: np.take(theil[m][ts][u], poly_ind) for u in theil[m][ts]} plt.bar(bar_pos, u_data['Um'], bar_width, color=clr_marker(mtd_clr=True)[m], label=f'{m} Um') plt.bar(bar_pos, u_data['Us'], bar_width - epsilon, bottom=u_data['Um'], alpha=opacity, color='white', edgecolor=clr_marker(mtd_clr=True)[m], linewidth=line_width, hatch='//', label=slabel) plt.bar(bar_pos, u_data['Uc'], bar_width - epsilon, bottom=u_data['Um'] + u_data['Us'], alpha=opacity, color='white', edgecolor=clr_marker(mtd_clr=True)[m], linewidth=line_width, hatch='0', label=clabel) bar_position = {m: np.arange(N)+i*bar_width for i, m in enumerate(methods)} for m in methods: _plt_bar(bar_position[m], m, ind=methods.index(m)) pos_tick = [] for i in range(N): pos_tick.append(np.average([v[i] for m, v in bar_position.items()])) plt.xticks(pos_tick, xticks, rotation=45, fontsize=6) plt.legend(bbox_to_anchor=(0.98, 1.1), fontsize=5) plt.ylabel('Errors') sns.despine() plt.savefig(save_plot+f'{ts}_{name}', dpi=500) plt.clf()
def trim_load_param(fname, load_params): ''' Opens the directed csv file and returns the arrays we want Returns: X_t, U_t, dX_t, objv_t, Ts_t, time, terminal ''' # Grab params delta_state = load_params['delta_state'] include_tplus1 = load_params['include_tplus1'] takeoff_points = load_params['takeoff_points'] trim_0_dX = load_params['trim_0_dX'] find_move = load_params['find_move'] trime_large_dX = load_params['trime_large_dX'] bound_inputs = load_params['bound_inputs'] input_stack = load_params['stack_states'] collision_flag = load_params['collision_flag'] shuffle_here = load_params['shuffle_here'] timestep_flags = load_params['timestep_flags'] battery = load_params['battery'] fastLog = load_params['fastLog'] contFreq = load_params['contFreq'] bat_trim = load_params['trim_high_vbat'] zero_yaw = load_params['zero_yaw'] with open(fname, "rb") as csvfile: # laod data new_data = np.loadtxt(csvfile, delimiter=",") # zero yaw to starting condition if zero_yaw: new_data[:, 5] = new_data[:, 5] - new_data[0, 5] # raise NotImplementedError("Need to implement Yaw zeroing with wrap around of angles") ########### THESE BARS SEPARATE TRIMMING ACTIONS ######################### # For now, remove the last 4 columns becausee they're PWMS if np.shape(new_data)[1] == 20: new_data = new_data[:, :16] if bat_trim > 0: vbat = new_data[:, -1] new_data = new_data[vbat < bat_trim, :] # add pwm latency calculations pwm_rec = new_data[:, 9:13] pwm_com = new_data[:, 16:] # for each pwm in pwm_com # find the earliest next index in the pwm_rec # for each command record the delta index in a new array # this new array should be of length Uchange? # Finds the points where the input changes if fastLog: Uchange = np.where(new_data[:-1, 9:13] != new_data[1:, 9:13]) Uchange = np.unique(Uchange) # print(np.shape(Uchange)) # print(Uchange) # If control freq is faster, sample twice in the interval for each unique PWM point if contFreq > 1: if contFreq == 2: # training for twice control rate dT = Uchange[1:] - Uchange[:-1] add = Uchange[1:] - np.round(dT / 2) Uchange = np.concatenate([Uchange, add]) Uchange = np.sort(Uchange).astype(int) new_data = new_data[Uchange, :] if contFreq == 3: # training for three times control rate (150Hz when sampled at 50) dT = Uchange[1:] - Uchange[:-1] add = Uchange[1:] - np.round(dT / 3) add2 = Uchange[1:] - np.round(2 * dT / 3) Uchange = np.concatenate([Uchange, add, add2]) Uchange = np.sort(Uchange).astype(int) new_data = new_data[Uchange, :] # Else sample each unique point once else: new_data = new_data[Uchange, :] ########################################################################### # adding to make the input horizontally stacked set of inputs, rather than only the last input because of spinup time if input_stack > 1: n, du = np.shape(new_data[:, 9:13]) _, dx = np.shape(new_data[:, :9]) U = np.zeros((n - input_stack + 1, du * input_stack)) X = np.zeros((n - input_stack + 1, dx * input_stack)) for i in range(input_stack, n + 1, 1): U[i - input_stack, :] = np.flip(new_data[i - input_stack:i, 9:13], axis=0).reshape(1, -1) X[i - input_stack, :] = np.flip(new_data[i - input_stack:i, :9], axis=0).reshape(1, -1) if delta_state: # Starts after the data that has requesit U values dX = X[1:, :dx] - X[:-1, :dx] X = X[:-1, :] U = U[:-1, :] if battery: batt = np.array(new_data[input_stack - 1:-1, -1, None]) X = np.hstack((X, batt)) Time = new_data[input_stack - 1:, 13] Ts = ( Time[1:] - Time[:-1] ) / 1000000 # converts deltaT to ms for easy check if data was dropped Objv = new_data[input_stack - 1:-1, 14] Time = Time[:-1] else: # next state predictions dX = X[1:, :dx] # -X[:-1,:] X = X[:-1, :] U = U[:-1, :] if battery: batt = np.array(new_data[input_stack - 1:-1, -1, None]) X = np.hstack((X, batt)) Time = new_data[input_stack - 1:, 13] Ts = ( Time[1:] - Time[:-1] ) / 1000000 # converts deltaT to ms for easy check if data was dropped Objv = new_data[input_stack - 1:-1, 14] Time = Time[:-1] ########################################################################### else: if delta_state: X = new_data[1:-2, :9] U = new_data[1:-2, 9:13] if battery: batt = new_data[1:-2, -1, None] X = np.hstack((X, batt)) Time = new_data[1:-2, 13] Objv = new_data[1:-2, 14] # Reduces by length one for training dX = X[1:, :] - X[:-1, :] X = X[:-1, :] U = U[:-1, :] Ts = ( Time[1:] - Time[:-1] ) / 1000000 # converts deltaT to ms for easy check if data was dropped Objv = Objv[:-1] Time = Time[:-1] else: X = new_data[1:-2, :9] U = new_data[1:-2, 9:13] if battery: batt = new_data[1:-2, -1, None] X = np.hstack((X, batt)) Time = new_data[1:-2, 13] Objv = new_data[1:-2, 14] # Reduces by length one for training dX = X[1:, :] # -X[:-1,:] X = X[:-1, :] U = U[:-1, :] Ts = ( Time[1:] - Time[:-1] ) / 1000000 # converts deltaT to ms for easy check if data was dropped Objv = Objv[:-1] Time = Time[:-1] ########################################################################### # trim some points from takeoff is so desired if takeoff_points > 0 and not fastLog: takeoff_num = takeoff_points X = X[takeoff_num:, :] U = U[takeoff_num:, :] dX = dX[takeoff_num:, :] Objv = Objv[takeoff_num:] Ts = Ts[takeoff_num:] Time = Time[takeoff_num:] ########################################################################### if (bound_inputs != []): low_bound = bound_inputs[0] up_bound = bound_inputs[1] # Remove data where U = 0 X = X[np.array(np.all(U != 0, axis=1)), :] dX = dX[np.array(np.all(U != 0, axis=1)), :] Objv = Objv[np.array(np.all(U != 0, axis=1))] Ts = Ts[np.array(np.all(U != 0, axis=1))] Time = Time[np.array(np.all(U != 0, axis=1))] U = U[np.array(np.all(U != 0, axis=1)), :] # # Remove other values Uflag = ~((U[:, 0] > up_bound) | (U[:, 1] > up_bound) | (U[:, 2] > up_bound) | (U[:, 3] > up_bound) | (U[:, 0] < low_bound) | (U[:, 1] < low_bound) | (U[:, 2] < low_bound) | (U[:, 3] < low_bound)) # print(Uflag) X = X[Uflag, :] U = U[Uflag, :] dX = dX[Uflag, :] Objv = Objv[Uflag] Ts = Ts[Uflag] Time = Time[Uflag] ########################################################################### # timestep flag of 0 removes points where a 0 timestep is recorded. # looks for data where all timesteps are 0. Can change true to false if # that is so. Then removes all points higher than the second point if timestep_flags != []: for trim in timestep_flags: if np.mean(Ts) < 1: print( '~NOTE: heavy trimming may occur, timestamps may be corrupted' ) if trim == 0 and True: # Remove data where Ts = 0 X = X[np.array(np.where(Ts > 1)).flatten(), :] U = U[np.array(np.where(Ts > 1)).flatten(), :] dX = dX[np.array(np.where(Ts > 1)).flatten(), :] Objv = Objv[np.array(np.where(Ts > 1)).flatten()] Ts = Ts[np.array(np.where(Ts > 1)).flatten()] Time = Time[np.array(np.where(Ts > 1)).flatten()] else: # Remove data where the timestep is wrong # Remove data if timestep above 10ms X = X[np.array(np.where(Ts < trim)).flatten(), :] U = U[np.array(np.where(Ts < trim)).flatten(), :] dX = dX[np.array(np.where(Ts < trim)).flatten(), :] Objv = Objv[np.array(np.where(Ts < trim)).flatten()] Ts = Ts[np.array(np.where(Ts < trim)).flatten()] Time = Time[np.array(np.where(Ts < trim)).flatten()] ########################################################################### # for if the data may include collisions. Check to match this with the # emergency off command when you were collecting data if collision_flag and delta_state: # Remove all data for a set of flags # YPR step in (-7.5,7.5) deg # omega step in (-100,100) deg/s^2 # accel step in (-10,10) m.s^2 # STATE FLAGS # Create flag for collisions! collision_flag = (((X[:, 6] < -8)) | ((X[:, 7] < -8)) | ((X[:, 8] < -8)) | (abs(dX[:, 0]) > 75) | (abs(dX[:, 1]) > 75) | (abs(dX[:, 2]) > 75)) if len(np.where(collision_flag == True)[0]) > 0: idx_coll1 = min(np.where(collision_flag == True)[0]) else: idx_coll1 = len(Ts) X = X[:idx_coll1, :] dX = dX[:idx_coll1, :] Objv = Objv[:idx_coll1] Ts = Ts[:idx_coll1] Time = Time[:idx_coll1] U = U[:idx_coll1, :] ########################################################################### # trims large change is state as we think they are non-physical and a # result of the sensor fusion. Note, this could make prediction less stable if trime_large_dX and delta_state: # glag = ( # ((dX[:,0] > -40) & (dX[:,0] < 40)) & # ((dX[:,1] > -40) & (dX[:,1] < 40)) & # ((dX[:,2] > -40) & (dX[:,2] < 40)) & # ((dX[:,3] > -10) & (dX[:,3] < 10)) & # ((dX[:,4] > -10) & (dX[:,4] < 10)) & # ((dX[:,5] > -10) & (dX[:,5] < 10)) & # ((dX[:,6] > -8) & (dX[:,6] < 8)) & # ((dX[:,7] > -8) & (dX[:,7] < 8)) & # ((dX[:,8] > -8) & (dX[:,8] < 8)) # ) glag = (((dX[:, 3] > -7.5) & (dX[:, 3] < 7.5)) & ((dX[:, 4] > -7.5) & (dX[:, 4] < 7.5)) & ((dX[:, 5] > -7.5) & (dX[:, 5] < 7.5)) & ((dX[:, 6] > -8) & (dX[:, 6] < 8)) & ((dX[:, 7] > -8) & (dX[:, 7] < 8)) & ((dX[:, 8] > -8) & (dX[:, 8] < 8))) # X = X[glag, :] dX = dX[glag, :] Objv = Objv[glag] Ts = Ts[glag] Time = Time[glag] U = U[glag, :] ########################################################################### # removes tuples with 0 change in an angle (floats should surely always change) if trim_0_dX and delta_state: Objv = Objv[np.all(dX[:, 3:6] != 0, axis=1)] Ts = Ts[np.all(dX[:, 3:6] != 0, axis=1)] Time = Time[np.all(dX[:, 3:6] != 0, axis=1)] X = X[np.all(dX[:, 3:6] != 0, axis=1)] U = U[np.all(dX[:, 3:6] != 0, axis=1)] dX = dX[np.all(dX[:, 3:6] != 0, axis=1)] ########################################################################### # We do this again when training. if shuffle_here: # SHUFFLES DATA shuff = np.random.permutation(len(Time)) X = X[shuff, :] dX = dX[shuff, :] Objv = Objv[shuff] Ts = Ts[shuff] Time = Time[shuff] U = U[shuff, :] if find_move: # move_idx = np.argmax(np.all(dX[:,3:5] > 0.005, axis=1)) move_idx = np.argmax(Objv != -1) move_idx = int(2 * move_idx / 3) ########################################################################### # Can be used to plot trimmed data if False: font = {'size': 18} matplotlib.rc('font', **font) matplotlib.rc('lines', linewidth=2.5) # plt.tight_layout() with sns.axes_style("darkgrid"): ax1 = plt.subplot(311) ax2 = plt.subplot(312) ax3 = plt.subplot(313) ax1.plot(X[:, 3:5]) ax2.plot(U[:, :4]) ax3.plot(X[:, 6:9]) plt.show() # Make time counting up from first point if len(Time) > 0: Time -= min(Time[move_idx:]) Time /= 1000000 # end of traj marker terminals = np.zeros(len(Time)) if len(terminals) > 0: terminals[-1] = 1 return np.array(X), np.array(U), np.array(dX), np.array( Objv), np.array(Ts), np.array(Time), terminals
def draw_meta_or_not_curve_figure(dump_file_path): data_dict = defaultdict(dict) for mode in ["meta", "vanilla"]: json_file_path = "/home1/machen/meta_perturbations_black_box_attack/logs/AblationStudy_meta_or_not@CIFAR-10-cw_loss-l2-untargeted-mse/meta_mode_{}_WRN-28-10-drop_result.json".format( mode) with open(json_file_path, "r") as file_obj: data_txt = file_obj.read().replace( '"not_done_loss": NaN, "not_done_prob": NaN,', "") json_data = json.loads(data_txt.strip()) data_dict[mode]["is_finetune"] = [ is_finetune for iter, is_finetune in sorted( json_data["logits_error_finetune_iteration"].items(), key=lambda e: int(e[0])) ] data_dict[mode]["MSE_error"] = [ MSE_error for iter, MSE_error in sorted( json_data["logits_error_iteration"].items(), key=lambda e: int(e[0])) ] # plt.style.use('seaborn-whitegrid') with sns.axes_style("whitegrid", rc={"legend.framealpha": 0.5}): plt.figure(figsize=(10, 8)) colors = { "deep_benign_images": 'm', "vanilla": "b", "meta": 'r', "uninitial": 'y' } xtick_max = 125 for mode, data_info in data_dict.items(): x = np.arange(xtick_max) + 1 y = np.array(data_info["MSE_error"][:xtick_max]) is_finetune_list = data_info["is_finetune"][:xtick_max] if mode == "meta": simulator_name = "Simulator Attack" elif mode == "vanilla": simulator_name = "Vanilla Simulator" elif mode == "deep_benign_images": simulator_name = "Simulator$_{benign}$" else: simulator_name = "Rnd_init Simulator" line, = plt.plot( x, y, label=r"$\ell_2$ norm attack of {}".format(simulator_name), color=colors[mode], linestyle="-") first_finetune = True for x_, is_finetune in enumerate(is_finetune_list): if is_finetune == 1: if first_finetune: plt.axvline(x=x_ + 1, color='#778899', linestyle='--', linewidth=1, label="fine-tune iterations") first_finetune = False else: plt.axvline(x=x_ + 1, color='#778899', linestyle='--', linewidth=1) plt.xlim(min(x.tolist()), max(x.tolist())) plt.ylim(0, 20) plt.gcf().subplots_adjust(bottom=0.15) # xtick = [0, 5000, 10000] plt.xticks([1, 10] + np.arange(25, xtick_max + 1, 25).tolist(), fontsize=22) plt.yticks([0, 5, 10, 15, 20], fontsize=22) plt.xlabel("attack iterations", fontsize=25) plt.ylabel("MSE of the outputs", fontsize=25) plt.legend(loc='upper right', prop={'size': 22}, fancybox=True, framealpha=0.5) # legend = plt.legend(loc='upper right', prop={'size': 15}, shadow=True, facecolor="white") # legend.get_frame().set_facecolor('#E6E6FA') plt.savefig(dump_file_path, dpi=200)
def jointplot(): x, y = np.random.multivariate_normal([1, 2], [[1, 2], [1, 2]], 1000).T with sns.axes_style("white"): return sns.jointplot(x=x, y=y, kind="hex", color="k")
symlist = ['o', 'p', '*', 's', '+', 'x', 'D', 'v', '-', '^'] rc('font', **{'family': 'sans-serif', 'sans-serif': ['Computer Modern Roman']}) params = { 'axes.labelsize': 12, 'font.size': 16, 'legend.fontsize': 16, 'text.usetex': True, 'figure.figsize': (8, 6) } plt.rcParams.update(params) sns.set_context("poster") sns.set_palette("colorblind") sns.set_style("white") sns.axes_style() ############################################################################### # Data Generation (you can skip the understanding) ############################################################################### def rand_gauss(n=100, mu=[1, 1], sigmas=[0.1, 0.1]): """ Sample n points from a Gaussian variable with center mu, and std deviation sigma """ d = len(mu) res = np.random.randn(n, d) return np.array(res * sigmas + mu)
def plot_categorized_rmse( benchmarks: List[Benchmark], benchmark_results: List[BenchmarkResult], output_directory: str, file_type: Literal["png", "pdf"] = "png", ): """Plots the RMSE in each benchmarked property partitioned by the assigned statistics category. Parameters ---------- benchmarks The benchmarks which have been performed. benchmark_results The analyzed outputs of the benchmarks. output_directory The directory in which to save the plots. file_type The file type to use for the plots. """ import pandas import seaborn from matplotlib import pyplot # Reshape the statistics into a uniform data frame. data_rows = [{ "Data Type": f"{statistic.property_type}-{statistic.n_components}", "Name": benchmark.name, "Value": statistic.value, "Lower CI": numpy.abs(statistic.lower_95_ci - statistic.value), "Upper CI": numpy.abs(statistic.upper_95_ci - statistic.value), "Category": statistic.category, } for benchmark, result in zip(benchmarks, benchmark_results) for statistic in result.data_set_result.statistic_entries if statistic.statistic_type == StatisticType.RMSE and statistic.category is not None] plot_data = pandas.DataFrame(data_rows) if len(plot_data) == 0: return # Extract the unique data types (e.g. property types) which will be plotted # in separate figures. data_types = plot_data["Data Type"].unique() os.makedirs(os.path.join(output_directory, "categorized-rmse"), exist_ok=True) for data_type in data_types: # Extract a data frame containing only the data type which should # be included in this figure. type_plot_data = plot_data[plot_data["Data Type"] == data_type] categories = sorted(type_plot_data["Category"].unique(), key=sort_categories_key, reverse=True) category_indices = [x * 2 for x in range(1, len(categories) + 1)] n_categories = len(categories) with seaborn.axes_style("white"): figure, axis = pyplot.subplots(figsize=(5.0, 1.5 + (0.5 * n_categories))) shifts = numpy.linspace(-0.5, 0.5, len(benchmarks)) # Plot the data for each benchmark. for index, benchmark in enumerate(benchmarks): benchmark_plot_data = type_plot_data[type_plot_data["Name"] == benchmark.name] # Plot the error bars and with the circle marker on top. axis.barh( benchmark_plot_data["Category"].replace( categories, category_indices).values - shifts[index], benchmark_plot_data["Value"], xerr=[ benchmark_plot_data["Lower CI"], benchmark_plot_data["Upper CI"], ], height=1.0 / max(len(benchmarks) - 1, 1), label=benchmark.name, ) # Add a simple legend. axis.legend() # Add title and axis names axis.set_yticks(category_indices) axis.set_yticklabels(categories) axis.set_xlim(left=0.0) axis.set_ylim(0.5, (len(categories) + 1) * 2.0 - 0.5) axis.set_xlabel("RMSE") # Save the figure. figure.savefig( os.path.join( output_directory, "categorized-rmse", f"{camel_to_kebab_case(data_type)}.{file_type}", ), bbox_inches="tight", ) pyplot.close(figure)
# plt.xlim(Xmin, Xmax) # plt.ylim(Ymin, Ymax) # plt.xlabel(Xlabel) # 横轴名 # plt.ylabel(Ylabel) # 纵轴名 # plt.title(Title) # 按照固定区间长度绘制频率分布直方图 # bins_interval 区间的长度 # margin 设定的左边和右边空留的大小 # def probability_distribution(data, bins_interval, margin): # bins = np.arange(min(data), max(data) , bins_interval) # plt.xlim(min(data) - margin, max(data) + margin) # plt.title("Probability-distribution") # plt.xlabel('Interval') # plt.ylabel('Probability') # #频率分布normed=True,频次分布normed=False # plt.hist(x=data, bins=bins, histtype='bar') # seaborn.set(style='white') with seaborn.axes_style("dark"): seaborn.jointplot(x=width_ratios, y=height_ratios, kind="scatter", color='k', s=10).set_axis_labels("relative width", "relative height") # plt.figure(2) # temp=(area_ratios.tolist()) # probability_distribution(temp,0.001,0) plt.show()
* DIS and NOX * INDUS vs NOX * LSTAT vs RM * LSTAT vs PRICE * RM vs PRICE Try adding some opacity or `alpha` to the scatter plots using keyword arguments under `joint_kws`. #### Distance from Employment vs. Pollution **Challenge**: Compare DIS (Distance from employment) with NOX (Nitric Oxide Pollution) using Seaborn's `.jointplot()`. Does pollution go up or down as the distance increases? """ with sns.axes_style('darkgrid'): sns.jointplot(x=data['DIS'], y=data['NOX'], height=8, kind='scatter', color='deeppink', joint_kws={'alpha': 0.5}) plt.show() """We see that pollution goes down as we go further and further out of town. This makes intuitive sense. However, even at the same distance of 2 miles to employment centres, we can get very different levels of pollution. By the same token, DIS of 9 miles and 12 miles have very similar levels of pollution. #### Proportion of Non-Retail Industry 🏭🏭🏭 versus Pollution **Challenge**: Compare INDUS (the proportion of non-retail industry i.e., factories) with NOX (Nitric Oxide Pollution) using Seaborn's `.jointplot()`. Does pollution go up or down as there is a higher proportion of industry?
def plt_param_ranges(labelnames, dims, runs_sort, num_plt, synth_data=None, save_fig=''): # fig, (ax1) = plt.subplots(1, 1, figsize=(3,3)) fig, (ax1) = plt.subplots(1, 1, figsize=(2.5, 1.5)) # pal = sns.set_palette(param_colors.get(m_name)) # Hide the right and top spiness ax1.spines['right'].set_visible(False) ax1.spines['top'].set_visible(False) # Only show ticks on the left and bottom spines ax1.yaxis.set_ticks_position('left') ax1.xaxis.set_ticks_position('bottom') major_ticks = np.arange(-2, 2.1, 1) ax1.set_yticks(major_ticks) #data handling df_param = pd.DataFrame(runs_sort[1][:num_plt]) df_param.columns = labelnames df_param_log = df_param.apply(np.log10) df_melt = df_param_log.melt(var_name='param', value_name='vals') df_error = pd.DataFrame(runs_sort[0][:num_plt], columns=['error']) mses_df = df_error['error'].copy() mses_df = pd.concat([mses_df] * 5, ignore_index=True) df_plot = pd.concat([df_melt, mses_df], axis=1) pal = sns.light_palette("purple", reverse=True, as_cmap=True) # cmap = sns.light_palette("seagreen", reverse=False, as_cmap=True ) # Normalize to the range of possible values from df["c"] norm = matplotlib.colors.Normalize(vmin=df_error['error'].min(), vmax=df_error['error'].max()) # create a color dictionary (value in c : color from colormap) colors = {} for cval in df_plot["error"]: colors.update({cval: pal(norm(cval))}) with sns.axes_style("whitegrid"): plt.bar(range(0, len(labelnames)), height=dims[0], bottom=dims[1], align='center', tick_label=labelnames, color='#dcdcdc', alpha=0.8) ax1 = sns.swarmplot(x='param', y='vals', data=df_plot, hue='error', palette=colors, size=5) #size 3 ax1.set_xticklabels(labelnames, rotation=90) # plt.xlabel('Parameters', fontsize=20, fontweight='medium') plt.grid(color='#606060', which='major', axis='y', linestyle='solid') if synth_data.any: # synth_data['param']=synth_data.index # synth_data.melt(var_name='param', value_name='vals') print(synth_data) ax1 = sns.swarmplot(data=synth_data, color='black', size=8, alpha=1, marker='*') ax1.set_ylabel('') ax1.set_xlabel('') plt.gca().legend_.remove() divider = make_axes_locatable(plt.gca()) ax_cb = divider.new_horizontal(size="5%", pad=0.05) fig.add_axes(ax_cb) cb1 = matplotlib.colorbar.ColorbarBase(ax_cb, cmap=pal, norm=norm) if save_fig: plt.savefig(fig_folder + save_fig, dpi=300, bbox_inches='tight') plt.show()
df = df[df["Index"] <= min(max(list(indexCutoff)), indexOveride)] df = df[df["condition"] != "GINR"] df = df[df["condition"] != "GIPJ"] df2 = df[df["variable"] == "NAD"].copy() df2 = df2.rename(columns={"value": "NAD"}) df2 = df2.replace({ 'condition': { "contAIC": "AIC", "GIAIC": "GI+AIC", "GINRAIC": "GI+NR+AIC", "GIPJAIC": "GI+PJ34+AIC" } }) with sns.axes_style(style="ticks"): plt.figure() bp = sns.barplot(x="condition", y="NAD", hue="Index", data=df2) bp.get_figure().savefig(os.path.join(fig_dir, 'fig4NAD.png')) df2 = df[df["variable"] == "PGC1a_deacet"].copy() df2 = df2.rename(columns={"value": "PGC1a_deacet"}) df2 = df2.replace({ 'condition': { "contAIC": "AIC", "GIAIC": "GI+AIC", "GINRAIC": "GI+NR+AIC", "GIPJAIC": "GI+PJ34+AIC" } })
def correlation_reduction_worker(corrMatrix, target, threshold, reductionMetrics=''): ''' This function works to drop vars over threshold and heat map a pre and post correlation matrix of any sorts. The threshold can also be specify through the scenario file by "custom_correlationReduction_thresh" Parameters: corrMatrix - correlation matrix target - dataframe containing variables' correlation with your target variable threshold - your threshold for variable reduction reductionMetrics - one of four correlation reduction metrics: Pearson, Spearmans, EuclideanDistances or LogisticRegression Returns: droppedInfo - dataFrame of variables that were dicarded corrmat - post correlation matrix of any sorts Writes: droppedInfo - dataFrame of variables that were dicarded corrmat - post correlation matrix of any sorts ''' drop = [] seaborn.set(context="paper", font="monospace") corrmat = abs(corrMatrix.copy(deep=True)) # Set up the matplotlib figure f, ax = plt.subplots(figsize=(10, 8)) ax.set_yticks([]) # Draw the heatmap using seaborn mask = np.zeros_like(corrmat, dtype=np.bool) mask[np.triu_indices_from(mask)] = True with seaborn.axes_style("white"): seaborn.heatmap(corrmat, vmax=1, mask=mask, square=True, xticklabels=20, yticklabels=20, cmap='Blues') ax.set_title( "{0} - Original Correlation Matrix".format(reductionMetrics)) if not os.path.isfile('../outputs/variable_reduction/{}_Original.png'. format(reductionMetrics)): f.savefig('../outputs/variable_reduction/{}_Original.png'.format( reductionMetrics)) # Correlation Threshold mid = target.drop(target.index[0], 1).T mid['target_abs'] = abs(mid[target.index[0]]) mid.sort_values('target_abs', inplace=True, ascending=False) order = list(mid.index) corrMatrix = corrMatrix.reindex(order, axis=1) corrMatrix = corrMatrix.reindex(order) column_list = corrMatrix.keys() ### for col in column_list: ### # This is a temporary drop list that'll be emptied in each iteration, we use it to store the dropped vars and apply to corrMatrix drop drop_tmp = [] if col in corrMatrix.keys( ): # corrMatrix does inplace drop of columns and rows in each iteration, need to check whether col is still in corrMatrix for i in range(len(corrMatrix)): # Iterating through all the vars if col != corrMatrix.keys( )[i]: # Make sure col is not compared with itself if abs(corrMatrix[col] [i]) > threshold: # Correlation Threshold Checking # This is a cumulative drop list that'll be used as the output to show the summary of drop vars # Append [dropped_var, col, corr(dropped_var, target), corr(col, target)] drop.append([ corrMatrix.keys()[i], col, target[corrMatrix.keys()[i]].values[0], target[col].values[0] ]) drop_tmp.append( corrMatrix.keys()[i]) # Append dropped_var corrMatrix.drop(drop_tmp, axis=1, inplace=True) # Drop columns corrMatrix.drop(drop_tmp, axis=0, inplace=True) # Drop rows corrmat = abs(corrMatrix.copy(deep=True)) droppedInfo = pd.DataFrame(drop, columns=[ 'discarded_variable', 'correlated_to', 'discarded_variable_correlation_to_target', 'correlated_to_correlation_to_target' ]) # Set up the matplotlib figure f, ax = plt.subplots(figsize=(10, 8)) ax.set_yticks([]) # Draw the heatmap using seaborn mask = np.zeros_like(corrmat, dtype=np.bool) mask[np.triu_indices_from(mask)] = True with seaborn.axes_style("white"): seaborn.heatmap(corrmat, vmax=1, mask=mask, square=True, xticklabels=20, yticklabels=20, cmap='Blues') ax.set_title("{} - Post Reduction Correlation Matrix - {}".format( reductionMetrics, threshold)) f.savefig( '../outputs/variable_reduction/{}_Post_Reduction_Correlation_Matrix_{}.png' .format(reductionMetrics, str(threshold).replace('.', ''))) return corrmat, droppedInfo
"tdse_projection_plots_{}".format("n_level" if nlevel else "full")) if not iap: save_folder += "_20fs" if not os.path.isdir(save_folder): os.mkdir(save_folder) for iint, ii_name in zip((ints_15, ints_13, ints_13_15), ("15", "13", "13_15")): iint = np.array(iint) np.savez(os.path.join( save_folder, "detector_brightness_dependence_{}_harm_on_ir_wavelength.npz".format( ii_name)), ints=iint, wavelengths=np.arange(790, 806)) with sns.axes_style("whitegrid"), context: if printing: f, ax = plt.subplots(1, 1, figsize=(32, 16)) else: f, ax = plt.subplots(1, 1, figsize=(16, 8)) ax.plot(np.arange(790, 806), iint, linewidth=2) ax.axhline(1, color="tab:red", linestyle="--") ax.set_xlabel(r"Intensity [$W/cm^2$]") ax.set_title("Detector Brightness dependence on the IR wavelength") ax.set_ylim(0, 1.5) f.tight_layout() if printing: f.savefig(os.path.join( save_folder, "detector_brightness_dependence_{}_harm_on_ir_wavelength.pdf".
import cantera as ct from matplotlib import animation import sys import statistics import itertools max_cpus = multiprocessing.cpu_count() # set up the LSR grid, for the smaller, more interesting one carbon_range = (-7.5, -5.5) oxygen_range = (-5.25, -3.25) grid_size = 9 mesh = np.mgrid[carbon_range[0]:carbon_range[1]:grid_size*1j, oxygen_range[0]:oxygen_range[1]:grid_size*1j] with sns.axes_style("whitegrid"): plt.axis('square') plt.xlim(carbon_range) plt.ylim(oxygen_range) plt.yticks(np.arange(-5.25,-3,0.5)) plt.show() # just to double-check experiments = mesh.reshape((2,-1)).T with sns.axes_style("whitegrid"): plt.axis('square') plt.xlim(carbon_range) plt.ylim(oxygen_range) plt.yticks(np.arange(-5.25,-3,0.5)) plt.plot(*experiments.T, marker='o', linestyle='none')
def prettyPlotAgainst(states, rewards, title="Representation", fit_pca=False, cmap='coolwarm'): """ State dimensions are plotted one against the other (it creates a matrix of 2d representation) using rewards for coloring, the diagonal is a distribution plot, and the scatter plots have a density outline. :param states: (np.ndarray) :param rewards: (np.ndarray) :param title: (str) :param fit_pca: (bool) :param cmap: (str) """ with sns.axes_style('white'): n = states.shape[1] fig, ax_mat = plt.subplots(n, n, figsize=(10, 10), sharex=False, sharey=False) fig.subplots_adjust(hspace=0.2, wspace=0.2) if fit_pca: title += " (PCA)" states = PCA(n_components=n).fit_transform(states) c_idx = cm.get_cmap(cmap) norm = colors.Normalize(vmin=np.min(rewards), vmax=np.max(rewards)) for i in range(n): for j in range(n): x, y = states[:, i], states[:, j] ax = ax_mat[i, j] if i != j: ax.scatter(x, y, c=rewards, cmap=cmap, s=5) sns.kdeplot(x, y, cmap="Greys", ax=ax, shade=True, shade_lowest=False, alpha=0.2) ax.set_xlim([np.min(x), np.max(x)]) ax.set_ylim([np.min(y), np.max(y)]) else: if len(np.unique(rewards)) < 10: for r in np.unique(rewards): sns.distplot(x[rewards == r], color=c_idx(norm(r)), ax=ax) else: sns.distplot(x, ax=ax) if i == 0: ax.set_title("Dim {}".format(j), y=1.2) if i != j: # Hide ticks if i != 0 and i != n - 1: ax.xaxis.set_visible(False) if j != 0 and j != n - 1: ax.yaxis.set_visible(False) # Set up ticks only on one side for the "edge" subplots... if j == 0: ax.yaxis.set_ticks_position('left') if j == n - 1: ax.yaxis.set_ticks_position('right') if i == 0: ax.xaxis.set_ticks_position('top') if i == n - 1: ax.xaxis.set_ticks_position('bottom') plt.suptitle(title, fontsize=16) plt.show()
def dynamic_heatmap(df, columns, fontsize=20, annot=False, palette=None, figsize=(15, 10), squaresize=500): """Plots a heatmap that changes size values depending on correlation Adapted from: https://towardsdatascience.com/better-heatmaps-and-correlation-matrix-plots-in-python-41445d0f2bec""" plt.figure(figsize=figsize) corr = df[columns].corr() sns.set(style="dark") grid_bg_color = sns.axes_style()['axes.facecolor'] # Generate a mask for the upper triangle mask = np.zeros_like(corr, dtype=np.bool) mask[np.triu_indices_from(mask)] = True corr = pd.melt(corr.reset_index(), id_vars='index') # Unpivot the dataframe, so we can get pair of arrays for x and y corr.columns = ['x', 'y', 'value'] x = corr['x'] y = corr['y'] size = corr['value'].abs() # Set up the matplotlib figure f, ax = plt.subplots(figsize=figsize) ax.set_xticklabels( ax.get_xticklabels(), rotation=45, horizontalalignment='right'); # Mapping from column names to integer coordinates x_labels = [v for v in sorted(x.unique())] y_labels = [v for v in sorted(y.unique())] x_to_num = {p[1]: p[0] for p in enumerate(x_labels)} y_to_num = {p[1]: p[0] for p in enumerate(y_labels)} size_scale = squaresize if palette: n_colors = len(palette) else: n_colors = 256 # Use 256 colors for the diverging color palette palette = sns.diverging_palette(20, 220, n=n_colors) # Create the palette color_min, color_max = [-1, 1] # Range of values that will be mapped to the palette, i.e. min and max possible correlation color = corr["value"] def value_to_color(val): val_position = float((val - color_min)) / ( color_max - color_min) # position of value in the input range, relative to the length of the input range ind = int(val_position * (n_colors - 1)) # target index in the color palette return palette[ind] plot_grid = plt.GridSpec(1, 15, hspace=0.2, wspace=0.1) # Setup a 1x15 grid ax = plt.subplot(plot_grid[:, :-1]) # Use the leftmost 14 columns of the grid for the main plot ax.scatter( x=x.map(x_to_num), # Use mapping for x y=y.map(y_to_num), # Use mapping for y s=size * size_scale, # Vector of square sizes, proportional to size parameter c=color.apply(value_to_color), # Vector of square colors, mapped to color palette marker='s' # Use square as scatterplot marker ) # Show column labels on the axes ax.set_xticks([x_to_num[v] for v in x_labels]) ax.set_xticklabels(x_labels, rotation=45, horizontalalignment='right') ax.set_yticks([y_to_num[v] for v in y_labels]) ax.set_yticklabels(y_labels) # ax.set_fontsize(font_scale) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(fontsize) numbers = corr['value'].round(decimals=2) if annot: for i, txt in enumerate(numbers): annot_font_size = int(fontsize * size[i] * annot) ax.annotate(txt, (x.map(x_to_num)[i], y.map(x_to_num)[i]), horizontalalignment="center", verticalalignment="center", color=grid_bg_color, fontweight="black", fontsize=annot_font_size) ax.grid(False, 'major') ax.grid(True, 'minor') ax.set_xticks([t + 0.5 for t in ax.get_xticks()], minor=True) ax.set_yticks([t + 0.5 for t in ax.get_yticks()], minor=True) ax.set_xlim([-0.5, max([v for v in x_to_num.values()]) + 0.5]) ax.set_ylim([-0.5, max([v for v in y_to_num.values()]) + 0.5]) # Add color legend on the right side of the plot ax = plt.subplot(plot_grid[:, -1]) # Use the rightmost column of the plot col_x = [0] * len(palette) # Fixed x coordinate for the bars bar_y = np.linspace(color_min, color_max, n_colors) # y coordinates for each of the n_colors bars bar_height = bar_y[1] - bar_y[0] ax.barh( y=bar_y, width=[5] * len(palette), # Make bars 5 units wide left=col_x, # Make bars start at 0 height=bar_height, color=palette, linewidth=0 ) ax.set_xlim(1, 2) # Bars are going from 0 to 5, so lets crop the plot somewhere in the middle ax.grid(False) # Hide grid ax.set_xticks([]) # Remove horizontal ticks ax.set_yticks(np.linspace(min(bar_y), max(bar_y), 3)) # Show vertical ticks for min, middle and max ax.yaxis.tick_right() # Show vertical ticks on the right plt.show()
async def country(self, ctx, *countries): """Plots distribution of server members by countries. When no countries are specified, plots a bar graph of all members by country. When one or more countries are specified, plots a swarmplot of members by country and rating. Only members with registered handles and countries set on Codeforces are considered. """ max_countries = 8 if len(countries) > max_countries: raise GraphCogError( f'At most {max_countries} countries may be specified.') users = cf_common.user_db.get_cf_users_for_guild(ctx.guild.id) counter = collections.Counter(user.country for _, user in users if user.country) if not countries: # list because seaborn complains for tuple. countries, counts = map(list, zip(*counter.most_common())) plt.clf() fig = plt.figure(figsize=(15, 5)) with sns.axes_style(rc={'xtick.bottom': True}): sns.barplot(x=countries, y=counts) # Show counts on top of bars. ax = plt.gca() for p in ax.patches: x = p.get_x() + p.get_width() / 2 y = p.get_y() + p.get_height() + 0.5 ax.text(x, y, int(p.get_height()), horizontalalignment='center', color='#30304f', fontsize='x-small') plt.xticks(rotation=40, horizontalalignment='right') ax.tick_params(axis='x', length=4, color=ax.spines['bottom'].get_edgecolor()) plt.xlabel('Country') plt.ylabel('Number of members') discord_file = gc.get_current_figure_as_file() plt.close(fig) embed = discord_common.cf_color_embed( title='Distribution of server members by country') else: countries = [country.title() for country in countries] data = [ [user.country, user.rating] for _, user in users if user.rating and user.country and user.country in countries ] if not data: raise GraphCogError( 'No rated members from the specified countries are present.' ) color_map = { rating: f'#{cf.rating2rank(rating).color_embed:06x}' for _, rating in data } df = pd.DataFrame(data, columns=['Country', 'Rating']) column_order = sorted( (country for country in countries if counter[country]), key=counter.get, reverse=True) plt.clf() if len(column_order) <= 5: sns.swarmplot(x='Country', y='Rating', hue='Rating', data=df, order=column_order, palette=color_map) else: # Add ticks and rotate tick labels to avoid overlap. with sns.axes_style(rc={'xtick.bottom': True}): sns.swarmplot(x='Country', y='Rating', hue='Rating', data=df, order=column_order, palette=color_map) plt.xticks(rotation=30, horizontalalignment='right') ax = plt.gca() ax.tick_params(axis='x', color=ax.spines['bottom'].get_edgecolor()) plt.legend().remove() plt.xlabel('Country') plt.ylabel('Rating') discord_file = gc.get_current_figure_as_file() embed = discord_common.cf_color_embed( title='Rating distribution of server members by ' 'country') discord_common.attach_image(embed, discord_file) discord_common.set_author_footer(embed, ctx.author) await ctx.send(embed=embed, file=discord_file)
roary.shape[1], histtype="stepfilled", alpha=.7) plt.xlabel('No. of genomes') plt.ylabel('No. of genes') sns.despine(left=True, bottom=True) plt.savefig('pangenome_frequency.%s' % options.format, dpi=300) plt.clf() # Sort the matrix according to tip labels in the tree roary_sorted = roary_sorted[[x.name for x in t.get_terminals()]] # Plot presence/absence matrix against the tree with sns.axes_style('whitegrid'): fig = plt.figure(figsize=(17, 10)) ax1 = plt.subplot2grid((1, 40), (0, 10), colspan=30) a = ax1.matshow( roary_sorted.T, cmap=plt.cm.Blues, vmin=0, vmax=1, aspect='auto', interpolation='none', ) ax1.set_yticks([]) ax1.set_xticks([]) ax1.axis('off')
def create_plots(df, out_data, out_plot='synth_sample_complexity.pdf', colors=None, hue_order=None, show_fig=False): """Create figures for paper.""" # Make sample complexity Fig 1 fig, (ax, ax2) = plt.subplots(1, 2, sharey=True, figsize=(9, 5), gridspec_kw={'width_ratios': [5, 1]}) sns.set_style("white") sns.set_context("paper", font_scale=1) # Ax1 g = sns.lineplot( data=df[np.logical_and(df.train_dataset != 'Baseline, N=200', df.val_dataset == 'Same')], x='ds', y='average_precision', hue='model', # legend='brief', ax=ax, palette=colors, hue_order=hue_order, legend=False, markers=True, dashes=False) # , palette=sns.color_palette("mako_r", 4)) (g.set(ylim=(0.25, 1))) ax.legend(labels=hue_order, frameon=False) ax.set_xticks([1, 5, 10, 20]) ax.set_xticklabels(['1 (5.0)', '5 (5.7)', '10 (6.0)', '20 (6.3)']) ax.set_yticks([0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.]) ax.set_xlabel('Number of training images\n($log_{10}$ training samples)') ax.set_ylabel('Mean average precision') sns.despine() # Ax2 new_df = df[df.train_dataset == 'Baseline, N=200'] ext_df = df[df.train_dataset == 'Baseline, N=200'] ext_df.train_dataset = 'Baseline, N=190' ext_df.ds = 190 new_df.train_dataset = 'Baseline, N=210' new_df.ds = 210 new_df = pd.concat((ext_df, new_df)) sns.axes_style({'axes.spines.left': False}) g2 = sns.lineplot( data=new_df, x='ds', y='average_precision', hue='model', legend=False, ax=ax2, palette=colors, hue_order=hue_order, markers=True, dashes=False) # , palette=sns.color_palette("mako_r", 4)) # (g2.set(ylim=(0.8, 1))) ax2.set_xticks([200]) ax2.set_xticklabels(['200 (7.3)']) ax2.set_xlim([180, 220]) ax2.set_xlabel('') g2.spines['left'].set_visible(False) # ax2.set_xlabel('Number of training examples') sns.despine() plt.tight_layout() plt.savefig(os.path.join(out_data, out_plot)) if show_fig: plt.show() plt.close(fig)
def setup_figure(self): with sns.axes_style("white"): super(Visualizer, self).setup_figure()