def gradient_color(color_list, color_sum=700,is_show=True): ''' 生成渐变色 color_list:list,16进制的颜色list color_sum:颜色个数 ''' # 主颜色个数 color_center_count = len(color_list) if color_sum < color_center_count * 3: color_sum=color_center_count * 3 # 两个颜色之中的个数 color_sub_count = int(color_sum / (color_center_count - 1)) color_index_start = 0 color_map = [] for color_index_end in range(1, color_center_count): color_rgb_start = Hex_to_RGB(color_list[color_index_start])[1] color_rgb_end = Hex_to_RGB(color_list[color_index_end])[1] r_step = (color_rgb_end[0] - color_rgb_start[0]) / color_sub_count g_step = (color_rgb_end[1] - color_rgb_start[1]) / color_sub_count b_step = (color_rgb_end[2] - color_rgb_start[2]) / color_sub_count # 生成中间渐变色 now_color = color_rgb_start color_map.append(RGB_to_Hex(now_color)) for color_index in range(1, color_sub_count): now_color = [now_color[0] + r_step, now_color[1] + g_step, now_color[2] + b_step] color_map.append(RGB_to_Hex(now_color)) color_index_start = color_index_end if is_show: sn.palplot(color_map) return color_map
def plot_spixel_segmentation(img, final_partition_nodes, segments, path="./"): """Displays segmentation mask to be overlayed on the image""" sns.set_palette(FLATUI[:len(final_partition_nodes)]) color_palette = sns.color_palette() rmask = np.zeros(img.shape) pred = np.zeros(img.shape[:-1]) fig = plt.figure(figsize=(30, 30)) plt.subplot(1, 2, 1) for i, superpixels in enumerate(final_partition_nodes): for spixel in superpixels: rmask[np.where(segments == spixel)] = color_palette[i] pred[np.where(segments == spixel)] = i + 1 plt.imshow(img) plt.subplot(1, 2, 2) plt.imshow(img) plt.imshow( rmask, alpha=0.5, ) if path != "./": plt.savefig(path) else: plt.show() sns.palplot(color_palette) plt.close() return pred, rmask
def summary_plot(output_file): import seaborn as sns sns.set(style="whitegrid") summary_data = pd.read_csv('data/summary_htru.csv') print(summary_data) # Draw a nested barplot to show survival for class and sex flatui = [ "#e17701", "#738595", "#658cbb", "#7af9ab", "#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71" ] sns.palplot(sns.color_palette(flatui)) g = sns.catplot(x="class", y="value", hue="metric", data=summary_data, height=6, kind="bar", palette=sns.color_palette(flatui)) g.despine(left=True) g.set_ylabels("value") g.set_xlabels("algorithm") g.set(ylim=(0.5, 1.0)) g.savefig('./{}'.format(output_file), format='png', dpi=150)
def plot_actives_distribution(self, colors={ 1: '#e74c3c', 0: '#FCD988' }, only_keys=None, max_position_to_plot=200, add_to_title='', show_num_actives=False): y_pred_dict = self.y_pred_dict # If only a subset of keys is requested if type(only_keys) is list and len(only_keys) > 0: keys_exist = np.all([i in y_pred_dict.keys() for i in only_keys]) assert keys_exist, 'Requested keys were not found.' y_pred_dict = {key: y_pred_dict[key] for key in only_keys} for key, y_pred in y_pred_dict.items(): order = np.argsort(-y_pred) y_pred_ord = y_pred[order] y_true = self.y_true[order] title = F'\n{add_to_title} {key} \n' if self.N > max_position_to_plot: y_true = y_true[:max_position_to_plot] # Get number of actives up to that position n_actives = (y_true[y_true == 1]).sum() title = F'\n{add_to_title} {key} [first {max_position_to_plot} positions, {n_actives}/{self.n} actives found]\n' colors_array = [colors[i] for i in y_true] sns.palplot(sns.color_palette(colors_array)) plt.title(title, fontsize=100) plt.show()
def test_color(): # 查看默认的颜色空间 current_plt = sns.color_palette() sns.palplot(current_plt) # 设置指定的画板 sns.palplot(sns.color_palette('hls', 8)) plt.show()
def calc_color(data, color=None): if color == 1: color_sq = ['#dadaebFF','#bcbddcF0','#9e9ac8F0','#807dbaF0','#6a51a3F0','#54278fF0'] colors = 'Purples' elif color == 2: color_sq = ['#c7e9b4','#7fcdbb','#41b6c4','#1d91c0','#225ea8','#253494']; colors = 'YlGnBu'; elif color == 3: color_sq = ['#f7f7f7','#d9d9d9','#bdbdbd','#969696','#636363','#252525']; colors = 'Greys'; elif color == 9: color_sq = ['#ff0000','#ff0000','#ff0000','#ff0000','#ff0000','#ff0000'] else: color_sq = ['#ffffd4','#fee391','#fec44f','#fe9929','#d95f0e','#993404']; colors = 'YlOrBr'; new_data, bins = pd.qcut(data, 6, retbins=True,labels=list(range(6))) color_ton = [] for val in new_data: color_ton.append(color_sq[val]) if color != 9: colors = sns.color_palette(colors, n_colors=6) sns.palplot(colors, 0.6); for i in range(6): print ("\n"+str(i+1)+': '+str(int(bins[i]))+" => "+str(int(bins[i+1])-1)) print("\n\n 1 2 3 4 5 6") return color_ton, bins;
def generate_palette(df, col, show_palette=False): ''' Assigns a unique color to each category of the given column Parameters ---------- df : pandas.DataFrame Table with column to assign colors to. col : str Column to assign colors to. show_palette : boolean (False) If True, it will display the generated palette. Returns ------- df : pandas.DataFrame Copy of the given DataFrame with an extra column with colors in hex format. ''' palette = color_palette("hls", len(set(df[col]))) if show_palette: palplot(palette) palette = pd.DataFrame(zip(set(df[col]), palette.as_hex()), columns=[col, col + '_COLOR']) df = pd.merge(df, palette) return df
def print_and_plot_stuff(stimulus_on_time, stimulus_off_time, stimulus_train, color_mat): print 'For this experiment:\n Stimulus ON at : %s\n Stimulus OFF at : %s\n Type of Stimulus is : %s\n' \ ' Colormap for PCA is:' % (str(stimulus_on_time), str(stimulus_off_time), stimulus_train) sns.palplot(color_mat) # Plot colormap chosen for PCA plots plt.show() plt.close()
def plot_adjmatrices(gt, pred, outputdir, vmax=30): sns.set_palette('bright') current_palette = sns.color_palette() sns.palplot(current_palette) green = current_palette[2] red = current_palette[3] lightblue = current_palette[-1] n_bin = 120 colors = [green, red, lightblue] cm = LinearSegmentedColormap.from_list('fp_fn_color', colors, N=n_bin) # Plot Ground Truth plot_connectivity_matrix_scatter(gt, cmap=cm, vmax=vmax) plt.title('Ground-truth connectivity matrix') cbar = plt.colorbar(fraction=0.013, pad=0.01) labels = [int(item) for item in cbar.get_ticks()] labels[-1] = f'>{vmax}' cbar.ax.set_yticklabels(labels) cbar.set_label('# synaptic connections') sns.despine() plt.savefig(outputdir + 'gt.png', dpi=300, transparent=True, bbox_inches='tight') # Plot Pred plot_connectivity_matrix_scatter(pred, cmap=cm, vmax=vmax) plt.title('Predicted connectivity matrix') cbar = plt.colorbar(fraction=0.013, pad=0.01) labels = [int(item) for item in cbar.get_ticks()] labels[-1] = f'>{vmax}' cbar.ax.set_yticklabels(labels) cbar.set_label('# synaptic connections') sns.despine() plt.savefig(outputdir + 'pred.png', dpi=300, transparent=True, bbox_inches='tight') # Plot Difference difference = gt - pred n_bin = 120 colors = ['red', 'white', 'blue'] cm = LinearSegmentedColormap.from_list('fp_fn_color', colors, N=n_bin) plot_connectivity_matrix_scatter(difference, vmin=-22.5, vmax=22.5, cmap=cm) cbar = plt.colorbar(fraction=0.013, pad=0.01) plt.title('Difference') plt.ylabel('Pre-synaptic') plt.xlabel('Post-synaptic') cbar.set_label('# synaptic connections') sns.despine() plt.savefig(outputdir + 'difference_scatter.png', dpi=300, transparent=True, bbox_inches='tight')
def plot_comparisons( reference_da: xr.DataArray, comparison_da: xr.DataArray, colors: List[str], remapping_dict: Union[Dict[int, int], Dict[float, float]], title: Optional[str] = None, ) -> None: """check that the remapping is sensible""" # convert to numpy array remapping_list = np.array([int(k) for k in remapping_dict.values()]) # plot the colormaps sns.palplot(colors) ax = plt.gca() ax.set_xticks([i for i in range(0, 5)]) ax.set_xticklabels([i for i in range(0, 5)]) if title is not None: ax.set_title(title) sns.palplot(np.array(colors)[remapping_list]) ax = plt.gca() ax.set_xticks([i for i in range(0, 5)]) ax.set_xticklabels(remapping_list) # create cmaps new_cmap = ListedColormap(colors[remapping_list]) cmap = ListedColormap(colors) # plot the spatial patterns fig, axs = plt.subplots(1, 3, figsize=((6.4 / 2) * 3, 4.8)) axs[1].imshow(reference_da.values[::-1, :], cmap=cmap) axs[1].set_title("Reference DA") axs[0].imshow(comparison_da.values[::-1, :], cmap=cmap) axs[0].set_title("Comparison DA") axs[2].imshow(comparison_da.values[::-1, :], cmap=new_cmap) axs[2].set_title("Comparison DA Remapped")
def plot_color_legend(): """Show how colors map back to the labeled cell types""" sns.palplot(cluster_names_to_color) ax = plt.gca() xticks = np.arange(0, cluster_names_to_color.shape[0]) ax.set(xticklabels=cluster_names_to_color.index, xticks=xticks) ax.grid(False)
def palette(nb_colors=5, preview=False, **kwargs): defaults = dict(rot=0.85, hue=1) defaults.update(kwargs) pal = sns.cubehelix_palette(nb_colors, **defaults) if preview: sns.palplot(pal2) return pal
def build_color_palette(num_items, weeks_before_switch): num_pre_switch_colors = weeks_before_switch num_post_switch_colors = num_items - num_pre_switch_colors print('preparing colors for {} pre-oxygen-switch'.format( num_pre_switch_colors), 'samples and {} post-switch samples' .format(num_post_switch_colors)) # get the first colors from this pallete: pre_switch_colors = \ sns.cubehelix_palette(11, start=.5, rot=-.75)[0:num_pre_switch_colors] print(pre_switch_colors) # get post-switch colors here: # post_switch_colors = sns.diverging_palette(220, 20, # n=6)[::-1][0:num_post_switch_colors] post_switch_colors = \ sns.color_palette("coolwarm", num_post_switch_colors) # sns.light_palette("navy", reverse=True)[0:num_post_switch_colors] rgb_colors = pre_switch_colors + post_switch_colors sns.palplot(rgb_colors) # check that we got the right amount print(num_items) assert (num_items == len(rgb_colors)) print("") return rgb_colors
def scatter_plot_matrix(): pth = "/Users/jonathan/PycharmProjects/networkclassifer/saved_clf" classifier = pickle.load(open("../saved_clf", "rb")) # cs = ['none','b','r','k','grey','grey'] import seaborn as sns import pandas as pd sns.palplot(sns.color_palette("hls", 8)) sns.color_palette("hls", 8) mc2 = [ (0.14901960784313725, 0.13725490196078433, 0.13725490196078433), (0.8235294117647058, 0.34509803921568627, 0.34509803921568627), (0.30196078431372547, 0.4588235294117647, 0.7019607843137254), (0.7725490196078432, 0.7764705882352941, 0.7803921568627451), ] sns.palplot(mc2) X = classifier.iss_features[:, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]] fig = plt.figure(figsize=(8, 11)) sns.set_palette(mc2) df = pd.DataFrame(X, columns=["Feature" + str(i + 1) for i in range(X.shape[1])]) print classifier.labels.shape df["Network States"] = classifier.labels pg = sns.PairGrid(df, vars=["Feature" + str(i + 1) for i in range(X.shape[1])], hue="Network States", size=2) # pg = sns.pairplot(df)# hue="Network States") pg.map(plt.scatter) # pg.map_lower(plt.scatter) # pg.map_upper(plt.scatter) # pg.map_diag(plt.scatter) # plt.savefig(static+'scattermatrix.pdf') plt.show()
def plot_color(r, g, b): print("Hexadecimal: %s" % rgb_to_hex( (round(r * 255), round(g * 255), round(b * 255)))) sns.palplot([(r, 0, 0), (0, g, 0), (0, 0, b), (r, g, b)]) plt.xticks([0, 1, 2, 3], ["Rojo", "Azul", "Verde", "(r,g,b)"]) plt.grid(False) plt.show()
def view_hex_color(hex_colorList): import seaborn as sns import matplotlib.pyplot as plt sns.palplot(sns.color_palette(hex_colorList)) plt.axis('off') plt.show()
def plotStyle(style, hmcmp): colors = [] legendNames = [] for x in ["RLat", "ELat", "DLat"]: c = style["Lats"][x] colors.append(c) legendNames.append(x) for x in ["New", "Reused"]: c = style["CNew"][x] colors.append(c) legendNames.append(x) for x in ["p0", "p1", "p2"]: c = style["Phases"][x] colors.append(c) legendNames.append(x) for x in ["aws", "ibm", "gcf", "azure"]: c = style[x]["color"] colors.append(c) legendNames.append(x) sns.palplot(hmcmp) sns.palplot(colors) fig, ax = plt.subplots(facecolor='white', figsize=(12, 1), frameon=False) ax.axis([0, 10, 0, 1]) ax.axis('off') # transform=ax.transData is the default, but we'll specify it anyway for x in range(0, len(legendNames)): name = legendNames[x] ax.text(0.125 + x * 0.85, 1, name, transform=ax.transData, rotation=45)
def plot_facets(df: pd.DataFrame, si: StorageInfo, facet_opts: dict, map_func: callable, map_opts: dict, basepath: str, name: str, use_legend: bool = True, meta: dict = {}): current_palette = sns.color_palette() sns.palplot(current_palette) g = sns.FacetGrid(df, margin_titles=True, **facet_opts) g.map_dataframe(map_func, **map_opts) g.add_legend() dh = Storage(si) with tempfile.TemporaryDirectory() as tdir: basefile = tdir + '/' + uuid.uuid4().hex svgfile = basefile + '.svg' g.savefig(svgfile, dpi=300) out = dh.upload_file(svgfile, basepath + '/' + name + '.svg', 'svg', meta=meta) return out return {}
def region_cmap(region_name, rot=0.1, plotme=False): """ Parameters ---------- region_name : str Region abbreviation (VISp, TH, SCs, DG, etc) rot : float plotme : optional, bool plot colormap, default = false Returns ------- cmap : seaborn colormap """ starts = np.linspace(0.2, 2.8, 10) regions = ('VISam', 'VISpm', 'TH', 'SCs', 'DG', 'CA', 'VISp', 'VISl', 'VISal', 'VISrl') try: ind = regions.index(region_name) ind = starts[ind] except ValueError: print('Invalid Region {}. Accepted regions: '.format(region_name)) print(regions) return cmap = sns.cubehelix_palette(start=ind, rot=rot, as_cmap=True) if plotme: sns.palplot(sns.cubehelix_palette(start=ind, rot=rot)) return cmap
def plot_cp2sb_with_object(model, path): sns.set_style("darkgrid") sns.palplot(sns.color_palette("hls", 8)) # font_path = '/usr/share/fonts/truetype/takao-gothic/TakaoPGothic.ttf' # font_prop = FontProperties(fname=font_path) # matplotlib.rcParams['font.family'] = font_prop.get_name() n = copy.copy(model.n) del n[model.new_class] labels = ["spatial_{}".format(i) for i in n ] + ["object_{}".format(id_o) for id_o in range(model._O)] data = np.r_[np.array(list(model.phi.values())), model.psi] for label, y in zip(labels, data): plt.figure(figsize=(10, 5)) ax = plt.subplot2grid((10, 1), (0, 0), rowspan=9) ymajorFormatter = FormatStrFormatter('%1.1f') ax.yaxis.set_major_formatter(ymajorFormatter) x = np.arange(10) - 1 y_max_indices = np.argsort(y)[::-1][:10] word_list = [model._Dict[i] for i in y_max_indices] y = np.sort(y)[::-1][:10] saved_data = pd.DataFrame(y) saved_data.to_csv('%s/import_%s.csv' % (path, label), encoding='utf-8') plt.xticks(x, word_list, size='small', rotation=30) plt.ylabel("Import") plt.bar(x, y, width=.35, align='center', color='r', alpha=0.8) plt.savefig("%s/import_%s.png" % (path, label), dpi=600) plt.close()
def _find_palette(start, rot, gamma, dark, light): chp = partial(sns.cubehelix_palette, start=start, rot=rot, gamma=gamma, dark=dark, light=light) print(("cmap = sns.cubehelix_palette(start=%(start)s, " "rot=%(rot)s, gamma=%(gamma)s, dark=%(dark)s, " "light=%(light)s)") % locals()) sns.palplot(chp()) return chp(as_cmap=True)
def set_palette(pal=None, show=False): """Set default color palette.""" pal = kp_pal if pal is None else pal sns.set_palette(pal) if show is True: sns.palplot(pal) elif show is False: return pal
def figure_correlation_matrix_pFBA(): df_names = pd.read_csv(names_fn, index_col=0) df_env = pd.read_csv(env_fn, index_col=0).T df = pd.read_csv(pFBA_fn, index_col=0).T df[["LightEX", "HCO3EXcar", "AmmoniaEX", "FAKEOrthophosphateEX" ]] = df_env[["Light", "HCO3", "Nitrogen", "Phosphate"]].abs() drop_columns = [ "tRNAEX", 'CadaverineEX', 'CadmiumEX', 'CalciumEX', 'ChlorideEX', 'CalciumEX', 'ChlorideEX', 'IronEX', 'MagnesiumEX', 'PotassiumEX', 'SelenateEX', 'SodiumEX', 'StrontiumEX', 'ZincEX', 'R00024', 'H2OEX', "HEX" ] df["SulfateEX"] *= -1 df.drop(columns=drop_columns, inplace=True) df.drop(columns=df.columns[(df.abs() < 1e-3).all()], inplace=True) #df.columns = pd.merge(df.T, df_names["Name"], left_index = True, right_index = True, how = "left")["Name"] corr_matrix = df.corr() linked = linkage(corr_matrix, 'single', optimal_ordering=True) d = dendrogram(linked) df_corr = pd.DataFrame(corr_matrix) df_corr = df_corr.iloc[d['leaves'], d['leaves']] # Row colors row_class = pd.merge(df_corr, df_names, left_index=True, right_index=True, how="left") row_class.set_index("Name", inplace=True) row_class = row_class["Class"] lut = dict(zip(CLASS_NAMES, sns.hls_palette(len(CLASS_NAMES)))) row_colors = row_class.map(lut) df_corr.columns = pd.merge(df_corr.T, df_names, left_index=True, right_index=True, how="left")["Name"] df_corr.index = df_corr.columns sns.clustermap(df_corr, mask=np.triu(df_corr, 1), yticklabels=True, xticklabels=True, row_cluster=False, col_cluster=False, cmap="coolwarm", row_colors=row_colors, col_colors=row_colors, figsize=(16, 16), vmin=-1, vmax=1) #, annot = True, fmt='.1g') plt.savefig("pFBA_correlation.svg") sns.palplot(sns.hls_palette(len(df_names["Class"].unique()))) plt.savefig("pFBA_colorbar.svg")
def plot_extras(self): # In[ ]: # g = sns.PairGrid(d_bin_v_others.loc[:,["R2", "one_minus_cdf_BH", "SNP-pairs", "contigs_per_bin",]]) # g.map_upper(plt.scatter) # g.map_lower(sns.kdeplot, cmap="Blues_d") # g.map_diag(sns.kdeplot, lw=3, legend=False) d_bin_vars = self.d.d_bin_v_others.loc[:, ["R2", "one_minus_cdf_BH", "SNP-pairs", "contigs_per_bin", ]] sns.palplot(sns.cubehelix_palette(8, start=.5, rot=-.75)) # In[ ]: my_cmap = sns.cubehelix_palette(40, start=.5, rot=-.75, as_cmap=True) cc = sns.mpl.colors.ColorConverter() marginal_color = cc.to_rgb(arg=my_cmap.colors[int(255 * 1)]) # In[ ]: # sns.jointplot('SNP-pairs','R2',d_bin_vars, kind='kde', # joint_kws=dict(shade=True, # cmap=my_cmap, # n_levels=40 # ), # marginal_kws=dict(shade=True, color=my_cmap.colors[int(256*0.66)]) # ) g = sns.JointGrid('SNP-pairs', 'R2', d_bin_vars) g.plot_marginals(sns.distplot, kde=False, color=marginal_color) g.plot_joint(sns.kdeplot, shade=True, cmap=my_cmap, n_levels=40); # In[ ]: # sns.jointplot('one_minus_cdf_BH','R2',d_bin_vars, kind='kde', # joint_kws=dict(shade=True, # cmap=my_cmap, # n_levels=40 # ), # marginal_kws=dict(shade=True, color=my_cmap.colors[int(256*0.66)]) # ) g = sns.JointGrid('one_minus_cdf_BH', 'R2', d_bin_vars) g.plot_marginals(sns.distplot, kde=False, color=marginal_color) g.plot_joint(sns.kdeplot, shade=True, cmap=my_cmap, n_levels=40, alpha=1); # In[ ]: sns.jointplot('contigs_per_bin', 'SNP-pairs', d_bin_vars, kind='kde', joint_kws=dict(shade=True, cmap=my_cmap, n_levels=8 ), marginal_kws=dict(shade=True, color=my_cmap.colors[int(256 * 0.66)]) )
def createEspressoPalette(allFeedData): # Set up a custom palette for plots import seaborn as sns CPalette = esp.create_palette(['k', 'gray', 'cyan', 'orangered'], # green for test, use this for ACR expriments allFeedData.feeds.Genotype.cat.categories ) sns.palplot(CPalette.values()) print(allFeedData.feeds.Genotype.cat.categories) return CPalette
def show_colors(self): print "The top5 colours are:" + '\n' + "hex rgb frequency" toplist = [] for keys in sorted(self.colors_count, key=self.colors_count.__getitem__, reverse = True)[:5]: # Sorts dictionary by value and show the top5 toplist.append(rgb_to_hex(keys)) print rgb_to_hex(keys), keys, ": ",self.colors_count[keys] # print hex, rgb and frequency palettecolours = [toplist[0], toplist[1], toplist[2], toplist[3], toplist[4]] sns.palplot(sns.color_palette(palettecolours)) # create a palette with the top5 colours plt.show() # show the palette
def palette_legend(): cmap = Series({'UOK262': u'#34495e', 'UOK262pFH': u'#919daa'}) sns.set(rc={'axes.linewidth': .3, 'xtick.major.width': .3, 'ytick.major.width': .3, 'lines.linewidth': .75, 'xtick.direction': 'out', 'ytick.direction': 'out'}, style='white', font_scale=1.25) sns.palplot(cmap) plt.xticks(range(len(cmap)), [i.replace('_', ' ') for i in cmap.index], rotation='vertical') plt.gcf().set_size_inches(1, .5) plt.savefig('./reports/palette_legend.pdf', bbox_inches='tight') plt.gcf().set_size_inches(2, 1) plt.close('all')
def show_colorpal(self): """Show color palette info. """ try: import matplotlib.pyplot as plt sns.palplot(self.colorpal / 255.0) plt.show() except ImportError: self.logger.error('Module matplotlib is not installed.')
def test_palette(self, name): print "Testing palette " + name import seaborn import matplotlib.pyplot as plt if not name in self._palettes: print "[SeabornColors::test_palette] ERROR : Palette " + name + " does not exist" sys.exit(1) seaborn.palplot(self._palettes[name]) plt.show()
def calc_color(data, color=None): """ Set the color scales for a heating map """ if color == 1: color_sq = ['#dadaebFF', '#bcbddcF0', '#9e9ac8F0', '#807dbaF0', '#6a51a3F0', '#54278fF0'] colors = 'Purples' elif color == 2: color_sq = ['#c7e9b4', '#7fcdbb', '#41b6c4', '#1d91c0', '#225ea8', '#253494'] colors = 'YlGnBu' elif color == 3: color_sq = ['#f7f7f7', '#d9d9d9', '#bdbdbd', '#969696', '#636363', '#252525'] colors = 'Greys' elif color == 9: color_sq = ['#ff0000', '#ff0000', '#ff0000', '#ff0000', '#ff0000', '#ff0000'] else: color_sq = ['#ffffd4', '#fee391', '#fec44f', '#fe9929', '#d95f0e', '#993404'] colors = 'YlOrBr' dat = set(data) dat = list(dat) new_data, bins = pd.qcut(dat, 6, retbins=True, labels=list(range(6))) color_ton = [] for val in new_data: color_ton.append(color_sq[val]) tonos = [] for i in range(len(data)): if data[i] > 0 and data[i] < int(bins[1]): tonos.append(color_sq[0]) if data[i] > int(bins[1])-1 and data[i] < int(bins[2]): tonos.append(color_sq[1]) if data[i] > int(bins[2])-1 and data[i] < int(bins[3]): tonos.append(color_sq[2]) if data[i] > int(bins[3])-1 and data[i] < int(bins[4]): tonos.append(color_sq[3]) if data[i] > int(bins[4])-1 and data[i] < int(bins[5]): tonos.append(color_sq[4]) if data[i] > int(bins[5])-1: tonos.append(color_sq[5]) if color != 9: colors = sns.color_palette(colors, n_colors=6) sns.palplot(colors, 0.6) #for i in range(6): # print("\n" + str(i + 1) + ': ' + str(int(bins[i])) + " => " + str(int(bins[i + 1]) - 1), end=" ") #print("\n\n 1 2 3 4 5 6") return tonos, bins
def _set_colors(self): palette = sns.diverging_palette(10, 220, sep=20, n=11) sns.palplot(palette) plt.savefig("colors.png") plt.close() colors = [] for color in palette: r, g, b, _ = color colors.append((int(r * 255), int(g * 255), int(b * 255))) return colors
def get_pal(ncolors, npercolor, plot=False): base_colors = sns.color_palette("deep")[:ncolors] n_off = npercolor // 3. pal = np.vstack([ sns.light_palette(c, npercolor + n_off, reverse=True)[:npercolor] for c in base_colors ]) sns.set_palette(pal) if plot: sns.palplot(pal) return pal
def colors(cmap_name): palette = sns.color_palette(cmap_name, 10) flip = 1 x = np.linspace(0, 14, 100) sns.palplot(palette) sns.set_palette(palette) fig, axs = plt.subplots() for i in range(10): axs.plot(x, np.sin(x + i * 0.5) * (11 - i) * flip) fig.suptitle(cmap_name)
def show_colors(self, n_bins=None): """Display the colors in self.cmap. Parameters ---------- n_bins : None | int How many bins to use in displaying the colormap. If None, a continuous representation is used and plotted with matplotlib. """ if isinstance(n_bins, int): palplot(self.to_numeric(n_bins)) elif n_bins is None: gradient = np.linspace(0, 1, 256) gradient = np.vstack((gradient, gradient)) f, ax = plt.subplots(figsize=(5, .5)) ax.imshow(gradient, aspect='auto', cmap=self.cmap) ax.set_axis_off() else: raise ValueError('n_bins must be type int or None')
# In[18]: sinplot() sns.despine() # ### Set the colors # https://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html # In[19]: colorblind_palette = sns.color_palette('colorblind') deep_palette = sns.color_palette('deep') sns.palplot(colorblind_palette) sns.palplot(deep_palette) # You can pass these color palettes to your plotting function # ### Histogram # In[20]: sns.distplot(df_auto['price'], kde=False) sns.despine() # ### Scatterplot
def mplhls_to_hls(a): return (a[0] * 360, a[1] * 100, a[2] * 100) def display_palette(p): a = [] print 'RGB\t HLS' for p in palette: print mcl.rgb2hex(p), print ['{:.2f}'.format(cc) for cc in mplrgb_to_rgb(p)], print ['{:.2f}'.format(cc) for cc in mplhls_to_hls(colorsys.rgb_to_hls(*p))] # print ['{:1.2f}'.format(cc * hls_scale[i]) # for i, cc in enumerate(mcl.rgb_to_hsv(p))] a.append( mplhls_to_hls(colorsys.rgb_to_hls(*p))) sns.palplot(palette) return a # ====================================================================== # COLOR SCHEMER # ====================================================================== beigetone = np.array([ (176, 102, 96), (202, 143, 66), (171, 156, 115), (94, 119, 3), (106, 125, 142)]) earthtone = np.array([ (73, 56, 41), (97, 51, 25), (213, 117, 0),
""" MSMExplorer Colors ================== """ import seaborn as sns import matplotlib.pyplot as pp from msmexplorer.palettes import msme_rgb names, values = zip(*msme_rgb.items()) sns.palplot(values, size=3) pp.xticks(range(len(names)), names, size=20)
# Wall time: 6.65 s # # # # ###Geo dist # Tempo previsto di calcolo con $\sim$ 7000 dati: $\sim$ 620 sec $\sim$ 10 minuti # # ###Euclid dist # Tempo previsto di calcolo con $\sim$ 7000 dati: $\sim$ 80 sec $\sim$ 1,3 minuti # In[24]: colori = ["#4d4d4d", "#004184", "#ff3300", "#ff8000", "#018ECC"] paletta = seaborn.color_palette(palette=colori) seaborn.palplot(paletta) paletta = seaborn.color_palette(palette="muted") seaborn.palplot(paletta) paletta = seaborn.color_palette(palette="bright") seaborn.palplot(paletta) paletta = seaborn.color_palette(palette="pastel") seaborn.palplot(paletta) paletta = seaborn.color_palette(palette="dark") seaborn.palplot(paletta) paletta = seaborn.color_palette(palette="colorblind") seaborn.palplot(paletta) paletta = seaborn.color_palette print paletta
def readScores(opPrefix, n, trainingPosFile, trainingNegFile, allMarkPeaks, allMarkSignal): testScores = [] testResults = [] trainingScores = [] trainingResults = [] totalAuc, totalAupr = OrderedDict(), OrderedDict() fpr, tpr = OrderedDict(), OrderedDict() prec, rec = OrderedDict(), OrderedDict() colorIdx = OrderedDict() idx = 0 colorIdx["RandomForest"] = 0 totalAuc["RandomForest"], fpr["RandomForest"], tpr["RandomForest"] = 0, [], [] totalAupr["RandomForest"], prec["RandomForest"], rec["RandomForest"] = 0, [], [] colorIdx["SVM"] = 1 totalAuc["SVM"], fpr["SVM"], tpr["SVM"] = 0, [], [] totalAupr["SVM"], prec["SVM"], rec["SVM"] = 0, [], [] colorIdx["LR"] = 2 totalAuc["LR"], fpr["LR"], tpr["LR"] = 0, [], [] totalAupr["LR"], prec["LR"], rec["LR"] = 0, [], [] colorIdx["NB"] = 3 totalAuc["NB"], fpr["NB"], tpr["NB"] = 0, [], [] totalAupr["NB"], prec["NB"], rec["NB"] = 0, [], [] featureNames = [] for currMark in allMarkSignal: featureNames.append(currMark) totalAuc[currMark], fpr[currMark], tpr[currMark] = 0, [], [] totalAupr[currMark], prec[currMark], rec[currMark] = 0, [], [] colorIdx[currMark] = idx totalAuc[currMark + "Peaks"], fpr[currMark + "Peaks"], tpr[currMark + "Peaks"] = 0, [], [] totalAupr[currMark + "Peaks"], prec[currMark + "Peaks"], rec[currMark + "Peaks"] = 0, [], [] colorIdx[currMark + "Peaks"] = idx idx += 1 colors_ = list(six.iteritems(colors.cnames)) for name, rgb in six.iteritems(colors.ColorConverter.colors): hex_ = colors.rgb2hex(rgb) colors_.append((name, hex_)) hex_ = [color[1] for color in colors_] rgb = [colors.hex2color(color) for color in hex_] hsv = [colors.rgb_to_hsv(color) for color in rgb] hue = [color[0] for color in hsv] sat = [color[1] for color in hsv] val = [color[2] for color in hsv] ind = np.lexsort((val, sat, hue)) sorted_colors = [colors_[i] for i in ind] usedColors = [] n1 = len(sorted_colors) current_palette = sns.color_palette(n_colors=len(allMarkSignal)) sns.palplot(current_palette) for i in range(0, len(allMarkSignal)): usedColors.append(sorted_colors[int(i*float(n1)/(len(allMarkSignal)))]) c = current_palette for idx in range(0, n): testScores.append(OrderedDict()) trainingScores.append(OrderedDict()) trainingResults.append([]) testResults.append([]) markIdx = 0 for currMark in allMarkSignal: if markIdx == 0: markIdx += 1 markName = "Master" else: markIdx += 1 markName = currMark testScores1 = scoresCurrFile(opPrefix + "_testPos" + str(idx) + "_MFscores.bed", markName) testScores2 = scoresCurrFile(opPrefix + "_testNeg" + str(idx) + "_MFscores.bed", markName) trainScores1 = scoresCurrFile(opPrefix + "_trainPos_MFscores.bed", markName) trainScores2 = scoresCurrFile(opPrefix + "_trainNeg_MFscores.bed", markName) renTrainScores1, renTrainScores2 = calculateZscores(trainScores1, trainScores2, trainScores2, pp, "train_" + markName) renTestScores1, renTestScores2 = calculateZscores(testScores1, testScores2, testScores2, pp, "test_" + markName) #renormalizeScores(trainScores1, trainScores2, currMark + "Train") #renormalizeScores(testScores1, testScores2, currMark + "Test") testScores[idx][currMark] = renTestScores1 + renTestScores2 trainingScores[idx][currMark] = renTrainScores1 + renTrainScores2 testResults1 = [1] * len(testScores1) testResults2 = [0] * len(testScores2) trainResults1 = [1] * len(trainScores1) trainResults2 = [0] * len(trainScores2) testResults[idx] = testResults1 + testResults2 trainingResults[idx] = trainResults1 + trainResults2 for currMark in testScores[idx]: currFpr, currTpr, currRoc_auc, currPrec, currRec, curr_aupr = calculateMetrics(testScores[idx][currMark], testResults[idx], currMark) currFpr = list(currFpr) currTpr = list(currTpr) currPrec = list(currPrec) currRec = list(currRec) if idx == 0: for element in currFpr: fpr[currMark].append(element) for element in currTpr: tpr[currMark].append(element) for element in currPrec: prec[currMark].append(element) for element in currRec: rec[currMark].append(element) totalAuc[currMark] += currRoc_auc totalAupr[currMark] += curr_aupr # for idx in range(0, n): # for currMark in allMarkPeaks: # currFpr, currTpr, currRoc_auc, currPrec, currRec, curr_aupr = calculatePeakAUC(opPrefix + "_testPos" + str(idx) + "_MFscores.bed", opPrefix + "_testNeg" + str(idx) + "_MFscores.bed", allMarkPeaks[currMark], opPrefix, currMark) # currFpr = list(currFpr) # currTpr = list(currTpr) # currPrec = list(currPrec) # currRec = list(currRec) # if idx == 0: # for element in currFpr: # fpr[currMark].append(element) # for element in currTpr: # tpr[currMark].append(element) # for element in currPrec: # prec[currMark].append(element) # for element in currRec: # rec[currMark].append(element) # totalAuc[currMark + "Peaks"] += currRoc_auc # totalAupr[currMark + "Peaks"] += curr_aupr scores_pred = [] featureImportance = [] SVMcoeff = [] LRcoeff = [] for idx in range(0, n): scores_pred.append(OrderedDict()) scores_pred[idx]["RandomForest"], currFeatureImportance = performRandomForest(trainingScores[idx], trainingResults[idx], testScores[idx]) featureImportance.append(currFeatureImportance) scores_pred[idx]["SVM"], currFeatureImportance = performSVM(trainingScores[idx], trainingResults[idx], testScores[idx]) SVMcoeff.append(currFeatureImportance) scores_pred[idx]["LR"], currFeatureImportance = performLR(trainingScores[idx], trainingResults[idx], testScores[idx]) LRcoeff.append(currFeatureImportance) scores_pred[idx]["NB"] = performNB(trainingScores[idx], trainingResults[idx], testScores[idx]) #print len(testScores[idx]), np.size(scores_pred[idx]["SVM"]), np.size(testResults[idx]) for currMark in scores_pred[idx]: currFpr, currTpr, currRoc_auc, currPrec, currRec, curr_aupr = calculateMetrics(scores_pred[idx][currMark], testResults[idx], currMark) currFpr = list(currFpr) currTpr = list(currTpr) currPrec = list(currPrec) currRec = list(currRec) if idx == 0: for element in currFpr: fpr[currMark].append(element) for element in currTpr: tpr[currMark].append(element) for element in currPrec: prec[currMark].append(element) for element in currRec: rec[currMark].append(element) totalAuc[currMark] += currRoc_auc totalAupr[currMark] += curr_aupr print currRoc_auc, curr_aupr featureImportanceMean = np.mean(featureImportance, axis=0) featureImportanceStd = np.std(featureImportance, axis = 0) SVMcoeffMean = np.mean(SVMcoeff, axis=0) SVMcoeffStd = np.std(SVMcoeff, axis = 0) LRcoeffMean = np.mean(LRcoeff, axis=0) LRcoeffStd = np.std(LRcoeff, axis = 0) plt.figure() fig, ax = plt.subplots() ind = np.arange(len(featureNames)) width = 0.5 rects1 = ax.bar(ind, featureImportanceMean, width, color='g', yerr=featureImportanceStd) ax.set_xlabel('Feature') ax.set_ylabel('Feature Importance') ax.set_title('Importance of feature in Random Forest Model') ax.set_xticks(ind + width/2) ax.set_xticklabels(tuple(featureNames)) plt.savefig(pp, format="pdf") plt.figure() fig, ax = plt.subplots() ind = np.arange(len(featureNames)) width = 0.5 rects1 = ax.bar(ind, SVMcoeffMean, width, color='g', yerr=SVMcoeffStd) ax.set_xlabel('Feature') ax.set_ylabel('Feature Importance') ax.set_title('Coefficients in SVM Model') ax.set_xticks(ind + width/2) ax.set_xticklabels(tuple(featureNames)) plt.savefig(pp, format="pdf") fig, ax = plt.subplots() ind = np.arange(len(featureNames)) width = 0.5 rects1 = ax.bar(ind, LRcoeffMean, width, color='g', yerr=LRcoeffStd) ax.set_xlabel('Feature') ax.set_ylabel('Feature Importance') ax.set_title('Coefficients in LR Model') ax.set_xticks(ind + width/2) ax.set_xticklabels(tuple(featureNames)) plt.savefig(pp, format="pdf") plt.figure() ax = plt.subplot(111) for currMark in totalAuc: totalAuc[currMark] /= n if "Asym" not in currMark and "Peak" not in currMark and "+" not in currMark and currMark not in ["RandomForest", "SVM", "LR", "NB"]: print currMark ax.plot(fpr[currMark], tpr[currMark], linewidth=2, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAuc[currMark]) elif "Asym" not in currMark and "+" not in currMark and currMark not in ["RandomForest", "SVM", "LR", "NB"]: pass #ax.plot(fpr[currMark], tpr[currMark], "-.", linewidth=2, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAuc[currMark]) elif currMark in ["RandomForest", "SVM", "LR", "NB"]:# or "Random" in currMark: ax.plot(fpr[currMark], tpr[currMark], ":", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAuc[currMark]) else: pass #ax.plot(fpr[currMark], tpr[currMark], "--", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAuc[currMark]) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.75, box.height * 0.75]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=7) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title("ROC Plot") #plt.legend(loc="lower right") plt.savefig(pp, format="pdf") plt.figure() ax = plt.subplot(111) for currMark in totalAupr: totalAupr[currMark] /= n if "Asym" not in currMark and "Peak" not in currMark and "+" not in currMark and currMark not in ["RandomForest", "SVM", "LR", "NB"]: ax.plot(rec[currMark], prec[currMark], linewidth=2, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAupr[currMark]) elif "Asym" not in currMark and "+" not in currMark and currMark not in ["RandomForest", "SVM", "LR", "NB"]: pass #ax.plot(rec[currMark], prec[currMark], "-.", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAupr[currMark]) elif currMark in ["RandomForest", "SVM", "LR", "NB"]: #or "Random" in currMark: #pass print currMark ax.plot(rec[currMark], prec[currMark], ":", linewidth=2, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAupr[currMark]) else: pass #ax.plot(rec[currMark], prec[currMark], "--", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAupr[currMark]) #plt.plot(rec[currMark], prec[currMark], linewidth=2, label=currMark+' (area = %0.2f)' % totalAupr[currMark]) plt.xlabel('Recall') plt.ylabel('Precision') plt.title("PR Plot") box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.75, box.height * 0.75]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=7) #plt.legend(loc="upper right") plt.savefig(pp, format="pdf") #sys.exit() #print "Final AUC (symmetric) = ", totalAuc["Master"] #print "Final AUC (asymmetric) = ", totalAuc["MasterAsym"] return
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sns current_palette = sns.color_palette() #默认颜色 #sns.palplot(current_palette) # 环形颜色 #sns.palplot(sns.color_palette("hls", 16)) #调整深度和饱和度 sns.palplot(sns.hls_palette(8, l=.3, s=.6)) plt.show()
THRESHOLD=1 N_ITEMS_FOR_USER_U=0 N_USERS_FOR_ITEM_I=0 MAX_LENGTH_PREFIX=8 TITLE=r'\textbf{Yelp tag}' FILENAME='yelp-test' # OTHER STUFF parameters = ['t', 'l', 'alpha', 'theta', 'nItemsForUserU', 'nUsersForItemI'] columns = parameters + ['ranking'] palette = sns.color_palette() palette[-1], palette[-2] = palette[-2], palette[-1] sns.palplot(palette) # PLOT TIME LIMIT EFFECTS #def plot_t(f, A, matrix, times, lengths): # for k in precisions: # P = matrix[k] # # TIME EVOLUTIONS # alpha = ALPHA # theta = THRESHOLD # fig = plt.figure() # for t in times: # res = [] # for l in lengths: # res.append(P[(t,l,alpha,theta,N_ITEMS_FOR_USER_U,N_USERS_FOR_ITEM_I)]) # plt.plot(lengths, res, label=r'$%d$' % t) # plt.xlabel(r'Prefix length $l$')
import networkx as nx import seaborn as sns with open("final_grid_gameBeta0.1Prob1.dat", 'rb') as file: G=pk.load(file) uninteractingnodes=[x for x in G.nodes() if G.node[x]['agent'].dict==[]] G.remove_nodes_from(uninteractingnodes) nodeColor=[G.node[x]['agent'].dict[0] for x in G.nodes()] for x in range(len(nodeColor)): nodeColor[x]=float(nodeColor[x])/(max(nodeColor)) difcolor=[] for x in nodeColor: if x not in difcolor: difcolor.append(x) numberofcolors=len(difcolor) print numberofcolors fig1=sns.palplot(sns.diverging_palette(240,10, n=numberofcolors)) plt.show() palette=sns.diverging_palette(240, 10, as_cmap=True) sns.palplot(palette(1.0)) plt.show() fig2=nx.draw(G, pos={i:i for i in G.nodes()}, node_color=nodeColor, node_cmap=sns.diverging_palette(240, 10, as_cmap=True), node_size=20) plt.show()
sns.pointplot(data=results[results["statistics"] == stat], x="classification", y="value", hue="system", order=['correct', 'topcwa', 'cwa', 'other', 'missing']) if True: results2 = results[results["context"] == "all"] plt.subplot(121) sns.barplot(data=results2[results2["statistics"] == "wfreq"], x="classification", y="value", hue="system", order=['topcwa', 'cwa', 'other', 'missing'], ) plt.ylim(0, 0.5) if True: plt.subplot(122) results2 = results[results["system"] == "slepemapy"] print(results["context"].unique()) sns.barplot(data=results2[results2["statistics"] == "wfreq"], x="classification", y="value", hue="context", order=['topcwa', 'cwa', 'other', 'missing'], hue_order=['United States-state', 'Africa-state', 'World-state', 'Europe-state', 'Czech Rep.-river',] ) if False: STATS.remove('wfreq') STATS.remove('repetition') for i, stat in enumerate(STATS): plt.subplot(2, 2, i+1) plt.title(stat) sns.pointplot(data=results[results["statistics"] == stat], x="classification", y="value", hue="system", order=['correct', 'topcwa', 'cwa', 'other', 'missing']) plt.show() current_palette = sns.color_palette() sns.palplot(current_palette) print([[c * 255 for c in p] for p in current_palette])
ax.set_xlim((-1.2, 1.2)) ax.set_ylim((-.1, 1.1)) ax.axis('off') plt.savefig(os.path.join(plot_dir, conf, conf+'_graph.png'), dpi=cur_dpi, transparent=True) for h in h_txt: h.remove() plt.tight_layout() plt.savefig(os.path.join(plot_dir, conf, conf+'_mini_graph.png'), dpi=cur_dpi/2.5, transparent=True) # In[162]: fig = plt.figure(figsize=(6, 2)) sns.palplot(dot_colors) axt = plt.gca() xm = axt.get_xlim() axt.text(xm[-1], -1.1, 'ref > citation', fontsize=80, horizontalalignment='right') axt.text(xm[0], -1.1, 'ref < citation', fontsize=80, horizontalalignment='left') axt.text(.5*(xm[0]+xm[-1]), -1.1, 'ref = citation', fontsize=80, horizontalalignment='center') plt.tight_layout() plt.savefig(os.path.join(plot_dir, conf, 'color_bar.png'), dpi=cur_dpi/2, transparent=True) # In[163]: # generate bar graphs visualizing overall in/out citation volume plt.figure(figsize=(15, 5))
from scipy import signal acorr = signal.fftconvolve(theta, theta[::-1], mode='full') # much faster than np.correlate plt.plot(theta_bits[12000:12010,:].T) pca = PCA(n_components='mle') X_S = pca.fit(theta_bits) lgn.line(np.cumsum(pca.explained_variance_)) V = pca.components_ V.shape plot(V[:,0:5]) sns.set_palette("husl",10) sns.palplot(sns.color_palette) from lpproj import LocalityPreservingProjection lpp = LocalityPreservingProjection(n_components=2) X_2D = lpp(theta_bits) X_2D = lpp.fit_transform(theta_bits) # Jake VDP wpca example function from wpca import WPCA, EMPCA, PCA def plot_results(ThisPCA, X, weights=None, Xtrue=None, ncomp=2): # Compute the standard/weighted PCA if weights is None: kwds = {} else: kwds = {'weights': weights} # Compute the PCA vectors & variance
def readScores(opPrefix, n, positiveFile, negativeFile, peakFile, otherMarkSignal, otherMarkPeak): scores = [] results = [] trainingScores = [] trainingResults = [] totalAuc, totalAupr = OrderedDict(), OrderedDict() fpr = OrderedDict() tpr = OrderedDict() prec = OrderedDict() rec = OrderedDict() colorIdx = OrderedDict() totalAuc["Master"], fpr["Master"], tpr["Master"] = 0, [], [] totalAuc["MasterAsym"], fpr["MasterAsym"], tpr["MasterAsym"] = 0, [], [] totalAuc["MasterPeak"], fpr["MasterPeak"], tpr["MasterPeak"] = 0, [], [] totalAuc["RandomForest"], fpr["RandomForest"], tpr["RandomForest"] = 0, [], [] totalAupr["Master"], prec["Master"], rec["Master"] = 0, [], [] totalAupr["MasterAsym"], prec["MasterAsym"], rec["MasterAsym"] = 0, [], [] totalAupr["MasterPeak"], prec["MasterPeak"], rec["MasterPeak"] = 0, [], [] totalAupr["RandomForest"], prec["RandomForest"], rec["RandomForest"] = 0, [], [] colorIdx["Master"] = 0 colorIdx["MasterAsym"] = 0 colorIdx["MasterPeak"] = 0 colorIdx["RandomForest"] = 0 colorIdx["SVM"] = 1 totalAuc["SVM"], fpr["SVM"], tpr["SVM"] = 0, [], [] totalAupr["SVM"], prec["SVM"], rec["SVM"] = 0, [], [] colorIdx["LR"] = 2 totalAuc["LR"], fpr["LR"], tpr["LR"] = 0, [], [] totalAupr["LR"], prec["LR"], rec["LR"] = 0, [], [] colorIdx["NB"] = 3 totalAuc["NB"], fpr["NB"], tpr["NB"] = 0, [], [] totalAupr["NB"], prec["NB"], rec["NB"] = 0, [], [] idx = 1 peakMarks = {"MasterPeak": peakFile} featureNames = ["Master"] if otherMarkSignal != None: for currMark in otherMarkSignal: print currMark featureNames.append(currMark) totalAuc[currMark], fpr[currMark], tpr[currMark] = 0, [], [] totalAuc[currMark + "Asym"], fpr[currMark + "Asym"], tpr[currMark + "Asym"] = 0, [], [] totalAuc[currMark + "Peak"], fpr[currMark + "Peak"], tpr[currMark + "Peak"] = 0, [], [] totalAupr[currMark], prec[currMark], rec[currMark] = 0, [], [] totalAupr[currMark + "Asym"], prec[currMark + "Asym"], rec[currMark + "Asym"] = 0, [], [] totalAupr[currMark + "Peak"], prec[currMark + "Peak"], rec[currMark + "Peak"] = 0, [], [] colorIdx[currMark] = idx colorIdx[currMark + "Asym"] = idx colorIdx[currMark + "Peak"] = idx peakMarks[currMark + "Peak"] = otherMarkPeak[currMark] totalAuc["Master + " + currMark], fpr["Master + " + currMark], tpr["Master + " + currMark] = 0, [], [] totalAupr["Master + " + currMark], prec["Master + " + currMark], rec["Master + " + currMark] = 0, [], [] colorIdx["Master + " + currMark] = idx idx += 1 colors_ = list(six.iteritems(colors.cnames)) for name, rgb in six.iteritems(colors.ColorConverter.colors): hex_ = colors.rgb2hex(rgb) colors_.append((name, hex_)) hex_ = [color[1] for color in colors_] rgb = [colors.hex2color(color) for color in hex_] hsv = [colors.rgb_to_hsv(color) for color in rgb] hue = [color[0] for color in hsv] sat = [color[1] for color in hsv] val = [color[2] for color in hsv] ind = np.lexsort((val, sat, hue)) sorted_colors = [colors_[i] for i in ind] usedColors = [] n1 = len(sorted_colors) current_palette = sns.color_palette(n_colors=len(otherMarkSignal) + 1) sns.palplot(current_palette) for i in range(0, len(otherMarkSignal) + 1): usedColors.append(sorted_colors[int(i*float(n1)/(len(otherMarkSignal) + 1))]) c = current_palette for idx in range(0, n): #print idx, n scores.append(OrderedDict()) trainingScores.append(OrderedDict()) trainingResults.append([]) results.append([]) trainingScores1 = scoresCurrFile(opPrefix + "_positives" + str(idx) + "_MFscores.bed", "Master") trainingResults1 = [1] * len(trainingScores1) trainingScores3 = scoresCurrFile(opPrefix + "_positives_asym" + str(idx) + "_MFscores.bed", "Master") trainingScores2 = scoresCurrFile(opPrefix + "_negatives" + str(idx) + "_MFscores.bed", "Master") trainingResults2 = [0] * len(trainingScores2) trainingScores4 = scoresCurrFile(opPrefix + "_negatives_asym" + str(idx) + "_MFscores.bed", "Master") #renTrainScores1, renTrainScores2 = renormalizeScores(trainingScores1, trainingScores2, "Master") renTrainScores1, renTrainScores2 = calculateZscores(trainingScores1, trainingScores2, trainingScores2, pp, "trainMaster") renTrainScores3, renTrainScores4 = calculateZscores(trainingScores3, trainingScores4, trainingScores4, pp, "trainMasterAsym") trainingScores[idx]["Master"] = renTrainScores1 + renTrainScores2 trainingScores[idx]["MasterAsym"] = trainingScores3 + trainingScores4 trainingResults[idx] = trainingResults1 + trainingResults2 scores1 = scoresCurrFile(opPrefix + "_testPos" + str(idx) + "_MFscores.bed", "Master") results1 = [1] * len(scores1) scores3 = scoresCurrFile(opPrefix + "_testPos_asym" + str(idx) + "_MFscores.bed", "Master") scores2 = scoresCurrFile(opPrefix + "_testNeg" + str(idx) + "_MFscores.bed", "Master") results2 = [0] * len(scores2) scores4 = scoresCurrFile(opPrefix + "_testNeg_asym" + str(idx) + "_MFscores.bed", "Master") #renTestScores1, renTestScores2 = renormalizeScores(scores1, scores2, "Master") renTestScores1, renTestScores2 = calculateZscores(scores1, scores2, scores2, pp, "testMaster") renTestScores3, renTestScores4 = calculateZscores(scores3, scores4, scores4, pp, "testMasterAsym") scores[idx]["Master"] = renTestScores1 + renTestScores2 scores[idx]["MasterAsym"] = scores3 + scores4 results[idx] = results1 + results2 if otherMarkSignal != None: for currMark in otherMarkSignal: #print opPrefix + "_testPos" + str(idx) + "_MFscores.bed" scores1 = scoresCurrFile(opPrefix + "_testPos" + str(idx) + "_MFscores.bed", currMark) scores3 = scoresCurrFile(opPrefix + "_testPos_asym" + str(idx) + "_MFscores.bed", currMark) scores2 = scoresCurrFile(opPrefix + "_testNeg" + str(idx) + "_MFscores.bed", currMark) scores4 = scoresCurrFile(opPrefix + "_testNeg_asym" + str(idx) + "_MFscores.bed", currMark) trainingScores1 = scoresCurrFile(opPrefix + "_positives" + str(idx) + "_MFscores.bed", currMark) trainingScores3 = scoresCurrFile(opPrefix + "_positives_asym" + str(idx) + "_MFscores.bed", currMark) trainingScores2 = scoresCurrFile(opPrefix + "_negatives" + str(idx) + "_MFscores.bed", currMark) trainingScores4 = scoresCurrFile(opPrefix + "_negatives_asym" + str(idx) + "_MFscores.bed", currMark) #renTrainScores1, renTrainScores2 = renormalizeScores(trainingScores1, trainingScores2, currMark) renTrainScores1, renTrainScores2 = calculateZscores(trainingScores1, trainingScores2, trainingScores2, pp, "train" + currMark) renTrainScores3, renTrainScores4 = calculateZscores(trainingScores3, trainingScores4, trainingScores4, pp, "train" + currMark + "Asym") #trainingScores[idx][currMark] = renTrainScores1 + renTrainScores2 #trainingScores[idx][currMark + "Asym"] = renTrainScores3 + renTrainScores4 trainingScores[idx][currMark] = renTrainScores1 + renTrainScores2 trainingScores[idx][currMark + "Asym"] = trainingScores3 + trainingScores4 renTestScores1, renTestScores2 = calculateZscores(scores1, scores2, scores2, pp, "test" + currMark) renTestScores3, renTestScores4 = calculateZscores(scores3, scores4, scores4, pp, "test" + currMark + "Asym") scores[idx][currMark] = renTestScores1 + renTestScores2 scores[idx][currMark + "Asym"] = scores3 + scores4 for currMark in scores[idx]: currFpr, currTpr, currRoc_auc, currPrec, currRec, curr_aupr = calculateMetrics(scores[idx][currMark], results[idx], currMark) currFpr = list(currFpr) currTpr = list(currTpr) currPrec = list(currPrec) currRec = list(currRec) if idx == 0: for element in currFpr: fpr[currMark].append(element) for element in currTpr: tpr[currMark].append(element) for element in currPrec: prec[currMark].append(element) for element in currRec: rec[currMark].append(element) totalAuc[currMark] += currRoc_auc totalAupr[currMark] += curr_aupr for idx in range(0, n): for currMark in peakMarks: currFpr, currTpr, currRoc_auc, currPrec, currRec, curr_aupr = calculatePeakAUC(opPrefix + "_testPos" + str(idx) + "_MFscores.bed", opPrefix + "_testNeg" + str(idx) + "_MFscores.bed", peakMarks[currMark], opPrefix, currMark) currFpr = list(currFpr) currTpr = list(currTpr) currPrec = list(currPrec) currRec = list(currRec) if idx == 0: for element in currFpr: fpr[currMark].append(element) for element in currTpr: tpr[currMark].append(element) for element in currPrec: prec[currMark].append(element) for element in currRec: rec[currMark].append(element) totalAuc[currMark] += currRoc_auc totalAupr[currMark] += curr_aupr if otherMarkSignal != None: scores_pred = [] featureImportance = [] SVMcoeff = [] LRcoeff = [] for idx in range(0, n): scores_pred.append(OrderedDict()) #scores_pred.append(performLinearRegression(scores[idx], results[idx], scores[idx])) scores_pred[idx]["RandomForest"], currFeatureImportance = performRandomForest(trainingScores[idx], trainingResults[idx], scores[idx]) featureImportance.append(currFeatureImportance) scores_pred[idx]["SVM"], currFeatureImportance = performSVM(trainingScores[idx], trainingResults[idx], scores[idx]) SVMcoeff.append(currFeatureImportance) scores_pred[idx]["LR"], currFeatureImportance = performLR(trainingScores[idx], trainingResults[idx], scores[idx]) LRcoeff.append(currFeatureImportance) scores_pred[idx]["NB"] = performNB(trainingScores[idx], trainingResults[idx], scores[idx]) for currMark in scores_pred[idx]: currFpr, currTpr, currRoc_auc, currPrec, currRec, curr_aupr = calculateMetrics(scores_pred[idx][currMark], results[idx], currMark) currFpr = list(currFpr) currTpr = list(currTpr) currPrec = list(currPrec) currRec = list(currRec) if idx == 0: for element in currFpr: fpr[currMark].append(element) for element in currTpr: tpr[currMark].append(element) for element in currPrec: prec[currMark].append(element) for element in currRec: rec[currMark].append(element) totalAuc[currMark] += currRoc_auc totalAupr[currMark] += curr_aupr featureImportanceMean = np.mean(featureImportance, axis=0) featureImportanceStd = np.std(featureImportance, axis = 0) SVMcoeffMean = np.mean(SVMcoeff, axis=0) SVMcoeffStd = np.std(SVMcoeff, axis = 0) LRcoeffMean = np.mean(LRcoeff, axis=0) LRcoeffStd = np.std(LRcoeff, axis = 0) plt.figure() fig, ax = plt.subplots() ind = np.arange(len(featureNames)) width = 0.5 rects1 = ax.bar(ind, featureImportanceMean, width, color='g', yerr=featureImportanceStd) ax.set_xlabel('Feature') ax.set_ylabel('Feature Importance') ax.set_title('Importance of feature in Random Forest Model') ax.set_xticks(ind + width/2) ax.set_xticklabels(tuple(featureNames)) plt.savefig(pp, format="pdf") fig, ax = plt.subplots() ind = np.arange(len(featureNames)) width = 0.5 rects1 = ax.bar(ind, SVMcoeffMean, width, color='g', yerr=SVMcoeffStd) ax.set_xlabel('Feature') ax.set_ylabel('Feature Importance') ax.set_title('Coefficients in SVM Model') ax.set_xticks(ind + width/2) ax.set_xticklabels(tuple(featureNames)) plt.savefig(pp, format="pdf") fig, ax = plt.subplots() ind = np.arange(len(featureNames)) width = 0.5 rects1 = ax.bar(ind, LRcoeffMean, width, color='g', yerr=LRcoeffStd) ax.set_xlabel('Feature') ax.set_ylabel('Feature Importance') ax.set_title('Coefficients in LR Model') ax.set_xticks(ind + width/2) ax.set_xticklabels(tuple(featureNames)) plt.savefig(pp, format="pdf") plt.figure() ax = plt.subplot(111) for currMark in totalAuc: totalAuc[currMark] /= n if "Master" in currMark and "Asym" not in currMark and "Peak" not in currMark and "+" not in currMark: #ax.plot(fpr[currMark], tpr[currMark], "--", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAuc[currMark]) pass elif "Peak" in currMark: #ax.plot(fpr[currMark], tpr[currMark], "-.", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' Peak (area = %0.2f)' % totalAuc[currMark]) pass elif "Asym" not in currMark and "Peak" not in currMark and "+" not in currMark and currMark not in ["RandomForest", "SVM", "LR", "NB"]: #ax.plot(fpr[currMark], tpr[currMark], "--", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAuc[currMark]) pass elif "Asym" not in currMark and "+" not in currMark and currMark not in ["RandomForest", "SVM", "LR", "NB"]: pass #ax.plot(fpr[currMark], tpr[currMark], "-.", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAuc[currMark]) elif currMark in ["RandomForest", "SVM", "LR", "NB"]:# or "Random" in currMark: #pass ax.plot(fpr[currMark], tpr[currMark], ":", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAuc[currMark]) else: pass #ax.plot(fpr[currMark], tpr[currMark], "--", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAuc[currMark]) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.75, box.height * 0.75]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=7) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title("ROC Plot") #plt.legend(loc="lower right") plt.savefig(pp, format="pdf") plt.figure() ax = plt.subplot(111) for currMark in totalAupr: totalAupr[currMark] /= n if "Master" in currMark and "Asym" not in currMark and "Peak" not in currMark and "+" not in currMark: #ax.plot(rec[currMark], prec[currMark], "--", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAupr[currMark]) pass elif "Peak" in currMark: pass #ax.plot(rec[currMark], prec[currMark], "-.", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' Peak (area = %0.2f)' % totalAupr[currMark]) elif "Asym" not in currMark and "Peak" not in currMark and "+" not in currMark and currMark not in ["RandomForest", "SVM", "LR", "NB"]: pass #ax.plot(rec[currMark], prec[currMark], "--", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAupr[currMark]) elif "Asym" not in currMark and "+" not in currMark and currMark not in ["RandomForest", "SVM", "LR", "NB"]: pass #ax.plot(rec[currMark], prec[currMark], "-.", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAupr[currMark]) elif currMark in ["RandomForest", "SVM", "LR", "NB"]: #or "Random" in currMark: #pass ax.plot(rec[currMark], prec[currMark], ":", linewidth=2, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAupr[currMark]) else: pass #ax.plot(rec[currMark], prec[currMark], "--", linewidth=1, c=c[colorIdx[currMark]], label=currMark+' (area = %0.2f)' % totalAupr[currMark]) #plt.plot(rec[currMark], prec[currMark], linewidth=2, label=currMark+' (area = %0.2f)' % totalAupr[currMark]) plt.xlabel('Recall') plt.ylabel('Precision') plt.title("PR Plot") plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.0]) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.75, box.height * 0.75]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=7) #plt.legend(loc="upper right") plt.savefig(pp, format="pdf") #sys.exit() #print "Final AUC (symmetric) = ", totalAuc["Master"] #print "Final AUC (asymmetric) = ", totalAuc["MasterAsym"] return
import seaborn as sns import matplotlib.pyplot as plt import os, os.path import pandas as pd sns.set(style="ticks", context="talk") workpath = os.path.dirname(os.path.abspath(__file__)) filepath = os.path.join(workpath, "data", "tips.txt" ) tips = pd.read_csv(filepath, sep = '\t') print(tips) days = ["Thur", "Fri", "Sat", "Sun"] #pal = sns.cubehelix_palette(4, 1.5, .75, light=.6, dark=.2) #g = sns.lmplot("total_bill", "tip", hue="day", data=tips, # palette=pal, size=6) #hue_order=days, #g.set_axis_labels("Total bill ($)", "Tip ($)") colors = ['#F0A3FF', '#0075DC', '#993F00', '#4C005C', '#191919', '#005C31', '#2BCE48', '#FFCC99', '#808080', '#94FFB5', '#8F7C00', '#9DCC00', '#C20088', '#003380', '#FFA405', '#FFA8BB', '#426600', '#FF0010', '#5EF1F2', '#00998F', '#E0FF66', '#740AFF', '#990000', '#FFFF00', '#FF5005'] #colors_r = reversed(colors) colors_r = colors[::-1] pal = sns.palplot(colors_r) plt.show()
def plot_palette(self): sns.palplot(self.color_map)