def binning(data, quantity, show_plot=False, save_plot=False): """ Calculate autocorrelation time, mean and error for a quantity using the binning method. The bins become uncorrelated when the error approaches a constant. These uncorrelated bins can be used for jackknife resampling. """ original_length = len(data) errors = [] errors.append((original_length, calculate_error(data))) while len(data) > 128: data = np.asarray([(a + b) / 2 for a, b in zip(data[::2], data[1::2])]) errors.append((len(data), calculate_error(data))) autocorrelation_time = 0.5 * ((errors[-1][1] / errors[0][1])**2 - 1) if np.isnan(autocorrelation_time) or autocorrelation_time <= 0: autocorrelation_time = 1 if show_plot: # plt.title(r'${0}$'.format('\mathrm{' + quantity.replace(' ', '\ ') + '\ Error}')) plt.xlabel(r'$\mathrm{Number\ of\ Data\ Points}$') plt.ylabel(r'$\mathrm{Error}$') plt.xlim(original_length, 1) plt.ylim(ymin=0, ymax=max(errors, key=lambda x: x[1])[1] * 1.15) plt.semilogx([e[0] for e in errors], [e[1] for e in errors], basex=2) sns.despine() if save_plot: plt.savefig("{0}/{1}_{2}_binning_error.pdf".format(SAVE_LOCATION, time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())), quantity.replace(" ", "_"), bbox_inches='tight')) plt.show() return np.mean(data), errors[-1][1], autocorrelation_time, data
def scatter(x, y, equal=False, xlabel=None, ylabel=None, xinvert=False, yinvert=False): """ Plot a scatter with simple formatting options """ plt.scatter(x, y, 200, color=[0.3, 0.3, 0.3], edgecolors="white", linewidth=1, zorder=2) sns.despine() if xlabel: plt.xlabel(xlabel) if ylabel: plt.ylabel(ylabel) if equal: plt.axes().set_aspect("equal") plt.plot([0, max([x.max(), y.max()])], [0, max([x.max(), y.max()])], color=[0.6, 0.6, 0.6], zorder=1) bmin = min([x.min(), y.min()]) bmax = max([x.max(), y.max()]) rng = abs(bmax - bmin) plt.xlim([bmin - rng * 0.05, bmax + rng * 0.05]) plt.ylim([bmin - rng * 0.05, bmax + rng * 0.05]) else: xrng = abs(x.max() - x.min()) yrng = abs(y.max() - y.min()) plt.xlim([x.min() - xrng * 0.05, x.max() + xrng * 0.05]) plt.ylim([y.min() - yrng * 0.05, y.max() + yrng * 0.05]) if xinvert: plt.gca().invert_xaxis() if yinvert: plt.gca().invert_yaxis()
def plot_by_groups(df, plot_dir, af_key, config): """Plot allele frequencies of grouped/paired samples. """ out_file = os.path.join(plot_dir, "cohort-group-af-comparison.pdf") df["sample_label"] = df.apply(lambda row: "%s\n%s" % (row["group_class"], row["sample"]), axis=1) sns.despine() sns.set(style="white") with PdfPages(out_file) as pdf_out: for (cohort, group), cur_df in df.groupby(["cohort", "group"]): labels = sorted(list(cur_df["sample_label"].unique())) labels.reverse() cur_df["sample_label"].categories = labels g = sns.violinplot(x=af_key, y="sample_label", data=cur_df, inner=None, bw=.1) #sns.swarmplot(x=af_key, y="sample_label", data=cur_df, color="w", alpha=.5) try: group = int(group) except ValueError: pass g.set_title("%s: %s" % (cohort, group)) g = _af_violinplot_shared(g) pdf_out.savefig(g.figure) if config and (cohort, group) in config.group_detailed: out_dir = utils.safe_makedir(os.path.join(plot_dir, "detailed")) out_file = os.path.join(out_dir, "group-%s-%s.png" % (cohort, group)) g.figure.savefig(out_file) plt.clf() return out_file
def data_collapse(data_set, quantity, critical_temperature, critical_exponent1, nu, name1, save=False, heat_capacity_correction=0): """Perform a data collapse with a given set of critical exponents. Used to see how the exact result collapses.""" scaling_functions = {} for lattice_size, data in sorted(data_set.items()): for v in data: t = (v[0] - critical_temperature) / critical_temperature scaling_variable = lattice_size**(1 / nu) * t if critical_exponent1 == 0: v_tilde = np.log(lattice_size) * (v[1] + heat_capacity_correction) else: v_tilde = lattice_size**(critical_exponent1 / nu) * (v[1] + heat_capacity_correction) v_tilde_error = lattice_size**(critical_exponent1 / nu) * v[2] scaling_functions.setdefault(lattice_size, []).append((scaling_variable, v_tilde, v_tilde_error)) for lattice_size, data in sorted(scaling_functions.items()): plt.xlabel(r'$L^{(1 / \nu)}t$') plt.ylabel(r'${0}$'.format('\mathrm{' + quantity.replace(' ', '\ ') + '\ Scaling\ Function}')) sns.despine() plt.errorbar([k[0] for k in data], [k[1] for k in data], [k[2] for k in data], linestyle='None', marker='o', label=r"${0}$".format(str(lattice_size) + '\mathrm{\ by\ }' + str(lattice_size) + "\mathrm{\ Lattice}")) print("{0} = {1}, nu = {2}".format(name1, critical_exponent1, nu)) plt.legend(loc='best') sns.despine() if save: plt.savefig("{0}/{1}_{2}_data_collapse.pdf".format(SAVE_LOCATION, time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())), quantity.replace(" ", "_"), bbox_inches='tight')) plt.show()
def tuning(x, y, err=None, smooth=None, ylabel=None, pal=None): """ Plot a tuning curve """ if smooth is not None: xs, ys = smoothfit(x, y, smooth) plt.plot(xs, ys, linewidth=4, color="black", zorder=1) else: ys = asarray([0]) if pal is None: pal = sns.color_palette("husl", n_colors=len(x) + 6) pal = pal[2 : 2 + len(x)][::-1] plt.scatter(x, y, s=300, linewidth=0, color=pal, zorder=2) if err is not None: plt.errorbar(x, y, yerr=err, linestyle="None", ecolor="black", zorder=1) plt.xlabel("Wall distance (mm)") plt.ylabel(ylabel) plt.xlim([-2.5, 32.5]) errTmp = err errTmp[isnan(err)] = 0 rng = max([nanmax(ys), nanmax(y + errTmp)]) plt.ylim([0 - rng * 0.1, rng + rng * 0.1]) plt.yticks(linspace(0, rng, 3)) plt.xticks(range(0, 40, 10)) sns.despine() return rng
def dist_small_multiples(df, figsize=(20, 20)): """ Small multiples plots of the distribution of a dataframe's variables. """ import math sns.set_style("white") num_plots = len(df.columns) n = int(math.ceil(math.sqrt(num_plots))) fig = plt.figure(figsize=figsize) axes = [plt.subplot(n, n, i) for i in range(1, num_plots + 1)] i = 0 for k, v in df.iteritems(): ax = axes[i] sns.kdeplot(v, shade=True, ax=ax, legend=False) sns.rugplot(v, ax=ax, c=sns.color_palette("husl", 3)[0]) [label.set_visible(False) for label in ax.get_yticklabels()] ax.xaxis.set_ticks([v.min(), v.max()]) ax.set_title(k) i += 1 sns.despine(left=True, trim=True, fig=fig) plt.tight_layout() return fig, axes
def PlotIOCurve(stRaster, rasterKey, figPath=[]): """ Plot the IO curves for the spikes in stRaster. :param stRaster: dict of pandas.DataFrame of spike times for each cycle, for each intensity :type stRaster: dict of pandas.DataFrame :param rasterKey: Raster key with intensity in dB following '_' :type rasterKey: str :param figPath: Directory location for plots to be saved :type figPath: str :returns: tuningCurves: pandas.DataFrame with frequency, intensity, response rate and standard deviation """ tuning = [] sortedKeys = sorted(stRaster.keys()) for traceKey in sortedKeys: spl = int(traceKey.split('_')[-1]) raster = stRaster[traceKey] res = ResponseStats( raster ) tuning.append({'intensity': spl, 'response': res[0], 'responseSTD': res[1]}) tuningCurves = pd.DataFrame(tuning) testNum = int(rasterKey.split('_')[-1]) tuningCurves.plot(x='intensity', y='response', yerr='responseSTD', capthick=1, label='test '+str(testNum)) plt.legend(loc='upper left', fontsize=12, frameon=True) sns.despine() plt.grid(False) plt.xlabel('Intensity (dB)', size=14) plt.ylabel('Response Rate (Hz)', size=14) plt.tick_params(axis='both', which='major', labelsize=14) title = rasterKey.split('_')[0]+'_'+rasterKey.split('_')[1]+'_'+rasterKey.split('_')[2] plt.title(title, size=14) if len(figPath)>0: plt.savefig(figPath + 'ioCurves_' + title +'.png') return tuningCurves
def plot_dist_matrix(matrix, fasta_names, heatmap_out, dendrogram_out): """Cluster the distance matrix hierarchically and plot using seaborn. Average linkage method is used.""" # Load required modules for plotting import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import seaborn as sns import pandas as pd from scipy.cluster.hierarchy import dendrogram, linkage # Create pdm = pd.DataFrame(matrix, index=fasta_names, columns=fasta_names) # Plot heatmap figsizex = max(10, len(fasta_names) / 4) clustergrid = sns.clustermap(pdm, metric='euclidean', method='average', figsize=(figsizex, figsizex)) clustergrid.savefig(heatmap_out) # Plot dendrogram sns.set_style('white') figsizey = max(10, len(fasta_names) / 8) f, ax = plt.subplots(figsize=(figsizex, figsizey)) link = linkage(pdm, metric='euclidean', method='average') dendrogram(link, labels=pdm.index, ax=ax) no_spine = {'left': True, 'bottom': True, 'right': True, 'top': True} sns.despine(**no_spine) plt.xticks(rotation=90) f.tight_layout() plt.savefig(dendrogram_out)
def plot_data(self): wanted_vars = [ ("initial_length", "Birth length", r"\si{\micro\metre}"), ("final_length", "Division length", r"\si{\micro\metre}"), ("added_length", "Added length", r"\si{\micro\metre}"), # ("elong_rate", "Linear elongation rate", r"\si{\micro\metre\per\hour}"), ("growth_rate", "Exponential growth rate", r"\si{\per\hour}"), ("doubling_time", "Interdivision time", r"\si{\hour}"), ("asymmetry", "Division asymmetry", None), ("slope", r"Slope, $a$", None), ] if not os.path.exists("noise"): os.mkdir("noise") # fig = plt.figure(figsize=(3.5, 2.4 * len(wanted_vars))) # rows, cols, ax_num = len(wanted_vars), 2, 1 for var, label, unit in wanted_vars: # ax = fig.add_subplot(rows, cols, ax_num) fig = plt.figure(figsize=(3.34, 2.4)) if var == "slope": ax = fig.add_subplot(111) ax.plot([0.5, 3.5], [1, 1], "k--") else: ax = fig.add_subplot(121) sns.despine(ax=ax) self.plot_variable(var, ax) if unit: ylabel = "{0} ({1})".format(label, unit) else: ylabel = label ax.set_ylabel(ylabel) # if ax_num == 1: # self.add_legend(ax) # elif ax_num == rows * cols - 1: # ax.set_xticklabels(self.datalabels, rotation=90) # ax_num += 1 self.add_legend(ax) ax.set_xticklabels(self.datalabels, rotation=90) if var != "slope": # ax = fig.add_subplot(rows, cols, ax_num) ax = fig.add_subplot(122) sns.despine(ax=ax) self.plot_variable(var, ax, cv=True) ax.set_ylabel(r"CV {0} (\si{{\percent}})".format(label.lower())) # if ax_num == 2: # self.add_legend(ax) # elif ax_num == rows * cols: # ax.set_xticklabels(self.datalabels, rotation=90) # ax_num += 1 self.add_legend(ax) ax.set_xticklabels(self.datalabels, rotation=90) fig.tight_layout() fig.savefig( "noise/{0}.pdf".format(var), transparent=True )
def DUPLICATE_remove(data, distance_threshold, graph=False): sizeFormat=pixelFormat * pixelSize poly=overlapping_grid(nrow, ncol, overlap_region, [sizeFormat, sizeFormat]) result=np.zeros(data.shape[0], dtype=bool) for i in xrange(data.shape[0]): result[i]=poly.contains(Point(data[i, 2], data[i, 3])) data_to_evaluate=data[result.ravel(), :] output=data[np.invert(result.ravel()), :] # Calculate Nearest Neighbors distance distance, indexes=DISTANCE_spatial( data_to_evaluate, data_to_evaluate, neighbors=2) if graph: sns.plt.grid(False) sns.distplot(distance) plt.title("%d spots to evaluate" % data_to_evaluate.shape[0]) sns.despine() sns.plt.show() # Find which spots is below the distance_threshold mask_to_keep=distance > distance_threshold to_keep=[] to_exclude=[] for source in xrange(len(indexes)): target=indexes[source] if indexes[target] == source and distance[source] < distance_threshold and source not in to_exclude: to_keep.append(source) to_exclude.append(target) mask_to_keep[to_keep]=True print str(len(to_exclude)) + ' spots discarded' data_to_evaluate=data_to_evaluate[mask_to_keep, :] output=np.row_stack((output, data_to_evaluate)) return output
def Cluster_plot(v1, v2, min_bin, min_eps, distances, total_HSC, save_plot): bins=np.linspace(min_bin, max(v1 + v2), int(max(v1 + v2) / 2)) # V1 length clusters # V2 length clusters fig=plt.figure() ax=fig.add_subplot(111) fig.suptitle('Cluster size: %d total HSC' % sum(total_HSC), fontsize=14, fontweight='bold') info_title="Close HSC clusters: %d (%d um) Far HSC cluster: %d (%d um)" % ( len(v1), distances[0], len(v2), distances[1]) ax.set_title(info_title) ax.set_xlabel('Cells per cluster') ax.set_ylabel('Nb of cluster') c1=int(sum(v1) / float(total_HSC[0]) * 100) c2=int(sum(v2) / float(total_HSC[1]) * 100) sns.plt.grid(False) plt.hist(v1, bins, alpha=0.5, label='Close HSC clusters: ' + ' - ' + str(c1) + '%') plt.hist(v2, bins, alpha=0.5, label='Far HSC clusters: ' + ' - ' + str(c2) + '%') plt.legend(loc='upper right') sns.despine() if save_plot: suffix=str(min_bin) + 'pts_' + str(min_eps) + 'perc' savefig("data//" + sample + "//clustering_" + suffix + ".png") else: plt.show()
def fig2(ppl, fname): '''For each contact, plot number of characters sent and received. (UNUSED)''' sns.lmplot("lensent", "lenrec",ppl) plt.xlabel('Characters Sent') plt.ylabel('Characters Received') sns.despine() savefig(fname)
def traceplot(traces, thin, burn): ''' Plot parameter estimates for different levels of the model into the same plots. Black lines are individual observers and red lines are mean estimates. ''' variables = ['Slope1', 'Slope2', 'Offset', 'Split'] for i, var in enumerate(variables): plt.subplot(2, 2, i + 1) vals = get_values(traces, var, thin, burn) dim = (vals.min() - vals.std(), vals.max() + vals.std()) x = plt.linspace(*dim, num=1000) for v in vals.T: a = gaussian_kde(v) y = a.evaluate(x) y = y / y.max() plt.plot(x, y, 'k', alpha=.5) try: vals = get_values(traces, 'Mean_' + var, thin, burn) a = gaussian_kde(vals) y = a.evaluate(x) y = y / y.max() plt.plot(x, y, 'r', alpha=.75) except KeyError: pass plt.ylim([0, 1.1]) plt.yticks([0]) sns.despine(offset=5, trim=True) plt.title(var)
def show_binomial(): """Show an example of binomial distributions""" bd1 = stats.binom(20, 0.5) bd2 = stats.binom(20, 0.7) bd3 = stats.binom(40, 0.5) k = np.arange(40) sns.set_context('paper') sns.set_style('ticks') mystyle.set(14) markersize = 8 plt.plot(k, bd1.pmf(k), 'o-b', ms=markersize) plt.hold(True) plt.plot(k, bd2.pmf(k), 'd-r', ms=markersize) plt.plot(k, bd3.pmf(k), 's-g', ms=markersize) plt.title('Binomial distribuition') plt.legend(['p=0.5 and n=20', 'p=0.7 and n=20', 'p=0.5 and n=40']) plt.xlabel('X') plt.ylabel('P(X)') sns.despine() mystyle.printout_plain('Binomial_distribution_pmf.png') plt.show()
def plot(self, event, logliks, logsumexps, modality_colors, renamed=''): modality = logsumexps.idxmax() sns.violinplot(event.dropna(), bw=0.2, ax=self.ax_violin, color=modality_colors[modality]) self.ax_violin.set_ylim(0, 1) self.ax_violin.set_title('Guess: {}'.format(modality)) self.ax_violin.set_xticks([]) self.ax_violin.set_yticks([0, 0.5, 1]) # self.ax_violin.set_xlabel(renamed) for name, loglik in logliks.iteritems(): # print name, self.ax_loglik.plot(loglik, 'o-', label=name, color=modality_colors[name]) self.ax_loglik.legend(loc='best') self.ax_loglik.set_title('Log likelihoods at different ' 'parameterizations') self.ax_loglik.grid() self.ax_loglik.set_xlabel('phantom', color='white') for i, (name, height) in enumerate(logsumexps.iteritems()): self.ax_bayesfactor.bar(i, height, label=name, color=modality_colors[name]) self.ax_bayesfactor.set_title('$\log$ Bayes factors') self.ax_bayesfactor.set_xticks([]) self.ax_bayesfactor.grid() self.fig.tight_layout() self.fig.text(0.5, .025, '{} ({})'.format(event.name, renamed), fontsize=10, ha='center', va='bottom') sns.despine() return self
def plot_quantity_range(data_range, quantity, exact=None, show_plot=True, save=False): """Plot quantity over a temperature range.""" for lattice_size, data in sorted(data_range.items()): plt.errorbar([d[0] for d in data], [d[1] for d in data], [d[2] for d in data], linestyle='None', label=r"${0}$".format(str(lattice_size) + '\mathrm{\ by\ }' + str(lattice_size) + "\mathrm{\ Lattice}"), marker='o') if exact is not None: plt.plot([e[0] for e in exact], [e[1] for e in exact], label=r'$\mathrm{Thermodynamic\ Limit}$') # We put all data together so it is easy to find the maximum and minimum values. # print(list(data_range.values())) zipped_data = list(itertools.chain(*data_range.values())) x_data_range = sorted(list(set(m[0] for m in zipped_data))) min_x = x_data_range[0] max_x = x_data_range[-1] x_step = x_data_range[1] - x_data_range[0] plt.xlim(min_x - x_step, max_x + x_step) data_min = min(zipped_data, key=lambda x: x[1])[1] data_max = max(zipped_data, key=lambda x: x[1])[1] if data_max <= 0: plt.ylim(ymin=1.15 * data_min, ymax=0.85 * data_max) else: plt.ylim(ymin=0, ymax=data_max * 1.15) plt.xlabel(r'$T$') plt.ylabel(r'${0}$'.format('\mathrm{' + quantity.replace(' ', '\ ') + '}')) plt.legend(loc="best") sns.despine() if save: plt.savefig("{0}/{1}_{2}.pdf".format(SAVE_LOCATION, time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())), quantity.replace(" ", "_"), bbox_inches='tight')) if show_plot: plt.show()
def plot_af_diffs(all_audio_diffs): """ Plots the mean album audio features differences as a function of recommendation rank. Does this for all individual audio features. Args: all_audio_diffs: list of dataframes as returned from get_af_diffs Returns: None """ plt.close('all') fig = plt.figure() colors = sns.color_palette('Set1', 10) for i, feature_name in enumerate(FEATURE_NAMES): temp = [seed[feature_name] for seed in all_audio_diffs] df = pd.concat(temp) df.index.name = 'recc_rank' mean_diff = df.groupby(df.index).mean()[:].values.flatten() sem_diff = df.groupby(df.index).sem()[:].values.flatten() ax = fig.add_subplot(3, 3, i+1) plot_indiv_diff(ax, mean_diff, sem_diff, LABEL_DICT[feature_name], colors[i]) if i != 6: # ax.xaxis.set_major_formatter(NullFormatter()) # ax.yaxis.set_major_formatter(NullFormatter()) pass else: ax.set_xlabel('Recommendation rank') # ax.set_ylabel('# of instances') sns.despine(offset=5, trim=True) plt.tight_layout()
def testFilterLinearCoh(n_iterations=5): cohs = np.array([-0.512, -0.256, -0.128, -0.064, -0.032, 0, 0.032, 0.064, 0.128, 0.256, 0.512]) inputs = [] for coh in cohs: for j in xrange(n_iterations): seed = [np.random.randint(999), np.random.randint(999)] if np.sign(coh) == 1: direction = 0 else: direction = 180 inputs.append( gd.generateDots(seed, 0, 0, 5, 60, direction, np.abs(coh), 5)) pool = multiprocessing.Pool() out = pool.map(filterDots, inputs) pool.close() data = np.array(out) summed_me = np.sum(data, axis=1) mean_me = np.mean(summed_me.reshape(-1, n_iterations), axis=1) fig = plt.figure() ax = fig.add_subplot(111) colors = b2mpl.get_map('Set1', 'Qualitative', 3).mpl_colors ax.add_line(plt.Line2D(cohs, mean_me, color=colors[0])) ax.set_xlabel('Motion strength (proportion coh)') ax.set_ylabel('Total motion energy') ax.set_xlim(-.61, .61) ax.set_ylim(np.min(mean_me) * 1.2, np.max(mean_me) * 1.2) sns.despine(offset=5, trim=True) plt.tight_layout() return cohs, mean_me
def testFilterSpeedBandwidth(n_iterations=5): speeds = np.arange(1, 9.5, 0.5) inputs = [] for speed in speeds: for j in xrange(n_iterations): seed = [np.random.randint(999), np.random.randint(999)] inputs.append( gd.generateDots(seed, 0, 0, 5, 60, 0, 1, speed)) pool = multiprocessing.Pool() out = pool.map(filterDots, inputs) pool.close() data = np.array(out) summed_me = np.sum(data, axis=1) mean_me = np.mean(summed_me.reshape(-1, n_iterations), axis=1) fig = plt.figure() ax = fig.add_subplot(111) colors = b2mpl.get_map('Set1', 'Qualitative', 3).mpl_colors ax.add_line(plt.Line2D(speeds, mean_me, color=colors[0])) ax.set_xlabel('Motion speed (degrees per s))') ax.set_ylabel('Total motion energy') majorLocator = MultipleLocator(1) ax.xaxis.set_major_locator(majorLocator) majorLocator = MultipleLocator(50) ax.yaxis.set_major_locator(majorLocator) ax.set_xlim(1, 9) ax.set_ylim(np.min(mean_me) - 10, np.max(mean_me) + 10) sns.despine(offset=5, trim=True) plt.tight_layout() return speeds, mean_me
def plot_sorted_tc(sorted_tc, savepath, savefig=True): """Plots sorted tuning curves from a single trajectory for a session. Parameters ---------- sorted_tc : list of lists Where each inner list contains the tuning curve (floats) for an individual neuron. savepath : str Location and filename for the saved plot. savefig : boolean Default is True and will save the plot to the specified location. False shows with plot without saving it. """ fig, ax = plt.subplots() heatmap = ax.pcolor(sorted_tc, cmap='YlGn') plt.ylim(0, len(sorted_tc)) plt.xlim(0, len(sorted_tc[0])) plt.ylabel('Neuron number') plt.xlabel('Location (cm)') sns.despine() if savefig: plt.savefig(savepath, dpi=300, bbox_inches='tight') plt.close() else: plt.show()
def plot_bydurations(durations, savepath, savefig=True): """Plots duration for each trial separated by trajectories. Behavior only. Parameters ---------- durations : dict With u, shortcut, novel, num_sessions as keys. Each value is a list of durations (float) for a each session. savepath : str Location and filename for the saved plot. savefig : boolean Default is True and will save the plot to the specified location. False shows with plot without saving it. """ ax = sns.boxplot(data=[durations['u'], durations['shortcut'], durations['novel']]) sns.color_palette("hls", 18) ax.set(xticklabels=['U', 'Shortcut', 'Novel']) plt.ylabel('Duration of trial (s)') plt.xlabel('sessions=' + str(durations['num_sessions'])) plt.ylim(0, 140) sns.despine() if savefig: plt.savefig(savepath, dpi=300, bbox_inches='tight') plt.close() else: plt.show()
def main(): runResults = [] # Traverse files, extract matrix, architecture and params for f in [f for f in os.listdir(".") if os.path.isfile(f)]: if f.startswith("run_Spmv"): runResults.append(RunResult(f)) df = pd.DataFrame([[r.prj, r.matrix, r.gflops_est] for r in runResults]) grouped = df.groupby(0) groups = [] names = [] for name, group in grouped: group.set_index(1, inplace=True) # group.sort_index(inplace=True) groups.append(group[2]) names.append(name) new_df = pd.concat(groups, axis=1) new_df.columns = names sns.set_style("white") sns.set_palette(sns.color_palette("cubehelix", 13)) bar = new_df.plot(kind="bar") sns.despine() fig = bar.get_figure() fig.set_size_inches(15, 15) fig.tight_layout() fig.savefig("est_gflops.pdf")
def PlotBBNResponseCurve(self, bbnResponseProb, measure, unit=[], filePath=[], attn=False): """ Plots measure for multiple frequencies and intensities an a contour plot. :param stResponseProb: DataFrames results of Bayesian response analysis for multiple tone stimulus intensities :type stResponseProb: pandas DataFrame :param measure: Bayesian response analysis measure ['resProb', 'vocalResMag', 'vocalResMag_MLE', 'effectSize', 'effectSize_MLE', 'spontRate', 'spontRateSTD', 'responseLatency', 'responseLatencySTD', 'responseDuration'] :type measure: integer [0-9] :param unit: Unique identifier for cell :type unit: str :param filePath: Path to directory where results will be saved :type filePath: str :returns: Handle to plot """ measureName = ['resProb', 'vocalResMag', 'vocalResMag_MLE', 'effectSize', 'effectSize_MLE', 'spontRate', 'spontRateSTD', 'responseLatency', 'responseLatencySTD', 'responseDuration'] tuningData = bbnResponseProb sns.set_palette(sns.color_palette("bright", 8)) sns.set_context(rc={"figure.figsize": (5, 3)}) sns.set_style("white") sns.set_style("ticks") if attn: ax = bbnResponseProb.loc[::-1,measure].fillna(0).plot(figsize=(6,4)) else: ax = bbnResponseProb.loc[:,measure].fillna(0).plot(figsize=(6,4)) sns.despine() plt.grid(False) plt.title(unit, fontsize=14) plt.xlabel('SPL (dB)', fontsize=12) plt.ylabel(measureName[measure], fontsize=12) plt.ylim(0.5,1.0) # plt.gca().invert_xaxis() if len(filePath)>0: plt.savefig(self.dirPath + filePath + 'bbn_'+measureName[measure]+'_'+unit+'.pdf') plt.close() else: plt.show() return ax
def PlotFrequencyTuningCurves(self, stResponseProb, measure, unit=[], filePath=[]): """ Plots measure for multiple frequencies, with a trace for each tone intensity. :param stResponseProb: DataFrames results of Bayesian response analysis for multiple tone stimulus intensities :type stResponseProb: pandas DataFrame :param measure: Bayesian response analysis measure ['resProb', 'vocalResMag', 'vocalResMag_MLE', 'effectSize', 'effectSize_MLE', 'spontRate', 'spontRateSTD', 'responseLatency', 'responseLatencySTD', 'responseDuration'] :type measure: int [0-9] :param unit: Unique identifier for cell :type unit: str :param filePath: Path to directory where results will be saved :type filePath: str :returns: Handle to plot """ measureName = ['resProb', 'vocalResMag', 'vocalResMag_MLE', 'effectSize', 'effectSize_MLE', 'spontRate', 'spontRateSTD', 'responseLatency', 'responseLatencySTD', 'responseDuration'] tuningData = stResponseProb # sns.set_palette(sns.color_palette("bright", 8)) attn = stResponseProb.keys()[0] firstFreq = stResponseProb[attn].index.tolist()[1] sns.set_style("white") sns.set_style("ticks") ax = stResponseProb.loc[:,firstFreq:,measure].fillna(0).plot(figsize=(6,4)) sns.despine() plt.grid(False) plt.title(unit, fontsize=14) plt.xlabel('Frequency (kHz)', fontsize=12) plt.ylabel(measureName[measure], fontsize=12) plt.tick_params(axis='both', which='major', labelsize=14) if len(filePath)>0: plt.savefig(self.dirPath + filePath + 'freqTuning_'+measureName[measure]+'_'+unit+'.pdf') plt.close() else: plt.show() return ax
def PlotSTResponseEst(self, stResponseDF, label, duration=250, firstFreq=1): """ Plots response rate estimate for multiple frequencies and intensities as a contour plot. :param stResponseDF: DataFrames results of Bayesian response analysis for multiple tone stimulus intensities :type stResponseDF: pandas DataFrame :param label: Figure name :type label: str :param duration: Duration of recording window :type duration: float :param firstFreq: Set to skip first (spurious) entry :type firstFreq: int :returns: Handle to plot """ stResponseE = np.array(stResponseDF) freqs = np.array(stResponseDF.index.tolist())[1:].astype(np.float) sns.set_context(rc={"figure.figsize": (8, 4)}) maxRes = np.max(abs(stResponseE[firstFreq:,:])) spontRate = np.average(stResponseE[firstFreq:,-1]) ax = plt.imshow(stResponseE[firstFreq:,:], vmax=maxRes+spontRate, vmin=-maxRes+spontRate, extent=[0,duration,min(freqs),max(freqs)], aspect='auto', interpolation='nearest', origin='lower', cmap = cm.bwr) sns.despine() plt.grid(False) plt.title(label) plt.xlabel('Time (ms)') plt.ylabel('Frequency (kHz)') plt.colorbar() return ax
def plot_reduced_space(self, binned_reduced, modalities_assignments, ax=None, title=None, xlabel='', ylabel=''): if ax is None: fig, ax = plt.subplots(figsize=(8, 8)) # For easy aliasing X = binned_reduced # import pdb # pdb.set_trace() for modality, df in X.groupby(modalities_assignments, axis=0): color = self.modalities_colors[modality] ax.plot(df.ix[:, 0], df.ix[:, 1], 'o', color=color, alpha=0.25, label=modality) sns.despine() xmax, ymax = X.max() ax.set_xlim(0, 1.05 * xmax) ax.set_ylim(0, 1.05 * ymax) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.legend() if title is not None: ax.set_title(title)
def plot_e_score_dist(df, data_name, filepath): # The Expect value (E) is a parameter that describes the number of hits # one can "expect" to see by chance when searching a database of a # particular size. It decreases exponentially as the Score (S) of the # match increases. Essentially, the E value describes the random # background noise. For example, an E value of 1 assigned to a hit can be # interpreted as meaning that in a database of the current size one might # expect to see 1 match with a similar score simply by chance. # The lower the E-value, or the closer it is to zero, the more # "significant" the match is. However, keep in mind that virtually # identical short alignments have relatively high E values. This is # because the calculation of the E value takes into account the length of # the query sequence. These high E values make sense because shorter # sequences have a higher probability of occurring in the database purely # by chance. For more details please see the calculations in the BLAST # Course: http://www.ncbi.nlm.nih.gov/BLAST/tutorial/Altschul-1.html fig, ax = plt.subplots(1, 1) # figsize=(5, 6)) sns.despine() # Removes the boxes around the plots. plot_data = df.evalue MIN, MAX = min(plot_data), max(plot_data) plot_data.plot.hist(ax=ax, bins=10**np.linspace(np.log10(MIN), np.log10(MAX), 50)) ax.set_xscale('log') ax.set_title('Distributions e values: {}'.format(data_name), y=1.05) ax.set_xlabel('e-value') ax.figure.savefig(filepath + data_name + "_e_value" + '.pdf') return ax
def behavioral_analysis(self): """some analysis of the behavioral data, such as mean percept duration, dominance ratio etc""" self.assert_data_intern() # only do anything if this is not a no report trial if 'RP' in self.file_alias: all_percepts_and_durations = [[],[]] else: all_percepts_and_durations = [[],[],[]] if not 'NR' in self.file_alias: # and not 'RP' in self.file_alias for x in range(len(self.trial_indices)): if len(self.events) != 0: events_this_trial = self.events[(self.events['EL_timestamp'] > self.timestamps_pt[x][0]) & (self.events['EL_timestamp'] < self.timestamps_pt[x][-1])] for sc, scancode in enumerate(self.scancode_list): percept_start_indices = np.arange(len(events_this_trial))[np.array(events_this_trial['scancode'] == scancode)] percept_end_indices = percept_start_indices + 1 # convert to times start_times = np.array(events_this_trial['EL_timestamp'])[percept_start_indices] - self.timestamps_pt[x,0] if len(start_times) > 0: if percept_end_indices[-1] == len(events_this_trial): end_times = np.array(events_this_trial['EL_timestamp'])[percept_end_indices[:-1]] - self.timestamps_pt[x,0] end_times = np.r_[end_times, len(self.from_zero_timepoints)] else: end_times = np.array(events_this_trial['EL_timestamp'])[percept_end_indices] - self.timestamps_pt[x,0] these_raw_event_times = np.array([start_times + self.timestamps_pt[x,0], end_times + self.timestamps_pt[x,0]]).T these_event_times = np.array([start_times, end_times]).T + x * self.trial_duration * self.sample_rate durations = np.diff(these_event_times, axis = -1) all_percepts_and_durations[sc].append(np.hstack((these_raw_event_times, these_event_times, durations))) self.all_percepts_and_durations = [np.vstack(apd) for apd in all_percepts_and_durations] # last element is duration, sum inclusive and exclusive of transitions total_percept_duration = np.concatenate([apd[:,-1] for apd in self.all_percepts_and_durations]).sum() total_percept_duration_excl = np.concatenate([apd[:,-1] for apd in [self.all_percepts_and_durations[0], self.all_percepts_and_durations[-1]]]).sum() self.ratio_transition = 1.0 - (total_percept_duration_excl / total_percept_duration) self.ratio_percept_red = self.all_percepts_and_durations[0][:,-1].sum() / total_percept_duration_excl self.red_durations = np.array([np.mean(self.all_percepts_and_durations[0][:,-1]), np.median(self.all_percepts_and_durations[0][:,-1])]) self.green_durations = np.array([np.mean(self.all_percepts_and_durations[-1][:,-1]), np.median(self.all_percepts_and_durations[-1][:,-1])]) self.transition_durations = np.array([np.mean(self.all_percepts_and_durations[1][:,-1]), np.median(self.all_percepts_and_durations[1][:,-1])]) self.ratio_percept_red_durations = self.red_durations / (self.red_durations + self.green_durations) plot_mean_or_median = 0 # mean f = pl.figure(figsize = (8,4)) s = f.add_subplot(111) for i in range(len(self.colors)): pl.hist(self.all_percepts_and_durations[i][:,-1], bins = 20, color = self.colors[i], histtype='step', lw = 3.0, alpha = 0.4, label = ['Red', 'Trans', 'Green'][i]) pl.hist(np.concatenate([self.all_percepts_and_durations[0][:,-1], self.all_percepts_and_durations[-1][:,-1]]), bins = 20, color = 'k', histtype='step', lw = 3.0, alpha = 0.4, label = 'Percepts') pl.legend() s.set_xlabel('time [ms]') s.set_ylabel('count') sn.despine(offset=10) s.annotate("""ratio_transition: %1.2f, \nratio_percept_red: %1.2f, \nduration_red: %2.2f,\nduration_green: %2.2f, \nratio_percept_red_durations: %1.2f"""%(self.ratio_transition, self.ratio_percept_red, self.red_durations[plot_mean_or_median], self.green_durations[plot_mean_or_median], self.ratio_percept_red_durations[plot_mean_or_median]), (0.5,0.65), textcoords = 'figure fraction') pl.tight_layout() pl.savefig(os.path.join(self.analyzer.fig_dir, self.file_alias + '_dur_hist.pdf'))
def loglog_exponent_finding(data_set, quantity, save=False, heat_capacity_correction=0): """Find ratios of critical exponents by finding a linear relation between logarithms of lattice sizes and quantity values.""" lattice_sizes_log = [] magnetizations_log = [] magnetizations_log_error = [] for lattice_size, data in data_set.items(): magnetization_log = np.log(data[0][1] + heat_capacity_correction) magnetization_log_error = data[0][2] / data[0][1] lattice_sizes_log.append(np.log(lattice_size)) magnetizations_log.append(magnetization_log) magnetizations_log_error.append(magnetization_log_error) lattice_sizes = np.asarray(lattice_sizes_log) magnetizations_log = np.asarray(magnetizations_log) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(lattice_sizes, magnetizations_log) fitted_line = slope * lattice_sizes + intercept plt.xlabel(r'$\log(L)$') plt.ylabel(r'$\log({0})$'.format('\mathrm{' + quantity.replace(" ", "\ ") + '}')) plt.errorbar(lattice_sizes, magnetizations_log, magnetizations_log_error, linestyle='None', marker='o') plt.plot(lattice_sizes, fitted_line) sns.despine() if save: plt.savefig("{0}/{1}_{2}_loglog_plot.pdf".format(SAVE_LOCATION, time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())), quantity.lower().replace(" ", "_"), bbox_inches='tight')) plt.show() return slope, std_err
def raster_plot(spikes, savepath, savefig=False): """Plots raster plot of spikes from multiple neurons. Parameters ---------- spikes : list of np.arrays Where each inner array contains the spike times (floats) for an individual neuron. savepath : str Location and filename for the saved plot. savefig : boolean Default is False show the plot without saving it. True and will save the plot to the specified location. """ location = 1 for neuron in spikes: if len(neuron) > 0: plt.plot(neuron, np.ones(len(neuron))+location, '|', color='k', ms=4, mew=1) location += 1 plt.xlabel('Time (ms)') plt.ylabel('Neuron number') sns.despine() plt.ylim(0, location+1) if savefig: plt.savefig(savepath, dpi=300, bbox_inches='tight') plt.close() else: plt.show()
def __init__(self, stream, fig, axes, window, scale, dejitter=True): """Init""" self.stream = stream self.window = window self.scale = scale self.dejitter = dejitter self.inlet = StreamInlet(stream, max_chunklen=buf) self.filt = True info = self.inlet.info() description = info.desc() self.sfreq = info.nominal_srate() self.n_samples = int(self.sfreq * self.window) self.n_chan = info.channel_count() ch = description.child('channels').first_child() ch_names = [ch.child_value('label')] for i in range(self.n_chan): ch = ch.next_sibling() ch_names.append(ch.child_value('label')) self.ch_names = ch_names fig.canvas.mpl_connect('key_press_event', self.OnKeypress) fig.canvas.mpl_connect('button_press_event', self.onclick) self.fig = fig self.axes = axes sns.despine(left=True) self.data = np.zeros((self.n_samples, self.n_chan)) self.times = np.arange(-self.window, 0, 1. / self.sfreq) impedances = np.std(self.data, axis=0) lines = [] self.rects = self.axes[1].bar(0, 1) lines = [] for ii in range(self.n_chan): line, = self.axes[0].plot(self.times[::subsample], self.data[::subsample, ii] - ii, lw=1) lines.append(line) self.lines = lines # self.text = axes. self.axes[1].xaxis.grid(False) self.axes[1].set_xticks([]) self.axes[1].set_ylim([0, 120]) self.value = None self.display_every = int(refresh / (12 / self.sfreq)) self.bf, self.af = butter(4, np.array([0.5, 20]) / (self.sfreq / 2.), 'bandpass') self.low = 10000 self.high = 0
if logplot: r = numpy.log2(r) max_ratio = r.max() for k in range(ns): r[:, k] = numpy.sort(r[ :, k]) # Plot stair graphs with markers. n = [] for k in range(1, np + 1): n.append(k / np) plt.figure(figsize = (8, 5)) plt.plot(r[:, 0], n, color = '#3CB371', linewidth = 1, label = 'GRASP') plt.plot(r[:, 1], n, color = '#FFD700', linewidth = 1, label = 'FFM') plt.plot(r[:, 2], n, color = '#4B0082', linewidth = 1, label = 'M-FFM') plt.plot(r[:, 3], n, color = '#FF6347', linewidth = 1, label = 'REF') plt.ylabel('Probabilidade (%)') plt.xlabel('$log_2(r)$') sn.despine(left = True, bottom = True) plt.grid(True, axis = 'x') plt.grid(True, axis = 'y') plt.legend() plt.savefig(splitext(filename)[0] + '.eps', dpi = 1500, transparent = True, bbox_inches = 'tight') plt.show()
###################################################################### fig, axarr = plt.subplots(K, 1, figsize=(12, 6)) ranges = [[800, 8000], [100, 8000], [800, 4000], [800, 2000]] for k in range(K): axarr[k].plot(lam0, B_mle[k, :], color=sns.color_palette()[k]) axarr[k].set_xlim(ranges[k]) axarr[k].tick_params(axis='both', which='major', labelsize=12) axarr[k].tick_params(axis='x', which='major', labelsize=16) max_yticks = 3 yloc = plt.MaxNLocator(max_yticks) axarr[k].yaxis.set_major_locator(yloc) plt.xlabel(" $\lambda$ (wavelength in $\AA$) ", fontsize=20, labelpad=20) plt.text(.02, .5, "$B_k(\lambda)$", transform=fig.transFigure, fontsize=20) plt.subplots_adjust(hspace=.6) #plt.tight_layout() sns.despine(top=True) plt.savefig( "/Users/acm/Proj/DESIMCMC/tex/quasar_z/figs/rank_%d_basis-random.pdf" % B.shape[0], bbox_inches='tight') plt.close('all') ## plot a reconstruction lam_obs, qtrain, qtest = \ load_data_clean_split(spec_fits_file = 'quasar_data.fits', Ntrain = 400) quasar_spectra = qtrain['spectra'] quasar_z = qtrain['Z'] quasar_ivar = qtrain['spectra_ivar'] quasar_zerr = qtrain['Z_err'] N = quasar_spectra.shape[0]
def assignment7_own(): # ### Point 1. usps_data = loadmat('./usps.mat') y = usps_data['data_labels'].T X = usps_data['data_patterns'].T indices = np.arange(len(y)) te_indices = [] for i in range(10): digit_indices = indices[np.argmax(y, axis=1) == i] te_indices.extend(list( np.random.choice(digit_indices, len(digit_indices)//4, replace=False) )) te_indices = np.array(te_indices) tr_indices = indices[~np.isin(indices, te_indices)] X_train = X[tr_indices] y_train = y[tr_indices] X_test = X[te_indices] y_test = y[te_indices] X_train = ((X_train - X_train.min(axis=0)) / (X_train.max(axis=0) - X_train.min(axis=0))) X_test = ((X_test - X_test.min(axis=0)) / (X_test.max(axis=0) - X_test.min(axis=0))) X_train -= X_train.mean(axis=0) X_test -= X_test.mean(axis=0) parameters = [ { 'kernel_name': ['linear'], 'C': np.logspace(0, 4, 5), 'kernelparameter': np.logspace(-1, 2, 4), }, { 'kernel_name': ['polynomial'], 'C': np.logspace(0, 4, 5), 'kernelparameter': np.logspace(-1, 2, 4), }, { 'kernel_name': ['gaussian'], 'C': np.logspace(0, 4, 5), 'kernelparameter': np.logspace(-1, 2, 4), }, ] def cv_digit(i): print(f"Training {i}") y_tr = (np.argmax(y_train, axis=1) == i).astype(int) y_tr[y_tr == 0] = -1 gs = GridSearchCV( svm_smo('gaussian'), parameters, cv=5, verbose=1, scoring=lambda m, X, y: -zero_one_loss(y, m.predict(X)) ) gs.fit(X_train, y_tr) best_model = gs.best_estimator_ y_te = (np.argmax(y_test, axis=1) == i).astype(int) y_te[y_te == 0] = -1 return { 'model': best_model, 'params': best_model.get_params(True), 'error': zero_one_loss(y_te, best_model.predict(X_test)) } results = Parallel(n_jobs=-1)(delayed(cv_digit)(i) for i in range(10)) print(results) # ### Point 2. _, axes = plt.subplots(10, 2, figsize=(16, 20)) axes[0][0].set_title("Support vectors") axes[0][1].set_title("Sample images") for i, (ax1, ax2) in enumerate(axes): sv = results[i]['model'].support_vectors_ indices = np.arange(len(sv)) indices = np.random.choice(indices, 5, replace=False) sv = sv[indices] ax1.imshow(sv.reshape(5, 16, 16).transpose(1, 0, 2).reshape(16, 80)) ax1.set_xticks([]) ax1.set_yticks([]) ax1.set_ylabel(f"Digit: {i}") sv = X_train[np.argmax(y_train, axis=1) == i] indices = np.arange(len(sv)) indices = np.random.choice(indices, 5, replace=False) sv = sv[indices] ax2.imshow(sv.reshape(5, 16, 16).transpose(1, 0, 2).reshape(16, 80)) ax2.set_xticks([]) ax2.set_yticks([]) sns.despine(left=True, bottom=True) plt.tight_layout() plt.show() plt.savefig('assignment7_2.png')
edgecolors='face', vmin=0.2, alpha=0.5) ax_inset.set_xlim(*-np.log10([0.5, 0.001])) ax_inset.set_xticks( -np.log10([0.5, 0.4, 0.3, 0.2, 0.1, 0.05, 0.01, 0.001])) ax_inset.set_xticklabels( ['', '', '', '', '', 0.05, '', 0.001], color='#383838') ax_inset.set_ylim(-0.05, 1.05) ax_inset.axvline( -np.log10(0.05), color='red', linestyle='--') pvals_orig = (10 ** (-1 * ax_inset.get_xticks())).round(3) ax_inset.set_yticklabels(['', 0, 1, ''], color='#383838') sns.despine(trim=True, ax=ax) ax_inset.tick_params(labelsize=8) if i_case > 2: ax.set_xlabel(r'$-log_{10}(p)$', fontsize=10, fontweight=150) if i_case in (0, 3): ax.set_ylabel(r'prediction [$R^2$]', fontsize=12, fontweight=150) ax.annotate( 'ABCDEFG'[i_case], xy=(-0.20, 0.99), fontweight=200, fontsize=20, xycoords='axes fraction') plt.subplots_adjust(hspace=0.33, wspace=.46, left=.07, right=.94, top=.94, bottom=.10) plt.savefig('./figures/simulations_by_aspect.png', bbox_inches='tight', dpi=300)
filename = "{}.cnf".format(instance) with open(filename, 'r') as file: all_lines = file.readlines() clauses = [] for line in all_lines: if line.startswith('p'): _, _, n_vars, _ = line.split() n_vars = int(n_vars) elif line.startswith('%'): break elif not line.startswith('c'): v1, v2, v3, _ = line.split() clauses.append([to_tuple(v1), to_tuple(v2), to_tuple(v3)]) initial_solutions = [initial_solution(n_vars) for _ in range(10)] mean, std = simmulated_annealing(clauses, initial_solutions, n_vars) print("SA: {} +- {}".format(mean, std)) mean, std = random_search(clauses, initial_solutions, n_vars) print("RS: {} +- {}".format(mean, std)) fig, ax = plt.subplots() sns.boxplot(data=results, orient='v', ax=ax) sns.despine() fig.savefig('{}_boxblot.png'.format(instance))
plt.plot([0 for i in Forman], color='gray', ls='dashed') plt.xticks(ticks, moving_ticks) plt.xticks(rotation='90', size=20) plt.xticks(size=20) plt.yticks(size=20) plt.tick_params(width=2, length=4) plt.legend(framealpha=0, fontsize=20) plt.margins(x=0) color_index = moving_ticks.index('11/Mar-17/Mar') plt.gca().get_xticklabels()[color_index].set_color("red") import seaborn seaborn.despine(top=True) plt.savefig('Ricci_on_COVID_new_cases.png', dpi=300, bbox_inches='tight') # In[23]: Df = JH_df.copy() #m=50 time = 7 Forman = [] now = datetime.now() for i in range(1, len(Df) - time + 2): E = e(i, i + time - 1) G = graph(i, i + time - 1, E) formanCurvature(G) value = np.mean(list( nx.get_edge_attributes(G, 'formanCurvature').values()))
def plot( self, val: tf.Tensor, key: str = None, therm_frac: float = 0., num_chains: int = 0, title: str = None, outdir: str = None, subplots_kwargs: dict[str, Any] = None, plot_kwargs: dict[str, Any] = None, ): plot_kwargs = {} if plot_kwargs is None else plot_kwargs if subplots_kwargs is None: subplots_kwargs = {'figsize': (4, 3)} figsize = subplots_kwargs.get('figsize', (4, 3)) # assert key in self.data # val = self.data[key] # color = f'C{idx%9}' assert val is not None try: tmp = val[0] except IndexError: raise IndexError(f'Unable to index {val},\n' f'type(val)={type(val)}') if isinstance(tmp, tf.Tensor): arr = val.numpy() elif isinstance(tmp, float): arr = np.array(val) else: try: arr = np.array([np.array(i) for i in val]) except (AttributeError, ValueError) as exc: raise exc subfigs = None steps = np.arange(arr.shape[0]) if therm_frac > 0: drop = int(therm_frac * arr.shape[0]) arr = arr[drop:] steps = steps[drop:] if len(arr.shape) == 2: _ = subplots_kwargs.pop('constrained_layout', True) figsize = (2 * figsize[0], figsize[1]) fig = plt.figure(figsize=figsize, constrained_layout=True, dpi=200) subfigs = fig.subfigures(1, 2, wspace=0.1, width_ratios=[1., 1.5]) gs_kw = {'width_ratios': [1.25, 0.5]} (ax, ax1) = subfigs[1].subplots(1, 2, gridspec_kw=gs_kw, sharey=True) # (ax, ax1) = fig.subfigures(1, 1).subplots(1, 2) # gs = fig.add_gridspec(ncols=3, nrows=1, width_ratios=[1.5, 1., 1.5]) color = plot_kwargs.get('color', None) label = r'$\langle$' + f' {key} ' + r'$\rangle$' ax.plot(steps, arr.mean(-1), lw=2. * LW, label=label, **plot_kwargs) sns.kdeplot(y=arr.flatten(), ax=ax1, color=color, shade=True) #ax1.set_yticks([]) #ax1.set_yticklabels([]) ax1.set_xticks([]) ax1.set_xticklabels([]) sns.despine(ax=ax, top=True, right=True) sns.despine(ax=ax1, top=True, right=True, left=True, bottom=True) ax.legend(loc='best', frameon=False) ax1.grid(False) ax1.set_xlabel('') ax1.set_ylabel('') ax.set_ylabel(key, fontsize='large') axes = (ax, ax1) else: if len(arr.shape) == 1: fig, ax = plt.subplots(**subplots_kwargs) ax.plot(steps, arr, **plot_kwargs) axes = ax elif len(arr.shape) == 3: fig, ax = plt.subplots(**subplots_kwargs) for idx in range(arr.shape[1]): ax.plot(steps, arr[:, idx, :].mean(-1), label='idx', **plot_kwargs) axes = ax else: raise ValueError('Unexpected shape encountered') ax.set_ylabel(key) if num_chains > 0 and len(arr.shape) > 1: num_chains = arr.shape[1] if ( num_chains > arr.shape[1]) else num_chains for idx in range(num_chains): ax.plot(steps, arr[:, idx], alpha=0.5, lw=LW / 4, **plot_kwargs) ax.set_xlabel('draw', fontsize='large') if title is not None: fig.suptitle(title) if outdir is not None: fig.savefig(Path(outdir).joinpath(f'{key}.pdf')) return fig, subfigs, axes
fit = np.polyfit(x, y, 1) xSmooth = np.linspace(0.5, 6.5, 101) ySmooth = np.polyval(fit, xSmooth) fig, axs = plt.subplots(1, 2) # Plot the datapoints, re \bar{y} axs[0].plot(x, y, 'o') axs[0].plot(xSmooth, ySmooth) axs[0].set_xlim([0, 7]) axs[0].set_ylim([0, 6]) axs[0].set_aspect('equal') axs[0].hlines(yMean, 0, 7, lw=0.5) axs[0].text(0.5, yMean + 0.1, r'$\bar{y}$', fontsize=18) sns.despine(ax=axs[0]) for ii in range(len(y)): width = yMean - y[ii] rect = Rectangle((x[ii], y[ii]), width=width, height=width, facecolor='r', alpha=0.2) axs[0].add_patch(rect) # Plot the datapoints, re \hat{y} axs[1].plot(x, y, 'o') axs[1].plot(xSmooth, ySmooth) axs[1].set_xlim([0, 7])
def plot_gradual_relaxation(par): stay_duration = par["stay_duration"] n_ages = par["n_ages"] # Read in exp_design matrix. Simulation script uses row 54 exp_design = pd.read_csv(h.exp_design_filepath, index_col=0) # To get HI threshold if all SD but SI remains at 0.1 R0_relaxed = exp_design.loc[60, "R0"] # todo: not robust to no. of strats # HI threshold for *susceptible* population hi_threshold = (1 / R0_relaxed) * par["N"].sum() # Read in simulation results out_df = pd.read_csv(h.gradual_relaxation_filepath) # Total susceptible and new cases C_cols = ["C" + str(y) for y in range(0, n_ages)] S_cols = ["S" + str(y) for y in range(0, n_ages)] out_df["Stot"] = out_df[S_cols].sum(axis=1) out_df["Ctot"] = out_df[C_cols].sum(axis=1) # Daily fraction of new cases hospitalised out_df["hosp_rate"] = out_df["new_hosp"] / out_df["Ctot"] # Total cumulative fatalities out_df["fatalities"] = out_df[['<60_fatal', '>60_fatal']].sum(axis=1) # maximum hospital burden print("Max. hosp. burden:", out_df.groupby("run")["hosp_burden"].max()) # maximum hospital rate max_hosp_rate = out_df.groupby("run")["hosp_rate"].max() print("Max. hosp. rate:", max_hosp_rate) # Time to reach herd immunity at max susceptible depletion rate using social # distancing hosp_capacity = 1e5 min_time_to_hi = (par["N"].sum() - hi_threshold) * stay_duration\ * max_hosp_rate[1] / hosp_capacity print("Hospital capacity:", hosp_capacity) print("Minimum time to herd immunity:", min_time_to_hi) # Final fatalities print("Final fatalities:", out_df.groupby("run")["fatalities"].max()) colours = h.colours runs = out_df["run"].unique() runs_names = dict( zip(runs, [ "No intervention", "SI and 60+ social\ndistancing (SD)", "0-59 SD relaxed over 100 days", "0-59 SD relaxed over 200 days", "0-59 SD relaxed over 300 days" ])) # HI threshold for *susceptible* population hi_threshold = (1 / R0_relaxed) * par["N"].sum() runs_cols = dict( zip(runs, [colours[11], colours[8], colours[5], colours[12], colours[0]])) use_thin = False fig_width = 3.4252 if use_thin else 5.2 fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(fig_width, 5.0)) fig.subplots_adjust(left=0.18, bottom=0.09, right=0.92, top=0.96, wspace=0.4, hspace=0.4) legend_loc = (0.3, 0.25) fontsize = 8 label_fontsize = 8 for run, g in out_df.groupby("run"): ax = axes[0] ax.plot(g["time"], g["hosp_burden"] / 1e5, label=runs_names[run], color=runs_cols[run]) ax = axes[1] ax.plot(g["time"], g["fatalities"] / 1e5, label=run, color=runs_cols[run]) ax = axes[2] ax.plot(g["time"], g["Stot"] / 1e6, label=run, color=runs_cols[run]) axes[0].set_title("a)", loc="left", fontsize=label_fontsize) axes[0].set_ylabel("Hospital burden\n(hundred thousands)", fontsize=label_fontsize) axes[0].set_yticks(np.linspace(0, 12, 5)) axes[0].legend(frameon=False, fontsize=8, loc=1) #legend_loc) axes[0].axvline(par["tint"], color="grey", linestyle="--") axes[1].set_title("b)", loc="left", fontsize=label_fontsize) axes[1].set_ylabel("Fatalities\n(hundred thousands)", fontsize=label_fontsize) axes[1].set_yticks([0, 1, 2, 3, 4]) axes[2].set_title("c)", loc="left", fontsize=label_fontsize) axes[2].set_xlabel("Day", fontsize=label_fontsize) axes[2].set_ylabel("Susceptible population\n(millions)", fontsize=label_fontsize) axes[2].set_yticks([10, 20, 30, 40, 50, 60, 70]) axes[2].text(-0, 48, "Herd\nimmunity\nthreshold", fontsize=8, va="top") axes[2].axhline(hi_threshold / 1e6, color="grey", linestyle="--") for ax in axes: ax.set_xlim(0, 300) sns.despine(ax=ax, trim=True, offset=5) ax.tick_params(labelsize=fontsize) fig.savefig("./figures/fig_gradual_relaxation.pdf")
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns data=pd.read_csv('/BioII/lulab_b/huashuo/multiprimer/region/plot_CYP2E1.csv',sep='\t') for i in list(data.columns[1:-1]): plt.figure(figsize=(4,5)) sns.set_context("talk", font_scale=1, rc={"lines.linewidth": 2.5}) ax = sns.boxplot(x="label", y=i, data=data,order=["NC", "HCC_0","HCC_1"],palette=["#808080", "#239B56","#2980B9"]) plt.ylabel(i.split('|')[0]) # plt.ylim([0,400]) plt.tight_layout() sns.despine(offset=5, trim=True) plt.savefig('/BioII/lulab_b/huashuo/multiprimer/box/'+i.split('|')[0]+'|'+i.split('|')[1]+'.png') plt.close()
def plot_summary_figure(par): n_ages = par["n_ages"] stay_duration = par["stay_duration"] hosp_cap = 17800 Ntot = par["N"].sum() ts_df = pd.read_csv(h.no_fatigue_filepath) exp_df = pd.read_csv(h.exp_design_filepath, index_col=0) exp_df["max_burden"] = ts_df.groupby("run")["hosp_burden"].max() ts_df["S_tot"] = ts_df[["S" + str(i) for i in range(n_ages)]].sum(axis=1) R0_relaxed = exp_df.loc[0, "R0"] hi_threshold = (1/R0_relaxed)*par["N"].sum() exp_df["name"] = exp_df["name"].replace(to_replace="SI and 60+ social " "distancing (SD)", value="SI and 60+ social\n" "distancing (SD)") exp_df["name"] = exp_df["name"].replace(to_replace="SI; 0-24, 25-59 " "and 60+ SD", value="SI; 0-24, 25-59\nand 60+ SD") eg = exp_df.groupby("name") eg_max = eg.apply(lambda x: x[x["max_burden"] < hosp_cap]["R0"].max()) exp_df_f = exp_df[exp_df["Intervention strategy"] != 5] exp_df_f = exp_df_f.reset_index() select_runs = exp_df[((exp_df["comp_factor"] * 100 % 10) == 0)].index ts_df_light = ts_df[ts_df["run"].isin(select_runs)] # Time to hospitalise C_cols = ["C" + str(i) for i in range(15)] ts_df["Ctot"] = ts_df[C_cols].sum(axis=1) ts_df["hosp_rate"] = ts_df["new_hosp"]/ts_df["Ctot"] # # maximum hospital rate max_hosp_rate = ts_df.groupby("run")["hosp_rate"].max().sort_values() print("Max hosp rate for each strategy: ") print(max_hosp_rate[np.arange(0,6)]) # print(max_hosp_rate) # # max_hosp_rate.hist() # plt.show() # Time to reach herd immunity at max susceptible depletion rate min_time_to_hi = (par["N"].sum() - hi_threshold) \ * stay_duration * max_hosp_rate[1]/hosp_cap print("min_time_to_hi", min_time_to_hi) print("max R0:", eg_max.max()) def col_function(x): hosp_max = x["hosp_burden"].max() if (x["S_tot"].iloc[-1] < hi_threshold) & (hosp_max < hosp_cap): c = "green" elif hosp_max < hosp_cap: c = h.colours[9] else: c = h.colours[12] return c use_thin = True fig_width = 3.4252 if use_thin else 5.2 #fig_width= 8 #fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(fig_width, 5.0)) fig = plt.figure(figsize=(fig_width, 3.5)) gs = GridSpec(6, 1) axes = np.array([plt.subplot(gs[0:3, 0]), plt.subplot(gs[3:, 0])]) fontsize = 8 label_fontsize = 8 legend_loc0 = (0.52, -0.0) legend_loc1 = (0.1, -0.05) fig.subplots_adjust(left=0.18, bottom=0.13, right=0.96, top=0.95, wspace=0.4, hspace=40) # fig.subplots_adjust(left=0.17, bottom=0.09, # right=0.96, top=0.96, wspace=0.5, hspace=0.5) ax = axes[0] for _, g in exp_df.groupby("Intervention strategy"): col = g["color"].iloc[0] name = g["name"].iloc[0] if name == "SI and no 60+ contacts": name = "SI and no\n60+ contacts" ax.plot(g["R0"], np.log10(g["max_burden"]), color=col, label=name) if name == "Self-isolation (SI)": ax.plot(g["R0"], np.log10(g["max_burden"]), color=col, label=name) ax.axvline(eg_max.max(), color="grey", linestyle="--") ax.fill_between([1, eg_max.max()], [3, 3], [np.log10(hosp_cap), np.log10(hosp_cap)], facecolor="#65C8D0", alpha=0.4) ax.axvline(1, linestyle="--", color="grey") ax.axhline(np.log10(hosp_cap), linestyle="--", color="grey", xmax=2.3/2.5) ax.text(2.05, 4.3, s="Hospital\ncapacity\nexceeded", color="grey", ha="center", va="bottom", fontsize=label_fontsize) ax.text(0.5, 4.2, s="Suppression\nachieved", color="grey", ha="center", va="top", fontsize=label_fontsize) ax.set_xlabel("Reproductive number", fontsize=label_fontsize) ax.set_ylabel("Peak hospital\nburden (Log$_{10}$)", fontsize=label_fontsize) ax.set_xlim(0, 2.5) ax.set_ylim(3.0, 6.) ax.set_yticks(np.linspace(3, 6, 4)) ax.legend(frameon=False, fontsize=6, loc=2, handlelength=1, columnspacing=0.5, facecolor="white") df_list = [ts_df_light] #todo: fix handeling of this for i, df in enumerate(df_list): ax = axes[1] td_g = df.groupby("run") for run, g in td_g: col = col_function(g) ax.plot(g["hosp_burden"]/1e5, g["S_tot"]/1e6, color=col, alpha=0.8) ax.axhline(hi_threshold/1e6, linestyle="--", color="grey") ax.axvline(hosp_cap/1e5, linestyle="--", color="grey") ax.set_xlabel("Hospital burden (hundred thousands)", fontsize=label_fontsize) ax.set_ylabel("Susceptible individuals\n(millions)", fontsize=label_fontsize) ax.set_xlim(0, 8) ax.set_ylim(0, 67) ax.set_yticks(np.linspace(0, 60, 4)) ax.text(0.6, 62, s="Hospital capacity", color="grey", fontsize=label_fontsize) ax.text(8, 32, s="Herd immunity\nthreshold", color="grey", ha="right", fontsize=label_fontsize) # ax.plot(df_seir["hosp_burden"]/1e5, df_seir["S"]/1e6, color=h.colours[2]) legend_elements = [Line2D([0], [0], color=h.colours[9], label='Below capacity'), Line2D([0], [0], color=h.colours[12], label='Exceeds capacity'), # Line2D([0], [0], color=h.colours[2], # label='At capacity'), ] ax.legend(handles=legend_elements, ncol=2, loc=legend_loc1, fontsize=6, handlelength=1, columnspacing=2, frameon=False) titles = ["a)", "b)", "c)", "d)", "e)"] for i, ax in enumerate(axes): sns.despine(ax=ax, offset=5, trim=True) ax.set_title(titles[i], loc="left", fontsize=label_fontsize) ax.tick_params(labelsize=fontsize) fig.savefig("./figures/fig_summary.pdf")
_ = ax0.plot(time_range, best_fit, label='fit', lw=1, alpha=0.8) _ = ax0.fill_between(time_range, cred_region[0, :], cred_region[1, :], alpha=0.3, color='firebrick', label='__nolegend__') _ = ax0.legend() # Plot the sampling distributions. _ = ax1.hist(1 / trace_df['lambda'], bins=80, normed=True, alpha=0.5, lw=1, edgecolor='k', histtype='stepfilled') _ = ax2.hist(trace_df['a0'], bins=80, normed=True, alpha=0.5, lw=1, edgecolor='k', histtype='stepfilled') plt.tight_layout() sns.despine(offset=7) plt.savefig('output/{}_r{}_{}C_{}_{}_growth.png'.format( DATE, RUN_NUMBER, TEMP, CARBON, OPERATOR), bbox_inches='tight')
sns.kdeplot(df_seqid_bin.molprobity_score, label=label, color=palette[i], shade=True) ax = plt.gca() plt.setp(ax, yticks=[]) legend = plt.legend(title='Sequence identity (%)', fontsize='xx-small', loc='upper left') plt.setp(legend.get_title(), fontsize='x-small') # plt.xlim(0,30) plt.xlabel('MolProbity score') sns.despine(left=True) plt.tight_layout() plt.savefig('molprobity_dist.pdf') plt.savefig('molprobity_dist.png', dpi=300) plt.close() # # Plot sequence identity distribution # # sns.set(style="white") # sns.kdeplot(df_has_model.seqid) # sns.despine(left=True) # plt.setp(plt.gca(), yticks=[]) # plt.xlim(0,102)
# In[11]: 12 / 16 # In[13]: plt.figure(figsize=(6, 6)) plt.style.use('seaborn-white') sns.boxplot(x="variable", y="value", data=df_melt, width=0.25) plt.ylabel("Accuracy") plt.xlabel("") plt.xticks([0, 1], ["Expect", "Actual"]) sns.despine(left=False, bottom=False) # plt.savefig("../result_graph/expect_actual.png",bbox_inches='tight', dpi = 300) # In[14]: # !!!! T-test results !!!! scipy.stats.ttest_rel(df["actual_acc_percent"], df["expect_acc"]) # In[15]: # Male Expect me = df_melt[(df_melt["Gender"] == "Male") & (df_melt["variable"] == "expect_acc")]["value"]
def plot(df): """Plot the results.""" df["order"] = [3, 2, 4, 1, 5, 6, 7] df = df.sort_values("order") df.drop("order", axis=1, inplace=True) deviation = df.iloc[-1].values df = df.iloc[:-1] print("\nResults:\n") print(df) print("") colors = np.array( ["#9467bd", "#ff7f0e", "#1f77b4", "#d62728", "#8c564b", "0.5"]) # plot dx = 0.4 x1 = np.arange(1, len(colors) + 1, 1) x2 = x1 + dx bbox = dict(boxstyle="square", ec="none", fc="1", lw=1, alpha=0.7) fig, (ax3, ax1, ax2) = plt.subplots(1, 3, figsize=(16, 6), sharex=True, sharey=False) # plot dominance ax1.bar(x1, df["IG dominance"].values, width=dx, label="Infragravity", facecolor="navy", edgecolor="k", alpha=0.75) ax1.bar(x2, df["SW dominance"].values, width=dx, label="Sea-swell", facecolor="darkgreen", edgecolor="k", alpha=0.75) # plot IG phase ax2.bar(x1, df["Positive phase"].values, width=dx, label="Positive phase", facecolor="orangered", edgecolor="k", alpha=0.75) ax2.bar(x2, df["Negative phase"].values, width=dx, label="Negative phase", facecolor="dodgerblue", edgecolor="k", alpha=0.75) # plot IG/SW ratio ax3.bar(x1, df["Ravg"].values, width=dx + dx, label="Positive phase", color=colors, edgecolor="k", alpha=0.75) ax3.errorbar(x1, df["Ravg"].values, df["Rstd"].values, fmt="none", color="k", capsize=5, lw=2) # add numbers to first plot for xa, xb, ya, yb in zip(x1, x2, df["IG dominance"].values, df["SW dominance"].values): ax1.text(xa, ya * 1.05, str(round(ya, 1)) + "%", ha="center", va="bottom", rotation=90, fontsize=12, zorder=100, bbox=bbox) ax1.text(xb, yb * 1.05, str(round(yb, 1)) + "%", ha="center", va="bottom", rotation=90, fontsize=12, zorder=100, bbox=bbox) # add numbers to second plot for xa, xb, ya, yb in zip(x1, x2, df["Positive phase"].values, df["Negative phase"].values): ax2.text(xa, ya * 1.05, str(round(ya, 1)) + "%", ha="center", va="bottom", rotation=90, fontsize=12, zorder=100, bbox=bbox) ax2.text(xb, yb * 1.05, str(round(yb, 1)) + "%", ha="center", va="bottom", rotation=90, fontsize=12, zorder=100, bbox=bbox) # 50% line ax1.axhline(50, lw=3, ls="--", color="r") ax2.axhline(50, lw=3, ls="--", color="r") # legends lg = ax2.legend() lg.get_frame().set_color("w") lg = ax1.legend() lg.get_frame().set_color("w") # fix ticks ax1.set_xticks(x1 + (dx / 8)) # set labels labels = [] for label in df.index.values: lbl = "\n".join(textwrap.wrap(label, 10)) labels.append(lbl) ax1.set_xticklabels(labels, fontsize=14, rotation=90) ax2.set_xticklabels(labels, fontsize=14, rotation=90) ax3.set_xticklabels(labels, fontsize=14, rotation=90) ax1.set_ylabel(r"Perc. of occurrence $[\%]$") ax2.set_ylabel(r"Perc. of occurrence $[\%]$") ax3.set_ylabel(r"$E_{ig}/E_{sw}$ $[-]$") ax1.set_ylim(0, 100) ax2.set_ylim(0, 100) ax1.grid(color="w", lw=2, ls="-") ax2.grid(color="w", lw=2, ls="-") ax3.grid(color="w", lw=2, ls="-") sns.despine(ax=ax1) sns.despine(ax=ax2) sns.despine(ax=ax3) ax1.text(0.025, 0.965, "b)", transform=ax1.transAxes, ha="left", va="top", bbox=bbox, zorder=100) ax2.text(0.025, 0.965, "c)", transform=ax2.transAxes, ha="left", va="top", bbox=bbox, zorder=100) ax3.text(1 - 0.025, 0.965, "a)", transform=ax3.transAxes, ha="right", va="top", bbox=bbox, zorder=100) fig.tight_layout() plt.show()
simsAx[0].semilogx([1, 1], [-1.5, 1], 'k--') simsAx[0].semilogx([10, 10], [-1.5, 1], 'k--') simsAx[0].semilogx([100, 100], [-1.5, 1], 'k--') # now the real stuff ex = simsAx[0].semilogx(omega, sfExc, 'k-') nm = simsAx[0].semilogx(omega, -sfNorm, 'r-', linewidth=2.5) simsAx[0].set_xlim([omega[0], omega[-1]]) simsAx[0].set_ylim([-0.1, 1.1]) simsAx[0].set_xlabel('SF (cpd)', fontsize=12) simsAx[0].set_ylabel('Normalized response (a.u.)', fontsize=12) simsAx[0].set_title('CELL %d' % (which_cell), fontsize=20) simsAx[0].legend([ex[0], nm[0]], ('excitatory %.2f' % (modParamsCurr[0]), 'normalization %.2f' % (np.exp(modParamsCurr[-2])))) # Remove top/right axis, put ticks only on bottom/left sns.despine(ax=simsAx[0], offset=5) #### Now simulate #### # construct by hand for now; 5 dispersions with the old stimulus set val_con_by_disp = [] val_con_by_disp.append( np.array([1, 0.688, 0.473, 0.325, 0.224, 0.154, 0.106, 0.073, 0.05, 0.01])) val_con_by_disp.append(np.array([1, 0.688, 0.473, 0.325])) val_con_by_disp.append(np.array([1, 0.688, 0.473, 0.325])) val_con_by_disp.append(np.array([1, 0.688, 0.473, 0.325])) val_con_by_disp.append(np.array([1, 0.688, 0.473, 0.325])) v_sfs = np.logspace(np.log10(0.3), np.log10(10), 11) # for now print('\nSimulating enhanced range of contrasts from model\n\n') #print('\tTesting at range of spatial frequencies: ' + str(v_sfs));
['variable'])['value'].mean()[['rewarded', 'unrewarded', 'bias']], color='black', linewidth=0, marker='_', markersize=13, zorder=100) ax[1].get_legend().set_visible(False) ax[1].set(xlabel='', ylabel='', ylim=[-1, 1.2], yticks=[-1, -0.5, 0, 0.5, 1], xticks=[0, 1, 2, 3], xlim=[-0.5, 3.5]) ax[1].axhline(color='darkgray', linestyle=':') ax[1].set_xticklabels([], ha='right', rotation=15) sns.despine(trim=True) plt.tight_layout(w_pad=-0.1) fig.savefig(figpath / 'figure5c_basic_weights.pdf') # ========================= # # SAME BUT FOR FULL TASK # ========================= # # reshape the data and average across labs for easy plotting full_summ_visual = pd.melt(params_full, id_vars=['institution_code', 'subject_nickname'], value_vars=['6.25', '12.5', '25', '100']).groupby([ 'institution_code', 'subject_nickname', 'variable' ]).mean().reset_index() full_summ_bias = pd.melt(
def plot_density_maps(densities=None, figure=None): """ functions to plot density maps from densities dictionary with data from x,y,z,xy,xz,yz projections :return: figure will be returned with all plotted maps from densities :param densities: dictionary which holds all projections for the plots :param figure: you can pass a figure if you want to use a custom plot format """ # if figure is not passed create a new one if figure is None: figure = plt.figure(figsize=(8, 8)) if densities is None: return figure # set the axes layout right ax_x = plt.subplot2grid((4, 4), (0, 1), rowspan=1, colspan=2) ax_y = plt.subplot2grid((4, 4), (1, 3), rowspan=2, colspan=1) ax_z = plt.subplot2grid((4, 4), (3, 3), rowspan=1, colspan=1) ax_xy = plt.subplot2grid((4, 4), (1, 1), rowspan=2, colspan=2, sharex=ax_x, sharey=ax_y) ax_xz = plt.subplot2grid((4, 4), (3, 1), rowspan=1, colspan=2, sharex=ax_xy, sharey=ax_z) ax_yz = plt.subplot2grid((4, 4), (1, 0), rowspan=2, colspan=1, sharey=ax_xy) axes = { 'x': ax_x, 'y': ax_y, 'z': ax_z, 'xy': ax_xy, 'xz': ax_xz, 'yz': ax_yz } # loop over all densities, keys contain type of data for name, density in densities.items(): # get name and split projection axes from it p_axes = name.split('_')[0] dim = len(p_axes) if dim > 1: ax = axes[p_axes] x_edges = density['edges'][0] y_edges = density['edges'][1] x_min = np.floor(np.min(x_edges)) x_max = np.ceil(np.max(x_edges)) y_min = np.floor(np.min(y_edges)) y_max = np.ceil(np.max(y_edges)) if p_axes == 'yz': ax.imshow(density['data'], extent=(y_min, y_max, x_max, x_min)) ax.set_xlabel(p_axes[1].capitalize() + r' ($\mu$m)') ax.set_ylabel(p_axes[0].capitalize() + r' ($\mu$m)') else: ax.imshow(density['data'].T, extent=(x_min, x_max, y_max, y_min)) ax.set_xlabel(p_axes[0].capitalize() + r' ($\mu$m)') ax.set_ylabel(p_axes[1].capitalize() + r' ($\mu$m)') ax.invert_yaxis() else: ax = axes[p_axes] if p_axes == 'x': ax.plot(density['edges'][0][:-1], density['data']) ax.set_xlabel(p_axes.capitalize() + r' ($\mu$m)') ax.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0)) else: ax.plot(density['data'], density['edges'][0][:-1]) ax.set_ylabel(p_axes.capitalize() + r' ($\mu$m)') ax.ticklabel_format(axis='x', style='scientific', scilimits=(0, 0)) sns.despine() ax.set_aspect('auto') figure.tight_layout(rect=[0, 0.03, 1, 0.9]) return figure
def plot_importance(df): """ Plot the importance of each domain. """ #TODO: parametrize df = df[df.link_type == 'results'] df.loc[df.category == 'top_insurance', 'category'] = 'insurance' df.loc[df.category == 'top_loans', 'category'] = 'loans' df.loc[df.category == 'med_sample_first_20', 'category'] = 'medical' df.loc[df.category == 'procon_popular', 'category'] = 'controversial' df.loc[df.domain == 'people also ask', 'domain'] = 'PeopleAlsoAsk' # make a copy before fillna counts_copy = df[(df.metric == 'domain_count') & (df.category != 'all')] df = df.fillna(0) pal = 'muted' # placeholder for qual coded values # df['fake_val'] = df['val'].map(lambda x: x / 2) categorized = df[df.category != 'all'] print('df head', df.head()) plot_col_dfs = [categorized] sns.set_context("paper", rc={ # "font.size": 8, # "font.family": "Times New Roman", #"axes.titlesize": 8, "axes.labelsize": 9 } ) width, height = 5.5, 5 fig, axes = plt.subplots(ncols=2, nrows=len(plot_col_dfs), figsize=(width, height), dpi=300) for colnum, subdf in enumerate(plot_col_dfs): if subdf.empty: continue tmpdf = subdf[ (subdf.subset == FULL) & (subdf.metric == 'domain_appears') ][['domain', 'val', 'is_ugc_col']] grouped_and_sorted = tmpdf.groupby('domain').mean().sort_values( 'val', ascending=False) order = list(grouped_and_sorted.index) ugc_cols = list(grouped_and_sorted[grouped_and_sorted.is_ugc_col == True].index) #print('ugc_cols', ugc_cols) nonugc_count = 0 selected_order = [] # add all UGC cols and up to num_nonugc non-ugc cols num_nonugc = 5 non_ugcs = [] for i_domain, domain in enumerate(order): if domain in ugc_cols: selected_order.append(domain) else: nonugc_count += 1 if nonugc_count <= num_nonugc: selected_order.append(domain) non_ugcs.append(i_domain) ranks_and_counts = [] for domain in selected_order: ranks = subdf[(subdf.metric == 'domain_rank') & (subdf.domain == domain)] ranks = ranks[ranks.val != 0].val #print('rank', domain, ranks.mean()) # will be a Series counts = counts_copy[counts_copy.domain == domain].val #print('count', domain, counts.mean()) full_rates = subdf[(subdf.metric == 'domain_appears') & (subdf.domain == domain) & (subdf.subset == FULL)].val top3_rates = subdf[(subdf.metric == 'domain_appears') & (subdf.domain == domain) & (subdf.subset == TOP_THREE)].val if top3_rates.empty: top3 = 0 else: top3 = top3_rates.mean() # print(full_rates) if top3_rates.empty: print(domain) print(top3_rates) ranks_and_counts.append({ 'domain': domain, 'average rank': round(ranks.mean(), 1), 'average count': round(counts.mean(), 1), 'average full-page incidence': round(full_rates.mean(), 2), 'average top-three incidence': round(top3, 2), }) # _, histax = plt.subplots() # sns.distplot( # ranks.dropna(), rug=True, bins=list(range(1, 13)), # kde=False, color="b", ax=histax) # histax.set_title('Histogram for {}'.format(domain)) ranks_and_counts_df = pd.DataFrame(ranks_and_counts) ranks_and_counts_df.to_csv('ranks_and_counts.csv') title_kwargs = {} # for subset in [FULL, TOP_THREE]: # subdf.loc[:, 'domain'] = subdf['domain'].apply(strip_domain_strings_wrapper(subset)) #print(subdf) if colnum in [0]: mask1 = (subdf.metric == 'domain_appears') & (subdf.subset == FULL) mask2 = (subdf.metric == 'domain_appears') & (subdf.subset == TOP_THREE) sns.barplot(x='val', y='domain', hue='category', order=selected_order, data=subdf[mask1], ax=axes[0], ci=None, palette=pal) # sns.barplot(x='fake_val', y='domain', hue='category', order=order, # data=subdf[mask1], ax=axes[0], ci=None, palette=pal_lower) sns.barplot(x='val', y='domain', hue='category', order=selected_order, data=subdf[mask2], ax=axes[1], ci=None, palette=pal) # sns.barplot(x='fake_val', y='domain', hue='category', order=order, # data=subdf[mask2], ax=axes[1], ci=None, palette=pal_lower) title_kwargs['metric'] = 'Fraction of pages where domain appears' # might be swapped. num_rows = len(selected_order) for rownum in [0, 1]: ax = axes[rownum] ax.set_xlim([0, 1]) ax.legend(loc='lower right', frameon=True) if rownum == 0: title_kwargs['subset'] = ALL_SUBSET_STRING ax.set_xlabel('Full-page incidence rate') #ax.set_xlabel('NewsCarousel') ax.legend().set_visible(False) elif rownum == 1: title_kwargs['subset'] = TOP_THREE_SUBSET_STRING ax.set_xlabel('Top-three incidence rate') ax.set(yticklabels=[], ylabel='') start_y = 0.6 * 1/num_rows y_size = 1 - 1.1 * 1/num_rows the_table = plt.table(cellText=ranks_and_counts_df[['average full-page incidence', 'average top-three incidence', 'average rank']].values, bbox=(1.1, start_y, 0.7, y_size)) the_table.auto_set_font_size(False) the_table.set_fontsize(8) the_labels = plt.table(cellText=[['average\nfull\npage\nrate', 'average\ntop\nthree\nrate', 'average\nrank']], bbox=(1.1,-1.2/num_rows,0.7,1/num_rows)) # for cell in the_labels._cells.values(): # cell.set_text_props(fontname="Times New Roman") the_labels.auto_set_font_size(False) the_labels.set_fontsize(7) for _, cell in the_table.get_celld().items(): # cell.set_linewidth(0) cell.set_linestyle('--') cell.set_linewidth(1.5) cell.set_edgecolor('lightgray') for _, cell in the_labels.get_celld().items(): cell.set_linewidth(0) ax.add_table(the_table) ax.add_table(the_labels) boxes = [] # Loop over data points; create box from errors at each point for i_non_ugc in non_ugcs: rect = Rectangle(xy=(0, i_non_ugc-0.5), width=1, height=1) boxes.append(rect) # if rownum == 1: # rect = Rectangle(xy=(1, i_non_ugc-0.5), width=1, height=1) # Create patch collection with specified colour/alpha pc = PatchCollection(boxes, facecolor='gray', alpha=0.1, edgecolor=None) ax.add_collection(pc) title_kwargs['type'] = QUERYSET_BREAKDOWN_STRING sns.despine(ax=ax, bottom=True, left=True) ax.set_ylabel('') #print('line y values') #print([x + 0.5 for x in range(len(selected_order))]) ax.hlines([x + 0.5 for x in range(len(selected_order))], *ax.get_xlim(), linestyle='--', color='lightgray') #ax.set_title(title_template.format(**title_kwargs)) fig.savefig( 'figures/importance.svg', bbox_inches='tight' )
dispAx[d][c_plt_ind, i].set_xscale('log') dispAx[d][c_plt_ind, i].set_xlabel('sf (c/deg)') dispAx[d][c_plt_ind, i].set_title('D%02d: contrast: %.3f' % (d, all_cons[v_cons[c]])) # Set ticks out, remove top/right axis, put ticks only on bottom/left dispAx[d][c_plt_ind, i].tick_params(labelsize=15, width=1, length=8, direction='out') dispAx[d][c_plt_ind, i].tick_params(width=1, length=4, which='minor', direction='out') # minor ticks, too... sns.despine(ax=dispAx[d][c_plt_ind, i], offset=10, trim=False) dispAx[d][c_plt_ind, 0].set_ylim((0, 1.5 * maxResp)) dispAx[d][c_plt_ind, 0].set_ylabel('resp (sps)') dispAx[d][c_plt_ind, 1].set_ylabel('ratio (pred:measure)') dispAx[d][c_plt_ind, 1].set_ylim((1e-1, 1e3)) dispAx[d][c_plt_ind, 1].set_yscale('log') fCurr.suptitle('cell #%d, loss %.2f' % (which_cell, modParams[which_cell - 1]['NLL'])) saveName = "/cell_%02d.pdf" % (which_cell) full_save = os.path.dirname(str(save_loc + 'byDisp/')) if not os.path.exists(full_save): os.makedirs(full_save) pdfSv = pltSave.PdfPages(full_save + saveName)
def plot_cell_cn_profile(ax, cn_data, value_field_name, cn_field_name=None, max_cn=13, chromosome=None, s=5, squashy=False, rawy=False, cmap=None): """ Plot copy number profile on a genome axis Args: ax: matplotlib axis cn_data: copy number table value_field_name: column in cn_data to use for the y axis value Kwargs: cn_field_name: state column to color scatter points max_cn: max copy number for y axis chromosome: single chromosome plot s: size of scatter points squashy: compress y axis rawy: raw data on y axis The cn_data table should have the following columns (in addition to value_field_name and optionally cn_field_name): - chr - start - end """ chromosome_info = refgenome.info.chromosome_info[['chr', 'chromosome_start', 'chromosome_end']].copy() chromosome_info['chr'] = pd.Categorical(chromosome_info['chr'], categories=cn_data['chr'].cat.categories) plot_data = cn_data.merge(chromosome_info) plot_data = plot_data[plot_data['chr'].isin(refgenome.info.chromosomes)] plot_data['start'] = plot_data['start'] + plot_data['chromosome_start'] plot_data['end'] = plot_data['end'] + plot_data['chromosome_start'] squash_coeff = 0.15 squash_f = lambda a: np.tanh(squash_coeff * a) if squashy: plot_data[value_field_name] = squash_f(plot_data[value_field_name]) if cn_field_name is not None: if cmap is not None: ax.scatter( plot_data['start'], plot_data[value_field_name], c=plot_data[cn_field_name], s=s, cmap=cmap, ) else: ax.scatter( plot_data['start'], plot_data[value_field_name], c=plot_data[cn_field_name], s=s, cmap=get_cn_cmap(plot_data[cn_field_name].astype(int).values), ) else: ax.scatter( plot_data['start'], plot_data[value_field_name], s=s, ) if chromosome is not None: chromosome_length = refgenome.info.chromosome_info.set_index('chr').loc[chromosome, 'chromosome_length'] chromosome_start = refgenome.info.chromosome_info.set_index('chr').loc[chromosome, 'chromosome_start'] chromosome_end = refgenome.info.chromosome_info.set_index('chr').loc[chromosome, 'chromosome_end'] xticks = np.arange(0, chromosome_length, 2e7) xticklabels = ['{0:d}M'.format(int(x / 1e6)) for x in xticks] xminorticks = np.arange(0, chromosome_length, 1e6) ax.set_xlabel(f'chromosome {chromosome}') ax.set_xticks(xticks + chromosome_start) ax.set_xticklabels(xticklabels) ax.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(xminorticks + chromosome_start)) ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter()) ax.set_xlim((chromosome_start, chromosome_end)) else: ax.set_xlim((-0.5, refgenome.info.chromosome_end.max())) ax.set_xlabel('chromosome') ax.set_xticks([0] + list(refgenome.info.chromosome_end.values)) ax.set_xticklabels([]) ax.xaxis.tick_bottom() ax.yaxis.tick_left() ax.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(refgenome.info.chromosome_mid)) ax.xaxis.set_minor_formatter(matplotlib.ticker.FixedFormatter(refgenome.info.chromosomes)) if squashy and not rawy: yticks = np.array([0, 2, 4, 7, 20]) yticks_squashed = squash_f(yticks) ytick_labels = [str(a) for a in yticks] ax.set_yticks(yticks_squashed) ax.set_yticklabels(ytick_labels) ax.set_ylim((-0.01, 1.01)) ax.spines['left'].set_bounds(0, 1) elif not rawy: ax.set_ylim((-0.05*max_cn, max_cn)) ax.set_yticks(range(0, int(max_cn) + 1)) ax.spines['left'].set_bounds(0, max_cn) if chromosome is not None: seaborn.despine(ax=ax, offset=10, trim=False) else: seaborn.despine(ax=ax, offset=10, trim=True) return chromosome_info
def boxplot_grid(): sns.set(font='serif') sns.set_style("white", { "font.family": "serif", "font.serif": ["Times", "Palatino", "serif"] }) sns.set_context('paper', font_scale=1.5) # Load data data = pickle.load(open('../scripts/haxby_mrt_texture_normalized.pickle', 'rb')) data = data.replace('haxby', 'Haxby\n(ISI = 12s)') data = data.replace('mirror-reversed text', 'Mirror-reversed text\n(ISI = 3-12s)') data = data.replace('texture decoding', 'Texture decoding\n(ISI = 4s)') # Colors cmap = sns.color_palette("Set2", n_colors=4) # Specify that I want each subplot to correspond to # a different robot type g = sns.FacetGrid( data, row="dataset", row_order=['Haxby\n(ISI = 12s)', 'Mirror-reversed text\n(ISI = 3-12s)', 'Texture decoding\n(ISI = 4s)'], sharey=False) # Create the bar plot on each subplot g.map( sns.boxplot, "accuracy", "dataset", "model", orient='h', palette=cmap, hue_order=['GLM', 'GLMs', 'spatiotemporal SVM', 'logistic deconvolution'], width=0.5, linewidth=1.75) # Now I need to draw the 50% lines on each subplot # separately axes = np.array(g.axes.flat) for ax in axes: ax.vlines(0, -0.5, 0.5, linestyle='--', linewidth=1.2) ax.set_xlim(-0.4, 0.4) ax.set_ylim(0.3, -0.3) # Remove the "spines" (the lines surrounding the subplot) # including the left spine for the 2nd and 3rd subplots sns.despine(ax=axes[0], left=True, bottom=True) sns.despine(ax=axes[1], left=True, bottom=True) sns.despine(ax=axes[2], left=True) text = ['-20%', '', '-10%', '', '0%', '', '10%', '', '20%'] pos_text = ['20%', '', '10%', '', '0%', '', '10%', '', '20%'] pos = [-0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4] """ axes[2].set_xticks([p + .02 for p, t in zip(pos, pos_text)]) """ axes[2].set_xticklabels(text) # These are the labels of each subplot labels = ["", "", ""] # Iterate over each subplot and set the labels for i, ax in enumerate(axes): """ # Set the x-axis ticklabels ax.set_xticks([-.3, -.1, .1, .3]) ax.set_xticklabels(['GLM', 'GLMs', 'Spatio-\ntemporal\nSVM', 'Logistic\ndeconvo-\nlution']) """ # Set the label for each subplot ax.set_ylabel(labels[i]) # Remove the y-axis label and title ax.set_xlabel("") ax.set_title("") # axes.flat[0].set_ylabel("accuracy") fig = plt.gcf() fig.set_size_inches(6, 6) plt.tight_layout() plt.savefig('all_boxplot.png') plt.show()
import seaborn as sns import matplotlib.pyplot as plt import numpy as np #DADOS USADOS tips = sns.load_dataset("tips") flights_long = sns.load_dataset("flights") #CONFIGURACOES sns.set(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1, color_codes=False) '''(size das labels/lines/outros elementos, cor dos eixos, estilo da cor dos retangulos, fonte, size fonte, ativa o palette) ex:(notebook/paper/poster, white/dark/ticks/whitegrid, muted/bright/pastel/dark/colorblind) {antes do grafico ser criado}''' sns.despine(top=False, right=False, left=False, bottom=False, offset=None, trim=False) #padrao '''(top-right-left-bottom: if true remove the line, move os retangulos dos eixos, if true tira os eixos) {depois do grafico ser criado}''' #FACTORPLOT #DISTPLOT #BOXPLOT sns.boxplot(x="day", y="total_bill", hue="smoker", palette=["m", "g"], data=tips) '''(nome col no x, nome col no y, variaveis q o boxplot usa, cor(entre chaves se for + de 1) , dados)''' plt.show() #BARPLOT x = np.array(list("ABCDEFGHIJ")) y1 = np.arange(1, 11) - 5.5 sns.barplot(x=x, y=y1, palette="vlag") '''(eixo x, eixo y, cor)'''
def plot_Residue_MMS_2D(df, opt1, opt2): # .plot plots the index against every column import matplotlib.pyplot as plt print('> Dataframe\n{}\n'.format(df.head(10))) if opt1 == '월별' and opt2 == 'plt': # (53-1) plot :: 월별 > 요일별 플롯 print('> Dataframe for plot\n{}\n'.format(df.head(20))) fig, (axes) = plt.subplots(nrows=3, ncols=4, figsize=(10, 8), sharex=True, sharey=True) ## (여기부터 수정) df.loc[pd.IndexSlice[1, :], :].plot(ax=axes[0, 0], title='1월', lw=2) df.loc[pd.IndexSlice[2, :], :].plot(ax=axes[0, 1], title='2월', lw=2) df.loc[pd.IndexSlice[3, :], :].plot(ax=axes[0, 2], title='3월', lw=2) df.loc[pd.IndexSlice[4, :], :].plot(ax=axes[0, 3], title='4월', lw=2) df.loc[pd.IndexSlice[5, :], :].plot(ax=axes[1, 0], title='5월', lw=2) df.loc[pd.IndexSlice[6, :], :].plot(ax=axes[1, 1], title='6월', lw=2) df.loc[pd.IndexSlice[7, :], :].plot(ax=axes[1, 2], title='7월', lw=2) df.loc[pd.IndexSlice[8, :], :].plot(ax=axes[1, 3], title='8월', lw=2) df.loc[pd.IndexSlice[9, :], :].plot(ax=axes[2, 0], title='9월', lw=2) df.loc[pd.IndexSlice[10, :], :].plot(ax=axes[2, 1], title='10월', lw=2) df.loc[pd.IndexSlice[11, :], :].plot(ax=axes[2, 2], title='11월', lw=2) df.loc[pd.IndexSlice[12, :], :].plot(ax=axes[2, 3], title='12월', lw=2) plt.xticks([0, 1, 2, 3, 4, 5, 6], ['월', '화', '수', '목', '금', '토', '일']) # fig.autofmt_xdate(rotation=60) # x축 글자 각도 조정 _ = plt.legend(loc='upper left') _ = plt.tight_layout() _ = plt.show() # _ = fig.savefig('./output.png') elif opt1 == '요일별' and opt2 == 'plt': # (53-2) plot :: 요일별 > 월별 print('> dataframe for plot\n{}\n'.format(df.head(20))) fig, (axes) = plt.subplots(nrows=2, ncols=4, figsize=(10, 8), sharex=True, sharey=True) df.loc[pd.IndexSlice[:, 0], :].plot(ax=axes[0, 0], title='월요일', lw=2) df.loc[pd.IndexSlice[:, 1], :].plot(ax=axes[0, 1], title='화요일', lw=2) df.loc[pd.IndexSlice[:, 2], :].plot(ax=axes[0, 2], title='수요일', lw=2) df.loc[pd.IndexSlice[:, 3], :].plot(ax=axes[0, 3], title='목요일', lw=2) df.loc[pd.IndexSlice[:, 4], :].plot(ax=axes[1, 0], title='금요일', lw=2) df.loc[pd.IndexSlice[:, 5], :].plot(ax=axes[1, 1], title='토요일', lw=2) df.loc[pd.IndexSlice[:, 6], :].plot(ax=axes[1, 2], title='일요일', lw=2) plt.xticks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [ '1월', '2월', '3월', '4월', '5월', '6월', '7월', '8월', '9월', '10월', '11월', '12월' ]) # fig.autofmt_xdate(rotation=60) # x축 글자 각도 조정 _ = plt.legend(loc='upper left') _ = plt.tight_layout() _ = plt.show() # _ = fig.savefig('./output.png') elif opt == 'sns': # (53-3) plot using Seaborn :: (잘됨) import matplotlib.pyplot as plt import seaborn as sns sns.set(style='whitegrid') fig, (ax1) = plt.subplots(nrows=1, ncols=1, figsize=(10, 8), sharex=True, sharey=True) # ax = sns.violinplot(data=df[opt1[1]], palette='Set3') # 월별 평균값 # ax = sns.boxplot(data=df[opt1[0]], palette='Set3') # 월별 최댓값 # ax = sns.boxplot(by=ddd, data=df[opt1[0]], palette='Set3') # 요일별 평균값 sns.despine(left=True, bottom=True) _ = plt.legend(loc='best') _ = plt.tight_layout() _ = plt.show() # _ = fig.savefig('./output.png') else: print('> !!!ERROR: Check Your Option!!!') return ()
linewidth=1) # ax = sns.stripplot(data=alldata, x="organism", y="dnds", hue="type", palette=palette, # dodge=True, size=3, linewidth=0.5, alpha=0.3) # https://stackoverflow.com/questions/58476654/how-to-remove-or-hide-x-axis-label-from-seaborn-boxplot # plt.xlabel(None) will remove the Label, but not the ticks. ax.set(xlabel=None) # plt.xlabel("Organism") # for tick in ax.get_xticklabels() : # tick.set_rotation(30) plt.ylabel("dN/dS") plt.ylim(0, 0.5) plt.xticks(rotation=30, ha='right') plt.yticks([0, 0.1, 0.2, 0.3, 0.4, 0.5]) # # ax.legend(ax.get_legend_handles_labels()[0], ["E", "NE"]) handles, labels = ax.get_legend_handles_labels() # # specify just one legend l = plt.legend(handles[0:2], labels[0:2], loc=0, fontsize=8) sns.despine(top=True, right=True) plt.savefig("../complementaryData/figure/dnds_boxplot_italic.pdf", dpi=400, bbox_inches='tight')
sns.set(style="white", context="talk") rs = np.random.RandomState(8) # Set up the matplotlib figure f, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(7, 5), sharex=True) # Generate some sequential data x = np.array(list("ABCDEFGHIJ")) y1 = np.arange(1, 11) sns.barplot(x=x, y=y1, palette="rocket", ax=ax1) ax1.axhline(0, color="k", clip_on=False) ax1.set_ylabel("Sequential") # Center the data to make it diverging y2 = y1 - 5.5 sns.barplot(x=x, y=y2, palette="vlag", ax=ax2) ax2.axhline(0, color="k", clip_on=False) ax2.set_ylabel("Diverging") # Randomly reorder the data to make it qualitative y3 = rs.choice(y1, len(y1), replace=False) sns.barplot(x=x, y=y3, palette="deep", ax=ax3) ax3.axhline(0, color="k", clip_on=False) ax3.set_ylabel("Qualitative") # Finalize the plot sns.despine(bottom=True) plt.setp(f.axes, yticks=[]) plt.tight_layout(h_pad=2) plt.savefig('output.png')
def gauthier_barplotv2(): sns.set(font='serif') sns.set_style("white", { "font.family": "serif", "font.serif": ["Times", "Palatino", "serif"] }) sns.set_context('paper', font_scale=1.5) # Load data data = pickle.load(open('../scripts/all_gauthier_separate.pickle', 'rb')) # Colors cmap = sns.color_palette("Set2", n_colors=4) # Specify that I want each subplot to correspond to # a different robot type g = sns.FacetGrid( data, col="isi", col_order=[1.6, 3.2, 4.8], sharex=False) # Create the bar plot on each subplot g.map( sns.barplot, "isi", "accuracy", "model", hue_order=['GLM', 'GLMs', 'spatiotemporal SVM', 'logistic deconvolution'], palette=cmap) # Now I need to draw the 50% lines on each subplot # separately axes = np.array(g.axes.flat) for ax in axes: ax.set_ylim(0.45, 0.75) ax.hlines(0.5, -0.5, 0.5, linestyle='--', linewidth=1) # Remove the "spines" (the lines surrounding the subplot) # including the left spine for the 2nd and 3rd subplots sns.despine(ax=axes[1], left=True) sns.despine(ax=axes[2], left=True) # These are the labels of each subplot labels = ["1.6s", "3.2s", "4.8s"] # Iterate over each subplot and set the labels for i, ax in enumerate(axes): # Set the x-axis ticklabels """ ax.set_xticks([-.3, -.1, .1, .3]) ax.set_xticklabels(['GLM', 'GLMs', 'Spatio-\ntemporal\nSVM', 'Logistic\ndeconvo-\nlution']) """ ax.set_xticks([-.3, -.1, .1, .3]) ax.set_xticklabels(['', '', '', '']) # Set the label for each subplot ax.set_xlabel(labels[i]) # Remove the y-axis label and title ax.set_ylabel("") ax.set_title("") axes.flat[0].set_ylabel("accuracy") axes.flat[0].set_yticklabels(['', '', '0.5', '', '0.6', '', '0.7']) fig = plt.gcf() fig.set_size_inches(6, 2) plt.tight_layout() plt.savefig('gauthier_barplot.png') plt.show()
def compare_cellmap_method(method1, method1_df, method2, method2_df, celltypes, color_map, outdir, extension): nplots = len(method1_df.index) ncols = 1 nrows = math.ceil(nplots / ncols) method1_name = method1[0].split("_")[0] method2_name = method2[0].split("_")[0] sns.set(rc={'figure.figsize': (12 * ncols, 9 * nrows)}) sns.set_style("ticks") fig = plt.figure() grid = fig.add_gridspec(ncols=ncols, nrows=nrows) row_index = 0 col_index = 0 for celltype in celltypes: method_celltype = celltype if method_celltype == "Microglia": method_celltype = "Macrophage" # Get the data. method1_data = method1_df.loc[method1[0] + method_celltype + method1[1], :] method2_data = method2_df.loc[method2[0] + method_celltype + method2[1], :] df = pd.DataFrame({ method1_name: method1_data, method2_name: method2_data }) df.dropna(inplace=True) # Get the color. color = color_map[celltype] # Creat the subplot. ax = fig.add_subplot(grid[row_index, col_index]) sns.despine(fig=fig, ax=ax) coef_str = "NA" p_str = "NA" if len(df.index) > 1: # Calculate the correlation. coef, p = stats.spearmanr(df[method1_name], df[method2_name]) coef_str = "{:.2f}".format(coef) # p_str = p_value_to_symbol(p) p_str = "p = {:.2e}".format(p) # Plot. g = sns.regplot( x=method1_name, y=method2_name, data=df, scatter_kws={ 'facecolors': '#000000', # 'edgecolor': '#000000', 'linewidth': 0, 'alpha': 0.5 }, line_kws={"color": color}, ax=ax) # Add the text. ax.annotate('r = {} [{}]'.format(coef_str, p_str), xy=(0.03, 0.94), xycoords=ax.transAxes, color=color, fontsize=18, fontweight='bold') ax.text(0.5, 1.05, method_celltype, fontsize=26, weight='bold', ha='center', va='bottom', color=color, transform=ax.transAxes) ax.set_ylabel((method1[0] + method1[1]).replace("_", " "), fontsize=18, fontweight='bold') ax.set_xlabel((method2[0] + method2[1]).replace("_", " "), fontsize=18, fontweight='bold') ax.axhline(0, ls='--', color="#D7191C", alpha=0.3, zorder=-1) ax.axvline(0, ls='--', color="#D7191C", alpha=0.3, zorder=-1) # Increment indices. col_index += 1 if col_index == ncols: col_index = 0 row_index += 1 # Safe the plot. plt.tight_layout() fig.savefig( os.path.join( outdir, "{}_vs_{}.{}".format( method1_name.split("_")[0], method2_name.split("_")[0], extension))) plt.close()
def plot_Residue_MMS_3D(df, opt1, opt2): # .plot plots the index against every column import matplotlib.pyplot as plt print('> Dataframe\n{}\n'.format(df.head(10))) if opt1 == '월별' and opt2 == 'plt': # (52-1) plot :: 월별 > 요일별 플롯 df = df.unstack(level=0) # [level=0: 요일별] df.columns = df.columns.droplevel(level=0) df = df.reset_index() print('> Dataframe for plot\n{}\n'.format(df.head(10))) fig, (axes) = plt.subplots(nrows=3, ncols=4, figsize=(10, 8), sharex=True, sharey=True) df.pivot(index='시간대', columns='요일', values=1).plot(ax=axes[0, 0], title='1월', lw=2) df.pivot(index='시간대', columns='요일', values=2).plot(ax=axes[0, 1], title='2월', lw=2) df.pivot(index='시간대', columns='요일', values=3).plot(ax=axes[0, 2], title='3월', lw=2) df.pivot(index='시간대', columns='요일', values=4).plot(ax=axes[0, 3], title='4월', lw=2) df.pivot(index='시간대', columns='요일', values=5).plot(ax=axes[1, 0], title='5월', lw=2) df.pivot(index='시간대', columns='요일', values=6).plot(ax=axes[1, 1], title='6월', lw=2) df.pivot(index='시간대', columns='요일', values=7).plot(ax=axes[1, 2], title='7월', lw=2) df.pivot(index='시간대', columns='요일', values=8).plot(ax=axes[1, 3], title='8월', lw=2) df.pivot(index='시간대', columns='요일', values=9).plot(ax=axes[2, 0], title='9월', lw=2) df.pivot(index='시간대', columns='요일', values=10).plot(ax=axes[2, 1], title='10월', lw=2) df.pivot(index='시간대', columns='요일', values=11).plot(ax=axes[2, 2], title='11월', lw=2) df.pivot(index='시간대', columns='요일', values=12).plot(ax=axes[2, 3], title='12월', lw=2) plt.xticks([ datetime.time(0, 0, 0), datetime.time(2, 0, 0), datetime.time(4, 0, 0), datetime.time(6, 0, 0), datetime.time(8, 0, 0), datetime.time(10, 0, 0), datetime.time(12, 0, 0), datetime.time(14, 0, 0), datetime.time(16, 0, 0), datetime.time(18, 0, 0), datetime.time(20, 0, 0), datetime.time(22, 0, 0) ]) # plt.xticks([0,1,2,3,4,5,6], ['월','화','수','목','금','토','일']) # 당연히 샘플임 # axes[0,0].set_xticks(df.index.get_level_values(level=1)) # axes[0,0].set_xticklabels(df.index.get_level_values(level=1)) # fig.autofmt_xdate(rotation=60) # x축 글자 각도 조정 plt.suptitle('2018년도 외곽(지하)주차장 통계') plt.legend(loc='upper left') plt.tight_layout() plt.show() # _ = fig.savefig('./output.png') elif opt1 == '요일별' and opt2 == 'plt': # (52-2) plot :: 요일별 > 월별 플롯 df = df.unstack(level=1) # [level=1: 월별] df.columns = df.columns.droplevel(level=0) df = df.reset_index() print('> dataframe for plot\n{}\n'.format(df.head(10))) fig, (axes) = plt.subplots(nrows=2, ncols=4, figsize=(10, 8), sharex=True, sharey=True) df.pivot(index='시간대', columns='월', values=0).plot(ax=axes[0, 0], title='월요일', lw=2) df.pivot(index='시간대', columns='월', values=1).plot(ax=axes[0, 1], title='화요일', lw=2) df.pivot(index='시간대', columns='월', values=2).plot(ax=axes[0, 2], title='수요일', lw=2) df.pivot(index='시간대', columns='월', values=3).plot(ax=axes[0, 3], title='목요일', lw=2) df.pivot(index='시간대', columns='월', values=4).plot(ax=axes[1, 0], title='금요일', lw=2) df.pivot(index='시간대', columns='월', values=5).plot(ax=axes[1, 1], title='토요일', lw=2) df.pivot(index='시간대', columns='월', values=6).plot(ax=axes[1, 2], title='일요일', lw=2) plt.xticks([ datetime.time(0, 0, 0), datetime.time(2, 0, 0), datetime.time(4, 0, 0), datetime.time(6, 0, 0), datetime.time(8, 0, 0), datetime.time(10, 0, 0), datetime.time(12, 0, 0), datetime.time(14, 0, 0), datetime.time(16, 0, 0), datetime.time(18, 0, 0), datetime.time(20, 0, 0), datetime.time(22, 0, 0) ]) plt.suptitle('2018년도 외곽(지하)주차장 통계') plt.legend(loc='upper left') plt.tight_layout() plt.show() # _ = fig.savefig('./output.png') elif opt2 == 'sns': # (52-3) plot using Seaborn :: (잘됨) import matplotlib.pyplot as plt import seaborn as sns sns.set(style='whitegrid') fig, (ax1) = plt.subplots(nrows=1, ncols=1, figsize=(10, 8), sharex=True, sharey=True) # ax = sns.violinplot(data=df[opt1[1]], palette='Set3') # 월별 평균값 ax = sns.boxplot(data=df[opt1[0]], palette='Set3') # 월별 최댓값 # ax = sns.boxplot(by=ddd, data=df[opt1[0]], palette='Set3') # 요일별 평균값 sns.despine(left=True, bottom=True) plt.legend(loc='best') plt.tight_layout() plt.show() # fig.savefig('./output.png') else: print('> !!!ERROR: Check Your Option!!!') return ()