def make_2D_KDE(self): """ Makes a 2 dimensional probability density plot of the outliers. Each dimension has a probability density histogram created and placed at the top of each axis. """ from matplotlib.mlab import griddata import matplotlib.pyplot as plt import seaborn as sns pal = sns.dark_palette(self.color_palette) self.logger.info("Generating 2D KDE plot...") ind = range(len(self.outliers)) rows = list(set(np.random.choice(ind,self.reduce_size))) self.logger.info("Using a smaller size for space ({0} objects)".format(self.reduce_size)) outliers = self.outliers[rows] measured = self.measured[rows] predicted = self.predicted[rows] xmin, xmax = 0, self.data_frame["redshift"].max() ymin, ymax = 0, xmax x_straight = np.arange(xmin, xmax+0.1, 0.1) plt.figure() pal = sns.dark_palette(self.color_palette, as_cmap=True) g = sns.JointGrid(measured, predicted, size=10, space=0) g.plot_marginals(sns.distplot, kde=True, color=self.color_palette) g.plot_joint(plt.scatter, color="silver", edgecolor="white") g.plot_joint(sns.kdeplot, kind="hex", color=self.color_palette, cmap=pal) g.ax_joint.set(xlim=[xmin, xmax], ylim=[ymin, ymax]) g.set_axis_labels(xlabel=r"$z_{\rm spec}$", ylabel=r"$z_{\rm phot}$") g.ax_joint.errorbar(x_straight, x_straight, lw=2) # Temp solution # http://stackoverflow.com/questions/21913671/subplots-adjust-with-seaborn-regplot axj, axx, axy = plt.gcf().axes axj.set_position([.15, .12, .7, .7]) axx.set_position([.15, .85, .7, .13]) axy.set_position([.88, .12, .13, .7]) for item in ([axj.xaxis.label, axj.yaxis.label]): item.set_fontsize(self.fontsize+10) for item in (axj.get_xticklabels() + axj.get_yticklabels()): item.set_fontsize(self.fontsize) self.kde_2d_ax = [axj, axx, axy] plt.savefig("PHOTZ_KDE_2D_{0}.pdf".format(self.family_name), format="pdf")
def age_histogram(df_age): age_counts = df_age.groupby('age').age.count() y = age_counts.values x = [int(age) for age in age_counts.index] f, ax = plt.subplots(1,1, figsize=(12,8)) sns.barplot(x,y, palette=sns.dark_palette('#008080', reverse=True, n_colors=60), linewidth=0) ax.set_ylabel('Postings') ax.set_xlabel('') ax.set_title('Histogram of Postings by Age') x_ticks = [0] x_ticks.extend(range(2,95, 5)) x_ticklabels = [''] x_ticklabels.extend(range(20,95,5)) ax.set_xticks(x_ticks) ax.set_xticklabels(x_ticklabels) #need to fix xlabels sns.despine(bottom=True, right=True) sns.plt.xlim(-1, 90) for i,p in enumerate(ax.patches): height = p.get_height() if ((i+18) % 5 == 0) and (i+18 < 70): ax.text(p.get_x()-1, height + 4, i+18, fontsize=18) plt.show()
def cmap_from_color(color, dark=False): ''' Generates a matplotlib colormap from a single color. Colormap will be built, by default, from white to ``color``. Args: color: Can be one of several things: 1. Hex code 2. HTML color name 3. RGB tuple dark (bool): If ``True``, colormap will be built from ``color`` to black. Default is ``False``, which builds a colormap from white to ``color``. Returns: colormap: A matplotlib colormap ''' if dark: return sns.dark_palette(color, as_cmap=True) else: return sns.light_palette(color, as_cmap=True)
def md_pressure(cutoff=25): """ current dirc の md 計算の圧力を出力する """ path = "." dir_list = [os.path.dirname(x) for x in glob.glob(path)] average = [] sb_dark = seaborn.dark_palette("skyblue", 10, reverse=True) # sb_dark = seaborn.dark_palette("seagreen", 10, reverse=True) # sb_dark = seaborn.dark_palette("purple", 10, reverse=True) seaborn.set(palette=sb_dark) fig, axes = pylab.subplots(2, 1, figsize=(5, 10)) for dirc in dir_list: with open(os.path.join(dirc, 'OUTCAR')) as wfile: lines = wfile.readlines() meta = re.compile(r".*external pressure = \s*(.*)\s+kB.+") pressure = [] for line in lines: match = meta.match(line) if match: pressure.append(float(match.group(1))) with open(os.path.join(dirc, 'OSZICAR')) as wfile: lines = wfile.readlines() meta = re.compile(r".+T=\s*(.*)\s+E=\s*(.*)\s+F=.+") energy = [] for line in lines: match = meta.match(line) if match: energy.append(float(match.group(2))) data = [{'energy':e, 'pressure':p} for e, p in zip(energy, pressure)] dbox = DataBox(data) # volume = float(dirc.split('_')[1]) volume = 1.0 press_ave = dbox['pressure'][cutoff:].mean() energy_ave = dbox['energy'][cutoff:].mean() average.append({'volume':volume, 'p': press_ave, 'e': energy_ave}) label = "V={0:.0f}".format(1.0) seaborn.distplot(dbox['pressure'][cutoff:], label=label, ax=axes[0]) axes[0].legend(loc='upper left') axes[0].set_ylabel("Frequency") axes[0].set_xlabel("Pressuer (kbar)") average_data = DataBox(average) # sb_dark = seaborn.dark_palette("seagreen", 10, reverse=True) # seaborn.set(palette=sb_dark) axes[1].plot(average_data['volume'], average_data['p'], 'p', color=sb_dark[-1]) # axes[2].plot(average_data['volume'], average_data['e'], 'p') axes[1].set_ylabel(r"Pressuer (kbar)") axes[1].set_xlabel(r"Volume ($\AA^3$)") # pylab.xticks(fontsize=14) # pylab.yticks(fontsize=14) fig.subplots_adjust(left=0.20) pylab.savefig("pressure.eps")
def corr(self): """ Returns a correlation matrix of the all the instruments with trading rules applied, with returns bagged by week. Not used for trading. Intended to be used in Jupyter. """ df = self.curve().returns().resample('W').sum().replace(0, np.nan).corr() cm = seaborn.dark_palette("green", as_cmap=True) s = df.style.background_gradient(cmap=cm) return s
def corr_pp(self): """ Returns a correlation matrix of all the instruments panama prices, with returns bagged by week. Not used for trading. Intended to be used in Jupyter. """ df = self.panama_prices().diff().resample('W').sum().corr() cm = seaborn.dark_palette("green", as_cmap=True) s = df.style.background_gradient(cmap=cm) return s
def plot_init(self): nx = self.nx self.nodeplot = nx.draw_networkx_nodes( self.G, pos=self.pos, node_color=self.prices.values[0], nodelist=self.nodelist, node_size=30, zorder=10, cmap=plt.cm.RdYlGn_r, vmin=defaults.MINPRICE, vmax=defaults.MAXPRICE) darkpal = sns.dark_palette(sns.xkcd_rgb['blood red'], as_cmap=True) self.edgeplot = nx.draw_networkx_edges( self.G, pos=self.pos, # edge_color=sns.xkcd_rgb['dark grey'], edge_color=self.relflows.values[0], edge_cmap=darkpal, edgelist=self.edgelist, zorder=9, width=2.0, vmin=0, vmax=1)
def plot_agent_power(x,time=None): import seaborn as sns if isinstance(x,np.ndarray): P_all=x else: P_all,time = collect_power(x) P_all = downsample(P_all) time = downsample(time) sns.tsplot(P_all,time=time, err_style="unit_traces", err_palette=sns.dark_palette("crimson", len(P_all)), color="k"); plt.xlabel("steps") plt.ylabel("P") return P_all
def post_length_by_category(df): sns.set_context('talk', font_scale=1.5) df['post_length'] = df['total_text'].apply(len) cat_means = df.groupby('category_code').post_length.mean() y = cat_means.values x = cat_means.index f, ax = plt.subplots(1,1, figsize=(10,8)) ax.set_ylabel('Post Length (characters)') sns.barplot(x,y,palette=sns.dark_palette('#008080', reverse=True, n_colors=10), linewidth=0) ax.set_xlabel('') ax.set_title('Average Post Length by Category') for item in ax.get_xticklabels(): item.set_rotation(15) #sns.plt.ylim(350, 700) sns.despine(bottom=True, right=True, trim=True) plt.show()
def plot_contour(results, get_x_function, get_y_function, filter_function, title, x_label, y_label, filename, include_output): # Create single-axis figure figure, axis = pylab.subplots(figsize=(plot_settings.column_width, 3), frameon=False) # Generate x, y, z data if include_output: filtered_results = [[get_x_function(r), get_y_function(r), float(r[6])] for r in results if filter_function(r)] else: filtered_results = [[get_x_function(r), get_y_function(r), float(r[6]) - float(r[9])] for r in results if filter_function(r)] # Sort tuples and unzip into stackplot friendly form filtered_results.sort() unzipped_results = zip(*filtered_results) xi = numpy.linspace(min(unzipped_results[0]), max(unzipped_results[0])) yi = numpy.linspace(min(unzipped_results[1]), max(unzipped_results[1])) zi = griddata((unzipped_results[0], unzipped_results[1]), unzipped_results[2], (xi[None,:], yi[:,None]), method='cubic') contours = axis.contour(xi, yi, zi, cmap=sns.dark_palette('gray', as_cmap=True, n_colors=4, reverse=True), levels=[0.2, 0.4, 0.6, 0.8]) manual = [(8.0, 175), (2.5, 740), (4.0, 960), (6, 1140)] axis.clabel(contours, fmt=lambda n: '{:.0f}%'.format(n*100), manual=manual) if title is not None: axis.set_title(title) axis.set_xlabel(x_label) axis.set_ylabel(y_label) # Use log2 scale for dimensions axis.set_xscale('log', basex=2) # Use dimensions of data points for x ticks axis.set_xticks(unzipped_results[0]) axis.set_xticklabels(unzipped_results[0]) axis.set_yticks([0, 250, 500, 750, 1000, 1250, 1500, 1750, 2000]) # Save figure figure.tight_layout(pad=0.0) figure.savefig(filename)
def num_images_by_age(df_age): sns.set_context('talk', font_scale=1.5) df_age_images = df_age.ix[df_age['age']<66, :] age_means = df_age_images.groupby('age').num_images.mean() age_bins = [18] age_bins.extend(list(range(25,66,5))) bin_labels = ['18-24'] bin_labels.extend(["{} - {}".format(i, i+4) for i in range(25,61,5)]) df_age_images['hist_age_group'] = pd.cut(df_age_images.ix[:,'age'], age_bins, right = False, labels= bin_labels) age_means = df_age_images.groupby('hist_age_group').num_images.mean() y = age_means.values x = age_means.index f, ax = plt.subplots(1,1, figsize=(16,8)) ax.set_ylabel('Average Number of Images') sns.barplot(x,y, palette=sns.dark_palette('#008080', reverse=True, n_colors=10), linewidth=0) ax.set_xlabel('') ax.set_title('Average Number of Images by Age') for item in ax.get_xticklabels(): item.set_rotation(15) sns.plt.ylim(0.0,0.75) sns.despine(bottom=True, right=True) plt.show()
def post_length_by_age(df_age): sns.set_context('talk', font_scale=1.5) df_age['post_length'] = df_age['total_text'].apply(len) df_age_length = df_age.ix[df_age['age']<66, :] age_means = df_age_length.groupby('age').post_length.mean() age_bins = [18] age_bins.extend(list(range(25,66,5))) bin_labels = ['18-24'] bin_labels.extend(["{} - {}".format(i, i+4) for i in range(25,61,5)]) df_age_length['hist_age_group'] = pd.cut(df_age_length.ix[:,'age'], age_bins, right = False, labels= bin_labels) age_means = df_age_length.groupby('hist_age_group').post_length.mean() y = age_means.values x = age_means.index f, ax = plt.subplots(1,1, figsize=(16,8)) ax.set_ylabel('Post Length (characters)') sns.barplot(x,y,palette=sns.dark_palette('#008080', reverse=True, n_colors=10), linewidth=0) ax.set_xlabel('') ax.set_title('Average Post Length by Age') for item in ax.get_xticklabels(): item.set_rotation(15) sns.plt.ylim(0, 650) sns.despine(bottom=True, right=True, trim=True) plt.show()
def plot_heatmap(df, title, file_name, vmax=1.5, vmin=-1.5, cmap=sns.diverging_palette(220, 20, n=7, as_cmap=True), figsize=(16, 8), dpi=300): sns.set_style("white") fig = plt.figure(figsize=figsize) df = df.reindex(['G', 'A', 'V', 'I', 'L', 'M', 'F', 'Y', 'W', 'C', 'S', 'T', 'P', 'D', 'E', 'N', 'Q', 'H', 'K', 'R', '*']) df.index.values[-1:] = ['STOP'] if 77 in df.columns: df = df.loc[:, :76] if 0 in df.columns: df.drop(0, axis=1, inplace=True) sns.heatmap(df, square=True, linewidths=0.25, xticklabels=2, cmap=cmap, vmin=vmin, vmax=vmax) yeast_ubq = np.array(utils.canonical_yeast_ubq) mask = df.apply(lambda x: x.index.isin(np.where(yeast_ubq == x.name)[0]+1), axis=1) df.loc[:, :] = 1 sns.heatmap(df, mask=~mask, cmap=sns.dark_palette("grey", n_colors=1, as_cmap=True, reverse=True), xticklabels=2, cbar=False, square=True, alpha=0.6) plt.yticks(rotation=0) plt.title(title, fontsize=16) pos = fig.axes[1].get_position() fig.axes[1].set_position([pos.x0, 0.4, 0.1, 0.2]) sns.axlabel("Ub position", "Amino acid") if file_name is not None: plt.savefig(file_name, dpi=dpi, bbox_inches='tight', pad_inches=0) return fig
def plot_word_cloud_single(output_dir, grades, index, color=None): import seaborn as sns if color is not None: colormap = sns.dark_palette(color, as_cmap=True) else: colormap = None # Scrap non-interesting contrasts contrasts = list(filter( lambda x: 'effects_of_interest' not in x and 'gauthier' not in x, grades))[:15] frequencies_cat = defaultdict(lambda: 0.) frequencies_single = defaultdict(lambda: 0.) occurences = defaultdict(lambda: 0.) for contrast in contrasts: grade = grades[contrast] study, contrast = contrast.split('::') contrast = contrast.replace('_', ' ').replace('&', ' ').replace('-', ' ') terms = contrast.split(' ') cat_terms = [] for term in terms: if term == 'baseline': break if term in ['vs']: break cat_terms.append(term) for term in cat_terms: frequencies_single[term] += grade occurences[term] += 1 cat_terms = ' '.join(cat_terms) frequencies_cat[cat_terms] += grade frequencies_single = {term: freq / math.sqrt(occurences[term]) for term, freq in frequencies_single.items()} width, height = (900, 450) wc = WordCloud(prefer_horizontal=1, background_color="rgba(255, 255, 255, 0)", width=width, height=height, colormap=colormap, relative_scaling=0.7) wc.generate_from_frequencies(frequencies=frequencies_single, ) wc.to_file(join(output_dir, 'wc_single_%i.png' % index)) width, height = (900, 300) wc = WordCloud(prefer_horizontal=1, background_color="rgba(255, 255, 255, 0)", width=width, height=height, mode='RGBA', colormap=colormap, relative_scaling=0.8) wc.generate_from_frequencies(frequencies=frequencies_cat, ) wc.to_file(join(output_dir, 'wc_cat_%i.png' % index)) width, height = (1200, 300) wc = WordCloud(prefer_horizontal=1, background_color="rgba(255, 255, 255, 0)", width=width, height=height, mode='RGBA', colormap=colormap, relative_scaling=0.8) wc.generate_from_frequencies(frequencies=frequencies_cat, ) wc.to_file(join(output_dir, 'wc_cat_%i_wider.png' % index))
def print_nice_scatterplot(data: pd.DataFrame, graph_filename: str, col_examined: str, col_grouped_by: str, col_related: str, title: str, legend_title: str, x_title: str, y_title: str, max_val=None, min_val=None): # preset configuration scale = 2 max_marker_size = 1000 * scale min_marker_size = 1 * scale if max_val is None: max_val = data[col_examined].max() if min_val is None: min_val = data[col_examined].min() tick = (max_val - min_val) / 40 y_ticks = np.concatenate([ np.arange(0, min_val - tick, -tick)[::-1], np.arange(0, max_val, tick) ]) # Scatterplot create figure fig = plt.figure(figsize=(8 * scale, 60 * scale)) # Create an axes instance ax1 = fig.add_subplot(111) ax1.set_title(title, fontsize=25 * scale) ax1.set_xlabel(x_title, fontsize=25 * scale) ax1.set_ylabel(y_title, rotation=90, fontsize=25 * scale) # this sorts times and labels for display in the boxplot by the parameters of the boxplots #data_to_plot_arr, labels = zip(*sorted(zip(data_to_plot_arr,labels), key=lambda e: e[1] )) groups = [] # get the dataframes with their group names into list for group, group_df in data.groupby(col_grouped_by): groups.append((group, group_df)) # sort the list by the parameter so we can apply reasonable coloring groups = sorted(groups, key=lambda x: x[0]) current_size = max_marker_size # use seaborn to generate list of enough colors from a color pallete - it is graded colors = sns.color_palette(sns.dark_palette('cyan', n_colors=len(groups)), n_colors=len(groups)) for group, group_df in groups: # Create the scatterplot ax1.scatter(x=group_df[col_related], y=group_df[col_examined], label=str(group) + ' % ', color=colors.pop(), s=current_size) current_size -= (max_marker_size - min_marker_size) / len(groups) #ax1.set_xticklabels(['1', '2', '5', '10', '50', '100', '500', '200']) ax1.set_yticks(y_ticks) ax1.tick_params(axis='x', labelsize=22 * scale) ax1.tick_params(axis='y', labelsize=22 * scale) #ax1.grid(True) legend = plt.legend(loc="upper center", title=legend_title, ncol=2, prop={'size': 16 * scale}) legend.get_title().set_fontsize(22 * scale) fig.savefig(graph_filename, bbox_inches="tight")
sns.palplot( sns.cubehelix_palette(8, start=2, rot=0, dark=0, light=.95, reverse=True)) #n_colors ->颜色个数 #start ->值区间在0-3,开始颜色 #rot ->颜色旋转角度 #gamma ->颜色伽马值,越大颜色越暗 #dark,light ->值区间0-1,颜色深浅 #reverse ->布尔值,默认值False,由浅到深 #4.dark_palette(color[,n_colors,reverse,...])/light_palette(color[,n_colors,reverse,...]) #颜色深浅 sns.palplot(sns.light_palette("green")) #按照green做浅色调色盘 #sns.palplot(sns.light_palette("Greens")) #cmap为Greens风格 sns.palplot(sns.dark_palette("red", reverse=True)) #按照green做浅色调色盘 #reverse ->转置颜色 #5.diverging_palette() #创建分散颜色 #seaborn.diverging_palette(h_neg, h_pos, s=75, l=50, sep=10, n=6, center='light', as_cmap=False) sns.palplot(sns.diverging_palette(145, 280, s=85, l=25, n=7)) #h_neg, h_pos:float in [0, 359] # ->起始/终止颜色值。图的正负范围的锚定色调 #s -> 值区间0-100,饱和度 #l -> 值区间0-100,亮度 #n -> 颜色个数 #center ->中心颜色为浅色还是绿色"light","dark",默认为light plt.figure(figsize=(8, 6))
def plot_metallicity_radius(append_profiles = False, output = 3035, append_observations = False, use_average_profile = False, \ weight_field = None, data_cut = None, profile = False, use_rvir = True): # metallicity vs spherical radius plot xfield = 'spherical_position_radius' yfield = 'metallicity' zfield = 'mass' # zfield = 'xray_emissivity' xlabel = '$\mathrm{Radius\ (kpc)}$' if use_rvir: xlabel = '$\mathrm{R\ /\ R}_{200}$' ylabel = '$\mathrm{Metallicity}\ (Z_{\odot})$' cbar_label = '$\mathrm{Relative\ Frequency}$' xlim = (0, 3000) xlim = (0, 3) ylim = (5e-4, 6) zlim = (3e-6, 1e-3) # zlim = (1e-10, 1e-3) cmap = 'binary' nbins = 40 profile_color = matplotlib.cm.get_cmap(cmap)(0.75) xscale = 'linear' profile_label = 'All Mass' fig, ax, im, cbar = ipd.plot_phase(xfield, yfield, zfield, output = output, weight_field = weight_field, profile = profile, profile_label = profile_label, use_rvir = use_rvir,\ profile_color = profile_color, nbins = nbins,\ xlabel = xlabel, ylabel = ylabel, xlim = xlim, ylim = ylim, zlim = zlim, \ cbar_label = cbar_label, xscale = xscale, cmap = cmap, data_cut = data_cut) if append_observations: #rvir = rom.get_romulus_rvir('romulusC', output) rvir = rom.get_romulusC_r200(output) rvir = 1 ipd.add_cluster_metallicity_observations(ax, color='firebrick', rvir=rvir) if append_profiles: data_list = ['hot', 'warm', 'cool', 'cold'] # data_list = ['hot_icm', 'warm_icm', 'cool_icm', 'cold_icm'] cmap_list = ['firebrick', 'goldenrod', 'seagreen', 'steelblue'] title_list = [ '$\mathrm{Hot\ Gas}$', '$\mathrm{Warm\ Gas}$', '$\mathrm{Cool\ Gas}$', '$\mathrm{Cold\ Gas}$' ] linestyle_list = ['solid', 'dashed', 'dashdot', 'dotted'] nbins = [20, 20, 20, 20] nbins = [40, 40, 40, 40] for i, data_cut in enumerate(data_list): cmap = sns.dark_palette(cmap_list[i], as_cmap=True) ffig, aax, im, cbar = ipd.plot_phase(xfield, yfield, zfield, output = output, weight_field = weight_field, \ fig = fig, ax = ax, do_pcolormesh = False, \ use_average_profile = use_average_profile, use_rvir = use_rvir,\ profile = True, profile_linestyle = linestyle_list[i], \ profile_color = cmap_list[i], profile_label = title_list[i],\ xlabel = xlabel, ylabel = ylabel, xlim = xlim, ylim = ylim, zlim = zlim, nbins = nbins[i],\ cbar_label = cbar_label, xscale = xscale, cmap = cmap, data_cut = data_cut) fs = 8 ax.legend(loc='lower left', fontsize=fs) fig.tight_layout() plt.savefig('metallicity_radius_%06d.png' % (output), dpi=300)
# Colors palette_colorblind = sns.color_palette("colorblind") color_cat1 = palette_colorblind[0] color_cat2 = palette_colorblind[1] palette_lightest = [ sns.light_palette(color_cat1, n_colors=20)[3], sns.light_palette(color_cat2, n_colors=20)[3], ] palette_light = [ sns.light_palette(color_cat1, n_colors=3)[1], sns.light_palette(color_cat2, n_colors=3)[1], ] palette_medium = [color_cat1, color_cat2] palette_dark = [ sns.dark_palette(color_cat1, n_colors=3)[1], sns.dark_palette(color_cat2, n_colors=3)[1], ] palette_cat1 = [ palette_lightest[0], palette_light[0], palette_medium[0], palette_dark[0], ] palette_cat2 = [ palette_lightest[1], palette_light[1], palette_medium[1], palette_dark[1], ] color_cat1_light = palette_light[0]
def macroMacroPDF(name, step, pData, varsDir, weight, mcFiles): import seaborn as sns from resample import unWeighted_resample params = { 'lines.linewidth': 1.5, 'backend': 'ps', 'axes.labelsize': 20, 'font.size': 20, 'legend.fontsize': 18, 'xtick.labelsize': 18, 'ytick.labelsize': 18, 'text.usetex': True, 'font.family': 'serif', 'legend.edgecolor': 'k', 'legend.fancybox': False } matplotlib.rcParams['text.latex.preamble'] = [r"\usepackage{amsmath}"] matplotlib.rcParams.update(params) # read in the original sampled parameter set labels = [r'$p$' + r' $(\mathrm{MPa})$', r'$q/p$', r'$n$', r'$C^*$'] # Monte Carlo sequence nSample, numOfObs = weight.shape enQOIs = np.zeros([4, nSample]) for i in range(nSample): C, CN, K0, e_r11, e_r21, e_a1, n1, overlap, p1, q1 = np.genfromtxt( mcFiles[i]).transpose() enQOIs[0, i] = p1[step] enQOIs[1, i] = q1[step] / p1[step] enQOIs[2, i] = n1[step] enQOIs[3, i] = CN[step] # importance resampling ResampleIndices = unWeighted_resample(weight[:, step], nSample * 10) sortedIndex = np.argsort(weight[:, step]) particles = np.copy(pData) particles[0, :] /= 1e9 particles[2, :] /= 1e3 particles = particles[:, ResampleIndices] enQOIs = enQOIs[:, ResampleIndices] # plot figures figNO = 0 fig = plt.figure(figsize=(18 / 2.54, 18 / 2.54)) sns.set(style="ticks", rc=matplotlib.rcParams) for i in range(4): for j in range(4): figNO += 1 # plot histograms at the initial and final steps ax = fig.add_subplot(4, 4, figNO) if i == j: # estimate pdf using gaussian kernals p0, w0 = getPDF(enQOIs[i, :], min(enQOIs[i, :]), max(enQOIs[i, :])) ax2 = ax.twinx() ax2.fill_between(p0, w0, alpha=0.63, facecolor='black') ax2.tick_params(axis='y', right='off', labelright='off') ax.set_ylim(min(p0), max(p0)) elif j < i: cmap = sns.dark_palette("black", as_cmap=True) ax = sns.kdeplot(enQOIs[j], enQOIs[i], cmap=cmap, kernel='gau', n_levels=20, bw='silverman', linewidths=0.8) ax.grid() ax.locator_params(axis='both', tight=True, nbins=2) ax.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off') ax.set_xlim(min(enQOIs[j, :]), max(enQOIs[j, :])) ax.set_ylim(min(enQOIs[i, :]), max(enQOIs[i, :])) elif j > i: cmap = sns.dark_palette("black", as_cmap=True) ax.scatter(enQOIs[j, sortedIndex], enQOIs[i, sortedIndex], s=25, c=weight[sortedIndex, step], cmap='Greys') ax.grid() ax.locator_params(axis='both', tight=True, nbins=2) ax.set_xlim(min(enQOIs[j, :]), max(enQOIs[j, :])) ax.set_ylim(min(enQOIs[i, :]), max(enQOIs[i, :])) if i == 3: ax.set_xlabel(labels[j], size=params['font.size'], labelpad=10) ax.tick_params(axis='both', which='both', bottom='on', top='off', labelbottom='on', right='off', left='off', labelleft='off', labelsize=params['xtick.labelsize']) if j == 0: ax.set_ylabel(labels[i], size=params['font.size'], labelpad=10) ax.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='on', labelleft='on', labelsize=params['xtick.labelsize']) if i == 3 and j == 0: ax.tick_params(axis='both', which='both', bottom='on', top='off', labelbottom='on', right='off', left='on', labelleft='on', labelsize=params['xtick.labelsize']) if i != 3 and j != 0: ax.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off') plt.tight_layout() fig.subplots_adjust(left=0.165, bottom=0.12, right=0.98, top=0.99, hspace=0.1, wspace=0.1) plt.savefig('macroMacroUQ_' + varsDir[-8:-1] + '_%i.pdf' % step, dpi=600) plt.show()
@cached_property # how to update kwargs when edited in UI? so preview may update def _get_kwargs(self): """Updates kwarg display on kind selection.""" return [Kwarg(key=key, val=self.kind_dict[key]) for key in self.kind_dict] @on_trait_change('kwargs') def replot_preview(self): """ Updates preview when kwargs is updated, which is only when kind is changed. Since kwargs is a cached property, edits in the UI window do not trigger udpate. Same lack of update when kwargs is a Property using @property_depends_on('kind_dict', settable=True). Does not update. """ self.plot(self.preview_axes) def _keep_plot_fired(self): """Updates static plot with current kwargs, on button press.""" self.plot(self.static_axes) def _update_preview_fired(self): """Updates preview plot with current kwargs, on button press.""" self.plot(self.preview_axes) # configure seaborn keep_plot appearance sns.despine() sb_dark = sns.dark_palette("skyblue", 8, reverse=True) sns.set(palette=sb_dark, style='whitegrid')
time_int = time_int.split(':') start, end = int(time_int[0]), int(time_int[1]) if start < end: track = AudioSegment.from_wav(session_state.wav_song) start_sec = start * 1000 end_sec = end * 1000 start = start * 10 end = end * 10 song_snippet = track[start_sec:end_sec] song_snippet.export('snippet.wav', format='wav') audio_file = open('snippet.wav', 'rb') audio_bytes = audio_file.read() st.audio(audio_bytes) cmap = sns.dark_palette('purple', as_cmap=True) chart = sns.heatmap(one_hot_preds[:, start:end], cmap=cmap, xticklabels=np.round( np.arange(start // 10, end // 10, 0.1), 1), yticklabels=chords.keys(), linewidths=.03, cbar=False) chart.set_xticklabels( chart.get_xticklabels(), rotation=-45, horizontalalignment='right', fontweight='light',
def d_mean_xy_plot(data, selections, colour_indices, legends, filename): pyplot.figure() for (l,c) in zip(selections, colour_indices): pyplot.scatter(data.loc[l, "delta_mpx"], data.loc[l, "delta_mpy"], c=data.loc[l, "Batch"], color=pyplot.rcParams['axes.color_cycle'][c], cmap=sb.dark_palette(pyplot.rcParams['axes.color_cycle'][c], as_cmap=True, reverse=True)) pyplot.xlabel("Change in Mean Position (in X)") pyplot.ylabel("Change in Mean Position (in Y)") pyplot.legend(legends, loc='upper left', fancybox=True) pyplot.tight_layout() pyplot.savefig(filename) pyplot.close()
def __init__(self, grid, resources, size, name, tasks): """ Arguments: - grid: a 2d array of sets of strings representing which resources are present in which cells. - resources: a list of strings representing all of the resources found anywhere in the world - size: a tuple containing the x and y dimensions of the grid - name: a string representing the name of the file this came from - tasks: a list of strings representing all of the tasks rewarded by the environment file (extracted from reactions), in order of appearance. """ self.grid = grid self.resources = resources self.size = size self.name = name.split("/")[-1] #Extract filename from path self.name = self.name[:-4] #Remove file extension self.tasks = tasks if len(self.resources) == 1: #Yay, we can make the background nice and simple! self.resource_palette = sns.dark_palette("black", 1) #If we're only using two colors, it's better for them to be red #and yellow than red and green self.task_palette = sns.hls_palette(max(len(tasks), 4), s=1) elif set(self.tasks) == set(self.resources): #Yay, the tasks and resources correspond to each other! #If we're only using two colors, it's better for them to be red #and yellow than red and green self.resource_palette = sns.hls_palette(max(len(tasks), 4), s=1) self.task_palette = sns.hls_palette(max(len(tasks), 4), s=1) elif len(self.tasks) == 1: #Yay, we can display the tasks easily! self.task_palette = sns.dark_palette("black", 1) #If we're only using two colors, it's better for them to be red #and yellow than red and green self.resource_palette = sns.hls_palette(max(len(tasks), 4), s=1) elif set(self.tasks).issubset(set(self.resources)): self.task_palette = sns.hls_palette(max(len(tasks), 4), s=1) self.resource_palette = sns.hls_palette(max(len(tasks), 4), s=1) self.resource_palette += sns.color_palette("bone", len(resources)-len(tasks)) elif set(self.tasks).issuperset(set(self.resources)): self.task_palette = sns.hls_palette(max(len(tasks), 4), s=1) self.resource_palette = sns.hls_palette(max(len(resources), 4), s=1) else: #Since we don't know anything in particular about the tasks #and resources, let's be color-blind friendly and keep the #background simple. The user probably wants to assign new #palettes based on their data. self.task_palette = sns.color_palette("colorblind", len(tasks)) self.resource_palette = sns.color_palette("bone", len(resources))
'E2S': "o", 'E3S': "o", 'P5S': "s", 'DHEAS': "s", 'DHEAG': "s", 'E2G': "o" } # Dictionary of colors to be used for plotting for each small molecule scolors = {'CA': sns.light_palette("purple", 5)[0], 'GCA': sns.light_palette("purple", 5)[1], 'TCA': sns.light_palette("purple", 5)[2], 'DCA': sns.light_palette("lightblue", 3)[0], 'GDCA': sns.light_palette("lightblue", 3)[1], 'TDCA': sns.light_palette("lightblue", 3)[2], 'CDCA': sns.dark_palette("blue", 5)[4], 'GCDCA': sns.dark_palette("blue", 5)[2], 'E1S': sns.light_palette("darkgreen", 6)[0], 'E2S': sns.light_palette("darkgreen", 6)[1], 'E3S': sns.light_palette("darkgreen", 6)[2], 'P5S': sns.light_palette("darkgreen", 6)[3], 'DHEAS': sns.light_palette("darkgreen", 6)[4], 'DHEAG': sns.light_palette("red", 4)[1], 'E2G': sns.light_palette("red", 4)[3] } # Dictionary of category colors to be used for plotting for each small moleculew catcolors = {'CA': sns.light_palette("purple", 5)[2], 'GCA': sns.light_palette("purple", 5)[2], 'TCA': sns.light_palette("purple", 5)[2], 'DCA': sns.light_palette("lightblue", 3)[2],
def plot_state(samples, degrees=True, confidence=True, rear_contact_trajectory=True, filename=None): # get time from timestamp and sample time t = samples.bicycle.dt.mean() * samples.ts cols = 2 if not samples.aux.mask.any(): rows = math.ceil((samples.x.shape[1] + samples.aux.shape[1]) / cols) aux_offset = int(rows*cols != samples.x.shape[1] + samples.aux.shape[1]) state_offset = aux_offset + samples.aux.shape[1] else: rows = math.ceil(samples.x.shape[1] / cols) aux_offset = int(rows*cols != samples.x.shape[1]) state_offset = aux_offset fig, axes = plt.subplots(rows, cols, sharex=True) axes = axes.ravel() if aux_offset: axes[0].axis('off') # We want C'*z so we have the measured state with noise at every timestep # (some states will be zero). # (C'*z)' = z'*C C = samples.bicycle.Cd[0] # C = Cd # first element of auxiliary state is masked if all states are zero: if samples.aux.mask[0].any() and not samples.aux.mask[1:].all(): samples.aux[0] = np.zeros(1, dtype=(samples.aux.dtype, samples.aux.shape[1:])) for n in range(samples.aux.shape[1]): if rear_contact_trajectory: if n == 0: continue elif n == 1: trajectory = samples.aux[:, :2].transpose(0, 2, 1) segments = np.concatenate([trajectory[:-1], trajectory[1:]], axis=1) lc = LineCollection(segments, cmap=sns.dark_palette(flatgrey, as_cmap=True)) lc.set_array(t) ax = plt.subplot2grid((rows, cols), (0, 0), colspan=2) ax.add_collection(lc) minima = np.min(trajectory, axis=0)[0] maxima = np.max(trajectory, axis=0)[0] ax.set_xlim(minima[0], maxima[0]) ax.set_ylim(minima[1], maxima[1]) ax.set_xlabel('{} [{}]'.format('x', unit('x'))) ax.set_ylabel('{} [{}]'.format('y', unit('y'))) ax.plot(trajectory[-1, 0, 0], trajectory[-1, 0, 1], color=auxiliary_state_color[0], label='rear contact point trajectory') ax.invert_yaxis() ax.legend() axes[1] = ax continue ax = axes[n + aux_offset] x_state = auxiliary_state_name[n] x_unit = unit(x_state, degrees) x = samples.aux[:, n] if degrees and '°' in x_unit: x = np.rad2deg(x) ax.set_xlabel('{} [{}]'.format('time', unit('time'))) ax.set_ylabel('{} [{}]'.format(x_state, x_unit)) ax.plot(t, x, color=auxiliary_state_color[n], label='auxiliary state') ax.legend() x_meas = np.dot(samples.z.transpose(0, 2, 1), C).transpose(0, 2, 1) for n in range(samples.x.shape[1]): ax = axes[n + state_offset] x_state = state_name[n] x_unit = unit(x_state, degrees) x = samples.x[:, n] x_hat = samples.kalman.x[:, n] z = x_meas[:, n] if degrees and '°' in x_unit: x = np.rad2deg(x) x_hat = np.rad2deg(x_hat) z = np.rad2deg(z) ax.set_xlabel('{} [{}]'.format('time', unit('time'))) ax.set_ylabel('{} [{}]'.format(x_state, x_unit)) ax.plot(t, x, color=state_color[2*n + 1], label='true', zorder=2) if not samples.z.mask.all() and z.any(): ax.plot(t[1:], z[1:], color=_grey_color(state_color[2*n + 1]), label='measurement', zorder=1) ax.legend() if not x_hat.mask.all(): ax.plot(t, x_hat, color=state_color[2*n], label='estimate', zorder=3) if confidence: std_dev = np.sqrt(samples.kalman.P[:, n, n]) if degrees and '°' in x_unit: np.rad2deg(std_dev, out=std_dev) # in-place conversion # plot confidence interval of 2 standard deviations low = x_hat.ravel() - 2*std_dev high = x_hat.ravel() + 2*std_dev limits = ax.get_ylim() ax.fill_between(t, low, high, alpha=0.2, color=state_color[2*n]) ax.set_ylim(limits) ax.legend() # register axes callback - this will redraw the trajectory subplot if the # x-axis (time) is changed on any ofther subplots if rear_contact_trajectory: class TrajectoryDisplay(object): def __init__(self, ax, t, x, y): self.ax = ax self.t = t self.x = x self.y = y def ax_update(self, ax): tmin, tmax = ax.get_xlim() imin = np.argmax(self.t >= tmin) imax = self.t.shape[0] - np.argmax(self.t[::-1] < tmax) x = self.x[imin:imax] y = self.y[imin:imax] self.ax.set_xlim(np.min(x), np.max(x)) self.ax.set_ylim(np.min(y), np.max(y)) td = TrajectoryDisplay(axes[1], t, samples.aux[:, 0], samples.aux[:, 1]) for n in range(2, rows*cols): axes[n].callbacks.connect('xlim_changed', td.ax_update) title = 'system state' _set_suptitle(fig, title, filename) return fig, axes
for timestamp in timeline_db.keys(): plot_data_timeline_dates.append(dt.datetime.fromtimestamp(timestamp).strftime("%Y-%b-%d")) plot_data_timeline_timestamps.append(timestamp) plot_data_timeline_caps.append(timeline_db[timestamp]['capacity']) plot_data_timeline_drvc.append(timeline_db[timestamp]['drive_count']) plot_data_timeline_sys.append(timeline_db[timestamp]['system_count']) df_timeline = pd.DataFrame() df_timeline['Timestamp'] = plot_data_timeline_timestamps df_timeline['Date'] = plot_data_timeline_dates df_timeline['Capacity (TB)'] = plot_data_timeline_caps df_timeline['Drive Count'] = plot_data_timeline_drvc df_timeline['System Count'] = plot_data_timeline_sys df_timeline = df_timeline.sort_values(by=['Timestamp'], ascending=[True]) #ax = sns.swarmplot(x='Timestamp', y="Capacity", data=df_timeline) fig10, ax23 = plt.subplots(1) sns.stripplot(x='Date', y="Capacity (TB)", data=df_timeline, ax=ax23, palette = sns.dark_palette("purple"),size=10) fig10.subplots_adjust(bottom=0.2,left=0.1,right=0.9,top=0.95) plt.setp(ax23.xaxis.get_majorticklabels(), rotation=90) plt.setp(ax23.get_xticklabels()[::], visible=False) plt.setp(ax23.get_xticklabels()[::5], visible=True) ax23.set_ylim(bottom=0) fig10.savefig(timeline_caps_plot_path) fig11, ax24 = plt.subplots(1) sns.stripplot(x='Date', y="Drive Count", data=df_timeline, ax=ax24, palette = sns.dark_palette("purple"),size=10) fig11.subplots_adjust(bottom=0.2,left=0.1,right=0.9,top=0.95) plt.setp(ax24.xaxis.get_majorticklabels(), rotation=90) plt.setp(ax24.get_xticklabels()[::], visible=False) plt.setp(ax24.get_xticklabels()[::5], visible=True) ax24.set_ylim(bottom=0) fig11.savefig(timeline_drvc_plot_path)
plt.figure() plot_confusion_matrix(cm, classes=['Alive', 'Death'], title='Ridge Classifier') train_accuracy.append(RC_parm.score(X_train_scaled, y_train)) accuracy_list.append(metrics.accuracy_score(y_test, y_RC) * 100) algorithm.append('Ridge Classifier') # <a id="9"></a> # # Evaluation of Accuracy # In[39]: #Train Accuracy f, ax = plt.subplots(figsize=(10, 5)) sns.barplot(x=train_accuracy, y=algorithm, palette=sns.dark_palette("blue", len(accuracy_list))) plt.xlabel("Accuracy") plt.ylabel("Algorithm") plt.title('Algorithm Train Accuracy') plt.show() # In[43]: #Testing Accuracy f, ax = plt.subplots(figsize=(10, 5)) sns.barplot(x=accuracy_list, y=algorithm, palette=sns.dark_palette("blue", len(accuracy_list))) plt.xlabel("Accuracy") plt.ylabel("Algorithm") plt.title('Algorithm Test Accuracy')
plt.savefig(fig_dir + 'tot_loans.png', dpi=500, format='png') plt.close() #%% PLOT EXAMPLE PAYMENT PROB PLOT outcome_map = { #expected principal recovery given status, view this is prob of all princ being charged off 'Current': 0., 'In Grace Period': 28./100, 'Late (16-30 days)': 58./100, 'Late (31-120 days)': 74./100} outcome_ord = ['Current','In Grace Period','Late (16-30 days)','Late (31-120 days)'] late_mnths = [0,0,1,2] plt.close('all') pal = sns.dark_palette('seagreen',n_colors = len(outcome_ord), reverse=True) age = 6 term = 36 grad_idx = 5 print_stages = [[outcome_ord[0]], outcome_ord[:2], outcome_ord] for print_idx in xrange(len(print_stages)): fig,ax=plt.subplots(figsize=(5.0,4.0)) cur_outcomes = print_stages[print_idx] cond_pays = {} for idx, out in enumerate(cur_outcomes): cur_haz = all_hazards[term][grad_idx,:].copy() cur_age = age - late_mnths[idx] cur_haz[:cur_age] = 0.0 pay_prob = 1 - np.cumsum(cur_haz)
new_w = w new_y = y + ix*h new_h = iw*h new_i = code, new_x, new_w, new_y, new_h final_division.append(new_i) final_division = final_division[1:] assert len(final_division) == len(hierachical_spacing_3) rectStruct = namedtuple('RectStruct', ['code', 'x', 'width', 'y', 'height']) final_division_named = [rectStruct(*i) for i in final_division] # %% current_palette = seaborn.color_palette()*5 #seaborn.palplot(current_palette) colori = list(it.chain.from_iterable(seaborn.dark_palette(current_palette[i], len(labels[2])+1)[::-1][:-1] for i in range(len(labels[1])))) #seaborn.palplot(colori) etichette = list(it.product(*labels[1:])) etichette2color = dict(zip(etichette, colori)) max_len = max(len(k.code) for k in final_division_named) with seaborn.axes_style("white"): fig, ax = plt.subplots(1, 1) ax.grid(False) for (k, x, w, y, h) in final_division_named: if len(k)<max_len: continue text = " ".join(str(i) for i in k) r = plt.Rectangle((x, y), w, h, facecolor=etichette2color[k[1:3]],
plot.set_ylabel('Total Number of Trips (in thousands)') plot.set_yticklabels(['0', '1,000', '2,000', '3,000', '4,000','5,000', '6,000', '7,000', '8,000']) plt.title('Distribution of Taxi Trips by Distance - Thursday', fontsize = 15) plt.savefig('./distance_weekday/dist_thurs.png', dpi=300, bbox_inches='tight') #### Friday sns.set(context='notebook', style='darkgrid', font='sans-serif', font_scale=1, color_codes=False, rc=None) plot = sns.barplot(x="distance", y="Total Number of Trips", data=dist4) plot.set_xlabel('Range of Distance (in miles)') plot.set_ylabel('Total Number of Trips (in thousands)') plot.set_yticklabels(['0', '1,000', '2,000', '3,000', '4,000','5,000', '6,000', '7,000', '8,000']) plt.title('Distribution of Taxi Trips by Distance - Friday', fontsize = 15) plt.savefig('./distance_weekday/dist_fri.png', dpi=300, bbox_inches='tight') #### Saturday sns.dark_palette("muted purple", input="xkcd") sns.set(context='notebook', style='darkgrid', font='sans-serif', font_scale=1, color_codes=False, rc=None) plot = sns.barplot(x="distance", y="Total Number of Trips", data=dist5, palette = sns.cubehelix_palette(9, reverse=True)) plot.set_xlabel('Range of Distance (in miles)') plot.set_ylabel('Total Number of Trips (in thousands)') plot.set_yticklabels(['0', '1,000', '2,000', '3,000', '4,000','5,000', '6,000', '7,000', '8,000']) plt.title('Distribution of Taxi Trips by Distance - Saturday', fontsize = 15) plt.savefig('./distance_weekday/dist_sat.png', dpi=300, bbox_inches='tight') #### Sunday sns.set(context='notebook', style='darkgrid', font='sans-serif', font_scale=1, color_codes=False, rc=None) plot = sns.barplot(x="distance", y="Total Number of Trips", data=dist6, palette=sns.diverging_palette(145, 280, s=85, l=25, n=9)) plot.set_xlabel('Range of Distance (in miles)') plot.set_ylabel('Total Number of Trips (in thousands)') plot.set_yticklabels(['0', '1,000', '2,000', '3,000', '4,000','5,000', '6,000', '7,000', '8,000']) plt.title('Distribution of Taxi Trips by Distance - Sunday', fontsize = 15)
Created on Mon Feb 24 20:59:11 2020 @author: Lindineu Duran """ # importar as bibliotecas import pandas as pd import locale import matplotlib.pyplot as plt import seaborn as sns from bs4 import BeautifulSoup import dateutil # inicializar configurações de ambiente sns.set(style="whitegrid") sns.set(style="whitegrid") pal = sns.dark_palette("palegreen", as_cmap=True) locale.setlocale(locale.LC_NUMERIC, 'Portuguese_Brazil.1252') # arquivo disponibilizado pela Caixa Ecônomica Federal # http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_mgsasc.zip # converting a html table to a csv in python html = open( "D:\Documents\Python\Megasena\Resultados Megasena\d_megasc.htm").read() soup = BeautifulSoup(html, features="lxml") table = soup.select_one("table.tblperiode") # find all rows, loop through them and add row text to array output_rows = [] for table_row in table.findAll('tr'):
def select_palette(shade_no, palette_colour, reverse=False): missing = ["#d8dcd6"] pal = sns.dark_palette(palette_colour, shade_no, reverse=reverse, input='xkcd') return missing + list(pal.as_hex())
def plot_model_metric_sets(metrics_sets, x_key, y_key, x_label=None, y_label=None, primary_differentiator_key=None, primary_differentiator_order=None, secondary_differentiator_key=None, secondary_differentiator_order=None, special_cases=None, other_method_metrics=None, palette=None, marker_styles=None, name=None): figure_name = saving.build_figure_name("model_metric_sets", name) if other_method_metrics: figure_name += "-other_methods" if not isinstance(metrics_sets, list): metrics_sets = [metrics_sets] if not palette: palette = style.STANDARD_PALETTE.copy() if not marker_styles: marker_styles = [ "X", # cross "s", # square "D", # diamond "o", # circle "P", # plus "^", # upright triangle "p", # pentagon "*", # star ] figure = pyplot.figure(figsize=(9, 6)) axis = figure.add_subplot(1, 1, 1) seaborn.despine() axis.set_xlabel(x_label) axis.set_ylabel(y_label) colours = {} markers = {} for metrics_set in metrics_sets: x = numpy.array(metrics_set[x_key]) y = numpy.array(metrics_set[y_key]) if x.dtype == "object" or y.dtype == "object": continue x_mean = x.mean() x_ddof = 1 if x.size > 1 else 0 x_sd = x.std(ddof=x_ddof) y_mean = y.mean() y_ddof = 1 if y.size > 1 else 0 y_sd = y.std(ddof=y_ddof) colour_key = metrics_set[primary_differentiator_key] if colour_key in colours: colour = colours[colour_key] else: try: index = primary_differentiator_order.index(colour_key) colour = palette[index] except (ValueError, IndexError): colour = "black" colours[colour_key] = colour axis.errorbar( x=x_mean, y=y_mean, yerr=y_sd, xerr=x_sd, capsize=2, linestyle="", color=colour, label=colour_key, markersize=7 ) marker_key = metrics_set[secondary_differentiator_key] if marker_key in markers: marker = markers[marker_key] else: try: index = secondary_differentiator_order.index(marker_key) marker = marker_styles[index] except (ValueError, IndexError): marker = None markers[marker_key] = marker axis.errorbar( x_mean, y_mean, color="black", marker=marker, linestyle="none", label=marker_key ) errorbar_colour = colour darker_colour = seaborn.dark_palette(colour, n_colors=4)[2] special_case_changes = special_cases.get(colour_key, {}) special_case_changes.update( special_cases.get(marker_key, {}) ) for object_name, object_change in special_case_changes.items(): if object_name == "errorbar_colour": if object_change == "darken": errorbar_colour = darker_colour axis.errorbar( x=x_mean, y=y_mean, yerr=y_sd, xerr=x_sd, ecolor=errorbar_colour, capsize=2, color=colour, marker=marker, markeredgecolor=darker_colour, markersize=7 ) baseline_line_styles = [ "dashed", "dotted", "dashdot", "solid" ] legend_outside = False if other_method_metrics: for method_name, metric_values in other_method_metrics.items(): x_values = metric_values.get(x_key, None) y_values = metric_values.get(y_key, None) if not y_values: continue y = numpy.array(y_values) y_mean = y.mean() if y.shape[0] > 1: y_sd = y.std(ddof=1) else: y_sd = None if x_values: legend_outside = True x = numpy.array(x_values) x_mean = x.mean() if x.shape[0] > 1: x_sd = x.std(ddof=1) else: x_sd = None axis.errorbar( x=x_mean, y=y_mean, yerr=y_sd, xerr=x_sd, ecolor=style.STANDARD_PALETTE[-1], color=style.STANDARD_PALETTE[-1], capsize=2, linestyle="none", label=method_name ) else: line_style = baseline_line_styles.pop(0) axis.axhline( y=y_mean, color=style.STANDARD_PALETTE[-1], linestyle=line_style, label=method_name, zorder=-1 ) if y_sd is not None: axis.axhspan( ymin=y_mean - y_sd, ymax=y_mean + y_sd, facecolor=style.STANDARD_PALETTE[-1], alpha=0.1, edgecolor=None, label=method_name, zorder=-2 ) if len(metrics_sets) > 1: order = primary_differentiator_order + secondary_differentiator_order handles, labels = axis.get_legend_handles_labels() label_handles = {} for label, handle in zip(labels, handles): label_handles.setdefault(label, []) label_handles[label].append(handle) labels, handles = [], [] for label, handle_set in label_handles.items(): labels.append(label) handles.append(tuple(handle_set)) labels, handles = zip(*sorted( zip(labels, handles), key=lambda l: [order.index(l[0]), l[0]] if l[0] in order else [len(order), l[0]] )) if legend_outside: legend_keywords = { "loc": 'center left', "bbox_to_anchor": (1.05, 0, .3, 1), "ncol": 1, # "mode": "expand", "borderaxespad": 0 } else: legend_keywords = {"loc": "best"} axis.legend(handles, labels, **legend_keywords) return figure, figure_name
temp = daily_stacked_count.copy(deep=True) # https://stackoverflow.com/a/45349235/2650427 dnum = mdates.date2num(temp.index.to_pydatetime()) start = dnum[0] - (dnum[1] - dnum[0]) / 2. stop = dnum[-1] + (dnum[1] - dnum[0]) / 2. extent = [start, stop, -0.5, len(temp.columns) - 0.5] fig, ax = plt.subplots(figsize=(15, 5)) im = ax.imshow( temp.T, vmax=500, aspect="auto", extent=extent, cmap=sns.dark_palette("palegreen", as_cmap=True, reverse=True) ) ax.xaxis.set_major_locator(mdates.YearLocator()) ax.xaxis.set_minor_locator(mdates.MonthLocator()) ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y')) ax.set_yticks(range(len(temp.columns))) ax.set_yticklabels(temp.columns.values[::-1]) # ax.set_title("Text count heatmap") fig.colorbar(im) filepath = plot_dir / "top_n_heatmap.jpeg" fig.savefig(filepath.as_posix(), bbox_inches='tight') plt.close(fig) print("Saved the plot", filepath)
# In[76]: plt.close('all') import seaborn as sns cmap = sns.cubehelix_palette(256,as_cmap=True) fig,ax =plt.subplots(1,1) the_img=ax.imshow(lum_img,cmap=cmap) plt.colorbar(the_img,ax=ax) # In[78]: cmap = sns.light_palette("green",256,as_cmap=True) fig,ax =plt.subplots(1,1) the_img=ax.imshow(lum_img,cmap=cmap) plt.colorbar(the_img,ax=ax) # In[79]: cmap = sns.dark_palette("red",256,as_cmap=True) fig,ax =plt.subplots(1,1) the_img=ax.imshow(lum_img,cmap=cmap) plt.colorbar(the_img,ax=ax) # In[ ]:
'Rn' : NSE(Rn_EC, Rn_mod_Kustas), 'G' : NSE(G_EC, G_mod_Kustas), 'H_BR' : NSE(H_EC_corr_EB, H_mod_Kustas), 'LE_BR' : NSE(LE_EC_corr_EB, LE_mod_Kustas)} diff_Stats = pd.DataFrame(Diff_stats, columns = ['NSE_kB23', 'NSE_Lhomme', 'NSE_Kustas']) #%% Plotting against raw EC fluxes colors = ['#3498db', '#2ecc71', '#f7cf33', '#fc9d1d','#fd484d', '#9b59b6', '#51677b'] pal = sns.color_palette(colors) # http://www.husl-colors.org/ pal_red_light = sns.light_palette((11.4, 97.4, 58.1), input="husl") pal_red_dark = sns.dark_palette((11.4, 97.4, 58.1), input="husl") pal_blue_light = sns.light_palette((242.2, 90.1, 60.2), input="husl") pal_blue_dark = sns.dark_palette((242.2, 90.1, 60.2), input="husl") pal_orange_light = sns.light_palette((41.2, 96.8, 72.8), input="husl") pal_orange_dark = sns.dark_palette((41.2, 96.8, 72.8), input="husl") pal_green_light = sns.light_palette((137.9, 93.2, 72.9), input="husl") pal_green_dark = sns.dark_palette((137.9, 93.2, 72.9), input="husl") sns.set(context = "paper", style = 'white', palette = pal, rc = {'axes.labelsize': 18.0, 'figure.figsize': [14, 7], 'legend.fontsize': 16.0, 'xtick.labelsize': 18.0, 'ytick.labelsize': 18.0, 'xtick.major.size': 4.0, 'ytick.major.size': 4.0}) # Options: EC_raw,
def bpt_nii(maps, ax=None, snr_min=None, deredden=False, return_figure=True, plot_map=False, plot_kde=False, radial_bin=None, return_data=None, overplot_dk_bpt=False, dk_bpt_kwargs={}, deredden_kwargs={}, classification_kwargs={}, **kwargs): """ NII / HA BPT Diagram ala Krishnarao+19 for MaNGA Galaxy Parameters ---------- maps: 'marvin.tools.maps.Maps' or 'dk_marvin_tools.DKMaps.DKMaps' MaNGA Maps Data ax: 'matplotlib.pyplot.figure.axes', optional, must be keyword Matplotlib axes to plot on snr_min: 'number', optional, must be keyword min SNR to use for data deredden: 'bool', optional, must be keyword if True, will deredden emission lines return_figure: 'bool', optional, must be keyword if False, will return BPT Classifications as a dictionary plot_map: 'bool', optional, must be keyword if True, will instead plot a map color coded by classifications plot_kde: 'bool', optional, must be keyword if True, will plot kde plot instead of scatter plot radial_bin: 'list' or 'tuple' or 'np.ndarray', optional, must be keyword if given, only plots points within provided radial bin in terms of R/R_e return_data: 'bool', optional, must be keyword if True, returns data instead of plotting overplot_dk_bpt: 'bool', optional, must be keyword if True, overplots dk_bpt dk_bpt_kwargs: 'dict', optional, must be keyword kwargs passed to dk_bpt deredden_kwargs: 'dict', optional, must be keyword kwargs passed to deredden classification_kwargs: 'dict', optional, must be keyword kwargs passed to draw_classification_lines kwargs: 'dict', optional, must be keywords keywords to pass to scatter plot of BPT points or keywords to pass to map.plot of plot_map """ # Get EmLine Data if deredden: try: ha = maps.deredden("emline gflux ha", **deredden_kwargs) except AttributeError: logging.warning( "provided maps object does not have a deredden method. Skipping dereddening process." ) ha = maps["emline gflux ha"] hb = maps["emline gflux hb"] oiii = maps["emline glfux oiii 5007"] nii = maps["emline glflux nii 6585"] else: hb = maps.deredden("emline glfux hb", **deredden_kwargs) oiii = maps.deredden("emline glfux oiii 5007", **deredden_kwargs) nii = maps.deredden("emline glfux nii 6585", **deredden_kwargs) else: ha = maps["emline gflux ha"] hb = maps["emline gflux hb"] oiii = maps["emline glfux oiii 5007"] nii = maps["emline glflux nii 6585"] if snr_min is None: snr_min = 3. # Get masked Data ha_masked = ha.masked hb_masked = hb.masked oiii_masked = oiii.masked nii_masked = nii.masked # SNR Cut ha_masked.mask |= ha.snr < snr_min hb_masked.mask |= hb.snr < snr_min oiii_masked.mask |= oiii.snr < snr_min nii_masked.mask |= nii.snr < snr_min ha_masked.mask |= ha.ivar == 0 hb_masked.mask |= hb.ivar == 0 oiii_masked.mask |= oiii.ivar == 0 nii_masked.mask |= nii.ivar == 0 # Mask Negative Flux ha_masked.mask |= ha_masked.data <= 0 hb_masked.mask |= hb_masked.data <= 0 oiii_masked.mask |= oiii_masked.data <= 0 nii_masked.mask |= nii_masked.data <= 0 # masked Logarithms log_oiii_hb = np.ma.log10(oiii_masked / hb_masked) log_nii_ha = np.ma.log10(nii_masked / ha_masked) # Calculate Masks for classification regions sf_mask_nii = (log_oiii_hb < nii_sf_line(log_nii_ha)) & (log_nii_ha < 0.05) comp_mask = ((log_oiii_hb > nii_sf_line(log_nii_ha)) & (log_nii_ha < 0.05)) & \ ((log_oiii_hb < nii_comp_line(log_nii_ha)) & (log_nii_ha < 0.465)) sub_agn_mask_nii = (log_oiii_hb > nii_comp_line(log_nii_ha)) | (log_nii_ha > 0.465) agn_mask_nii = sub_agn_mask_nii & (nii_agn_line(log_nii_ha) < log_oiii_hb) liner_mask_nii = sub_agn_mask_nii & (nii_agn_line(log_nii_ha) > log_oiii_hb) invalid_mask = ha_masked.mask | oiii_masked.mask | nii_masked.mask | hb_masked.mask sf_classification = {"nii": sf_mask_nii} comp_classification = {"nii": comp_mask} agn_classification = {"nii": agn_mask_nii} liner_classification = {"nii": liner_mask_nii} invalid_classification = {"nii": invalid_mask} bpt_return_classification = { 'sf': sf_classification, 'comp': comp_classification, 'agn': agn_classification, 'liner': liner_classification, 'invalid': invalid_classification } if not return_figure: return bpt_return_classification elif plot_map: # Make image bpt_image = np.empty(ha.shape) bpt_image[:] = np.nan # Star Forming bpt_image[bpt_return_classification['sf']['nii']] = 0.5 # Comp bpt_image[bpt_return_classification['comp']['nii']] = 1.5 # Seyfert bpt_image[bpt_return_classification['agn']['nii']] = 2.5 # LINER bpt_image[bpt_return_classification['liner']['nii']] = 3.5 # Ambiguous bpt_image[bpt_return_classification['invalid']['nii']] = 4.5 bpt_image = np.ma.masked_array(bpt_image, mask=np.isnan(bpt_image)) if ax is None: fig = plt.figure() ax = fig.add_subplot(111) else: fig = ax.figure if "cmap" not in kwargs: kwargs["cmap"] = ListedColormap( sns.color_palette([pal[1], pal[0], pal[9], pal[4], pal[8]])) if "title" not in kwargs: kwargs["title"] = "NII BPT Classification Map" fig, ax, cb = ha.plot(fig=fig, ax=ax, value=bpt_image, return_cb=True, cbrange=[0, 5], **kwargs) cb.set_ticks([0.5, 1.5, 2.5, 3.5, 4.5]) cb.ax.set_yticklabels( ['Star Forming', 'Composite', 'AGN', 'LI(N)ER', 'Invalid']) cb.set_label(r'BPT Classification', fontsize=14) cb.ax.tick_params(labelsize=12) return ax, cb elif plot_kde: # KDE Map if return_data: radius = maps['spx ellcoo r_re'] return radius, log_nii_ha, log_oiii_hb else: if ax is None: fig = plt.figure() ax = fig.add_subplot(111) # Draw classification lines ax = draw_classification_lines(ax, **classification_kwargs) # Default kwargs # Default colormap if "cmap" not in kwargs: kwargs["cmap"] = "plasma" if "zorder" not in kwargs: kwargs["zorder"] = 0 if "shade" not in kwargs: kwargs["shade"] = True if "shade_lowest" not in kwargs: kwargs["shade_lowest"] = False if radial_bin is not None: radius = maps['spx ellcoo r_re'] within = radius.value >= radial_bin[0] within &= radius.value <= radial_bin[1] ax = sns.kdeplot( log_nii_ha[(np.invert(log_nii_ha.mask | log_oiii_hb.mask) & within)], log_oiii_hb[(np.invert(log_nii_ha.mask | log_oiii_hb.mask) & within)], ax=ax, **kwargs) else: ax = sns.kdeplot( log_nii_ha[np.invert(log_nii_ha.mask | log_oiii_hb.mask)], log_oiii_hb[np.invert(log_nii_ha.mask | log_oiii_hb.mask)], ax=ax, **kwargs) ax.set_xlabel(r'$log_{10}$([NII] $\lambda$ 6585/H$\alpha$)', fontsize=12) ax.set_ylabel(r'$log_{10}$([OIII] $\lambda$ 5007/H$\beta$)', fontsize=12) if overplot_dk_bpt: ax = plot_dkbpt(ax, **dk_bpt_kwargs) ax = scale_to_dkbpt(ax) return ax else: # Do the plotting if ax is None: fig = plt.figure() ax = fig.add_subplot(111) # Draw classification lines ax = draw_classification_lines(ax, **classification_kwargs) # Defautl kwargs # Default colors if ("c" not in kwargs) & ("color" not in kwargs) & ("cmap" not in kwargs): # Default radial colormapping radius = maps['spx ellcoo r_re'] kwargs["c"] = radius kwargs["cmap"] = sns.dark_palette(pal[8], as_cmap=True) # Default vmin/vmax if "c" in kwargs: if "vmin" not in kwargs: kwargs["vmin"] = 0. if "vmax" not in kwargs: kwargs["vmax"] = 3. # Default Size if "s" not in kwargs: kwargs["s"] = 5 # plot the points pts = ax.scatter(log_nii_ha, log_oiii_hb, **kwargs) ax.set_xlabel(r'$log_{10}$([NII] $\lambda$ 6585/H$\alpha$)', fontsize=12) ax.set_ylabel(r'$log_{10}$([OIII] $\lambda$ 5007/H$\beta$)', fontsize=12) if overplot_dk_bpt: ax = plot_dkbpt(ax, **dk_bpt_kwargs) ax = scale_to_dkbpt(ax) return pts, ax
_df = pd.DataFrame({'tau':fcs.actime, 'autocorr':fcs.autocorr, 'file':f, }) df_list.append(_df) df = pd.concat(df_list, axis=0) # _ = fcs.autocorrelate(1) # %% plt.figure(figsize=(8,6)) sns.lineplot(x='tau', y='autocorr', data=df, err_style="bars", hue='file', alpha=.1, legend=False, palette=sns.dark_palette("blue", n_colors=10)) sns.lineplot(x='tau', y='autocorr', data=df) plt.xscale('log') # %% # %% plt.plot(fcs.txdisplay, fcs.trajdisplay) fcs.txdisplay[0:10] # %% fcs1 = fcs_1comp(p0 = [.1, .005, 0]) p, perr = fcs1(df.tau, df.autocorr) p
def test13(): # palette = sns.light_palette('red') palette = sns.dark_palette('red') # 填充颜色到图表 sns.palplot(palette) plt.show()
kf = kalman.Kalman(ssm=my_ssm, data=data) kf.filter() true_loglik = np.cumsum(kf.logpyt) true_filt_means = [f.mean for f in kf.filt] # Get results N = 10**3 results = particles.multiSMC(fk=models, N=N, nruns=25, moments=True) # PLOTS # ===== plt.style.use('ggplot') savefigs = False # whether you want to save figs as pdfs # black and white sb.set_palette(sb.dark_palette("lightgray", n_colors=len(models), reverse=True)) # box-plots for log-likelihood evaluation plt.figure() sb.boxplot(x=[r['fk'] for r in results], y=[r['output'].logLt for r in results]) plt.ylabel('log-likelihood estimate') plt.axhline(y=true_loglik[-1], ls=':', color='k') # adapt line below if you want to zoom (if boxplots for guided/APF are too tiny) #v = plt.axis(); plt.axis([v[0],v[1],-152.5,-150.5]) if savefigs: plt.savefig('lingauss_boxplot_loglik_boot_guided_APF.pdf') # MSE of particle estimate of E(X_t|Y_{0:t}) vs time plt.figure() for mod in models.keys():
# save results ############## # import pickle # pickle.dump({'results':results, 'Ms': Ms, 'N': N, 'nruns': nruns, 'T': T, 'p':p, # 'ESSrmin' : ESSrmin, 'data': data}, # open('%s_N%i.pickle' % (dataset, N), 'wb')) # Then reload like this # dp = pickle.load(file_name) # locals().update(dp) # plots ####### savefigs = True # do you want to save figures as pdfs plt.style.use('ggplot') pal = sb.dark_palette('white', n_colors=2) # Compare standard and path sampling estimates of the log-normalising cst plt.figure() diff_est = [(r['out'].logLts[-1] - r['path_sampling']) for r in results if r['type'] == 'tempering'] sb.distplot(diff_est) # Figure 17.1: typical behaviour of IBIS typ_ibis = [r for r in results if r['type'] == 'ibis' and r['M'] == typM][0] typ_ess = typ_ibis['out'].ESSs typ_rs_times = np.nonzero(typ_ibis['out'].rs_flags)[0] # Left panel: evolution of ESS fig, ax = plt.subplots() ax.plot(typ_ess, 'k')
def plot_base_classification(good_exs, stat_dict, out_infer, auc_dict, cdata, args): fig, ((expr_ax1, expr_ax2), (clf_ax1, clf_ax2), (clf_ax3, sum_ax)) = plt.subplots(figsize=(13, 8), nrows=3, ncols=2) for ax in expr_ax1, expr_ax2, clf_ax1, clf_ax2, clf_ax3, sum_ax: ax.axis('off') use_mtype1, use_mtype2 = good_exs['Conv'].index[0] use_gene1 = use_mtype1.subtype_list()[0][0] use_gene2 = use_mtype2.subtype_list()[0][0] mtype_str1 = get_mtype_str(use_mtype1) mtype_str2 = get_mtype_str(use_mtype2) mtype_clr1 = sns.light_palette(variant_clrs['Point'], n_colors=7, as_cmap=False)[2] mtype_clr2 = sns.dark_palette(variant_clrs['Point'], n_colors=7, as_cmap=False)[4] coh_genes = cdata.get_features() ex_genes = { gene for gene, annot in cdata.gene_annot.items() if annot['Chr'] in { cdata.gene_annot[use_gene1]['Chr'], cdata.gene_annot[use_gene2] ['Chr'] } } ex_prop = len(ex_genes) / len(coh_genes) stat_tbl = pd.crosstab(stat_dict[use_mtype1], stat_dict[use_mtype2], margins=True) prop_tbl = stat_tbl / len(cdata.get_samples()) ovlp_test = fisher_exact(stat_tbl.iloc[:2, :2]) if '_' in args.cohort: coh_lbl = "{}({})".format(*args.cohort.split('_')) else: coh_lbl = args.cohort if args.cohort != 'beatAML': coh_lbl = "TCGA-{}".format(coh_lbl) fig.text(0, 1, "Inferring mutation similarity in the {} patient " "cohort".format(coh_lbl), size=21, ha='left', va='bottom', weight='semibold') expr_ax1.text(0, 0.96, '1', size=16, ha='right', va='top', bbox={ 'boxstyle': 'circle', 'facecolor': 'white', 'linewidth': 2.3 }) expr_ax1.text(0.03, 1, "Remove expression\nfeatures on the\nsame chromosome\n" "as {} or {}.".format(use_gene1, use_gene2), size=13, ha='left', va='top') heat_ax11 = inset_axes(expr_ax1, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.37, 0.05, 0.58 * ex_prop, 0.93), bbox_transform=expr_ax1.transAxes) heat_ax11.axis('off') sns.heatmap(expr_mat[:, :9], ax=heat_ax11, cmap=expr_cmap, center=0.5, cbar=False, linewidths=6 / 7, linecolor='0.93', alpha=7 / 13) heat_ax12 = inset_axes(expr_ax1, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.58 * ex_prop + 0.39, 0.05, 0.58 * (1 - ex_prop), 0.93), bbox_transform=expr_ax1.transAxes) heat_ax12.axis('off') sns.heatmap(expr_mat[:, 9:], ax=heat_ax12, cmap=expr_cmap, center=0.5, cbar=False, linewidths=3 / 7, linecolor='black') expr_ax1.axvline(x=0.58 * ex_prop + 0.38, ymin=0.03, ymax=1, linestyle=':', linewidth=2.3) if args.cohort == 'beatAML': expr_ax1.text(0.66, 0.99, "{} Kallisto RNA-seq features".format(len(coh_genes)), size=12, ha='center', va='bottom') else: expr_ax1.text(0.66, 0.99, "{} Firehose RNA-seq features".format(len(coh_genes)), size=12, ha='center', va='bottom') expr_ax1.text(0.98, 0.51, "{} tumour samples".format(len(cdata.get_samples())), size=12, ha='left', va='center', rotation=270) expr_ax1.text(0.58 * ex_prop + 0.37, 0.04, "{} features\nfiltered out".format(len(ex_genes)), size=11, ha='right', va='top') expr_ax1.text(0.58 * ex_prop + 0.39, 0.04, "{} features\nleft in".format(len(set(coh_genes) - ex_genes)), size=11, ha='left', va='top') expr_ax2.text(0, 0.96, '2', size=16, ha='right', va='top', bbox={ 'boxstyle': 'circle', 'facecolor': 'white', 'linewidth': 2.3 }) expr_ax2.text(0.03, 1, "Stratify samples\naccording to the\npresence of a pair" "\nof mutation types:\n\nM1)\n {}" "\n\nM2)\n {}".format(mtype_str1, mtype_str2), size=13, ha='left', va='top') heat_ax21 = inset_axes( expr_ax2, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.38, 0.06 + 0.88 * (1 - prop_tbl.loc[False, False]), 0.35, 0.88 * prop_tbl.loc[False, False]), bbox_transform=expr_ax2.transAxes) heat_ax21.axis('off') sns.heatmap(expr_mat[:12, 27:], ax=heat_ax21, cmap=expr_cmap, center=0.5, cbar=False, linewidths=3 / 7, linecolor='black') heat_ax22 = inset_axes( expr_ax2, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.38, 0.05 + 0.88 * prop_tbl.loc[True, 'All'], 0.35, 0.88 * prop_tbl.loc[False, True]), bbox_transform=expr_ax2.transAxes) heat_ax22.axis('off') sns.heatmap(expr_mat[12:14, 27:], ax=heat_ax22, cmap=expr_cmap, center=0.5, cbar=False, linewidths=3 / 7, linecolor='black') heat_ax23 = inset_axes( expr_ax2, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.38, 0.04 + 0.88 * prop_tbl.loc[True, True], 0.35, 0.88 * prop_tbl.loc[True, False]), bbox_transform=expr_ax2.transAxes) heat_ax23.axis('off') sns.heatmap(expr_mat[14:16, 27:], ax=heat_ax23, cmap=expr_cmap, center=0.5, cbar=False, linewidths=3 / 7, linecolor='black') heat_ax24 = inset_axes(expr_ax2, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.38, 0.03, 0.35, 0.88 * prop_tbl.loc[True, True]), bbox_transform=expr_ax2.transAxes) heat_ax24.axis('off') sns.heatmap(expr_mat[16:, 27:], ax=heat_ax24, cmap=expr_cmap, center=0.5, cbar=False, linewidths=3 / 7, linecolor='black') expr_ax2.add_patch( ptchs.Rectangle((0.75, 0.03), 0.03, 0.91, facecolor=variant_clrs['WT'], alpha=0.31)) expr_ax2.text(0.765, 0.93, 'M1', ha='center', va='top', size=8) expr_ax2.add_patch( ptchs.Rectangle((0.79, 0.03), 0.03, 0.91, facecolor=variant_clrs['WT'], alpha=0.31)) expr_ax2.text(0.805, 0.93, 'M2', ha='center', va='top', size=8) if args.cohort == 'beatAML': expr_ax2.text(0.84, 0.81, "variant calls", size=12, ha='left', va='top') else: expr_ax2.text(0.84, 1.03, "mc3\nvariant calls\n&\nFirehose\nGISTIC2\nCNA calls", size=12, ha='left', va='top') expr_ax2.add_patch( ptchs.Rectangle((0.75, 0.04), 0.03, 0.91 * prop_tbl.loc[True, 'All'], facecolor=mtype_clr1, alpha=0.43)) expr_ax2.add_patch( ptchs.Rectangle((0.79, 0.04), 0.03, 0.91 * prop_tbl.loc[True, True], facecolor=mtype_clr2, alpha=0.43)) expr_ax2.add_patch( ptchs.Rectangle((0.79, 0.0475 + 0.91 * prop_tbl.loc[True, 'All']), 0.03, 0.91 * prop_tbl.loc[False, True], facecolor=mtype_clr2, alpha=0.43)) expr_ax2.text(0.77, 0.02, "{:.1%}".format(prop_tbl.loc[True, 'All']), size=10, ha='right', va='top', rotation=45) expr_ax2.text(0.81, 0.02, "{:.1%}".format(prop_tbl.loc['All', True]), size=10, ha='right', va='top', rotation=45) expr_ax2.axhline(y=0.055 + 0.88 * (1 - prop_tbl.loc[False, False]), xmin=0.37, xmax=0.83, linewidth=1.1, linestyle=':', color='black', alpha=0.67) expr_ax2.axhline(y=0.045 + 0.88 * prop_tbl.loc[True, 'All'], xmin=0.37, xmax=0.83, linewidth=1.1, linestyle=':', color='black', alpha=0.67) expr_ax2.axhline(y=0.035 + 0.88 * prop_tbl.loc[True, True], xmin=0.37, xmax=0.83, linewidth=1.1, linestyle=':', color='black', alpha=0.67) expr_ax2.text(0.84, 0.03 + 0.83 * (1 - prop_tbl.loc[False, False]), "genomic\nco-occurence:\n\n {} samples with" "\n both mutations\n\n two-sided Fisher's" "\n exact test p-val:\n {:.3g}".format( stat_tbl.loc[True, True], ovlp_test[1]), size=9, ha='left', va='center') clf_ax1.text(0, 0.96, '3', ha='right', va='top', size=16, bbox={ 'boxstyle': 'circle', 'facecolor': 'white', 'linewidth': 2.3 }) clf_ax1.text(0.03, 1, "Train classifiers\nto separate the\n{} samples with\n{}\n" "and without\n{}\nfrom the {} samples\nwith neither " "mutation.".format(stat_tbl.loc[True, False], mtype_str1, mtype_str2, stat_tbl.loc[False, False]), size=13, ha='left', va='top') heat_ax31 = inset_axes( clf_ax1, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=( 0.41, 0.04 + 0.96 * (prop_tbl.loc[False, False] / prop_tbl.loc['All', False]), 0.34, 0.96 * (prop_tbl.loc[True, False] / prop_tbl.loc['All', False])), bbox_transform=clf_ax1.transAxes) heat_ax31.axis('off') sns.heatmap(expr_mat[2:4, 27:], ax=heat_ax31, cmap=expr_cmap, center=0.5, cbar=False, linewidths=3 / 7, linecolor='black') heat_ax31.text(0.5, 1.01, 'M1 & ~M2', size=9, ha='center', va='bottom', transform=heat_ax31.transAxes) heat_ax32 = inset_axes( clf_ax1, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=( 0.41, 0, 0.34, 0.96 * (prop_tbl.loc[False, False] / prop_tbl.loc['All', False])), bbox_transform=clf_ax1.transAxes) heat_ax32.axis('off') sns.heatmap(expr_mat[4:, 27:], ax=heat_ax32, cmap=expr_cmap, center=0.5, cbar=False, linewidths=3 / 7, linecolor='black') heat_ax32.text(0.5, -0.01, '~M1 & ~M2', size=9, ha='center', va='top', transform=heat_ax32.transAxes) clf_ax1.axhline(y=0.02 + 0.96 * (prop_tbl.loc[False, False] / prop_tbl.loc['All', False]), xmin=0.4, xmax=0.76, linestyle='--', linewidth=2.3) infer_vals1 = np.array(out_infer.loc[[(use_mtype1, use_mtype2)], 0][0]) wt_vals1 = np.concatenate(infer_vals1[~stat_dict[use_mtype1] & ~stat_dict[use_mtype2]]) mut_vals1 = np.concatenate(infer_vals1[stat_dict[use_mtype1] & ~stat_dict[use_mtype2]]) oth_vals1 = np.concatenate(infer_vals1[~stat_dict[use_mtype1] & stat_dict[use_mtype2]]) clf_ax1.add_patch( ptchs.FancyArrow(0.77, 0.52, dx=0.04, dy=0, width=0.02, length_includes_head=True, head_length=0.02, linewidth=1.1, facecolor='white', edgecolor='black')) vio_ax11 = inset_axes(clf_ax1, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.83, 0, 0.17, 1), bbox_transform=clf_ax1.transAxes) vio_ax11.axis('off') sns.kdeplot(wt_vals1, shade=True, color=variant_clrs['WT'], vertical=True, linewidth=0, cut=0, ax=vio_ax11) sns.kdeplot(mut_vals1, shade=True, color=mtype_clr1, vertical=True, linewidth=0, cut=0, ax=vio_ax11) clf_ax1.text(1, 0.99, "task AUC: {:.3f}".format(auc_dict[use_mtype1, use_mtype2][0]), size=11, ha='right', va='bottom') clf_ax2.text(0, 0.96, '4', ha='right', va='top', size=16, bbox={ 'boxstyle': 'circle', 'facecolor': 'white', 'linewidth': 2.3 }) clf_ax2.text(0.03, 1, "Use trained classifiers\nto predict mutation\nscores for " "the {}\nheld-out samples\nwith {}\nand without\n{}." "\nCompare these scores\nto the scores obtained\nin " "(3).".format(stat_tbl.loc[False, True], mtype_str2, mtype_str1), size=13, ha='left', va='top') vio_ax12 = inset_axes(clf_ax2, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.41, 0, 0.17, 1), bbox_transform=clf_ax2.transAxes) vio_ax12.axis('off') sns.kdeplot(wt_vals1, shade=True, color=variant_clrs['WT'], vertical=True, linewidth=0, cut=0, ax=vio_ax12) sns.kdeplot(mut_vals1, shade=True, color=mtype_clr1, vertical=True, linewidth=0, cut=0, ax=vio_ax12) vio_ax12.axhline(y=np.mean(wt_vals1), linewidth=2.1, color=variant_clrs['WT'], linestyle=':', alpha=0.71, clip_on=False) vio_ax12.axhline(y=np.mean(mut_vals1), linewidth=2.1, color=mtype_clr1, linestyle=':', alpha=0.71, clip_on=False) vio_ax12.text(vio_ax12.get_xlim()[1] / 1.7, np.mean(mut_vals1), "{:.2f} \u2192 1".format(np.mean(mut_vals1)), size=9, ha='center', va='bottom') vio_ax12.text(vio_ax12.get_xlim()[1] / 1.7, np.mean(wt_vals1), "{:.2f} \u2192 0".format(np.mean(wt_vals1)), size=9, ha='center', va='bottom') oth_ax1 = inset_axes(clf_ax2, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.57, 0, 0.17, 1), bbox_transform=clf_ax2.transAxes) oth_ax1.axis('off') sns.kdeplot(oth_vals1, shade=False, color=mtype_clr2, vertical=True, linewidth=1.9, cut=0, ax=oth_ax1) oth_ax1.set_ylim(vio_ax12.get_ylim()) oth_ax1.axhline(y=np.mean(oth_vals1), linewidth=3.3, color=mtype_clr2, linestyle=':', alpha=0.71, clip_on=False) siml1 = np.mean(oth_vals1) - np.mean(wt_vals1) siml1 /= np.mean(mut_vals1) - np.mean(wt_vals1) oth_ax1.text(oth_ax1.get_xlim()[1] * 0.83, np.mean(oth_vals1), "{:.2f}\u2192({:.2f})".format(np.mean(oth_vals1), siml1), size=11, ha='left', va='bottom', weight='semibold') clf_ax3.text(0, 0.96, '5', ha='right', va='top', size=16, bbox={ 'boxstyle': 'circle', 'facecolor': 'white', 'linewidth': 2.3 }) clf_ax3.text(0.03, 1, "Repeat (3) and (4) with the\ntwo sets of mutations " "reversed.", size=13, ha='left', va='top') heat_ax41 = inset_axes(clf_ax3, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0, 0.65, 0.29, 0.13), bbox_transform=clf_ax3.transAxes) heat_ax41.axis('off') sns.heatmap(expr_mat[2:6, 27:], ax=heat_ax41, cmap=expr_cmap, center=0.5, cbar=False, linewidths=3 / 7, linecolor='black') heat_ax42 = inset_axes(clf_ax3, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0, 0, 0.29, 0.61), bbox_transform=clf_ax3.transAxes) heat_ax42.axis('off') sns.heatmap(expr_mat[6:, 27:], ax=heat_ax42, cmap=expr_cmap, center=0.5, cbar=False, linewidths=3 / 7, linecolor='black') clf_ax3.axhline(y=0.63, xmin=-0.01, xmax=0.3, linestyle='--', linewidth=2.3, clip_on=False) clf_ax3.add_patch( ptchs.FancyArrow(0.31, 0.43, dx=0.04, dy=0, width=0.02, length_includes_head=True, head_length=0.02, linewidth=1.1, facecolor='white', edgecolor='black')) infer_vals2 = np.array(out_infer.loc[[(use_mtype1, use_mtype2)], 1][0]) wt_vals2 = np.concatenate(infer_vals2[~stat_dict[use_mtype1] & ~stat_dict[use_mtype2]]) mut_vals2 = np.concatenate(infer_vals2[~stat_dict[use_mtype1] & stat_dict[use_mtype2]]) oth_vals2 = np.concatenate(infer_vals2[stat_dict[use_mtype1] & ~stat_dict[use_mtype2]]) vio_ax2 = inset_axes(clf_ax3, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.37, 0, 0.17, 0.76), bbox_transform=clf_ax3.transAxes) vio_ax2.axis('off') sns.kdeplot(wt_vals2, shade=True, color=variant_clrs['WT'], vertical=True, linewidth=0, cut=0, ax=vio_ax2) sns.kdeplot(mut_vals2, shade=True, color=mtype_clr2, alpha=0.77, vertical=True, linewidth=0, cut=0, ax=vio_ax2) clf_ax3.text(0.41, 0.73, "task AUC: {:.3f}".format(auc_dict[use_mtype1, use_mtype2][1]), size=10, ha='center', va='bottom') vio_ax2.axhline(y=np.mean(wt_vals2), linewidth=2.1, color=variant_clrs['WT'], linestyle=':', alpha=0.71, clip_on=False) vio_ax2.axhline(y=np.mean(mut_vals2), linewidth=2.1, color=mtype_clr2, linestyle=':', alpha=0.71, clip_on=False) vio_ax2.text(vio_ax2.get_xlim()[1] / 1.7, np.mean(mut_vals2), "{:.2f} \u2192 1".format(np.mean(mut_vals2)), size=9, ha='center', va='bottom') vio_ax2.text(vio_ax2.get_xlim()[1] / 1.7, np.mean(wt_vals2), "{:.2f} \u2192 0".format(np.mean(wt_vals2)), size=9, ha='center', va='bottom') oth_ax2 = inset_axes(clf_ax3, width='100%', height='100%', loc=10, borderpad=0, bbox_to_anchor=(0.53, 0, 0.17, 0.76), bbox_transform=clf_ax3.transAxes) oth_ax2.axis('off') sns.kdeplot(oth_vals2, shade=False, color=mtype_clr1, vertical=True, linewidth=1.9, cut=0, ax=oth_ax2) oth_ax2.set_ylim(vio_ax2.get_ylim()) oth_ax2.axhline(y=np.mean(oth_vals2), linewidth=3.3, color=mtype_clr1, linestyle=':', alpha=0.71, clip_on=False) siml2 = np.mean(oth_vals2) - np.mean(wt_vals2) siml2 /= np.mean(mut_vals2) - np.mean(wt_vals2) oth_ax2.text(oth_ax2.get_xlim()[1] * 0.83, np.mean(oth_vals2), "{:.2f}\u2192({:.2f})".format(np.mean(oth_vals2), siml2), size=11, ha='left', va='bottom', weight='semibold') sum_ax.text(0, 0.96, '6', ha='right', va='top', size=16, bbox={ 'boxstyle': 'circle', 'facecolor': 'white', 'linewidth': 2.3 }) sum_ax.text(0.03, 1, "Combine the M1\u21d2M2 score calculated\nin (3) and the " "M1\u21d0M2 score calculated\nin (4) to infer the " "transcriptomic\nsimilarity M1\u21d4M2: ({:.2f})".format( (siml1 + siml2) / 2), size=13, ha='left', va='top') plt.tight_layout(pad=1.7, w_pad=0.4, h_pad=1.3) plt.savefig(os.path.join( plot_dir, args.cohort, "mutex-classification_{}__{}.svg".format(args.cohort, args.classif)), bbox_inches='tight', format='svg') plt.close()
import matplotlib.pyplot as plt import seaborn as sns print('Seaborn version :', sns.__version__) sns.set() sns.set_style('whitegrid') sns.set_color_codes() current_palette = sns.color_palette() sns.palplot(current_palette) sns.palplot(sns.color_palette('Blues')) sns.palplot(sns.dark_palette("purple")) plt.show()
def md_voldep(cutoff, wo_fit, vrange): """ md 計算の体積依存性を plot する """ path = "voldep/*/OSZICAR" dir_list = [os.path.dirname(x) for x in glob.glob(path)] average = [] sb_dark = seaborn.dark_palette("skyblue", 10, reverse=True) # sb_dark = seaborn.dark_palette("seagreen", 10, reverse=True) # sb_dark = seaborn.dark_palette("purple", 10, reverse=True) seaborn.set(palette=sb_dark) fig, axes = pylab.subplots(2, 1, figsize=(5, 10)) for dirc in dir_list: with open(os.path.join(dirc, 'OUTCAR')) as wfile: lines = wfile.readlines() meta = re.compile(r".*external pressure = \s*(.*)\s+kB.+") pressure = [] for line in lines: match = meta.match(line) if match: pressure.append(float(match.group(1))) with open(os.path.join(dirc, 'OSZICAR')) as wfile: lines = wfile.readlines() meta = re.compile(r".+T=\s*(.*)\s+E=\s*(.*)\s+F=.+") energy = [] for line in lines: match = meta.match(line) if match: energy.append(float(match.group(2))) data = [{'energy':e, 'pressure':p} for e, p in zip(energy, pressure)] dbox = DataBox(data) volume = float(dirc.split('_')[1]) press_ave = dbox['pressure'][cutoff:].mean() energy_ave = dbox['energy'][cutoff:].mean() average.append({'volume':volume, 'p': press_ave, 'e': energy_ave}) label = "V={0:.0f}".format(float(dirc.split("_")[-1])) seaborn.distplot(dbox['pressure'][cutoff:], label=label, ax=axes[0]) axes[0].legend(loc='upper left') axes[0].set_ylabel("Frequency") axes[0].set_xlabel("Pressuer (kbar)") average_data = DataBox(average) average_data.sorted('volume') if vrange != (0, 0): average_data = average_data.trim_data_within_range('volume', vrange) axes[1].plot(average_data['volume'], average_data['p'], 'p', color=sb_dark[-1]) axes[1].set_ylabel(r"Pressuer (kbar)") axes[1].set_xlabel(r"Volume ($\AA^3$)") fig.subplots_adjust(left=0.20) if not wo_fit: xi, yi = FitData.Stineman_interp_fit(average_data['volume'], average_data['p']) axes[1].plot(xi, yi) idx = np.abs(yi).argmin() print("v0: {0}, p0: {1}".format(xi[idx], yi[idx])) print("v0**(1/3): {0}".format(xi[idx]**(1/3))) axes[1].plot(xi[idx], yi[idx], 'o') pylab.savefig("pressure.eps")
def animate(self): self.W_S_plus = [ load_manifold_refactored( '../../../data/raw/manifolds/refined_for_c/L' + str(1) + '_' + self.orbitType + '_' + str(self.orbitIds[0]) + '_W_S_plus.txt'), load_manifold_refactored( '../../../data/raw/manifolds/refined_for_c/L' + str(2) + '_' + self.orbitType + '_' + str(self.orbitIds[1]) + '_W_S_plus.txt') ] self.W_S_min = [ load_manifold_refactored( '../../../data/raw/manifolds/refined_for_c/L' + str(1) + '_' + self.orbitType + '_' + str(self.orbitIds[0]) + '_W_S_min.txt'), load_manifold_refactored( '../../../data/raw/manifolds/refined_for_c/L' + str(2) + '_' + self.orbitType + '_' + str(self.orbitIds[1]) + '_W_S_min.txt') ] self.W_U_plus = [ load_manifold_refactored( '../../../data/raw/manifolds/refined_for_c/L' + str(1) + '_' + self.orbitType + '_' + str(self.orbitIds[0]) + '_W_U_plus.txt'), load_manifold_refactored( '../../../data/raw/manifolds/refined_for_c/L' + str(2) + '_' + self.orbitType + '_' + str(self.orbitIds[1]) + '_W_U_plus.txt') ] self.W_U_min = [ load_manifold_refactored( '../../../data/raw/manifolds/refined_for_c/L' + str(1) + '_' + self.orbitType + '_' + str(self.orbitIds[0]) + '_W_U_min.txt'), load_manifold_refactored( '../../../data/raw/manifolds/refined_for_c/L' + str(2) + '_' + self.orbitType + '_' + str(self.orbitIds[1]) + '_W_U_min.txt') ] self.numberOfOrbitsPerManifold = len( set(self.W_S_plus[0].index.get_level_values(0))) color_palette_green = sns.dark_palette( 'green', n_colors=self.numberOfOrbitsPerManifold) color_palette_red = sns.dark_palette( 'red', n_colors=self.numberOfOrbitsPerManifold) self.lines = [ self.ax.plot([], [], color=color_palette_red[idx], alpha=self.orbitAlpha)[0] for idx in range(self.numberOfOrbitsPerManifold) ] self.lines.extend([ self.ax.plot([], [], color=color_palette_green[idx], alpha=self.orbitAlpha)[0] for idx in range(self.numberOfOrbitsPerManifold) ]) self.lines.extend([ self.ax.plot([], [], color=color_palette_red[idx], alpha=self.orbitAlpha)[0] for idx in range(self.numberOfOrbitsPerManifold) ]) self.lines.extend([ self.ax.plot([], [], color=color_palette_green[idx], alpha=self.orbitAlpha)[0] for idx in range(self.numberOfOrbitsPerManifold) ]) self.lines.extend([ self.ax.plot([], [], color=color_palette_red[idx], alpha=self.orbitAlpha)[0] for idx in range(self.numberOfOrbitsPerManifold) ]) self.lines.extend([ self.ax.plot([], [], color=color_palette_green[idx], alpha=self.orbitAlpha)[0] for idx in range(self.numberOfOrbitsPerManifold) ]) self.lines.extend([ self.ax.plot([], [], color=color_palette_green[idx], alpha=self.orbitAlpha)[0] for idx in range(self.numberOfOrbitsPerManifold) ]) self.lines.extend([ self.ax.plot([], [], color=color_palette_red[idx], alpha=self.orbitAlpha)[0] for idx in range(self.numberOfOrbitsPerManifold) ]) # Text object to display absolute normalized time of trajectories within the manifolds self.timeText = self.ax.text2D(0.05, 0.05, s='$\|t\| \\approx 0$', transform=self.ax.transAxes, size=self.timeTextSize) # Plot zero velocity surface x_range = np.arange(self.xLim[0], self.xLim[1], 0.001) y_range = np.arange(self.yLim[0], self.yLim[1], 0.001) x_mesh, y_mesh = np.meshgrid(x_range, y_range) z_mesh = cr3bp_velocity(x_mesh, y_mesh, self.cLevel) if z_mesh.min() < 0: self.ax.contour(x_mesh, y_mesh, z_mesh, [z_mesh.min(), 0], colors='black', alpha=0.3) # Plot both orbits for k in range(2): orbit_df = load_orbit('../../../data/raw/orbits/refined_for_c/L' + str(k + 1) + '_' + self.orbitType + '_' + str(self.orbitIds[k]) + '.txt') self.ax.plot(orbit_df['x'], orbit_df['y'], orbit_df['z'], color=self.orbitColor, alpha=self.orbitAlpha, linewidth=self.orbitLinewidth) # Plot both primaries u = np.linspace(0, 2 * np.pi, 100) v = np.linspace(0, np.pi, 100) bodies_df = load_bodies_location() for body in bodies_df: x = bodies_df[body]['r'] * np.outer( np.cos(u), np.sin(v)) + bodies_df[body]['x'] y = bodies_df[body]['r'] * np.outer(np.sin(u), np.sin(v)) z = bodies_df[body]['r'] * np.outer(np.ones(np.size(u)), np.cos(v)) self.ax.plot_surface(x, y, z, color='black') # Plot Lagrange points 1 and 2 lagrange_points_df = load_lagrange_points_location() lagrange_point_nrs = ['L1', 'L2'] for lagrange_point_nr in lagrange_point_nrs: self.ax.scatter3D(lagrange_points_df[lagrange_point_nr]['x'], lagrange_points_df[lagrange_point_nr]['y'], lagrange_points_df[lagrange_point_nr]['z'], color='black', marker='x', s=self.lagrangePointMarkerSize) title = self.orbitTypeForTitle + ' $\{ \mathcal{W}^{S \pm}, \mathcal{W}^{U \pm} \}$ - Orthografic projection (C = ' + str( c_level) + ')' self.ax.set_xlim(self.xLim) self.ax.set_ylim(self.yLim) self.ax.set_zlim(self.zLim) self.ax.set_xlabel('x [-]') self.ax.set_ylabel('y [-]') self.ax.set_zlabel('z [-]') self.ax.grid(True, which='both', ls=':') # fig.tight_layout() self.fig.subplots_adjust(top=0.9) self.fig.suptitle(title, size=self.suptitleSize) # Fix overlap between labels and ticks self.ax.xaxis._axinfo['label']['space_factor'] = 4.0 self.ax.yaxis._axinfo['label']['space_factor'] = 4.0 self.ax.zaxis._axinfo['label']['space_factor'] = 4.0 self.initialElevation = self.ax.elev self.initialAzimuth = self.ax.azim # Determine the maximum value of t t_max = 0 for lagrange_point_idx in [0, 1]: for index in range(self.numberOfOrbitsPerManifold): t_max = max( t_max, abs(self.W_S_plus[lagrange_point_idx].xs(index).head( 1).index.values[0])) t_max = max( t_max, abs(self.W_S_min[lagrange_point_idx].xs(index).head( 1).index.values[0])) t_max = max( t_max, abs(self.W_U_plus[lagrange_point_idx].xs(index).tail( 1).index.values[0])) t_max = max( t_max, abs(self.W_U_min[lagrange_point_idx].xs(index).tail( 1).index.values[0])) print('Maximum value for t = ' + str(t_max) + ', animation t: = ') # Introduce a new time-vector for linearly spaced time throughout the animation self.t = np.linspace(0, t_max, np.round(t_max / 0.01) + 1) self.animation_function = animation.FuncAnimation( self.fig, self.update_lines, init_func=self.initiate_lines, frames=len(self.t), interval=1, blit=True) self.empty_writer_object = animation.writers['ffmpeg'] self.animation_writer = self.empty_writer_object( fps=30, metadata=dict(artist='Koen Langemeijer')) self.file_name = '../../../data/animations/manifolds/spatial_manifolds_rotating_' + self.orbitType + '_' + str( self.cLevel) + '.mp4' self.animation_function.save(self.file_name, writer=self.animation_writer)
import os, sys import subprocess import matplotlib.pyplot as plt from numpy import * import seaborn as sns from matplotlib.backends.backend_pdf import PdfPages import re sns.set_style("whitegrid") # Plot bar graphs of select set of pdb codes on two side by side bar graphs. palettes=['pastel', 'bold', 'muted', 'deep', 'dark', 'colorblind', 'husl', 'hls'] cp = sns.dark_palette('Bisque', 4) cp = sns.color_palette(palettes[4],10) def plot_rfree(codes, ax): noafitt = [] afitt = [] for code in codes: with open('%s/%s_no_afitt___001.log' %(code,code)) as f: rfree=f.readlines()[-1].strip().split()[-1] noafitt.append(float(rfree)) with open('%s/%s_sc_10_adp__001.log' %(code,code)) as f: rfree=f.readlines()[-1].strip().split()[-1] afitt.append(float(rfree))
def plot_connectivity_seaborn(con, node_names, indices=None, n_lines=None, node_angles=None, node_width=None, node_size=None, node_thresh=None, con_thresh=None, node_colors=None, facecolor='black', textcolor='white', node_edgecolor='black', linewidth=1.1, colormap='hot', vmin=None, vmax=None, colorbar=True, title=None, colorbar_size=0.2, colorbar_pos=(-0.3, 0.1), fontsize_title=12, fontsize_names=7, fontsize_colorbar=8, padding=6., fig=None, subplot=111, interactive=False): """Visualize connectivity as a circular graph, using circles to identify ROIs, their size is proportional to the sum of the connections it has in absolute value. Note: This code is based on the circle graph example by Nicolas P. Rougier http://www.loria.fr/~rougier/coding/recipes.html Parameters ---------- con : array Connectivity scores. Can be a square matrix, or a 1D array. If a 1D array is provided, "indices" has to be used to define the connection indices. node_names : list of str Node names. The order corresponds to the order in con. indices : tuple of arrays | None Two arrays with indices of connections for which the connections strenghts are defined in con. Only needed if con is a 1D array. n_lines : int | None If not None, only the n_lines strongest connections (strength=abs(con)) are drawn. node_angles : array, shape=(len(node_names,)) | None Array with node positions in degrees. If None, the nodes are equally spaced on the circle. See mne.viz.circular_layout. node_width : float | None Width of each node in degrees. If None, "360. / len(node_names)" is used. node_size : array, shape=(len(node_names,)) | None Size of the circles. con_thresh : float | None Threshold of the connection used to exclude arcs with a smaller absolute value to be plotted. node_colors : list of tuples | list of str List with the color to use for each node. If fewer colors than nodes are provided, the colors will be repeated. Any color supported by matplotlib can be used, e.g., RGBA tuples, named colors. facecolor : str Color to use for background. See matplotlib.colors. textcolor : str Color to use for text. See matplotlib.colors. node_edgecolor : str Color to use for lines around nodes. See matplotlib.colors. linewidth : float Line width to use for connections. colormap : str Colormap to use for coloring the connections. vmin : float | None Minimum value for colormap. If None, it is determined automatically. vmax : float | None Maximum value for colormap. If None, it is determined automatically. colorbar : bool Display a colorbar or not. title : str The figure title. colorbar_size : float Size of the colorbar. colorbar_pos : 2-tuple Position of the colorbar. fontsize_title : int Font size to use for title. fontsize_names : int Font size to use for node names. fontsize_colorbar : int Font size to use for colorbar. padding : float Space to add around figure to accommodate long labels. fig : None | instance of matplotlib.pyplot.Figure The figure to use. If None, a new figure with the specified background color will be created. subplot : int | 3-tuple Location of the subplot when creating figures with multiple plots. E.g. 121 or (1, 2, 1) for 1 row, 2 columns, plot 1. See matplotlib.pyplot.subplot. interactive : bool When enabled, left-click on a node to show only connections to that node. Right-click shows all connections. Returns ------- fig : instance of matplotlib.pyplot.Figure The figure handle. axes : instance of matplotlib.axes.PolarAxesSubplot The subplot handle. """ import matplotlib.pyplot as plt import matplotlib.path as m_path import matplotlib.patches as m_patches from mne.externals.six import string_types from functools import partial import seaborn as sns n_nodes = len(node_names) if node_angles is not None: if len(node_angles) != n_nodes: raise ValueError('node_angles has to be the same length ' 'as node_names') # convert it to radians node_angles = node_angles * np.pi / 180 else: # uniform layout on unit circle node_angles = np.linspace(0, 2 * np.pi, n_nodes, endpoint=False) if node_width is None: node_width = 2 * np.pi / n_nodes else: node_width = node_width * np.pi / 180 if node_colors is not None: if len(node_colors) < n_nodes: node_colors = cycle(node_colors) node_colors = [node_colors.next() for _ in range(n_nodes)] else: # assign colors using colormap node_colors = [ plt.cm.spectral(i / float(n_nodes)) for i in range(n_nodes) ] if node_size is None: node_size = (con.sum(0) / con.sum(0).max()) * 5 if not isinstance(node_size, np.ndarray): node_size = np.full(len(node_names), node_size) # handle 1D and 2D connectivity information if con.ndim == 1: if indices is None: raise ValueError('indices has to be provided if con.ndim == 1') elif con.ndim == 2: if con.shape[0] != n_nodes or con.shape[1] != n_nodes: raise ValueError('con has to be 1D or a square matrix') # we use the lower-triangular part indices = np.tril_indices(n_nodes, -1) con = con[indices] else: raise ValueError('con has to be 1D or a square matrix') # Draw lines between connected nodes, only draw the strongest connections if n_lines is not None and len(con) > n_lines: if con_thresh != None: con_thresh = np.sort(np.abs(con).ravel())[-n_lines] else: if con_thresh == None: con_thresh = 0. # get the connections which we are drawing and sort by connection strength # this will allow us to draw the strongest connections first # This is to plot in gray the lower connections draw_thresh = con_thresh / 1.5 con_abs = np.abs(con) con_draw_idx = np.where(con_abs >= draw_thresh)[0] #con_draw_idx = np.where(con_abs >= con_thresh)[0] con = con[con_draw_idx] con_abs = con_abs[con_draw_idx] indices = [ind[con_draw_idx] for ind in indices] # now sort them sort_idx = np.argsort(con_abs) con_abs = con_abs[sort_idx] con = con[sort_idx] indices = [ind[sort_idx] for ind in indices] # Get vmin vmax for color scaling if vmin is None: vmin = np.min(con[np.abs(con) >= con_thresh]) if vmax is None: vmax = np.max(con) vrange = vmax - vmin # We want to add some "noise" to the start and end position of the # edges: We modulate the noise with the number of connections of the # node and the connection strength, such that the strongest connections # are closer to the node center nodes_n_con = np.zeros((n_nodes), dtype=np.int) for i, j in zip(indices[0], indices[1]): nodes_n_con[i] += 1 nodes_n_con[j] += 1 # initalize random number generator so plot is reproducible rng = np.random.mtrand.RandomState(seed=0) n_con = len(indices[0]) noise_max = 0.25 * node_width start_noise = rng.uniform(-noise_max, noise_max, n_con) end_noise = rng.uniform(-noise_max, noise_max, n_con) nodes_n_con_seen = np.zeros_like(nodes_n_con) # get the colormap if isinstance(colormap, string_types): str_cmap = colormap colormap = plt.get_cmap(colormap) axes = plt.gca() """ # Make figure background the same colors as axes if fig is None: fig = plt.figure(figsize=(13, 13), facecolor=facecolor) # Use a polar axes if not isinstance(subplot, tuple): subplot = (subplot,) axes = plt.subplot(*subplot, polar=True, axisbg=facecolor) """ # No ticks, we'll put our own plt.xticks([]) plt.yticks([]) # Set y axes limit, add additonal space if requested #plt.ylim(0, 10 + padding) for i, (start, end) in enumerate(zip(indices[0], indices[1])): nodes_n_con_seen[start] += 1 nodes_n_con_seen[end] += 1 start_noise[i] *= ((nodes_n_con[start] - nodes_n_con_seen[start]) / float(nodes_n_con[start])) end_noise[i] *= ((nodes_n_con[end] - nodes_n_con_seen[end]) / float(nodes_n_con[end])) # scale connectivity for colormap (vmin<=>0, vmax<=>1) con_val_scaled = (con - vmin) / vrange con_thresh_scaled = (con_thresh - vmin) / vrange # Finally, we draw the connections nodes = [] for pos, (i, j) in enumerate(zip(indices[0], indices[1])): # Start point t0, r0 = node_angles[i], 1.2 # End point t1, r1 = node_angles[j], 1.2 # Some noise in start and end point t0 += start_noise[pos] t1 += end_noise[pos] verts = [(t0, r0), (t0, 0.5), (t1, 0.5), (t1, r1)] codes = [ m_path.Path.MOVETO, m_path.Path.CURVE4, m_path.Path.CURVE4, m_path.Path.LINETO ] path = m_path.Path(verts, codes) if np.abs(con[pos]) <= con_thresh: #colormap = plt.get_cmap('gray') alpha = 0.4 mult = 1.2 else: nodes.append([i, j]) colormap = plt.get_cmap(str_cmap) alpha = 0.8 mult = np.abs(con[pos]) / con.max() * 3 color = colormap(con_val_scaled[pos]) # Actual line patch = m_patches.PathPatch(path, fill=False, edgecolor=color, linewidth=linewidth**mult, alpha=alpha, zorder=0) axes.add_patch(patch) # Draw ring with colored nodes height = np.ones(n_nodes) * 1.2 nodes = np.unique(np.hstack(nodes)) for i, (x, y) in enumerate(zip(node_angles, height)): cmap = sns.dark_palette(node_colors[i], n_colors=15, as_cmap=True, reverse=True) _ = axes.scatter(x, y, s=node_size[i], c=cmap(node_size[i] / node_size.sum()), zorder=1, alpha=0.9, linewidths=2, facecolor='.9') axes.set_ylim(0, 2) ''' bars = axes.bar(node_angles, height, width=node_width, bottom=9, edgecolor=node_edgecolor, lw=2, facecolor='.9', align='center') for bar, color in zip(bars, node_colors): bar.set_facecolor(color) ''' # Draw node labels angles_deg = 180 * node_angles / np.pi for i, (name, angle_rad, angle_deg, n_size) in enumerate( zip(node_names, node_angles, angles_deg, node_size)): if angle_deg >= 270: ha = 'left' else: # Flip the label, so text is always upright angle_deg += 180 ha = 'right' if i in nodes: txt_color = textcolor txt_size = fontsize_names + 3 axes.text(angle_rad, 1.25, name, size=txt_size, rotation=angle_deg, rotation_mode='anchor', horizontalalignment=ha, verticalalignment='center', color=txt_color) if title is not None: plt.title(title, color=textcolor, fontsize=fontsize_title, axes=axes) if colorbar: norm = plt.Normalize(vmin=vmin, vmax=vmax) sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm) sm.set_array(np.linspace(vmin, vmax)) cb = plt.colorbar(sm, ax=axes, use_gridspec=False, shrink=colorbar_size, anchor=colorbar_pos) cb_yticks = plt.getp(cb.ax.axes, 'yticklabels') cb.ax.tick_params(labelsize=fontsize_colorbar) plt.setp(cb_yticks, color=textcolor) return fig, axes
# 使用xkcd颜色来命名颜色:包含了一套众包努力的正对随机RGB色的命名,产生954个可随时通过xdcd_rgb # 字典中调用的命名颜色。 plt.plot([0, 1], [0, 1], sns.xkcd_rgb['pale red'], lw=3) plt.plot([0, 1], [0, 2], sns.xkcd_rgb['medium green'], lw=3) plt.plot([0, 1], [0, 3], sns.xkcd_rgb['denim blue'], lw=3) plt.show() #连续色板:色彩岁数据变化,比如数据越来越重要,颜色就越来越深 # sns.palplot(sns.color_palette('Blues'))#由浅到深 # plt.show() # # sns.palplot(sns.color_palette('Blues_r'))#由深到浅 # plt.show() # # sns.palplot(sns.color_palette('cubehelix',8))#线性变换的,色弱 # plt.show() # # sns.palplot(sns.cubehelix_palette(8,start=5,rot=75))#指定区间变化 # plt.show() #light_palette()和 dark_palette()调用定制连续色板 # sns.palplot(sns.dark_palette('green')) # sns.palplot(sns.light_palette('blue',reverse=True))#翻转颜色深浅 # plt.show() #渐变色 x, y = np.random.multivariate_normal([0, 0], [[1, -.5], [-.5, 1]], size=300).T pal = sns.dark_palette('green', as_cmap=True) sns.kdeplot(x, y, cmap=pal) plt.show()
def umap_dataset_properties_all_categories(best_values: pd.DataFrame, best_names: pd.DataFrame, cached_metaprops: str, random_state=5): if len(best_values.index) < 4: print("Can't create meaningful UMAP of less than 4 points") return if os.path.exists(cached_metaprops): df_metaprops = pd.read_csv(cached_metaprops, index_col=0) else: tasks = list(map(int, best_values.index)) dataset_ids = [ openml.tasks.get_task(task).dataset_id for task in tasks ] # This will take a while to get # Hence the caching dataset_metaprops = [ openml.datasets.get_dataset(dataset_id).qualities for dataset_id in dataset_ids ] available_keys = reduce( lambda acc, metaprops: acc.intersection(metaprops.keys()), dataset_metaprops, set(dataset_metaprops[0].keys())) dict_metaprops = { k: [metaprop[k] for metaprop in dataset_metaprops] for k in available_keys } df_metaprops = pd.DataFrame.from_dict(dict_metaprops, orient='index', columns=tasks) df_metaprops.to_csv(cached_metaprops) # Drop features that have more than 30% missing cut_percentage = 0.00 # Most features have 0%, 12% or 67% missing for row in df_metaprops.index: missing = sum(df_metaprops.loc[row].isnull()) / len( df_metaprops.loc[row]) if missing > cut_percentage: df_metaprops.drop(index=row, inplace=True) # Convert the rest of the nans to the mean (8/62 had 24/48 missing features) df_metaprops = df_metaprops.apply(lambda row: row.fillna(row.mean()), axis=1) df_metaprops = df_metaprops.T # Make the tasks be on the index # Scale Data according to UMAPS recommendation df_scaled_metaprops = StandardScaler().fit_transform(df_metaprops) # Use UMAP to produce embedding # Doesn't really make sense with low number of datasets n_datasets = len(df_metaprops) K = math.ceil(n_datasets / 2) if n_datasets < 20 else 10 umapper = UMAP(n_neighbors=K, random_state=random_state) embeddings = umapper.fit_transform(df_scaled_metaprops) figsize = (10, 12) fig = plt.figure(figsize=figsize) ax = fig.add_subplot(1, 1, 1) # We choose the label name of which performed better labels = [ best_names.loc[task, category] for task, category in best_values.idxmax(axis=1).items() ] # Assign colors so selectors and baselines are visually distinct classifier_names = set(filter(lambda name: 'classifier' in name, labels)) selector_names = set(filter(lambda name: 'selector' in name, labels)) baseline_names = set(filter(lambda name: 'baseline' in name, labels)) classifier_colors = sns.dark_palette("#69d", reverse=True, n_colors=len(classifier_names) * 2) selector_colors = sns.color_palette('viridis_r', len(selector_names) * 2) baseline_colors = sns.color_palette('rocket', len(baseline_names)) cmap = { **{name: selector_colors[i] for i, name in enumerate(selector_names)}, **{name: baseline_colors[i] for i, name in enumerate(baseline_names)}, **{ name: classifier_colors[i] for i, name in enumerate(classifier_names) }, } colors = [cmap[label] for label in labels] ax.scatter(embeddings[:, 0], embeddings[:, 1], c=colors) legend_lines = [ Line2D([0], [0], color='w', marker='o', markerfacecolor=col, label=name.replace('_', ' ')) for name, col in cmap.items() ] ax.legend(handles=legend_lines) ax.set_xlabel('UMAP axis 1') ax.set_ylabel('UMAP axis 2') ax.set_title('UMAP projection of dataset meta-features - all categories') return fig
['Oligo Enpp6_1', 'Oligo Enpp6_2', 'Oligo Opalin'], ['Sncg Ptprk'], ['Endo Slc38a5', 'Endo Slc38a5_Peri_2', 'Endo Slc38a5_Peri_1'] ] macosko_regev_colors = [ sns.color_palette("Greens")[2:6], # Pvalb sns.light_palette("green", 5)[0:3], # Pvalb Ex sns.light_palette("green", 5)[3:5], # Pvalb Astro sns.light_palette("orange", 6), # L2/3 sns.light_palette('red')[1:6], # Sst sns.light_palette("cyan", 3), # L5 PT sns.light_palette('purple', 8)[1:8], # L5 IT Tcap sns.light_palette('purple', 7)[4:7], # L5 IT Aldh1a7 sns.light_palette("navy", 7)[3:5], # L5 NP sns.light_palette("brown", 7)[2:7], # L6 IT sns.dark_palette("brown", 8)[1:5], # L6 CT sns.dark_palette("green", 8)[5:7], # L6 sns.dark_palette("yellow", 7)[1:4], # Lamp5 sns.dark_palette("yellow", 7)[4:7], # Vip sns.color_palette("Paired", 4), # Astro OPC VLMC sns.color_palette('Greys', 3), # Oligo sns.dark_palette('tan'), # sncg sns.light_palette('hotpink', 3) ] # endo] macosko_regev_labels_flattened = [ l1 for l2 in macosko_regev_labels for l1 in l2 ] macosko_regev_colors = [c1 for c2 in macosko_regev_colors for c1 in c2] macosko_regev_dictionary = dict(
plot_data_timeline_drvc.append(timeline_db[timestamp]['drive_count']) plot_data_timeline_sys.append(timeline_db[timestamp]['system_count']) df_timeline = pd.DataFrame() df_timeline['Timestamp'] = plot_data_timeline_timestamps df_timeline['Date'] = plot_data_timeline_dates df_timeline['Capacity (TB)'] = plot_data_timeline_caps df_timeline['Drive Count'] = plot_data_timeline_drvc df_timeline['System Count'] = plot_data_timeline_sys df_timeline = df_timeline.sort_values(by=['Timestamp'], ascending=[True]) #ax = sns.swarmplot(x='Timestamp', y="Capacity", data=df_timeline) fig10, ax23 = plt.subplots(1) sns.stripplot(x='Date', y="Capacity (TB)", data=df_timeline, ax=ax23, palette=sns.dark_palette("purple"), size=10) fig10.subplots_adjust(bottom=0.2, left=0.1, right=0.9, top=0.95) plt.setp(ax23.xaxis.get_majorticklabels(), rotation=90) plt.setp(ax23.get_xticklabels()[::], visible=False) plt.setp(ax23.get_xticklabels()[::5], visible=True) ax23.set_ylim(bottom=0) fig10.savefig(timeline_caps_plot_path) fig11, ax24 = plt.subplots(1) sns.stripplot(x='Date', y="Drive Count", data=df_timeline, ax=ax24, palette=sns.dark_palette("purple"), size=10)
extra_statements['ppf'] = """ multi = stats.rv_discrete("resampling", values=(np.arange(N), W)) B = multi.ppf(stats.uniform.rvs(size=M)) """ results = [] for N in Ns: for method, state in statements.items(): cputime = (1./nrep)*timeit.timeit(state, setup=setup%(N,N), number=nrep) results.append({'N':N, 'method':method, 'cputime':cputime}) # PLOT # ==== savefigs = False # change this to save the plots as PDFs plt.style.use('ggplot') sb.set_palette(sb.dark_palette("lightgray", n_colors=4, reverse=True)) plt.figure() for method in statements.keys(): plt.plot(Ns, [ [r['cputime'] for r in results if r['method']==method and r['N']==N] for N in Ns], label=method) plt.xscale('log') plt.yscale('log') plt.xlabel(r'$N$') plt.ylabel('CPU time (s)') plt.legend(loc=2) if savefigs: plt.savefig('comparison_numba_searchsorted.pdf') plt.show()
def __init__(self, orbit_type, lagrange_point_nr, orbit_id_per_c): print('=======================') print(str(orbit_type) + ' in L' + str(lagrange_point_nr)) print('=======================') self.orbitType = orbit_type self.orbitIdPerC = orbit_id_per_c self.orbitTypeForTitle = orbit_type.capitalize() if (self.orbitTypeForTitle == 'Horizontal') or (self.orbitTypeForTitle == 'Vertical'): self.orbitTypeForTitle += ' Lyapunov' self.lagrangePointNr = lagrange_point_nr EARTH_GRAVITATIONAL_PARAMETER = 3.986004418E14 SUN_GRAVITATIONAL_PARAMETER = 1.32712440018e20 MOON_GRAVITATIONAL_PARAMETER = SUN_GRAVITATIONAL_PARAMETER / ( 328900.56 * (1.0 + 81.30059)) self.massParameter = MOON_GRAVITATIONAL_PARAMETER / ( MOON_GRAVITATIONAL_PARAMETER + EARTH_GRAVITATIONAL_PARAMETER) initial_conditions_file_path = '../../data/raw/orbits/L' + str( lagrange_point_nr) + '_' + orbit_type + '_initial_conditions.txt' initial_conditions_incl_m_df = load_initial_conditions_incl_M( initial_conditions_file_path) self.C = [] self.orbitDf = [] self.W_S_plus = [] self.W_S_min = [] self.W_U_plus = [] self.W_U_min = [] for c_level in reversed(sorted(orbit_id_per_c)): orbit_id = orbit_id_per_c[c_level] # self.C.append(initial_conditions_incl_m_df.iloc[orbit_id][0]) self.orbitDf.append( load_orbit('../../data/raw/orbits/refined_for_c/L' + str(lagrange_point_nr) + '_' + orbit_type + '_' + str(orbit_id) + '.txt')) self.C.append( computeJacobiEnergy(self.orbitDf[-1].iloc[0]['x'], self.orbitDf[-1].iloc[0]['y'], self.orbitDf[-1].iloc[0]['z'], self.orbitDf[-1].iloc[0]['xdot'], self.orbitDf[-1].iloc[0]['ydot'], self.orbitDf[-1].iloc[0]['zdot'])) self.W_S_plus.append( load_manifold_refactored( '../../data/raw/manifolds/refined_for_c/L' + str(lagrange_point_nr) + '_' + orbit_type + '_' + str(orbit_id) + '_W_S_plus.txt')) self.W_S_min.append( load_manifold_refactored( '../../data/raw/manifolds/refined_for_c/L' + str(lagrange_point_nr) + '_' + orbit_type + '_' + str(orbit_id) + '_W_S_min.txt')) self.W_U_plus.append( load_manifold_refactored( '../../data/raw/manifolds/refined_for_c/L' + str(lagrange_point_nr) + '_' + orbit_type + '_' + str(orbit_id) + '_W_U_plus.txt')) self.W_U_min.append( load_manifold_refactored( '../../data/raw/manifolds/refined_for_c/L' + str(lagrange_point_nr) + '_' + orbit_type + '_' + str(orbit_id) + '_W_U_min.txt')) self.numberOfOrbitsPerManifold = len( set(self.W_S_plus[0].index.get_level_values(0))) self.figSize = (7 * (1 + np.sqrt(5)) / 2, 7 * 2) blues = sns.color_palette('Blues', 100) greens = sns.color_palette('BuGn', 100) self.colorPaletteStable = sns.dark_palette( 'green', n_colors=self.numberOfOrbitsPerManifold) self.colorPaletteUnstable = sns.dark_palette( 'red', n_colors=self.numberOfOrbitsPerManifold) self.plottingColors = { 'lambda1': blues[40], 'lambda2': greens[50], 'lambda3': blues[90], 'lambda4': blues[90], 'lambda5': greens[70], 'lambda6': blues[60], 'singleLine': blues[80], 'doubleLine': [greens[50], blues[80]], 'tripleLine': [blues[40], greens[50], blues[80]], 'W_S_plus': self.colorPaletteStable[90], 'W_S_min': self.colorPaletteStable[40], 'W_U_plus': self.colorPaletteUnstable[90], 'W_U_min': self.colorPaletteUnstable[40], 'limit': 'black', 'orbit': 'navy' } self.suptitleSize = 20 pass
# see also: https://github.com/mwaskom/seaborn/issues/315 scaled_size = 0.005 * s plt.scatter(x, y, s=scaled_size, edgecolor=color, facecolor='none', linewidth=1, **kwargs) g = sns.FacetGrid(df[df['Year'] % 25 == 0], col="Year", hue='PopulationCategory', col_wrap=2, height=3.5, aspect=2, palette=sns.dark_palette('Red', 5), subplot_kws=dict(projection=cartopy.crs.Mollweide())) g.map(scatterplot, "Longitude", "Latitude", "Population", transform=cartopy.crs.PlateCarree(), zorder=10) for ax in g.axes.ravel(): ax.add_feature(cartopy.feature.COASTLINE) ax.add_feature(cartopy.feature.BORDERS) ax.set_global() plt.show()
def microMacroPDF(name, step, pData, varsDir, weight, mcFiles, loadWeights=False): import seaborn as sns from resample import unWeighted_resample params = { 'lines.linewidth': 1.5, 'backend': 'ps', 'axes.labelsize': 18, 'font.size': 18, 'legend.fontsize': 16, 'xtick.labelsize': 16, 'ytick.labelsize': 16, 'text.usetex': True, 'font.family': 'serif', 'legend.edgecolor': 'k', 'legend.fancybox': False } matplotlib.rcParams['text.latex.preamble'] = [r"\usepackage{amsmath}"] matplotlib.rcParams.update(params) # read in the original sampled parameter set Xlabels = [ r'$E_c$' + r' $(\mathrm{GPa})$', r'$\mu$', r'$k_m$' + r' $(\mu\mathrm{Nm)}$', r'$\eta_m$' ] Ylabels = [r'$p$' + r' $(\mathrm{MPa})$', r'$q/p$', r'$n$', r'$C^*$'] if loadWeights: wPerPair = np.load(varsDir + '/wPerPair%i.npy' % step).item() else: wPerPair = {} # Monte Carlo sequence nSample, numOfObs = weight.shape enQOIs = np.zeros([4, nSample]) for i in range(nSample): C, CN, K0, e_r11, e_r21, e_a1, n1, overlap, p1, q1 = np.genfromtxt( mcFiles[i]).transpose() enQOIs[0, i] = p1[step] enQOIs[1, i] = q1[step] / p1[step] enQOIs[2, i] = n1[step] enQOIs[3, i] = CN[step] # importance resampling ResampleIndices = unWeighted_resample(weight[:, step], nSample * 10) particles = np.copy(pData) particles[0, :] /= 1e9 particles[2, :] /= 1e3 particles = particles[:, ResampleIndices] enQOIs = enQOIs[:, ResampleIndices] # remove porosity as QOI if iterNO > 0 if 'iterPF0' not in mcFiles[0]: enQOIs = np.vstack([enQOIs[:2, :], enQOIs[-1, :]]) for i in [9, 10, 11, 12]: wPerPair[i] = wPerPair[i + 4] Ylabels = [r'$p$' + r' $(\mathrm{MPa})$', r'$q/p$', r'$C^*$'] # plot figures figNO = 0 fig = plt.figure('microMacroUQ_iterPF' + varsDir[-1] + '_%i' % step, figsize=(12 / 2.54, 12 / 2.54)) sns.set(style="ticks", rc=matplotlib.rcParams) for i in range(enQOIs.shape[0]): for j in range(particles.shape[0]): figNO += 1 # plot histograms at the initial and final steps ax = fig.add_subplot(enQOIs.shape[0], 4, figNO) cmap = sns.dark_palette("black", as_cmap=True) #~ ax = sns.kdeplot(,,cmap=cmap,kernel='gau',cut=3,n_levels=20,bw='silverman',linewidths=0.8); ax.grid() if loadWeights: minScore, maxScore, p0, w0 = wPerPair[figNO] else: data = np.array([particles[j], enQOIs[i]]) pMin = [min(particles[j]), min(enQOIs[i])] pMax = [max(particles[j]), max(enQOIs[i])] minScore, maxScore, p0, w0 = getLogWeightFromGMM( data.T, pMin, pMax, 1e-2) wPerPair[figNO] = (minScore, maxScore, p0, w0) X, Y = p0 plt.contour(X, Y, w0, cmap=cmap, levels=np.linspace(minScore - abs(minScore) * 0.1, maxScore, 10), linewidths=0.7) plt.grid() ax.locator_params(axis='both', nbins=2) ax.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off') if i == enQOIs.shape[0] - 1: ax.set_xlabel(Xlabels[j], size=params['font.size'], labelpad=10) ax.tick_params(axis='both', which='both', bottom='on', top='off', labelbottom='on', right='off', left='off', labelleft='off', labelsize=params['xtick.labelsize']) if j == 0: ax.set_ylabel(Ylabels[i], size=params['font.size'], labelpad=10) ax.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='on', labelleft='on', labelsize=params['xtick.labelsize']) if i == enQOIs.shape[0] - 1 and j == 0: ax.tick_params(axis='both', which='both', bottom='on', top='off', labelbottom='on', right='off', left='on', labelleft='on', labelsize=params['xtick.labelsize']) if i != enQOIs.shape[0] - 1 and j != 0: ax.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off') xMin, xMax = ax.get_xlim() yMin, yMax = ax.get_ylim() dx = (xMax - xMin) / 4 dy = (yMax - yMin) / 4 ax.set_xticks( np.round([xMin + dx, xMax - dx], int(np.ceil(-np.log10(dx))))) ax.set_yticks( np.round([yMin + dy, yMax - dy], int(np.ceil(-np.log10(dy))))) if not loadWeights: np.save(varsDir + '/wPerPair%i.npy' % step, wPerPair) plt.tight_layout() fig.subplots_adjust(left=0.21, bottom=0.16, right=0.99, top=0.99, hspace=0., wspace=0.) plt.savefig('microMacroUQ_iterPF' + varsDir[-1] + '_%i.pdf' % step, dpi=600) #~ plt.show() return wPerPair
def build_graph(self, min_overlap=1, node_cmap='viridis', plot_cliques=False): """ Builds a graph from the dataframe from R :return: """ G = nx.Graph() edge_map = defaultdict(dict) gene_ids = self.df[self.overlap_column].values gene_ids = [set(genes.split(self.overlap_sep)) for genes in gene_ids] # Turn it into a list all_genes = 0 for g in gene_ids: all_genes += len(g) min_overlap = min_overlap # Want to iterate over and get the maps between the two for i, id_i in enumerate(self.df[self.id].values): for j, id_j in enumerate(self.df[self.id].values): if i != j: if edge_map.get(id_j): if edge_map[id_j].get(id_i): continue else: overlapping_genes = len(gene_ids[i] & gene_ids[j]) if overlapping_genes >= min_overlap: edge_map[id_i][id_j] = overlapping_genes else: overlapping_genes = len(gene_ids[i] & gene_ids[j]) if overlapping_genes >= min_overlap: edge_map[id_i][id_j] = overlapping_genes edges = [] for node1 in edge_map: for node2 in edge_map[node1]: edges.append((node1, node2)) seen_nodes = [] edge_groups = defaultdict(list) for node_from, node_to_lst in edge_map.items(): if node_from not in seen_nodes: # Now we want to traverse the graph visiting each node for node in node_to_lst: if node not in edge_groups[node_from] and node not in seen_nodes: edge_groups[node_from].append(node) seen_nodes.append(node) if edge_map.get(node) and node not in seen_nodes: for node2 in edge_map.get(node): edge_groups[node_from].append(node2) seen_nodes.append(node2) seen_nodes.append(node_from) edge_groups[node_from].append(node_from) G.add_edges_from(edges) nodes = G.nodes() # Check that all nodes have been added and if not add them nodes_to_add = [node_id for node_id in self.df[self.id].values if node_id not in nodes] for node in nodes_to_add: G.add_node(node) edge_groups[node].append(node) # So that we actually draw it! # Now we want a list of node sizes and colours mins = np.min(self.df[self.size].values) maxs = np.max(self.df[self.size].values) norms = maxs - mins counts = [20 + 100 * (maxs - c)/norms for c in self.df[self.size].values] sizes = counts colour = self.df[self.color].values # Need to create a layout when doing # separate calls to draw nodes and edges pos = nx.spring_layout(G, k=2) #nx.kamada_kawai_layout(G) # nx.spring_layout(G, k=2) # nx.draw_networkx_nodes(G, pos, cmap=plt.get_cmap(node_cmap), node_color=colour, node_size=sizes) labels = dict(zip(self.df[self.id].values, self.df[self.label].values)) if len(edges) > 0: # Colour the edges by the number of genes shared between the nodes edge_values = [edge_map[edge[0]][edge[1]] for edge in edges] lut = dict(zip(set(edge_values), sns.dark_palette("#d1d5db", len(set(edge_values)), reverse=True))) edge_cmap = ListedColormap(sns.dark_palette("#d1d5db", len(set(edge_values)), reverse=True)) edge_colours = pd.DataFrame(edge_values)[0].map(lut).values nx.draw_networkx_edges(G, pos, edgelist=edges, edge_color=edge_colours, arrows=False) else: # Set defaults for the legends edge_cmap = plt.get_cmap("Greys") edge_values = [0] self.u.warn_p(['Note, there are no edges drawn since there were no overlaps. Ensure you selected the ' 'overlap column correctly, you input:', self.overlap_column, ' with the separator:', self.sep]) # Plot the small labels and then for each "cluster" plot the smallest GO ID this should # correspond to the "top" term. # https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.clique.find_cliques.html#networkx.algorithms.clique.find_cliques labels_to_draw = {} gene_numbers = dict(zip(self.df[self.id].values, self.df[self.size].values)) # If the user has selected the smallest GO term as the one they want if plot_cliques: cliques = nx.find_cliques(G) for clique in cliques: # For each clique we want to get the GO term with the smallest ID smallest_GO = min([g.split(':')[1] for g in clique]) labels_to_draw[f'GO:{smallest_GO}'] = labels[f'GO:{smallest_GO}'] for node, node_group in edge_groups.items(): # For each clique we want to get the GO term with the smallest ID smallest_GO = np.argmax([gene_numbers.get(g) for g in node_group]) go = node_group[smallest_GO] labels_to_draw[go] = labels[go] small_labels = {} for go in labels: if not labels_to_draw.get(go): small_labels[go] = labels[go] # Clip on is only available in later versions if sys.version_info[0] < 3.8: nx.draw_networkx_labels(G, pos, small_labels, font_size=self.axis_font_size, font_color='black', font_family='sans-serif', verticalalignment='bottom') nx.draw_networkx_labels(G, pos, labels_to_draw, font_size=self.label_font_size, verticalalignment='bottom', font_family='sans-serif') else: nx.draw_networkx_labels(G, pos, small_labels, font_size=self.axis_font_size, font_color='black', font_family='sans-serif', verticalalignment='bottom', clip_on=False) nx.draw_networkx_labels(G, pos, labels_to_draw, font_size=self.label_font_size, verticalalignment='bottom', font_family='sans-serif', clip_on=False) cmin_node = '{:.2e}'.format(min(colour)) cmax_node = '{:.2e}'.format(max(colour)) cmap_labels = [f'P.adj:{cmin_node},{cmax_node}', f'G.shared:{min(edge_values)}-{max(edge_values)}'] # create proxy artists as handles: cmaps = [plt.get_cmap(node_cmap), edge_cmap] cmap_handles = [Rectangle((0, 0), 1, 1) for _ in cmaps] handler_map = dict(zip(cmap_handles, [HandlerColormap(cm, num_stripes=8) for cm in cmaps])) legend2 = plt.legend(handles=cmap_handles, labels=cmap_labels, handler_map=handler_map, fontsize=self.axis_font_size, bbox_to_anchor=(0.4, 0.3)) plt.gca().add_artist(legend2) gene_min = int(np.min(self.df[self.size].values)) gene_mean = int(np.mean(self.df[self.size].values)) gene_max = int(np.max(self.df[self.size].values)) gmin = plt.scatter([], [], s=int(np.min(counts)), marker='o', color='#222') gmid = plt.scatter([], [], s=int(np.mean(counts)), marker='o', color='#222') gmax = plt.scatter([], [], s=int(np.max(counts)), marker='o', color='#222') legend = plt.legend((gmin, gmid, gmax), (str(gene_min), str(gene_mean), str(gene_max)), scatterpoints=1, loc='lower left', ncol=1, fontsize=self.axis_font_size, bbox_to_anchor=(0, -0.1)) legend.set_title("No. Genes") plt.gca().add_artist(legend) plt.axis("off")
sum_all_tuples_heatmap = group_by_and_sum(sum_all_tuples_heatmap, sum_columns) # for r in sum_all_tuples_heatmap: # print(r.head(4000)) sns.set() # Load the example flights dataset and conver to long-form # print(df.head()) df = delete_dfs_column(sum_all_tuples_heatmap, BRESULT) df = df[0] df = df.loc[df[RRESULT] > 5] df = df.pivot(BLUEJUNGLECHAMP, REDJUNGLECHAMP, RRESULT) # grid_kws = {"height_ratios": (.9, .05), "hspace": 0.8} # f, (ax, cbar_ax) = plt.subplots(2, gridspec_kw=grid_kws) # sns.heatmap(df, ax=ax) # Draw a heatmap with the numeric values in each cell f, ax = plt.subplots(figsize=(12, 8)) cmap = sns.dark_palette("purple") g = sns.heatmap(df, annot=True, fmt="0.1f", linewidths=0.2, ax=ax, cmap=cmap) g.set_facecolor('xkcd:gray') # plt.figure(figsize=(20, 15)) # sns.heatmap(df, ax=ax) plt.show()
figs['agent_power']=agent_power overall_power = plt.figure() plot_power_usage(f['P_global'],f['t'][1:]) figs['overall_power']=overall_power plt.figure() plot_appl_matrix(f['appliances']) plt.figure() matrix = f['appliances'] app = downsample(matrix) time = downsample(f['t']) sns.tsplot(app,time=time, err_style="unit_traces", err_palette=sns.dark_palette("crimson", len(app)), color="k"); plt.xlabel('time') plt.ylabel('app') plt.figure() s = f['selfish'] plt.plot(s) plt.ylim([0,1]) plt.xlabel('agent') plt.ylabel('selfishness') meta = str(f['metadata']) meta_dict = {pair.split(':')[0]:pair.split(':')[1] for pair in meta.split(';')} P_max = float(meta_dict['U'])**2/float(meta_dict['Ri'])/4 p_matrix = f['P_all']
#G_mod_OS = np.array(extractFluxes(OS_Flux, 'Modelled_Flux_masked', 2)) # #EF_mod_PT = H_mod_PT/LE_mod_PT #EF_EC = H_EC/LE_EC #%% Plotting timeseries colors = ['#3498db', '#2ecc71', '#f7cf33', '#fc9d1d','#fd484d', '#9b59b6', '#51677b'] #colors = ['#3498db', '#2ecc71', '#f7cf33', '#fA8e63','#fd484d', '#51677b'] colors = ['#3498db', '#2ecc71', '#f7cf33', '#fc9d1d','#fd484d', '#9b59b6', '#51677b'] pal = sns.color_palette(colors) # created from '#e74c3c' from colors and husl input from # http://www.husl-colors.org/ pal_red_light = sns.light_palette((11.4, 97.4, 58.1), input="husl") pal_red_dark = sns.dark_palette((11.4, 97.4, 58.1), input="husl") pal_blue_light = sns.light_palette((242.2, 90.1, 60.2), input="husl") pal_blue_dark = sns.dark_palette((242.2, 90.1, 60.2), input="husl") sns.set() sns.set(context = "poster", style = 'darkgrid', palette = pal, rc = {'axes.labelsize': 20.0, 'figure.figsize': [22, 14], 'legend.fontsize': 20.0, 'xtick.labelsize': 20.0, 'ytick.labelsize': 20.0}) #%% First plot: Air temperature and humidity fig = plt.figure() ax1 = fig.add_subplot(311) Ta_plot = plt.plot_date(ecTime[dateCol_start: dateCol_stop], Ta_C, '-', color = pal[0], alpha = 1.0, lw=2.5, label = 'Air temperature')
saltKeys = saltLabels minKeys = dataLowI.loc[:, 'Mineral'].unique() saltIndex = np.arange(len(saltKeys)) minIndex = np.arange(len(minKeys)) saltMap = dict(zip(saltKeys, saltIndex)) minMap = dict(zip(minKeys, minIndex)) dataLowI.loc[:, "SaltKey"] = dataLowI.loc[:, "Salt"].map(saltMap) dataLowI.loc[:, "MinKey"] = dataLowI.loc[:, "Mineral"].map(minMap) f4Bar, ax4Bar = plt.subplots() colors = sns.cubehelix_palette(start=0, rot=1.5, as_cmap=True, gamma=1) colors = sns.dark_palette("green", as_cmap=True, reverse=True) spacePlot = ax4Bar.scatter(dataLowI.loc[:, "SaltKey"], dataLowI.loc[:, "MinKey"], cmap=colors, c=dataLowI.loc[:, 'fSorb'], s=dataLowI.loc[:, 'fSorb'] * 750) f4Bar.colorbar(spacePlot) ax4Bar.set_xticks(saltIndex) ax4Bar.set_yticks(minIndex) ax4Bar.set_xticklabels(saltKeys) ax4Bar.set_yticklabels(minKeys) ax4Bar.set_title("Radium Fraction Sorbed") sns.despine() """SECTION 4: IMPACT OF MASS ON SORPTION""" agwData = dataMass.loc[dataMass.loc[:, 'Salt'] == "AGW", :]